-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_dataset.py
153 lines (141 loc) · 6.92 KB
/
load_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
import numpy as np
from torch.utils.data import DataLoader, Dataset
from config import *
import torchvision.transforms as T
from torchvision.datasets import CIFAR100, CIFAR10, FashionMNIST, SVHN
class MyDataset(Dataset):
def __init__(self, dataset_name, data_path, train_flag, transf):
self.dataset_name = dataset_name
if self.dataset_name == "cifar10":
self.cifar10 = CIFAR10(root=data_path, train=train_flag,
download=False, transform=transf)
if self.dataset_name == "cifar100":
self.cifar100 = CIFAR100(root=data_path, train=train_flag,
download=False, transform=transf)
if self.dataset_name == "fashionmnist":
self.fmnist = FashionMNIST('../fashionMNIST', train=train_flag,
download=True, transform=transf)
if self.dataset_name == "svhn":
self.svhn = SVHN('../svhn', split="train",
download=True, transform=transf)
if self.dataset_name == "cifar10im":
self.cifar10 = CIFAR10('../cifar10', train=train_flag,
download=True, transform=transf)
imbal_class_counts = [50, 5000] * 5
targets = np.array(self.cifar10.targets)
classes, class_counts = np.unique(targets, return_counts=True)
nb_classes = len(classes)
class_indices = [np.where(targets == i)[0] for i in range(nb_classes)]
# Get imbalanced number of instances
imbal_class_indices = [class_idx[:class_count] for class_idx, class_count in
zip(class_indices, imbal_class_counts)]
imbal_class_indices = np.hstack(imbal_class_indices)
# Set target and data to dataset
self.cifar10.targets = targets[imbal_class_indices]
self.cifar10.data = self.cifar10.data[imbal_class_indices]
def __getitem__(self, index):
if self.dataset_name == "cifar10":
data, target = self.cifar10[index]
if self.dataset_name == "cifar10im":
data, target = self.cifar10[index]
if self.dataset_name == "cifar100":
data, target = self.cifar100[index]
if self.dataset_name == "fashionmnist":
data, target = self.fmnist[index]
if self.dataset_name == "svhn":
data, target = self.svhn[index]
return data, target, index
def __len__(self):
if self.dataset_name == "cifar10":
return len(self.cifar10)
elif self.dataset_name == "cifar10im":
return len(self.cifar10)
elif self.dataset_name == "cifar100":
return len(self.cifar100)
elif self.dataset_name == "fashionmnist":
return len(self.fmnist)
elif self.dataset_name == "svhn":
return len(self.svhn)
##
# Data
def load_dataset(dataset, data_path):
train_transform_cifar10 = T.Compose([
T.RandomHorizontalFlip(),
T.RandomCrop(size=32, padding=4),
T.ToTensor(),
T.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010]) # CIFAR10
])
test_transform_cifar10 = T.Compose([
T.ToTensor(),
T.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010])
])
train_transform_cifar100 = T.Compose([
T.RandomHorizontalFlip(),
T.RandomCrop(size=32, padding=4),
T.ToTensor(),
T.Normalize((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)) # CIFAR-100
])
test_transform_cifar100 = T.Compose([
T.ToTensor(),
T.Normalize((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)) # CIFAR-100
])
if dataset == 'cifar10':
data_train = CIFAR10(root=data_path, train=True, download=False, transform=train_transform_cifar10)
data_unlabeled = MyDataset(dataset, data_path, True, test_transform_cifar10) # note: test_transform
data_test = CIFAR10(root=data_path, train=False, download=False, transform=test_transform_cifar10)
NO_CLASSES = 10
adden = ADDENDUM
NUM_TRAIN = len(data_train)
no_train = NUM_TRAIN
elif dataset == 'cifar10im':
data_train = CIFAR10('../cifar10', train=True, download=True, transform=train_transform)
# data_unlabeled = CIFAR10('../cifar10', train=True, download=True, transform=test_transform)
targets = np.array(data_train.targets)
NUM_TRAIN = targets.shape[0]
classes, class_counts = np.unique(targets, return_counts=True)
nb_classes = len(classes)
imb_class_counts = [50, 5000] * 5
class_idxs = [np.where(targets == i)[0] for i in range(nb_classes)]
imb_class_idx = [class_id[:class_count] for class_id, class_count in zip(class_idxs, imb_class_counts)]
imb_class_idx = np.hstack(imb_class_idx)
no_train = imb_class_idx.shape[0]
data_train.targets = targets[imb_class_idx]
data_train.data = data_train.data[imb_class_idx]
data_unlabeled = MyDataset(dataset, True, test_transform)
# print(len(data_unlabeled))
# data_unlabeled.cifar10.train_labels = targets[imb_class_idx]
# data_unlabeled.cifar10.train_data = data_unlabeled.cifar10.train_data[imb_class_idx]
data_test = CIFAR10('../cifar10', train=False, download=True, transform=test_transform)
NUM_TRAIN = len(data_train)
NO_CLASSES = 10
adden = ADDENDUM
no_train = NUM_TRAIN
elif dataset == 'cifar100':
data_train = CIFAR100(root=data_path, train=True, download=False, transform=train_transform_cifar100)
data_unlabeled = MyDataset(dataset, data_path, True, test_transform_cifar100)
data_test = CIFAR100(root=data_path, train=False, download=False, transform=test_transform_cifar100)
NO_CLASSES = 100
adden = 2000
NUM_TRAIN = len(data_train)
no_train = NUM_TRAIN
elif dataset == 'fashionmnist':
data_train = FashionMNIST('../fashionMNIST', train=True, download=True,
transform=T.Compose([T.ToTensor()]))
data_unlabeled = MyDataset(dataset, True, T.Compose([T.ToTensor()]))
data_test = FashionMNIST('../fashionMNIST', train=False, download=True,
transform=T.Compose([T.ToTensor()]))
NO_CLASSES = 10
NUM_TRAIN = len(data_train)
adden = ADDENDUM
no_train = NUM_TRAIN
elif dataset == 'svhn':
data_train = SVHN('../svhn', split='train', download=True,
transform=T.Compose([T.ToTensor()]))
data_unlabeled = MyDataset(dataset, True, T.Compose([T.ToTensor()]))
data_test = SVHN('../svhn', split='test', download=True,
transform=T.Compose([T.ToTensor()]))
NO_CLASSES = 10
NUM_TRAIN = len(data_train)
adden = ADDENDUM
no_train = NUM_TRAIN
return data_train, data_unlabeled, data_test, adden, NO_CLASSES, no_train