-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
324 lines (299 loc) · 14.5 KB
/
dataset.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
"""
General utils for training, evaluation and data loading
"""
import os
import torch
import pickle
import numpy as np
import torchvision.transforms as transforms
from PIL import Image
from utils.config import BASE_DIR, N_ATTRIBUTES, concept_samples_10p, concept_samples_20p, concept_samples_30p, concept_samples_40p, concept_samples_50p
from torch.utils.data import BatchSampler
from torch.utils.data import Dataset, DataLoader
class CUBDataset(Dataset):
"""
Returns a compatible Torch Dataset object customized for the CUB dataset
"""
def __init__(self, pkl_file_paths, use_attr, no_img, uncertain_label,
image_dir, n_class_attr, transform=None, concept_percent=100):
"""
Arguments:
pkl_file_paths: list of full path to all the pkl data
use_attr: whether to load the attributes (e.g. False for simple finetune)
no_img: whether to load the images (e.g. False for A -> Y model)
uncertain_label: if True, use 'uncertain_attribute_label' field (i.e. label weighted by uncertainty score, e.g. 1 & 3(probably) -> 0.75)
image_dir: default = 'images'. Will be append to the parent dir
n_class_attr: number of classes to predict for each attribute. If 3, then make a separate class for not visible
transform: whether to apply any special transformation. Default = None, i.e. use standard ImageNet preprocessing
concept_percent: the ratio of concepts that are left
"""
self.data = []
self.is_train = any(["train" in path for path in pkl_file_paths])
if not self.is_train:
assert any([("test" in path) or ("val" in path) for path in pkl_file_paths])
for file_path in pkl_file_paths:
self.data.extend(pickle.load(open(file_path, 'rb')))
self.transform = transform
self.use_attr = use_attr
self.no_img = no_img
self.uncertain_label = uncertain_label
self.image_dir = image_dir
self.n_class_attr = n_class_attr
self.concept_percent = concept_percent
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
img_data = self.data[idx]
img_path = img_data['img_path']
# Trim unnecessary paths
try:
idx = img_path.split('/').index('CUB_200_2011')
if self.image_dir != 'images':
img_path = '/'.join([self.image_dir] + img_path.split('/')[idx+1:])
img_path = img_path.replace('images/', '')
else:
img_path = os.path.join(BASE_DIR, '/'.join(img_path.split('/')[idx:]))
img = Image.open(img_path).convert('RGB')
except:
img_path_split = img_path.split('/')
split = 'train' if self.is_train else 'test'
img_path = os.path.join(BASE_DIR, '/'.join(img_path_split[:2] + [split] + img_path_split[2:]))
img = Image.open(img_path).convert('RGB')
class_label = img_data['class_label']
if self.transform:
img = self.transform(img)
if self.use_attr:
if self.uncertain_label:
attr_label = img_data['uncertain_attribute_label']
else:
attr_label = img_data['attribute_label']
if self.concept_percent == 10:
attr_label = [attr_label[index] for index in concept_samples_10p]
elif self.concept_percent == 20:
attr_label = [attr_label[index] for index in concept_samples_20p]
elif self.concept_percent == 30:
attr_label = [attr_label[index] for index in concept_samples_30p]
elif self.concept_percent == 40:
attr_label = [attr_label[index] for index in concept_samples_40p]
elif self.concept_percent == 50:
attr_label = [attr_label[index] for index in concept_samples_50p]
elif self.concept_percent != 100:
print("Current percent is not supported. We use all concepts instead.")
if self.no_img:
if self.n_class_attr == 3:
one_hot_attr_label = np.zeros((N_ATTRIBUTES, self.n_class_attr))
one_hot_attr_label[np.arange(N_ATTRIBUTES), attr_label] = 1
return one_hot_attr_label, class_label
else:
return attr_label, class_label
else:
return img, class_label, attr_label
else:
return img, class_label
class DermDataset(Dataset):
"""
Returns a compatible Torch Dataset object customized for the Derm7pt dataset
"""
def __init__(self, pkl_file_paths, use_attr, no_img, uncertain_label,
image_dir, n_class_attr, transform=None, concept_percent=100):
"""
Arguments:
pkl_file_paths: list of full path to all the pkl data
use_attr: whether to load the attributes (e.g. False for simple finetune)
no_img: whether to load the images (e.g. False for A -> Y model)
uncertain_label: if True, use 'uncertain_attribute_label' field (i.e. label weighted by uncertainty score, e.g. 1 & 3(probably) -> 0.75)
image_dir: default = 'images'. Will be append to the parent dir
n_class_attr: number of classes to predict for each attribute. If 3, then make a separate class for not visible
transform: whether to apply any special transformation. Default = None, i.e. use standard ImageNet preprocessing
concept_percent: the ratio of concepts that are left
"""
self.data = []
self.is_train = any(["train" in path for path in pkl_file_paths])
if not self.is_train:
assert any([("test" in path) or ("val" in path) for path in pkl_file_paths])
for file_path in pkl_file_paths:
self.data.extend(pickle.load(open(file_path, 'rb')))
self.transform = transform
self.use_attr = use_attr
self.no_img = no_img
self.uncertain_label = uncertain_label
self.image_dir = image_dir
self.n_class_attr = n_class_attr
self.concept_percent = concept_percent
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
img_data = self.data[idx]
img_path = img_data['img_path']
# Trim unnecessary paths
try:
idx = img_path.split('/').index('release_v0')
if self.image_dir != 'images':
img_path = '/'.join([self.image_dir] + img_path.split('/')[idx+1:])
img_path = img_path.replace('images/', '')
else:
img_path = os.path.join(BASE_DIR, '/'.join(img_path.split('/')[idx:]))
img = Image.open(img_path).convert('RGB')
except:
img_path_split = img_path.split('/')
split = 'train' if self.is_train else 'test'
img_path = os.path.join(BASE_DIR, '/'.join(img_path_split[:2] + [split] + img_path_split[2:]))
img = Image.open(img_path).convert('RGB')
class_label = img_data['class_label']
if self.transform:
img = self.transform(img)
if self.use_attr:
if self.uncertain_label:
attr_label = img_data['uncertain_attribute_label']
else:
attr_label = img_data['attribute_label']
if self.concept_percent == 10:
attr_label = [attr_label[index] for index in concept_samples_10p]
elif self.concept_percent == 20:
attr_label = [attr_label[index] for index in concept_samples_20p]
elif self.concept_percent == 30:
attr_label = [attr_label[index] for index in concept_samples_30p]
elif self.concept_percent == 40:
attr_label = [attr_label[index] for index in concept_samples_40p]
elif self.concept_percent == 50:
attr_label = [attr_label[index] for index in concept_samples_50p]
elif self.concept_percent != 100:
print("Current percent is not supported. We use all concepts instead.")
if self.no_img:
if self.n_class_attr == 3:
one_hot_attr_label = np.zeros((N_ATTRIBUTES, self.n_class_attr))
one_hot_attr_label[np.arange(N_ATTRIBUTES), attr_label] = 1
return one_hot_attr_label, class_label
else:
return attr_label, class_label
else:
return img, class_label, attr_label
else:
return img, class_label
class ImbalancedDatasetSampler(torch.utils.data.sampler.Sampler):
"""Samples elements randomly from a given list of indices for imbalanced dataset
Arguments:
indices (list, optional): a list of indices
num_samples (int, optional): number of samples to draw
"""
def __init__(self, dataset, indices=None):
# if indices is not provided,
# all elements in the dataset will be considered
self.indices = list(range(len(dataset))) \
if indices is None else indices
# if num_samples is not provided,
# draw `len(indices)` samples in each iteration
self.num_samples = len(self.indices)
# distribution of classes in the dataset
label_to_count = {}
for idx in self.indices:
label = self._get_label(dataset, idx)
if label in label_to_count:
label_to_count[label] += 1
else:
label_to_count[label] = 1
# weight for each sample
weights = [1.0 / label_to_count[self._get_label(dataset, idx)]
for idx in self.indices]
self.weights = torch.DoubleTensor(weights)
def _get_label(self, dataset, idx): # Note: for single attribute dataset
return dataset.data[idx]['attribute_label'][0]
def __iter__(self):
idx = (self.indices[i] for i in torch.multinomial(
self.weights, self.num_samples, replacement=True))
return idx
def __len__(self):
return self.num_samples
def load_data(pkl_paths, use_attr, no_img, batch_size, uncertain_label=False, n_class_attr=2, image_dir='images', resampling=False, resol=299, concept_percent = 100, dataset = 'CUB', training_opt=None):
"""
Note: Inception needs (299,299,3) images with inputs scaled between -1 and 1
Loads data with transformations applied, and upsample the minority class if there is class imbalance and weighted loss is not used
NOTE: resampling is customized for first attribute only, so change sampler.py if necessary
concept_percent: the ratio of concepts that are left
dataset: to choose which dataset class
training_opt: Sometimes we need to control `is_training` depite that 'train.pkl' is in or not in pkl_paths
"""
resized_resol = int(resol * 256/224)
is_training = any(['train.pkl' in f for f in pkl_paths])
if training_opt is not None:
is_training = training_opt
if is_training:
transform = transforms.Compose([
#transforms.Resize((resized_resol, resized_resol)),
#transforms.RandomSizedCrop(resol),
transforms.ColorJitter(brightness=32/255, saturation=(0.5, 1.5)),
transforms.RandomResizedCrop(resol),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(), #implicitly divides by 255
transforms.Normalize(mean = [0.5, 0.5, 0.5], std = [2, 2, 2])
#transforms.Normalize(mean = [ 0.485, 0.456, 0.406 ], std = [ 0.229, 0.224, 0.225 ]),
])
else:
transform = transforms.Compose([
#transforms.Resize((resized_resol, resized_resol)),
transforms.CenterCrop(resol),
transforms.ToTensor(), #implicitly divides by 255
transforms.Normalize(mean = [0.5, 0.5, 0.5], std = [2, 2, 2])
#transforms.Normalize(mean = [ 0.485, 0.456, 0.406 ], std = [ 0.229, 0.224, 0.225 ]),
])
if dataset == 'CUB':
dataset = CUBDataset(pkl_paths, use_attr, no_img, uncertain_label, image_dir, n_class_attr, transform, concept_percent)
elif dataset == 'Derm':
dataset = DermDataset(pkl_paths, use_attr, no_img, uncertain_label, image_dir, n_class_attr, transform,
concept_percent)
if is_training:
drop_last = True
shuffle = True
else:
drop_last = False
shuffle = False
if resampling:
sampler = BatchSampler(ImbalancedDatasetSampler(dataset), batch_size=batch_size, drop_last=drop_last)
loader = DataLoader(dataset, batch_sampler=sampler, num_workers=16)
else:
loader = DataLoader(dataset, batch_size=batch_size, shuffle=shuffle, drop_last=drop_last, num_workers=16)
return loader
def find_class_imbalance(pkl_file, multiple_attr=False, attr_idx=-1, concept_percent=100):
"""
Calculate class imbalance ratio for binary attribute labels stored in pkl_file
If attr_idx >= 0, then only return ratio for the corresponding attribute id
If multiple_attr is True, then return imbalance ratio separately for each attribute. Else, calculate the overall imbalance across all attributes
concept_percent: the ratio of concepts that are left
"""
imbalance_ratio = []
data = pickle.load(open(pkl_file, 'rb'))
n = len(data)
n_attr = len(data[0]['attribute_label']) * concept_percent // 100
if attr_idx >= 0:
n_attr = 1
if multiple_attr:
n_ones = [0] * n_attr
total = [n] * n_attr
else:
n_ones = [0]
total = [n * n_attr]
for d in data:
labels = d['attribute_label']
if concept_percent == 50:
labels = [labels[index] for index in concept_samples_50p]
elif concept_percent == 40:
labels = [labels[index] for index in concept_samples_40p]
elif concept_percent == 30:
labels = [labels[index] for index in concept_samples_30p]
elif concept_percent == 20:
labels = [labels[index] for index in concept_samples_20p]
elif concept_percent == 10:
labels = [labels[index] for index in concept_samples_10p]
if multiple_attr:
for i in range(n_attr):
n_ones[i] += labels[i]
else:
if attr_idx >= 0:
n_ones[0] += labels[attr_idx]
else:
n_ones[0] += sum(labels)
for j in range(len(n_ones)):
imbalance_ratio.append(total[j]/n_ones[j] - 1)
if not multiple_attr: #e.g. [9.0] --> [9.0] * 312
imbalance_ratio *= n_attr
return imbalance_ratio