forked from LiJunnan1992/DivideMix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataloader_webvision.py
194 lines (176 loc) · 8.31 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
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+'imagenet/val/'
self.transform = transform
self.val_data = []
for c in range(num_class):
imgs = os.listdir(self.root+str(c))
for img in imgs:
self.val_data.append([c,os.path.join(self.root,str(c),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 == 'all':
self.train_imgs = train_imgs
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=='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)
return img, target, 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="all", 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=='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*20,
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='all', num_class=self.num_class)
eval_loader = DataLoader(
dataset=eval_dataset,
batch_size=self.batch_size*20,
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*20,
shuffle=False,
num_workers=self.num_workers,
pin_memory=True)
return imagenet_loader