-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathdata.py
254 lines (211 loc) · 8.94 KB
/
data.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
import logging
import math
import numpy as np
from PIL import Image
from torchvision import datasets
from torchvision import transforms
from torchvision.transforms import InterpolationMode
from augmentation import RandAugmentCIFAR
logger = logging.getLogger(__name__)
cifar10_mean = (0.491400, 0.482158, 0.4465231)
cifar10_std = (0.247032, 0.243485, 0.2615877)
cifar100_mean = (0.507075, 0.486549, 0.440918)
cifar100_std = (0.267334, 0.256438, 0.276151)
normal_mean = (0.5, 0.5, 0.5)
normal_std = (0.5, 0.5, 0.5)
def get_cifar10(args):
if args.randaug:
n, m = args.randaug
else:
n, m = 2, 10 # default
transform_labeled = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(size=args.resize,
padding=int(args.resize * 0.125),
fill=128,
padding_mode='constant'),
transforms.ToTensor(),
transforms.Normalize(mean=cifar10_mean, std=cifar10_std),
])
transform_finetune = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(size=args.resize,
padding=int(args.resize * 0.125),
fill=128,
padding_mode='constant'),
RandAugmentCIFAR(n=n, m=m),
transforms.ToTensor(),
transforms.Normalize(mean=cifar10_mean, std=cifar10_std),
])
transform_val = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=cifar10_mean, std=cifar10_std)
])
base_dataset = datasets.CIFAR10(args.data_path, train=True, download=True)
train_labeled_idxs, train_unlabeled_idxs, finetune_idxs = x_u_split(args, base_dataset.targets)
train_labeled_dataset = CIFAR10SSL(
args.data_path, train_labeled_idxs, train=True,
transform=transform_labeled
)
finetune_dataset = CIFAR10SSL(
args.data_path, finetune_idxs, train=True,
transform=transform_finetune
)
train_unlabeled_dataset = CIFAR10SSL(
args.data_path, train_unlabeled_idxs,
train=True,
transform=TransformMPL(args, mean=cifar10_mean, std=cifar10_std)
)
test_dataset = datasets.CIFAR10(args.data_path, train=False,
transform=transform_val, download=False)
return train_labeled_dataset, train_unlabeled_dataset, test_dataset, finetune_dataset
def get_cifar100(args):
if args.randaug:
n, m = args.randaug
else:
n, m = 2, 10 # default
transform_labeled = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(size=args.resize,
padding=int(args.resize * 0.125),
fill=128,
padding_mode='constant'),
transforms.ToTensor(),
transforms.Normalize(mean=cifar100_mean, std=cifar100_std)])
transform_finetune = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(size=args.resize,
padding=int(args.resize * 0.125),
fill=128,
padding_mode='constant'),
RandAugmentCIFAR(n=n, m=m),
transforms.ToTensor(),
transforms.Normalize(mean=cifar100_mean, std=cifar100_std)])
transform_val = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=cifar100_mean, std=cifar100_std)])
base_dataset = datasets.CIFAR100(args.data_path, train=True, download=True)
train_labeled_idxs, train_unlabeled_idxs, finetune_idxs = x_u_split(args, base_dataset.targets)
train_labeled_dataset = CIFAR100SSL(
args.data_path, train_labeled_idxs, train=True,
transform=transform_labeled
)
finetune_dataset = CIFAR100SSL(
args.data_path, finetune_idxs, train=True,
transform=transform_fintune
)
train_unlabeled_dataset = CIFAR100SSL(
args.data_path, train_unlabeled_idxs, train=True,
transform=TransformMPL(args, mean=cifar100_mean, std=cifar100_std)
)
test_dataset = datasets.CIFAR100(args.data_path, train=False,
transform=transform_val, download=False)
return train_labeled_dataset, train_unlabeled_dataset, test_dataset, finetune_dataset
def x_u_split(args, labels):
label_per_class = args.num_labeled // args.num_classes
labels = np.array(labels)
labeled_idx = []
# unlabeled data: all training data
unlabeled_idx = np.array(range(len(labels)))
for i in range(args.num_classes):
idx = np.where(labels == i)[0]
idx = np.random.choice(idx, label_per_class, False)
labeled_idx.extend(idx)
labeled_idx = np.array(labeled_idx)
assert len(labeled_idx) == args.num_labeled
if args.expand_labels or args.num_labeled < args.batch_size:
num_expand_x = math.ceil(
args.batch_size * args.eval_step / args.num_labeled)
labeled_idx_ex = np.hstack([labeled_idx for _ in range(num_expand_x)])
np.random.shuffle(labeled_idx_ex)
np.random.shuffle(labeled_idx)
return labeled_idx_ex, unlabeled_idx, labeled_idx
else:
np.random.shuffle(labeled_idx)
return labeled_idx, unlabeled_idx, lebeled_idx
def x_u_split_test(args, labels):
label_per_class = args.num_labeled // args.num_classes
labels = np.array(labels)
labeled_idx = []
unlabeled_idx = []
for i in range(args.num_classes):
idx = np.where(labels == i)[0]
np.random.shuffle(idx)
labeled_idx.extend(idx[:label_per_class])
unlabeled_idx.extend(idx[label_per_class:])
labeled_idx = np.array(labeled_idx)
unlabeled_idx = np.array(unlabeled_idx)
assert len(labeled_idx) == args.num_labeled
if args.expand_labels or args.num_labeled < args.batch_size:
num_expand_x = math.ceil(
args.batch_size * args.eval_step / args.num_labeled)
labeled_idx = np.hstack([labeled_idx for _ in range(num_expand_x)])
np.random.shuffle(labeled_idx)
np.random.shuffle(unlabeled_idx)
return labeled_idx, unlabeled_idx
class TransformMPL(object):
def __init__(self, args, mean, std):
if args.randaug:
n, m = args.randaug
else:
n, m = 2, 10 # default
self.ori = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(size=args.resize,
padding=int(args.resize * 0.125),
fill=128,
padding_mode='constant')])
self.aug = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(size=args.resize,
padding=int(args.resize * 0.125),
fill=128,
padding_mode='constant'),
RandAugmentCIFAR(n=n, m=m)])
self.normalize = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=mean, std=std)])
def __call__(self, x):
ori = self.ori(x)
aug = self.aug(x)
return self.normalize(ori), self.normalize(aug)
class CIFAR10SSL(datasets.CIFAR10):
def __init__(self, root, indexs, train=True,
transform=None, target_transform=None,
download=False):
super().__init__(root, train=train,
transform=transform,
target_transform=target_transform,
download=download)
if indexs is not None:
self.data = self.data[indexs]
self.targets = np.array(self.targets)[indexs]
def __getitem__(self, index):
img, target = self.data[index], self.targets[index]
img = Image.fromarray(img)
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
return img, target
class CIFAR100SSL(datasets.CIFAR100):
def __init__(self, root, indexs, train=True,
transform=None, target_transform=None,
download=False):
super().__init__(root, train=train,
transform=transform,
target_transform=target_transform,
download=download)
if indexs is not None:
self.data = self.data[indexs]
self.targets = np.array(self.targets)[indexs]
def __getitem__(self, index):
img, target = self.data[index], self.targets[index]
img = Image.fromarray(img)
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
return img, target
DATASET_GETTERS = {'cifar10': get_cifar10,
'cifar100': get_cifar100}