-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathutils.py
More file actions
165 lines (153 loc) · 6.42 KB
/
utils.py
File metadata and controls
165 lines (153 loc) · 6.42 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import random
import torch
import torchvision.transforms as transforms
import numpy as np
import cv2
def gen_input_mask(
shape, hole_size, hole_area=None, max_holes=1):
"""
* inputs:
- shape (sequence, required):
Shape of a mask tensor to be generated.
A sequence of length 4 (N, C, H, W) is assumed.
- hole_size (sequence or int, required):
Size of holes created in a mask.
If a sequence of length 4 is provided,
holes of size (W, H) = (
hole_size[0][0] <= hole_size[0][1],
hole_size[1][0] <= hole_size[1][1],
) are generated.
All the pixel values within holes are filled with 1.0.
- hole_area (sequence, optional):
This argument constraints the area where holes are generated.
hole_area[0] is the left corner (X, Y) of the area,
while hole_area[1] is its width and height (W, H).
This area is used as the input region of Local discriminator.
The default value is None.
- max_holes (int, optional):
This argument specifies how many holes are generated.
The number of holes is randomly chosen from [1, max_holes].
The default value is 1.
* returns:
A mask tensor of shape [N, C, H, W] with holes.
All the pixel values within holes are filled with 1.0,
while the other pixel values are zeros.
"""
mask = torch.zeros(shape)
bsize, _, mask_h, mask_w = mask.shape
for i in range(bsize):
n_holes = random.choice(list(range(1, max_holes+1)))
for _ in range(n_holes):
# choose patch width
if isinstance(hole_size[0], tuple) and len(hole_size[0]) == 2:
hole_w = random.randint(hole_size[0][0], hole_size[0][1])
else:
hole_w = hole_size[0]
# choose patch height
if isinstance(hole_size[1], tuple) and len(hole_size[1]) == 2:
hole_h = random.randint(hole_size[1][0], hole_size[1][1])
else:
hole_h = hole_size[1]
# choose offset upper-left coordinate
if hole_area:
harea_xmin, harea_ymin = hole_area[0]
harea_w, harea_h = hole_area[1]
offset_x = random.randint(harea_xmin, harea_xmin + harea_w - hole_w)
offset_y = random.randint(harea_ymin, harea_ymin + harea_h - hole_h)
else:
offset_x = random.randint(0, mask_w - hole_w)
offset_y = random.randint(0, mask_h - hole_h)
mask[i, :, offset_y: offset_y + hole_h, offset_x: offset_x + hole_w] = 1.0
return mask
def gen_hole_area(size, mask_size):
"""
* inputs:
- size (sequence, required)
A sequence of length 2 (W, H) is assumed.
(W, H) is the size of hole area.
- mask_size (sequence, required)
A sequence of length 2 (W, H) is assumed.
(W, H) is the size of input mask.
* returns:
A sequence used for the input argument 'hole_area' for function 'gen_input_mask'.
"""
mask_w, mask_h = mask_size
harea_w, harea_h = size
offset_x = random.randint(0, mask_w - harea_w)
offset_y = random.randint(0, mask_h - harea_h)
return ((offset_x, offset_y), (harea_w, harea_h))
def crop(x, area):
"""
* inputs:
- x (torch.Tensor, required)
A torch tensor of shape (N, C, H, W) is assumed.
- area (sequence, required)
A sequence of length 2 ((X, Y), (W, H)) is assumed.
sequence[0] (X, Y) is the left corner of an area to be cropped.
sequence[1] (W, H) is its width and height.
* returns:
A torch tensor of shape (N, C, H, W) cropped in the specified area.
"""
xmin, ymin = area[0]
w, h = area[1]
return x[:, :, ymin: ymin + h, xmin: xmin + w]
def sample_random_batch(dataset, batch_size=32):
"""
* inputs:
- dataset (torch.utils.data.Dataset, required)
An instance of torch.utils.data.Dataset.
- batch_size (int, optional)
Batch size.
* returns:
A mini-batch randomly sampled from the input dataset.
"""
num_samples = len(dataset)
batch = []
for _ in range(min(batch_size, num_samples)):
index = random.choice(range(0, num_samples))
x = torch.unsqueeze(dataset[index], dim=0)
batch.append(x)
return torch.cat(batch, dim=0)
def poisson_blend(input, output, mask):
"""
* inputs:
- input (torch.Tensor, required)
Input tensor of Completion Network, whose shape = (N, 3, H, W).
- output (torch.Tensor, required)
Output tensor of Completion Network, whose shape = (N, 3, H, W).
- mask (torch.Tensor, required)
Input mask tensor of Completion Network, whose shape = (N, 1, H, W).
* returns:
Output image tensor of shape (N, 3, H, W) inpainted with poisson image editing method.
"""
input = input.clone().cpu()
output = output.clone().cpu()
mask = mask.clone().cpu()
mask = torch.cat((mask, mask, mask), dim=1) # convert to 3-channel format
num_samples = input.shape[0]
ret = []
for i in range(num_samples):
dstimg = transforms.functional.to_pil_image(input[i])
dstimg = np.array(dstimg)[:, :, [2, 1, 0]]
srcimg = transforms.functional.to_pil_image(output[i])
srcimg = np.array(srcimg)[:, :, [2, 1, 0]]
msk = transforms.functional.to_pil_image(mask[i])
msk = np.array(msk)[:, :, [2, 1, 0]]
# compute mask's center
xs, ys = [], []
for j in range(msk.shape[0]):
for k in range(msk.shape[1]):
if msk[j, k, 0] == 255:
ys.append(j)
xs.append(k)
xmin, xmax = min(xs), max(xs)
ymin, ymax = min(ys), max(ys)
center = ((xmax + xmin) // 2, (ymax + ymin) // 2)
dstimg = cv2.inpaint(dstimg, msk[:, :, 0], 1, cv2.INPAINT_TELEA)
out = cv2.seamlessClone(srcimg, dstimg, msk, center, cv2.NORMAL_CLONE)
out = out[:, :, [2, 1, 0]]
out = transforms.functional.to_tensor(out)
out = torch.unsqueeze(out, dim=0)
ret.append(out)
ret = torch.cat(ret, dim=0)
return ret