-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyDatasets.py
More file actions
451 lines (369 loc) · 15.8 KB
/
myDatasets.py
File metadata and controls
451 lines (369 loc) · 15.8 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
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
440
441
442
443
444
445
446
447
448
449
450
import torch
import torchvision
import torchvision.transforms as transforms
import numpy as np
import os
import torch.utils.data as data
from PIL import Image
import json
from torch.utils.data import Dataset
from collections import Counter
class IMBALANCECIFAR10(torchvision.datasets.CIFAR10):
cls_num = 10
def __init__(self, root, imb_type='exp', imb_factor=0.01, rand_number=0, train=True,
transform=None, target_transform=None,
download=True):
super(IMBALANCECIFAR10, self).__init__(root, train, transform, target_transform, download)
np.random.seed(rand_number)
img_num_list = self.get_img_num_per_cls(self.cls_num, imb_type, imb_factor)
self.gen_imbalanced_data(img_num_list)
def get_img_num_per_cls(self, cls_num, imb_type, imb_factor):
img_max = len(self.data) / cls_num
img_num_per_cls = []
if imb_type == 'exp':
for cls_idx in range(cls_num):
num = img_max * (imb_factor ** (cls_idx / (cls_num - 1.0)))
img_num_per_cls.append(int(num))
elif imb_type == 'step':
for cls_idx in range(cls_num // 2):
img_num_per_cls.append(int(img_max))
for cls_idx in range(cls_num // 2):
img_num_per_cls.append(int(img_max * imb_factor))
else:
img_num_per_cls.extend([int(img_max)] * cls_num)
return img_num_per_cls
def gen_imbalanced_data(self, img_num_per_cls):
new_data = []
new_targets = []
targets_np = np.array(self.targets, dtype=np.int64)
classes = np.unique(targets_np)
# np.random.shuffle(classes)
self.num_per_cls_dict = dict()
for the_class, the_img_num in zip(classes, img_num_per_cls):
self.num_per_cls_dict[the_class] = the_img_num
idx = np.where(targets_np == the_class)[0]
np.random.shuffle(idx)
selec_idx = idx[:the_img_num]
new_data.append(self.data[selec_idx, ...])
new_targets.extend([the_class, ] * the_img_num)
new_data = np.vstack(new_data)
self.data = new_data
self.targets = new_targets
def get_cls_num_list(self):
cls_num_list = []
for i in range(self.cls_num):
cls_num_list.append(self.num_per_cls_dict[i])
return cls_num_list
# def _check_integrity(self):
# return True
class IMBALANCECIFAR100(IMBALANCECIFAR10):
"""`CIFAR100 <https://www.cs.toronto.edu/~kriz/cifar.html>`_ Dataset.
This is a subclass of the `CIFAR10` Dataset.
"""
base_folder = 'cifar-100-python'
url = "https://www.cs.toronto.edu/~kriz/cifar-100-python.tar.gz"
filename = "cifar-100-python.tar.gz"
tgz_md5 = 'eb9058c3a382ffc7106e4002c42a8d85'
train_list = [
['train', '16019d7e3df5f24257cddd939b257f8d'],
]
test_list = [
['test', 'f0ef6b0ae62326f3e7ffdfab6717acfc'],
]
meta = {
'filename': 'meta',
'key': 'fine_label_names',
'md5': '7973b15100ade9c7d40fb424638fde48',
}
cls_num = 100
# def _check_integrity(self):
# return True
class TINYIMAGENET(torchvision.datasets.ImageFolder):
cls_num = 200
def __init__(self, root, imb_type='exp', imb_factor=0.01, rand_number=0,
transform=None, target_transform=None, is_valid_file = None):
super(TINYIMAGENET, self).__init__(root, transform, target_transform, is_valid_file)
np.random.seed(rand_number)
img_num_list = self.get_img_num_per_cls(self.cls_num, imb_type, imb_factor)
self.gen_imbalanced_data(img_num_list)
# print("ImageFolder:", self.imgs[0:100])
def get_img_num_per_cls(self, cls_num, imb_type, imb_factor):
img_max = len(self.imgs) / cls_num
img_num_per_cls = []
if imb_type == 'exp':
for cls_idx in range(cls_num):
num = img_max * (imb_factor ** (cls_idx / (cls_num - 1.0)))
img_num_per_cls.append(int(num))
elif imb_type == 'step':
for cls_idx in range(cls_num // 2):
img_num_per_cls.append(int(img_max))
for cls_idx in range(cls_num // 2):
img_num_per_cls.append(int(img_max * imb_factor))
else:
img_num_per_cls.extend([int(img_max)] * cls_num)
return img_num_per_cls
def gen_imbalanced_data(self, img_num_per_cls):
new_data = []
new_targets = []
# print("img_num_per_cls:", img_num_per_cls)
# self.data = np.array([x[0] for x in self.imgs])
self.targets = [x[1] for x in self.imgs]
self.imgs = np.array(self.imgs)
# print(self.data[0:10])
targets_np = np.array(self.targets, dtype=np.int64)
classes = np.unique(targets_np)
# np.random.shuffle(classes)
self.num_per_cls_dict = dict()
for the_class, the_img_num in zip(classes, img_num_per_cls):
self.num_per_cls_dict[the_class] = the_img_num
idx = np.where(targets_np == the_class)[0]
np.random.shuffle(idx)
selec_idx = idx[:the_img_num]
new_data.extend(self.imgs[selec_idx, ...])
new_targets.extend([the_class, ] * the_img_num)
self.samples = new_data
self.targets = new_targets
def get_cls_num_list(self):
cls_num_list = []
for i in range(self.cls_num):
cls_num_list.append(self.num_per_cls_dict[i])
return cls_num_list
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
tuple: (sample, target) where target is class_index of the target class.
"""
path, target = self.samples[index]
target = int(target)
assert os.path.isfile(path) == True, "File not exists"
img = Image.open(path)
sample = img.convert('RGB')
if self.transform is not None:
sample = self.transform(sample)
if self.target_transform is not None:
target = self.target_transform(target)
return sample, target
class INAT_IMG_224(data.Dataset):
def __init__(self, root, ann_file, is_train=True):
# load annotations
print('Loading annotations from: ' + os.path.basename(ann_file))
with open(ann_file) as data_file:
ann_data = json.load(data_file)
# set up the filenames and annotations
self.imgs = [aa['file_name'] for aa in ann_data['images']]
self.ids = [aa['id'] for aa in ann_data['images']]
# if we dont have class labels set them to '0'
if 'annotations' in ann_data.keys():
self.classes = [aa['category_id'] for aa in ann_data['annotations']]
else:
self.classes = [0]*len(self.imgs)
# load taxonomy
self.tax_levels = ['id', 'genus', 'family', 'order', 'class', 'phylum', 'kingdom']
#8142, 4412, 1120, 273, 57, 25, 6
self.taxonomy, self.classes_taxonomic = load_taxonomy(ann_data, self.tax_levels, self.classes)
# print out some stats
print ('\t' + str(len(self.imgs)) + ' images')
print ('\t' + str(len(set(self.classes))) + ' classes')
self.root = root
self.is_train = is_train
self.loader = default_loader
# augmentation params
self.im_size = [224, 224] # can change this to train on higher res
print("image size 224 224")
self.mu_data = [0.466, 0.471, 0.380]
self.std_data = [0.195, 0.194, 0.192]
self.brightness = 0.4
self.contrast = 0.4
self.saturation = 0.4
self.hue = 0.25
# augmentations
self.center_crop = transforms.CenterCrop((self.im_size[0], self.im_size[1]))
self.scale_aug = transforms.RandomResizedCrop(size=self.im_size[0])
self.flip_aug = transforms.RandomHorizontalFlip()
self.color_aug = transforms.ColorJitter(self.brightness, self.contrast, self.saturation, self.hue)
self.tensor_aug = transforms.ToTensor()
self.norm_aug = transforms.Normalize(mean=self.mu_data, std=self.std_data)
def __getitem__(self, index):
path = self.root + self.imgs[index]
im_id = self.ids[index]
img = self.loader(path)
species_id = self.classes[index]
tax_ids = self.classes_taxonomic[species_id]
if self.is_train:
img = self.scale_aug(img)
img = self.flip_aug(img)
img = self.color_aug(img)
else:
img = self.center_crop(img)
img = self.tensor_aug(img)
img = self.norm_aug(img)
return img, species_id # im_id, tax_ids
def __len__(self):
return len(self.imgs)
def default_loader(path):
return Image.open(path).convert('RGB')
def load_taxonomy(ann_data, tax_levels, classes):
# loads the taxonomy data and converts to ints
taxonomy = {}
if 'categories' in ann_data.keys():
num_classes = len(ann_data['categories'])
for tt in tax_levels:
tax_data = [aa[tt] for aa in ann_data['categories']]
_, tax_id = np.unique(tax_data, return_inverse=True)
taxonomy[tt] = dict(zip(range(num_classes), list(tax_id)))
else:
# set up dummy data
for tt in tax_levels:
taxonomy[tt] = dict(zip([0], [0]))
# create a dictionary of lists containing taxonomic labels
classes_taxonomic = {}
for cc in np.unique(classes):
tax_ids = [0]*len(tax_levels)
for ii, tt in enumerate(tax_levels):
tax_ids[ii] = taxonomy[tt][cc]
classes_taxonomic[cc] = tax_ids
return taxonomy, classes_taxonomic
class INAT(data.Dataset):
def __init__(self, root, ann_file, is_train=True):
# load annotations
print('Loading annotations from: ' + os.path.basename(ann_file))
with open(ann_file) as data_file:
ann_data = json.load(data_file)
# set up the filenames and annotations
self.imgs = [aa['file_name'] for aa in ann_data['images']]
self.ids = [aa['id'] for aa in ann_data['images']]
# if we dont have class labels set them to '0'
if 'annotations' in ann_data.keys():
self.classes = [aa['category_id'] for aa in ann_data['annotations']]
else:
self.classes = [0]*len(self.imgs)
# load taxonomy
self.tax_levels = ['id', 'genus', 'family', 'order', 'class', 'phylum', 'kingdom']
#8142, 4412, 1120, 273, 57, 25, 6
self.taxonomy, self.classes_taxonomic = load_taxonomy(ann_data, self.tax_levels, self.classes)
# print out some stats
print ('\t' + str(len(self.imgs)) + ' images')
print ('\t' + str(len(set(self.classes))) + ' classes')
self.root = root
self.is_train = is_train
self.loader = default_loader
# augmentation params
self.im_size = [299, 299]
self.mu_data = [0.485, 0.456, 0.406]
self.std_data = [0.229, 0.224, 0.225]
self.brightness = 0.4
self.contrast = 0.4
self.saturation = 0.4
self.hue = 0.25
# augmentations
self.center_crop = transforms.CenterCrop((self.im_size[0], self.im_size[1]))
self.scale_aug = transforms.RandomResizedCrop(size=self.im_size[0])
self.flip_aug = transforms.RandomHorizontalFlip()
self.color_aug = transforms.ColorJitter(self.brightness, self.contrast, self.saturation, self.hue)
self.tensor_aug = transforms.ToTensor()
self.norm_aug = transforms.Normalize(mean=self.mu_data, std=self.std_data)
def __getitem__(self, index):
path = self.root + self.imgs[index]
im_id = self.ids[index]
img = self.loader(path)
species_id = self.classes[index]
tax_ids = self.classes_taxonomic[species_id]
if self.is_train:
img = self.scale_aug(img)
img = self.flip_aug(img)
img = self.color_aug(img)
else:
img = self.center_crop(img)
img = self.tensor_aug(img)
img = self.norm_aug(img)
return img, species_id #, im_id, tax_ids
def __len__(self):
return len(self.imgs)
# def get_cls_num_list(self):
# cls_num_list = []
# for i in range(self.cls_num):
# cls_num_list.append(self.num_per_cls_dict[i])
# return cls_num_list
class LT(Dataset):
def __init__(self, root, txt, transform=None):
self.img_path = []
self.labels = []
self.transform = transform
# print("LT:", txt)
# if 'test' in txt and 'ImageNet' in txt:
# with open(txt) as f:
# for line in f:
# img_name = '/'.join([line.split()[0].split('/')[0], line.split()[0].split('/')[2]])
# self.img_path.append(os.path.join(root, img_name))
# self.labels.append(int(line.split()[1]))
# else:
with open(txt) as f:
for line in f:
self.img_path.append(os.path.join(root, line.split()[0]))
self.labels.append(int(line.split()[1]))
def __len__(self):
return len(self.labels)
def __getitem__(self, index):
path = self.img_path[index]
label = self.labels[index]
with open(path, 'rb') as f:
sample = Image.open(f).convert('RGB')
if self.transform is not None:
sample = self.transform(sample)
return sample, label # , index
def get_cls_num_list(args):
if args.dataset == 'ina':
args.data_root = './data/ina/images/'
args.train_file = './data/ina/train2019.json'
# args.train_file = './data/ina/val2019.json'
args.val_file = './data/ina/val2019.json'
train_data = INAT_IMG_224(args.data_root, args.train_file, is_train=True)
class_dict = Counter(train_data.classes)
elif args.dataset == 'imagenet-LT':
args.data_root = '/dual_data/not_backed_up/imagenet-2012/ilsvrc/'
train_data = LT(args.data_root, './data/ImageNet_LT/ImageNet_LT_train.txt', transform = None)
# print("ImageNetLT # of class:", len(train_data.labels))
class_dict = Counter(train_data.labels)
elif args.dataset == 'places-LT':
args.data_root = "/dual_data/not_backed_up/places/"
train_data = LT(args.data_root, './data/Places_LT/Places_LT_train.txt', transform = None)
class_dict = Counter(train_data.labels)
elif args.dataset == 'covid-LT':
train_data = LT(args.data_root, './data/Covid_LT/' + str(args.imb_factor) + '_Covid_LT_train.txt', transform=None)
class_dict = Counter(train_data.labels)
elif args.dataset == 'iNaturalist18':
args.data_root = "/dual_data/not_backed_up/iNaturalist2018/"
train_data = LT(args.dataset, './data/iNaturalist18/iNaturalist18_train.txt', transform = None)
class_dict = Counter(train_data.labels)
cls_num_list = [value for key, value in sorted(class_dict.items())]
return cls_num_list
def get_num_classes(args):
num_classes = 0
if args.dataset == 'ina':
num_classes = 1010
elif args.dataset == 'imagenet-LT':
num_classes = 1000
elif args.dataset == 'cifar100':
num_classes = 100
elif args.dataset == 'cifar10':
num_classes = 10
elif args.dataset == 'tiny-imagenet':
num_classes = 200
elif args.dataset == 'places-LT':
num_classes = 365
elif args.dataset == 'covid-LT':
num_classes = 4
elif args.dataset == 'iNaturalist18':
num_classes = 8142
return num_classes
if __name__ == '__main__':
transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
trainset = IMBALANCECIFAR100(root='./data', train=True,
download=True, transform=transform)
trainloader = iter(trainset)
data, label = next(trainloader)
import pdb; pdb.set_trace()