-
Notifications
You must be signed in to change notification settings - Fork 16
/
datasets.py
219 lines (179 loc) · 8.69 KB
/
datasets.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
import os
from torchvision import transforms
from timm.data.constants import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD
from timm.data import create_transform
import numpy as np
import torch
from torch.utils.data import Dataset
import PIL.Image
def load_img_name_list(dataset_path):
img_gt_name_list = open(dataset_path).readlines()
img_name_list = [img_gt_name.strip() for img_gt_name in img_gt_name_list]
return img_name_list
def load_image_label_list_from_npy(img_name_list, label_file_path=None):
if label_file_path is None:
label_file_path = 'voc12/cls_labels.npy'
cls_labels_dict = np.load(label_file_path, allow_pickle=True).item()
label_list = []
for id in img_name_list:
if id not in cls_labels_dict.keys():
img_name = id + '.jpg'
else:
img_name = id
label_list.append(cls_labels_dict[img_name])
return label_list
# return [cls_labels_dict[img_name] for img_name in img_name_list ]
class COCOClsDataset(Dataset):
def __init__(self, img_name_list_path, coco_root, label_file_path, train=True, transform=None, gen_attn=False):
img_name_list_path = os.path.join(img_name_list_path, f'{"train" if train or gen_attn else "val"}_id.txt')
self.img_name_list = load_img_name_list(img_name_list_path)
self.label_list = load_image_label_list_from_npy(self.img_name_list, label_file_path)
self.coco_root = coco_root
self.transform = transform
self.train = train
self.gen_attn = gen_attn
def __getitem__(self, idx):
name = self.img_name_list[idx]
if self.train or self.gen_attn :
img = PIL.Image.open(os.path.join(self.coco_root, 'train2014', name + '.jpg')).convert("RGB")
else:
img = PIL.Image.open(os.path.join(self.coco_root, 'val2014', name + '.jpg')).convert("RGB")
label = torch.from_numpy(self.label_list[idx])
if self.transform:
img = self.transform(img)
return img, label
def __len__(self):
return len(self.img_name_list)
class COCOClsDatasetMS(Dataset):
def __init__(self, img_name_list_path, coco_root, label_file_path, scales, train=True, transform=None, gen_attn=False, unit=1):
img_name_list_path = os.path.join(img_name_list_path, f'{"train" if train or gen_attn else "val"}_id.txt')
self.img_name_list = load_img_name_list(img_name_list_path)
self.label_list = load_image_label_list_from_npy(self.img_name_list, label_file_path)
self.coco_root = coco_root
self.transform = transform
self.train = train
self.unit = unit
self.scales = scales
self.gen_attn = gen_attn
def __getitem__(self, idx):
name = self.img_name_list[idx]
if self.train or self.gen_attn:
img = PIL.Image.open(os.path.join(self.coco_root, 'train2014', name + '.jpg')).convert("RGB")
else:
img = PIL.Image.open(os.path.join(self.coco_root, 'val2014', name + '.jpg')).convert("RGB")
label = torch.from_numpy(self.label_list[idx])
rounded_size = (int(round(img.size[0] / self.unit) * self.unit), int(round(img.size[1] / self.unit) * self.unit))
ms_img_list = []
for s in self.scales:
target_size = (round(rounded_size[0] * s),
round(rounded_size[1] * s))
s_img = img.resize(target_size, resample=PIL.Image.CUBIC)
ms_img_list.append(s_img)
if self.transform:
for i in range(len(ms_img_list)):
ms_img_list[i] = self.transform(ms_img_list[i])
msf_img_list = []
for i in range(len(ms_img_list)):
msf_img_list.append(ms_img_list[i])
# msf_img_list.append(np.flip(ms_img_list[i], -1).copy())
msf_img_list.append(torch.flip(ms_img_list[i], [-1]))
return msf_img_list, label
def __len__(self):
return len(self.img_name_list)
class VOC12Dataset(Dataset):
def __init__(self, img_name_list_path, voc12_root, train=True, transform=None, gen_attn=False):
img_name_list_path = os.path.join(img_name_list_path, f'{"train_aug" if train or gen_attn else "val"}_id.txt')
self.img_name_list = load_img_name_list(img_name_list_path)
self.label_list = load_image_label_list_from_npy(self.img_name_list)
self.voc12_root = voc12_root
self.transform = transform
def __getitem__(self, idx):
name = self.img_name_list[idx]
img = PIL.Image.open(os.path.join(self.voc12_root, 'JPEGImages', name + '.jpg')).convert("RGB")
label = torch.from_numpy(self.label_list[idx])
if self.transform:
img = self.transform(img)
return img, label
def __len__(self):
return len(self.img_name_list)
class VOC12DatasetMS(Dataset):
def __init__(self, img_name_list_path, voc12_root, scales, train=True, transform=None, gen_attn=False, unit=1):
img_name_list_path = os.path.join(img_name_list_path, f'{"train_aug" if train or gen_attn else "val"}_id.txt')
self.img_name_list = load_img_name_list(img_name_list_path)
self.label_list = load_image_label_list_from_npy(self.img_name_list)
self.voc12_root = voc12_root
self.transform = transform
self.unit = unit
self.scales = scales
def __getitem__(self, idx):
name = self.img_name_list[idx]
img = PIL.Image.open(os.path.join(self.voc12_root, 'JPEGImages', name + '.jpg')).convert("RGB")
label = torch.from_numpy(self.label_list[idx])
rounded_size = (int(round(img.size[0] / self.unit) * self.unit), int(round(img.size[1] / self.unit) * self.unit))
ms_img_list = []
for s in self.scales:
target_size = (round(rounded_size[0] * s),
round(rounded_size[1] * s))
s_img = img.resize(target_size, resample=PIL.Image.CUBIC)
ms_img_list.append(s_img)
if self.transform:
for i in range(len(ms_img_list)):
ms_img_list[i] = self.transform(ms_img_list[i])
msf_img_list = []
for i in range(len(ms_img_list)):
msf_img_list.append(ms_img_list[i])
msf_img_list.append(torch.flip(ms_img_list[i], [-1]))
return msf_img_list, label
def __len__(self):
return len(self.img_name_list)
def build_dataset(is_train, args, gen_attn=False):
transform = build_transform(is_train, args)
dataset = None
nb_classes = None
if args.data_set == 'VOC12':
dataset = VOC12Dataset(img_name_list_path=args.img_list, voc12_root=args.data_path,
train=is_train, gen_attn=gen_attn, transform=transform)
nb_classes = 20
elif args.data_set == 'VOC12MS':
dataset = VOC12DatasetMS(img_name_list_path=args.img_list, voc12_root=args.data_path, scales=tuple(args.scales),
train=is_train, gen_attn=gen_attn, transform=transform)
nb_classes = 20
elif args.data_set == 'COCO':
dataset = COCOClsDataset(img_name_list_path=args.img_list, coco_root=args.data_path, label_file_path=args.label_file_path,
train=is_train, gen_attn=gen_attn, transform=transform)
nb_classes = 80
elif args.data_set == 'COCOMS':
dataset = COCOClsDatasetMS(img_name_list_path=args.img_list, coco_root=args.data_path, scales=tuple(args.scales), label_file_path=args.label_file_path,
train=is_train, gen_attn=gen_attn, transform=transform)
nb_classes = 80
return dataset, nb_classes
def build_transform(is_train, args):
resize_im = args.input_size > 32
if is_train:
# this should always dispatch to transforms_imagenet_train
transform = create_transform(
input_size=args.input_size,
is_training=True,
color_jitter=args.color_jitter,
auto_augment=args.aa,
interpolation=args.train_interpolation,
re_prob=args.reprob,
re_mode=args.remode,
re_count=args.recount,
)
if not resize_im:
# replace RandomResizedCropAndInterpolation with
# RandomCrop
transform.transforms[0] = transforms.RandomCrop(
args.input_size, padding=4)
return transform
t = []
if resize_im and not args.gen_attention_maps:
size = int((256 / 224) * args.input_size)
t.append(
transforms.Resize(size, interpolation=3), # to maintain same ratio w.r.t. 224 images
)
t.append(transforms.CenterCrop(args.input_size))
t.append(transforms.ToTensor())
t.append(transforms.Normalize(IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD))
return transforms.Compose(t)