-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
256 lines (186 loc) · 8.01 KB
/
utils.py
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import numpy as np
from PIL import Image
import os
from scipy import ndimage as ndi
from multiprocessing import Pool
from itertools import repeat
import random
def scheduler(epoch, lr):
if (epoch+1) % 200 == 0:
return lr * 3/4
else:
return lr
def load_img_paths(split_path):
img_paths = []
with open(split_path) as file:
for line in file:
img_paths.append(line.strip())
return img_paths
def load_imgs(root_dir, img_paths, img_size):
imgs = []
for path in img_paths:
path = os.path.join(root_dir, path)
img = Image.open(path).resize((img_size, img_size))
imgs.append(img)
return imgs
def standardize(x):
return np.array(x, dtype=np.float32)/127.5 - 1.
def destandardize(x, PIL=False):
# convert an array for an image to PIL object
assert len(x.shape) <= 3
y = ((x + 1.)*127.5).astype(np.int32)
if PIL:
return Image.fromarray(y)
else:
return y
def get_input_pool(x, num_processors=2):
y = []
size_per_proc = len(x)//num_processors
for i in range(num_processors - 1):
y.append(x[i*size_per_proc:(i+1)*size_per_proc])
y.append(x[(num_processors-1)*size_per_proc:])
return y
def get_xy_in_parallel(p, imgs, num_processors=1, mode=None, in_size=(64,64,3), augmentations=None,
jitter=None, delta=1., ch_label=False):
x_in = get_input_pool(imgs, num_processors)
outs = p.starmap(get_xy, zip(x_in, repeat(mode), repeat(in_size), repeat(augmentations),
repeat(jitter), repeat(delta), repeat(ch_label)))
# Collect from multi processes
x_out = np.concatenate([out[0] for out in outs], axis=0)
y_out = np.concatenate([out[1] for out in outs], axis=0)
return x_out, y_out
def get_xy(imgs, mode=None, in_size=(64,64), augmentations=None, jitter=None, delta=1., ch_label=False):
x_ag = np.empty((len(imgs),)+in_size)
y_ag = np.zeros((len(imgs),), dtype=np.float32)
if ch_label:
if mode == 'CH-Rand':
ag_idx = random.sample(range(len(imgs)), int(len(imgs)*26/27))
elif mode == 'CH-Perm':
ag_idx = random.sample(range(len(imgs)), int(len(imgs)*5/6))
else:
ag_idx = random.sample(range(len(imgs)), len(imgs)//2)
for i in range(len(imgs)):
x = imgs[i]
# Traditional augmentation: jitter, flip
if augmentations is not None:
for augmentation in augmentations:
x = augmentation(x)
if i in ag_idx:
if mode == 'CH-Rand' or mode == 'CH-Perm':
x, c = segmentation_ch_shuffle(x, rand_pixels=True, mode=mode, sobel_app=False, delta=delta)
elif mode == 'CutPaste':
x = cut_paste(x, area_ratios=(.02,.15), aspect_widths=(.3, 1.), aspect_heights=(1.,3.3),
jitter=augmentations[0], verbose=False)
if ch_label:
y_ag[i] = c + 1 # 1 ~ n_channels
else:
y_ag[i] = 1.
x_ag[i] = standardize(x)
return x_ag, y_ag
def segmentation_ch_shuffle(x, sobel_app=False, rand_pixels=False, mode='CH-Rand', delta=1., verbose=False):
img = np.array(x)
gray_img = x.convert('L')
gray_img = np.asarray(gray_img)
mask = np.zeros_like(gray_img)
########### Where? ###########
if sobel_app:
delta = .5
q_low = np.random.random()*.5
q_high = q_low + delta
seg_mask = np.zeros_like(gray_img)
seg_mask[gray_img < np.quantile(gray_img, q_low)] = 1
seg_mask[gray_img > np.quantile(gray_img, q_high)] = 2
edges = sobel(gray_img)
segmentation = watershed(edges, seg_mask)
segmentation = ndi.binary_fill_holes(segmentation - 1)
labeled, _ = ndi.label(segmentation)
# sample from top region
max_l = np.max(labeled)
counts = np.bincount(labeled.flatten())
top_counts = sorted(counts)[-1:]
weights = np.zeros(len(counts))
for i, count in enumerate(counts):
if count >= top_counts[0]:
# weights[i] = count/np.sum(top_counts)
weights[i] = 1 #len(top_counts)
weights = weights/np.sum(weights)
shuffle_l = random.choices(np.arange(max_l+1), weights=weights)
mask[labeled == shuffle_l] = 1
# n pixels in range (a, b) where n is delta*N
else:
lower_ths = random.random() * (1 - delta)
upper_ths = lower_ths + delta
lower = np.quantile(gray_img, lower_ths)
upper = np.quantile(gray_img, upper_ths)
mask[(gray_img >= lower) & (gray_img <= upper)] = 1
mask = mask.astype(bool)
########## How? ##########
if mode == 'BLANK':
img[mask] = 0
ch_choice = 0
# channel randomisation
elif mode == 'CH-Rand':
while True:
chs = np.asarray(random.choices([0,1,2], k=3))
if not np.all(chs == np.arange(3)):
break
chs = [[0,0,0], [0,0,1], [0,0,2], [0,1,0], [0,1,1],
[0,2,0], [0,2,1], [0,2,2], [1,0,0], [1,0,1], [1,0,2],
[1,1,0], [1,1,1], [1,1,2], [1,2,0], [1,2,1], [1,2,2],
[2,0,0], [2,0,1], [2,0,2], [2,1,0], [2,1,1], [2,1,2],
[2,2,0], [2,2,1], [2,2,2]]
ch_choice = random.choice(range(len(chs)))
img[mask] = img[mask][..., chs[ch_choice]]
# channel permutation
elif mode == 'CH-Perm':
chs = [[0,2,1],[1,0,2],[1,2,0],[2,0,1],[2,1,0]]
ch_choice = random.choice(range(len(chs)))
img[mask] = img[mask][..., chs[ch_choice]]
# channel splitting
elif mode == 'CH-Split':
ch_choice = random.choice([0,1,2])
for i in range(3):
img[mask, i] = img[mask][..., ch_choice]
else:
print('No {} mode'.format(mode))
ch_choice = -1
return img, ch_choice
def cut_paste(img, area_ratios=(.02, .15), aspect_widths=(.3, 1.), aspect_heights=(1.,3.3),
verbose=False, jitter=None):
(width, height) = img.size
area_ratio = random.uniform(area_ratios[0], area_ratios[1])
max_area = height * width * area_ratio
# sample width and height ratios
aspect_width = random.uniform(aspect_widths[0], aspect_widths[1])
aspect_height = random.uniform(aspect_heights[0], aspect_heights[1])
# determine width and height
unit = np.sqrt(max_area/(aspect_width*aspect_height))
patch_width = np.minimum(int(unit * aspect_width), width)
patch_height = np.minimum(int(unit * aspect_height), height)
# cut
rand_x = random.randint(0, width-patch_width) if width-patch_width >= 1 else 0
rand_y = random.randint(0, height-patch_height) if height-patch_height >= 1 else 0
# vars for paste
while True:
rand_x_p = random.randint(0, width-patch_width) if width-patch_width >= 1 else 0
rand_y_p = random.randint(0, height-patch_height) if height-patch_height >= 1 else 0
if rand_x != rand_x_p or rand_y != rand_y_p:
break
# extract patch
patch = img.crop((rand_x, rand_y, rand_x+patch_width, rand_y+patch_height))
# jitter in patch
patch = patch if jitter is None else jitter(patch)
if verbose:
print('area ratio={:.03f}'.format(area_ratio))
print('aspect width={:.03f}, aspect height={:.03f}'.format(aspect_width, aspect_height))
print('patch width={:.03f}, patch height={:.03f}'.format(patch_width, patch_height))
print('cut from ({}, {})'.format(rand_x, rand_y))
print('paste at ({}, {})\n'.format(rand_x_p, rand_y_p))
# make numpy array
img, patch = np.array(img), np.asarray(patch)
# paste
img[rand_y_p:rand_y_p+patch_height, rand_x_p:rand_x_p+patch_width] = patch
# if random.random() < .01:
# img2 = Image.fromarray(img)
# img2.save('cutpaste_ex.png')
return img