-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWSSSDatasets4twoStage.py
439 lines (315 loc) · 13.1 KB
/
WSSSDatasets4twoStage.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import numpy as np
import torch
from torch.utils.data import Dataset
import os.path
import imageio
import random
import numpy as np
from PIL import Image
### need namelist, cls_label.npy
"""main usage
- train_cam: VOC12ClassificationDataset
- require: image_name_list
- return
- name: str
- img: resize_long crop
- label: Tensor cls_label
- infer cam: VOC12ClassificationDatasetMSF
- require: image_name_list, scales
- return
- name: str
- img: List[imgs]
- size: (img.shape[0], img.shape[1])
- label: Tensor cls_label
- train aff: VOC12AffinityDataset
- require:
- image_name_list
- label_dir: to be accessed in code to save label
- indicie_from ???
- indicie_to ???
- return ???
- aff_bg_pos_label
- aff_fg_pos_label
- aff_neg_label
- train seg: VOC12SegmentationDataset
-require: label_dir, image_name_list
-return:
- name
- img
- label: [h, w]
"""
IMG_FOLDER_NAME = "JPEGImages"
IGNORE = 255
CAT_LIST = ['aeroplane', 'bicycle', 'bird', 'boat',
'bottle', 'bus', 'car', 'cat', 'chair',
'cow', 'diningtable', 'dog', 'horse',
'motorbike', 'person', 'pottedplant',
'sheep', 'sofa', 'train',
'tvmonitor']
N_CAT = len(CAT_LIST)
CAT_NAME_TO_NUM = dict(zip(CAT_LIST,range(len(CAT_LIST))))
cls_labels_dict = np.load('metadata/voc12/cls_labels.npy', allow_pickle=True).item()
def decode_int_filename(int_filename):
s = str(int(int_filename))
return s[:4] + '_' + s[4:]
def load_image_label_list_from_npy(img_name_list):
return np.array([cls_labels_dict[decode_int_filename(img_name)] for img_name in img_name_list])
def get_img_path(img_name, voc12_root):
if not isinstance(img_name, str):
img_name = decode_int_filename(img_name)
return os.path.join(voc12_root, IMG_FOLDER_NAME, img_name + '.jpg')
def load_img_name_list(dataset_path):
img_name_list = np.loadtxt(dataset_path, dtype=np.int32)
return img_name_list
class TorchvisionNormalize():
def __init__(self, mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)):
self.mean = mean
self.std = std
def __call__(self, img):
imgarr = np.asarray(img)
proc_img = np.empty_like(imgarr, np.float32)
proc_img[..., 0] = (imgarr[..., 0] / 255. - self.mean[0]) / self.std[0]
proc_img[..., 1] = (imgarr[..., 1] / 255. - self.mean[1]) / self.std[1]
proc_img[..., 2] = (imgarr[..., 2] / 255. - self.mean[2]) / self.std[2]
return proc_img
class GetAffinityLabelFromIndices():
def __init__(self, indices_from, indices_to):
self.indices_from = indices_from
self.indices_to = indices_to
def __call__(self, segm_map):
segm_map_flat = np.reshape(segm_map, -1)
segm_label_from = np.expand_dims(segm_map_flat[self.indices_from], axis=0)
segm_label_to = segm_map_flat[self.indices_to]
valid_label = np.logical_and(np.less(segm_label_from, 21), np.less(segm_label_to, 21))
equal_label = np.equal(segm_label_from, segm_label_to)
pos_affinity_label = np.logical_and(equal_label, valid_label)
bg_pos_affinity_label = np.logical_and(pos_affinity_label, np.equal(segm_label_from, 0)).astype(np.float32)
fg_pos_affinity_label = np.logical_and(pos_affinity_label, np.greater(segm_label_from, 0)).astype(np.float32)
neg_affinity_label = np.logical_and(np.logical_not(equal_label), valid_label).astype(np.float32)
return torch.from_numpy(bg_pos_affinity_label), torch.from_numpy(fg_pos_affinity_label), \
torch.from_numpy(neg_affinity_label)
class VOC12ImageDataset(Dataset):
def __init__(self, img_name_list_path, voc12_root,
resize_long=None, rescale=None, img_normal=TorchvisionNormalize(), hor_flip=False,
crop_size=None, crop_method=None, to_torch=True):
self.img_name_list = load_img_name_list(img_name_list_path)
self.voc12_root = voc12_root
self.resize_long = resize_long
self.rescale = rescale
self.crop_size = crop_size
self.img_normal = img_normal
self.hor_flip = hor_flip
self.crop_method = crop_method
self.to_torch = to_torch
def __len__(self):
return len(self.img_name_list)
def __getitem__(self, idx):
name = self.img_name_list[idx]
name_str = decode_int_filename(name)
img = np.asarray(imageio.imread(get_img_path(name_str, self.voc12_root)))
if self.resize_long:
img = random_resize_long(img, self.resize_long[0], self.resize_long[1])
if self.rescale:
img = random_scale(img, scale_range=self.rescale, order=3)
if self.img_normal:
img = self.img_normal(img)
if self.hor_flip:
img = random_lr_flip(img)
if self.crop_size:
if self.crop_method == "random":
img = random_crop(img, self.crop_size, 0)
else:
img = top_left_crop(img, self.crop_size, 0)
if self.to_torch:
img = HWC_to_CHW(img)
return {'name': name_str, 'img': img}
class VOC12ClassificationDataset(VOC12ImageDataset):
def __init__(self, img_name_list_path, voc12_root,
resize_long=None, rescale=None, img_normal=TorchvisionNormalize(), hor_flip=False,
crop_size=None, crop_method=None):
super().__init__(img_name_list_path, voc12_root,
resize_long, rescale, img_normal, hor_flip,
crop_size, crop_method)
self.label_list = load_image_label_list_from_npy(self.img_name_list)
def __getitem__(self, idx):
out = super().__getitem__(idx)
out['label'] = torch.from_numpy(self.label_list[idx])
return out
class VOC12ClassificationDatasetMSF(VOC12ClassificationDataset):
def __init__(self, img_name_list_path, voc12_root,
img_normal=TorchvisionNormalize(),
scales=(1.0,)):
self.scales = scales
super().__init__(img_name_list_path, voc12_root, img_normal=img_normal)
self.scales = scales
def __getitem__(self, idx):
name = self.img_name_list[idx]
name_str = decode_int_filename(name)
img = imageio.imread(get_img_path(name_str, self.voc12_root))
ms_img_list = []
for s in self.scales:
if s == 1:
s_img = img
else:
s_img = pil_rescale(img, s, order=3)
s_img = self.img_normal(s_img)
s_img = HWC_to_CHW(s_img)
ms_img_list.append(np.stack([s_img, np.flip(s_img, -1)], axis=0))
if len(self.scales) == 1:
ms_img_list = ms_img_list[0]
out = {"name": name_str, "img": ms_img_list, "size": (img.shape[0], img.shape[1]),
"label": torch.from_numpy(self.label_list[idx])}
return out
class VOC12SegmentationDataset(Dataset):
def __init__(self, img_name_list_path, label_dir, crop_size, voc12_root,
rescale=None, img_normal=TorchvisionNormalize(), hor_flip=False,
crop_method = 'random'):
self.img_name_list = load_img_name_list(img_name_list_path)
self.voc12_root = voc12_root
self.label_dir = label_dir
self.rescale = rescale
self.crop_size = crop_size
self.img_normal = img_normal
self.hor_flip = hor_flip
self.crop_method = crop_method
def __len__(self):
return len(self.img_name_list)
def __getitem__(self, idx):
name = self.img_name_list[idx]
name_str = decode_int_filename(name)
img = imageio.imread(get_img_path(name_str, self.voc12_root))
label = imageio.imread(os.path.join(self.label_dir, name_str + '.png'))
img = np.asarray(img)
if self.rescale:
img, label = random_scale((img, label), scale_range=self.rescale, order=(3, 0))
if self.img_normal:
img = self.img_normal(img)
if self.hor_flip:
img, label = random_lr_flip((img, label))
if self.crop_method == "random":
img, label = random_crop((img, label), self.crop_size, (0, 255))
else:
img = top_left_crop(img, self.crop_size, 0)
label = top_left_crop(label, self.crop_size, 255)
img = HWC_to_CHW(img)
return {'name': name, 'img': img, 'label': label}
class VOC12AffinityDataset(VOC12SegmentationDataset):
def __init__(self, img_name_list_path, label_dir, crop_size, voc12_root,
indices_from, indices_to,
rescale=None, img_normal=TorchvisionNormalize(), hor_flip=False, crop_method=None):
super().__init__(img_name_list_path, label_dir, crop_size, voc12_root, rescale, img_normal, hor_flip, crop_method=crop_method)
self.extract_aff_lab_func = GetAffinityLabelFromIndices(indices_from, indices_to)
def __len__(self):
return len(self.img_name_list)
def __getitem__(self, idx):
out = super().__getitem__(idx)
reduced_label = pil_rescale(out['label'], 0.25, 0)
out['aff_bg_pos_label'], out['aff_fg_pos_label'], out['aff_neg_label'] = self.extract_aff_lab_func(reduced_label)
return out
#----------------------------------------transforms
def pil_resize(img, size, order):
if size[0] == img.shape[0] and size[1] == img.shape[1]:
return img
if order == 3:
resample = Image.BICUBIC
elif order == 0:
resample = Image.NEAREST
return np.asarray(Image.fromarray(img).resize(size[::-1], resample))
def pil_rescale(img, scale, order):
height, width = img.shape[:2]
target_size = (int(np.round(height*scale)), int(np.round(width*scale)))
return pil_resize(img, target_size, order)
def random_resize_long(img, min_long, max_long):
target_long = random.randint(min_long, max_long)
h, w = img.shape[:2]
if w < h:
scale = target_long / h
else:
scale = target_long / w
return pil_rescale(img, scale, 3)
def random_scale(img, scale_range, order):
target_scale = scale_range[0] + random.random() * (scale_range[1] - scale_range[0])
if isinstance(img, tuple):
return (pil_rescale(img[0], target_scale, order[0]), pil_rescale(img[1], target_scale, order[1]))
else:
return pil_rescale(img[0], target_scale, order)
def random_lr_flip(img):
if bool(random.getrandbits(1)):
if isinstance(img, tuple):
return [np.fliplr(m) for m in img]
else:
return np.fliplr(img)
else:
return img
def get_random_crop_box(imgsize, cropsize):
h, w = imgsize
ch = min(cropsize, h)
cw = min(cropsize, w)
w_space = w - cropsize
h_space = h - cropsize
if w_space > 0:
cont_left = 0
img_left = random.randrange(w_space + 1)
else:
cont_left = random.randrange(-w_space + 1)
img_left = 0
if h_space > 0:
cont_top = 0
img_top = random.randrange(h_space + 1)
else:
cont_top = random.randrange(-h_space + 1)
img_top = 0
return cont_top, cont_top+ch, cont_left, cont_left+cw, img_top, img_top+ch, img_left, img_left+cw
def random_crop(images, cropsize, default_values):
if isinstance(images, np.ndarray): images = (images,)
if isinstance(default_values, int): default_values = (default_values,)
imgsize = images[0].shape[:2]
box = get_random_crop_box(imgsize, cropsize)
new_images = []
for img, f in zip(images, default_values):
if len(img.shape) == 3:
cont = np.ones((cropsize, cropsize, img.shape[2]), img.dtype)*f
else:
cont = np.ones((cropsize, cropsize), img.dtype)*f
cont[box[0]:box[1], box[2]:box[3]] = img[box[4]:box[5], box[6]:box[7]]
new_images.append(cont)
if len(new_images) == 1:
new_images = new_images[0]
return new_images
def top_left_crop(img, cropsize, default_value):
h, w = img.shape[:2]
ch = min(cropsize, h)
cw = min(cropsize, w)
if len(img.shape) == 2:
container = np.ones((cropsize, cropsize), img.dtype)*default_value
else:
container = np.ones((cropsize, cropsize, img.shape[2]), img.dtype)*default_value
container[:ch, :cw] = img[:ch, :cw]
return container
def center_crop(img, cropsize, default_value=0):
h, w = img.shape[:2]
ch = min(cropsize, h)
cw = min(cropsize, w)
sh = h - cropsize
sw = w - cropsize
if sw > 0:
cont_left = 0
img_left = int(round(sw / 2))
else:
cont_left = int(round(-sw / 2))
img_left = 0
if sh > 0:
cont_top = 0
img_top = int(round(sh / 2))
else:
cont_top = int(round(-sh / 2))
img_top = 0
if len(img.shape) == 2:
container = np.ones((cropsize, cropsize), img.dtype)*default_value
else:
container = np.ones((cropsize, cropsize, img.shape[2]), img.dtype)*default_value
container[cont_top:cont_top+ch, cont_left:cont_left+cw] = \
img[img_top:img_top+ch, img_left:img_left+cw]
return container
def HWC_to_CHW(img):
return np.transpose(img, (2, 0, 1))