-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataloader_webvision.py
322 lines (294 loc) · 14.4 KB
/
dataloader_webvision.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
from torch.utils.data import Dataset, DataLoader
import torchvision.transforms as transforms
import random
import numpy as np
from PIL import Image
import torch
import os
class imagenet_dataset(Dataset):
def __init__(self, root_dir, transform, num_class):
self.root = root_dir
self.transform = transform
self.val_data = []
with open(os.path.join(root_dir, 'info/synsets.txt')) as f:
lines = f.readlines()
synsets = [x.split()[0] for x in lines]
for c in range(num_class):
class_path = os.path.join(self.root, 'val', synsets[c])
imgs = os.listdir(class_path)
for img in imgs:
self.val_data.append([c, os.path.join(class_path, img)])
def __getitem__(self, index):
data = self.val_data[index]
target = data[0]
image = Image.open(data[1]).convert('RGB')
img = self.transform(image)
return img, target
def __len__(self):
return len(self.val_data)
class webvision_dataset(Dataset):
def __init__(self, root_dir, transform, mode, num_class, pred=[], probability=[], log=''):
self.root = root_dir
self.transform = transform
self.mode = mode
if self.mode=='test':
with open(self.root+'info/val_filelist.txt') as f:
lines=f.readlines()
self.val_imgs = []
self.val_labels = {}
for line in lines:
img, target = line.split()
target = int(target)
if target<num_class:
self.val_imgs.append(img)
self.val_labels[img]=target
else:
with open(self.root+'info/train_filelist_google.txt') as f:
lines=f.readlines()
train_imgs = []
self.train_labels = {}
for line in lines:
img, target = line.split()
target = int(target)
if target<num_class:
train_imgs.append(img)
self.train_labels[img]=target
if self.mode == 'warmup':
self.train_imgs = train_imgs
if self.mode == 'all':
self.train_imgs = train_imgs
self.pred_idx = pred.nonzero()[0]
else:
if self.mode == "labeled":
pred_idx = pred.nonzero()[0]
self.train_imgs = [train_imgs[i] for i in pred_idx]
self.probability = [probability[i] for i in pred_idx]
print("%s data has a size of %d"%(self.mode,len(self.train_imgs)))
log.write('Numer of labeled samples:%d \n'%(pred.sum()))
log.flush()
elif self.mode == "unlabeled":
pred_idx = (1-pred).nonzero()[0]
self.train_imgs = [train_imgs[i] for i in pred_idx]
print("%s data has a size of %d"%(self.mode,len(self.train_imgs)))
def __getitem__(self, index):
if self.mode=='labeled':
img_path = self.train_imgs[index]
target = self.train_labels[img_path]
prob = self.probability[index]
image = Image.open(self.root+img_path).convert('RGB')
img1 = self.transform(image)
img2 = self.transform(image)
return img1, img2, target, prob
elif self.mode=='unlabeled':
img_path = self.train_imgs[index]
image = Image.open(self.root+img_path).convert('RGB')
img1 = self.transform(image)
img2 = self.transform(image)
return img1, img2
elif self.mode=='warmup':
img_path = self.train_imgs[index]
target = self.train_labels[img_path]
image = Image.open(self.root+img_path).convert('RGB')
img = self.transform(image)
return img, target, index
elif self.mode=='all':
img_path = self.train_imgs[index]
target = self.train_labels[img_path]
image = Image.open(self.root+img_path).convert('RGB')
img = self.transform(image)
if index in self.pred_idx:
pred_clean = 1
else:
pred_clean = 0
return img, target, pred_clean, index
elif self.mode=='test':
img_path = self.val_imgs[index]
target = self.val_labels[img_path]
image = Image.open(self.root+'val_images_256/'+img_path).convert('RGB')
img = self.transform(image)
return img, target
def __len__(self):
if self.mode!='test':
return len(self.train_imgs)
else:
return len(self.val_imgs)
class webvision_dataloader():
def __init__(self, batch_size, num_class, num_workers, root_dir, log):
self.batch_size = batch_size
self.num_class = num_class
self.num_workers = num_workers
self.root_dir = root_dir
self.log = log
self.transform_train = transforms.Compose([
transforms.Resize(320),
transforms.RandomResizedCrop(299),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406),(0.229, 0.224, 0.225)),
])
self.transform_test = transforms.Compose([
transforms.Resize(320),
transforms.CenterCrop(299),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406),(0.229, 0.224, 0.225)),
])
self.transform_imagenet = transforms.Compose([
transforms.Resize(320),
transforms.CenterCrop(299),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406),(0.229, 0.224, 0.225)),
])
def run(self,mode,pred=[],prob=[]):
if mode=='warmup':
all_dataset = webvision_dataset(root_dir=self.root_dir, transform=self.transform_train, mode="warmup", num_class=self.num_class)
trainloader = DataLoader(
dataset=all_dataset,
batch_size=self.batch_size*2,
shuffle=True,
num_workers=self.num_workers,
pin_memory=True)
return trainloader
elif mode=='all':
all_dataset = webvision_dataset(root_dir=self.root_dir, transform=self.transform_train, mode="all", num_class=self.num_class, pred=pred,probability=prob)
trainloader = DataLoader(
dataset=all_dataset,
batch_size=self.batch_size,
shuffle=True,
num_workers=self.num_workers,
pin_memory=True)
return trainloader
elif mode=='train':
labeled_dataset = webvision_dataset(root_dir=self.root_dir, transform=self.transform_train, mode="labeled",num_class=self.num_class,pred=pred,probability=prob,log=self.log)
labeled_trainloader = DataLoader(
dataset=labeled_dataset,
batch_size=self.batch_size,
shuffle=True,
num_workers=self.num_workers,
pin_memory=True)
unlabeled_dataset = webvision_dataset(root_dir=self.root_dir, transform=self.transform_train, mode="unlabeled",num_class=self.num_class,pred=pred,log=self.log)
unlabeled_trainloader = DataLoader(
dataset=unlabeled_dataset,
batch_size=self.batch_size,
shuffle=True,
num_workers=self.num_workers,
pin_memory=True)
return labeled_trainloader, unlabeled_trainloader
elif mode=='test':
test_dataset = webvision_dataset(root_dir=self.root_dir, transform=self.transform_test, mode='test', num_class=self.num_class)
test_loader = DataLoader(
dataset=test_dataset,
batch_size=self.batch_size*10,
shuffle=False,
num_workers=self.num_workers,
pin_memory=True)
return test_loader
elif mode=='eval_train':
eval_dataset = webvision_dataset(root_dir=self.root_dir, transform=self.transform_test, mode='warmup', num_class=self.num_class)
eval_loader = DataLoader(
dataset=eval_dataset,
batch_size=self.batch_size*10,
shuffle=False,
num_workers=self.num_workers,
pin_memory=True)
return eval_loader
elif mode=='imagenet':
imagenet_val = imagenet_dataset(root_dir=self.root_dir, transform=self.transform_imagenet, num_class=self.num_class)
imagenet_loader = DataLoader(
dataset=imagenet_val,
batch_size=self.batch_size*10,
shuffle=False,
num_workers=self.num_workers,
pin_memory=True)
return imagenet_loader
class webvision_ddp_dataloader():
def __init__(self, batch_size, num_class, num_workers, root_dir, log):
self.batch_size = batch_size
self.num_class = num_class
self.num_workers = num_workers
self.root_dir = root_dir
self.log = log
self.transform_train = transforms.Compose([
transforms.Resize(320),
transforms.RandomResizedCrop(299),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406),(0.229, 0.224, 0.225)),
])
self.transform_test = transforms.Compose([
transforms.Resize(320),
transforms.CenterCrop(299),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406),(0.229, 0.224, 0.225)),
])
self.transform_imagenet = transforms.Compose([
transforms.Resize(320),
transforms.CenterCrop(299),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406),(0.229, 0.224, 0.225)),
])
def run(self,mode,pred=[],prob=[]):
if mode=='warmup':
all_dataset = webvision_dataset(root_dir=self.root_dir, transform=self.transform_train, mode="warmup", num_class=self.num_class)
train_sampler = torch.utils.data.distributed.DistributedSampler(all_dataset)
trainloader = DataLoader(
dataset=all_dataset,
batch_size=self.batch_size*2,
num_workers=self.num_workers,
pin_memory=True,
sampler = train_sampler)
return trainloader
elif mode=='all':
all_dataset = webvision_dataset(root_dir=self.root_dir, transform=self.transform_train, mode="all", num_class=self.num_class, pred=pred,probability=prob)
train_sampler = torch.utils.data.distributed.DistributedSampler(all_dataset)
trainloader = DataLoader(
dataset=all_dataset,
batch_size=self.batch_size,
num_workers=self.num_workers,
pin_memory=True,
sampler = train_sampler)
return trainloader
elif mode=='train':
labeled_dataset = webvision_dataset(root_dir=self.root_dir, transform=self.transform_train, mode="labeled",num_class=self.num_class,pred=pred,probability=prob,log=self.log)
labeled_train_sampler = torch.utils.data.distributed.DistributedSampler(labeled_dataset)
labeled_trainloader = DataLoader(
dataset=labeled_dataset,
batch_size=self.batch_size,
num_workers=self.num_workers,
pin_memory=True,
sampler = labeled_train_sampler)
unlabeled_dataset = webvision_dataset(root_dir=self.root_dir, transform=self.transform_train, mode="unlabeled",num_class=self.num_class,pred=pred,log=self.log)
unlabeled_train_sampler = torch.utils.data.distributed.DistributedSampler(unlabeled_dataset)
unlabeled_trainloader = DataLoader(
dataset=unlabeled_dataset,
batch_size=self.batch_size,
num_workers=self.num_workers,
pin_memory=True,
sampler = unlabeled_train_sampler)
return labeled_trainloader, unlabeled_trainloader
elif mode=='test':
test_dataset = webvision_dataset(root_dir=self.root_dir, transform=self.transform_test, mode='test', num_class=self.num_class)
test_loader = DataLoader(
dataset=test_dataset,
batch_size=self.batch_size*10,
shuffle=False,
num_workers=self.num_workers,
pin_memory=True)
return test_loader
elif mode=='eval_train':
eval_dataset = webvision_dataset(root_dir=self.root_dir, transform=self.transform_test, mode='warmup', num_class=self.num_class)
eval_loader = DataLoader(
dataset=eval_dataset,
batch_size=self.batch_size*10,
shuffle=False,
num_workers=self.num_workers,
pin_memory=True)
return eval_loader
elif mode=='imagenet':
imagenet_val = imagenet_dataset(root_dir=self.root_dir, transform=self.transform_imagenet, num_class=self.num_class)
imagenet_loader = DataLoader(
dataset=imagenet_val,
batch_size=self.batch_size*10,
shuffle=False,
num_workers=self.num_workers,
pin_memory=True)
return imagenet_loader