File size: 3,085 Bytes
d2c5d46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import torch

def compute_patch_overlap(size, has_left, has_right, base=8):

    if has_left and has_right:

        left = base
        right = base

        total = size + left + right
        mod = total % 64

        if mod != 0:
            extra = 64 - mod
            left += extra // 2
            right += extra - extra // 2

    elif has_left:

        left = base
        total = size + left
        mod = total % 64

        if mod != 0:
            left += 64 - mod

        right = 0

    elif has_right:

        right = base
        total = size + right
        mod = total % 64

        if mod != 0:
            right += 64 - mod

        left = 0

    else:
        left = right = 0

    return left, right

def build_patch_grid(H, W, n_rows, n_cols):


    h_list = [H // n_rows] * n_rows
    h_list[-1] += H - sum(h_list)

    w_list = [W // n_cols] * n_cols
    w_list[-1] += W - sum(w_list)

    hs = [0]
    for h in h_list:
        hs.append(hs[-1] + h)

    ws = [0]
    for w in w_list:
        ws.append(ws[-1] + w)

    patches = []
    patch_meta = []

    for i in range(n_rows):
        for j in range(n_cols):

            h0, h1 = hs[i], hs[i+1]
            w0, w1 = ws[j], ws[j+1]

            ph = h1 - h0
            pw = w1 - w0


            top, bottom = compute_patch_overlap(
                ph,
                has_left=(i > 0),
                has_right=(i < n_rows - 1)
            )

            left, right = compute_patch_overlap(
                pw,
                has_left=(j > 0),
                has_right=(j < n_cols - 1)
            )

            hs_slice = slice(h0 - top, h1 + bottom)
            ws_slice = slice(w0 - left, w1 + right)

            patches.append((hs_slice, ws_slice))
            patch_meta.append((top, bottom, left, right))

    return patches, patch_meta

def merge_patch_grid(image_shape, patches, enhanced_patches, patch_meta, device='cpu'):
    output = torch.zeros(image_shape, dtype=torch.float32, device=device)

    weight = torch.zeros((1, 1, image_shape[2], image_shape[3]), dtype=torch.float32, device=device)

    for (hs, ws), enhanced_patch, (top, bottom, left, right) in zip(patches, enhanced_patches, patch_meta):
        enhanced_patch = enhanced_patch.to(device)

        ph, pw = enhanced_patch.shape[2:]
        mask = torch.ones((1, 1, ph, pw), dtype=torch.float32, device=device)

        if left > 0:
            ramp = torch.linspace(0, 1, left, device=device).view(1, 1, 1, -1)
            mask[:,:,:, :left] *= ramp

        if right > 0:
            ramp = torch.linspace(1, 0, right, device=device).view(1, 1, 1, -1)
            mask[:,:,:, -right:] *= ramp

        if top > 0:
            ramp = torch.linspace(0, 1, top, device=device).view(1, 1, -1, 1)
            mask[:,:, :top, :] *= ramp

        if bottom > 0:
            ramp = torch.linspace(1, 0, bottom, device=device).view(1, 1, -1, 1)
            mask[:,:, -bottom:, :] *= ramp

        output[:, :, hs, ws] += enhanced_patch * mask
        weight[:, :, hs, ws] += mask

    output.div_(weight)
    return output