util.py
8.93 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import os
from random import randint, seed
import itertools
import numpy as np
import cv2
class MaskGenerator():
def __init__(self, height, width, channels=3, rand_seed=None, filepath=None):
"""Convenience functions for generating masks to be used for inpainting training
Arguments:
height {int} -- Mask height
width {width} -- Mask width
Keyword Arguments:
channels {int} -- Channels to output (default: {3})
rand_seed {[type]} -- Random seed (default: {None})
filepath {[type]} -- Load masks from filepath. If None, generate masks with OpenCV (default: {None})
"""
self.height = height
self.width = width
self.channels = channels
self.filepath = filepath
# If filepath supplied, load the list of masks within the directory
self.mask_files = []
if self.filepath:
filenames = [f for f in os.listdir(self.filepath)]
self.mask_files = [f for f in filenames if any(filetype in f.lower() for filetype in ['.jpeg', '.png', '.jpg'])]
print(">> Found {} masks in {}".format(len(self.mask_files), self.filepath))
# Seed for reproducibility
if rand_seed:
seed(rand_seed)
def _generate_mask(self):
"""Generates a random irregular mask with lines, circles and elipses"""
img = np.zeros((self.height, self.width, self.channels), np.uint8)
# Set size scale
size = int((self.width + self.height) * 0.03)
if self.width < 64 or self.height < 64:
raise Exception("Width and Height of mask must be at least 64!")
# Draw random lines
for _ in range(randint(1, 20)):
x1, x2 = randint(1, self.width), randint(1, self.width)
y1, y2 = randint(1, self.height), randint(1, self.height)
thickness = randint(3, size)
cv2.line(img,(x1,y1),(x2,y2),(1,1,1),thickness)
# Draw random circles
for _ in range(randint(1, 20)):
x1, y1 = randint(1, self.width), randint(1, self.height)
radius = randint(3, size)
cv2.circle(img,(x1,y1),radius,(1,1,1), -1)
# Draw random ellipses
for _ in range(randint(1, 20)):
x1, y1 = randint(1, self.width), randint(1, self.height)
s1, s2 = randint(1, self.width), randint(1, self.height)
a1, a2, a3 = randint(3, 180), randint(3, 180), randint(3, 180)
thickness = randint(3, size)
cv2.ellipse(img, (x1,y1), (s1,s2), a1, a2, a3,(1,1,1), thickness)
return 1-img
def _load_mask(self, rotation=True, dilation=True, cropping=True):
"""Loads a mask from disk, and optionally augments it"""
# Read image
mask = cv2.imread(os.path.join(self.filepath, np.random.choice(self.mask_files, 1, replace=False)[0]))
# Random rotation
if rotation:
rand = np.random.randint(-180, 180)
M = cv2.getRotationMatrix2D((mask.shape[1]/2, mask.shape[0]/2), rand, 1.5)
mask = cv2.warpAffine(mask, M, (mask.shape[1], mask.shape[0]))
# Random dilation
if dilation:
rand = np.random.randint(5, 47)
kernel = np.ones((rand, rand), np.uint8)
mask = cv2.erode(mask, kernel, iterations=1)
# Random cropping
if cropping:
x = np.random.randint(0, mask.shape[1] - self.width)
y = np.random.randint(0, mask.shape[0] - self.height)
mask = mask[y:y+self.height, x:x+self.width]
return (mask > 1).astype(np.uint8)
def sample(self, random_seed=None):
"""Retrieve a random mask"""
if random_seed:
seed(random_seed)
if self.filepath and len(self.mask_files) > 0:
return self._load_mask()
else:
return self._generate_mask()
class ImageChunker(object):
def __init__(self, rows, cols, overlap):
self.rows = rows
self.cols = cols
self.overlap = overlap
def perform_chunking(self, img_size, chunk_size):
"""
Given an image dimension img_size, return list of (start, stop)
tuples to perform chunking of chunk_size
"""
chunks, i = [], 0
while True:
chunks.append((i*(chunk_size - self.overlap/2), i*(chunk_size - self.overlap/2)+chunk_size))
i+=1
if chunks[-1][1] > img_size:
break
n_count = len(chunks)
chunks[-1] = tuple(x - (n_count*chunk_size - img_size - (n_count-1)*self.overlap/2) for x in chunks[-1])
chunks = [(int(x), int(y)) for x, y in chunks]
return chunks
def get_chunks(self, img, scale=1):
"""
Get width and height lists of (start, stop) tuples for chunking of img.
"""
x_chunks, y_chunks = [(0, self.rows)], [(0, self.cols)]
if img.shape[0] > self.rows:
x_chunks = self.perform_chunking(img.shape[0], self.rows)
else:
x_chunks = [(0, img.shape[0])]
if img.shape[1] > self.cols:
y_chunks = self.perform_chunking(img.shape[1], self.cols)
else:
y_chunks = [(0, img.shape[1])]
return x_chunks, y_chunks
def dimension_preprocess(self, img, padding=True):
"""
In case of prediction on image of different size than 512x512,
this function is used to add padding and chunk up the image into pieces
of 512x512, which can then later be reconstructed into the original image
using the dimension_postprocess() function.
"""
# Assert single image input
assert len(img.shape) == 3, "Image dimension expected to be (H, W, C)"
# Check if we are adding padding for too small images
if padding:
# Check if height is too small
if img.shape[0] < self.rows:
padding = np.ones((self.rows - img.shape[0], img.shape[1], img.shape[2]))
img = np.concatenate((img, padding), axis=0)
# Check if width is too small
if img.shape[1] < self.cols:
padding = np.ones((img.shape[0], self.cols - img.shape[1], img.shape[2]))
img = np.concatenate((img, padding), axis=1)
# Get chunking of the image
x_chunks, y_chunks = self.get_chunks(img)
# Chunk up the image
images = []
for x in x_chunks:
for y in y_chunks:
images.append(
img[x[0]:x[1], y[0]:y[1], :]
)
images = np.array(images)
return images
def dimension_postprocess(self, chunked_images, original_image, scale=1, padding=True):
"""
In case of prediction on image of different size than 512x512,
the dimension_preprocess function is used to add padding and chunk
up the image into pieces of 512x512, and this function is used to
reconstruct these pieces into the original image.
"""
# Assert input dimensions
assert len(original_image.shape) == 3, "Image dimension expected to be (H, W, C)"
assert len(chunked_images.shape) == 4, "Chunked images dimension expected to be (B, H, W, C)"
# Check if we are adding padding for too small images
if padding:
# Check if height is too small
if original_image.shape[0] < self.rows:
new_images = []
for img in chunked_images:
new_images.append(img[0:scale*original_image.shape[0], :, :])
chunked_images = np.array(new_images)
# Check if width is too small
if original_image.shape[1] < self.cols:
new_images = []
for img in chunked_images:
new_images.append(img[:, 0:scale*original_image.shape[1], :])
chunked_images = np.array(new_images)
# Put reconstruction into this array
new_shape = (
original_image.shape[0]*scale,
original_image.shape[1]*scale,
original_image.shape[2]
)
reconstruction = np.zeros(new_shape)
# Get the chunks for this image
x_chunks, y_chunks = self.get_chunks(original_image)
i = 0
s = scale
for x in x_chunks:
for y in y_chunks:
prior_fill = reconstruction != 0
chunk = np.zeros(new_shape)
chunk[x[0]*s:x[1]*s, y[0]*s:y[1]*s, :] += chunked_images[i]
chunk_fill = chunk != 0
reconstruction += chunk
reconstruction[prior_fill & chunk_fill] = reconstruction[prior_fill & chunk_fill] / 2
i += 1
return reconstruction