-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataloader_clothing.py
192 lines (172 loc) · 7.09 KB
/
dataloader_clothing.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
from torch.utils.data import Dataset, DataLoader
import torchvision.transforms as transforms
from torch.utils.data.dataset import Subset
import torch
import random
import numpy as np
from PIL import Image
import os
class clothing_dataset(Dataset):
def __init__(self, transform, mode):
self.train_imgs = []
self.test_imgs = []
self.val_imgs = []
self.noisy_labels = {}
self.clean_labels = {}
self.train_labels = []
self.test_labels = []
self.val_labels = []
self.transform = transform
self.mode = mode
self.clean_train_list = [[] for i in range(14)]
datapath = os.path.abspath('..') + '/scratch/jingyi/clothing1M/'
# datapath = "/dgxdata/jingyi/clothing1M/"
with open(datapath + 'noisy_label_kv.txt', 'r') as f:
lines = f.read().splitlines()
for l in lines:
entry = l.split()
img_path = datapath + entry[0]
self.noisy_labels[img_path] = int(entry[1])
with open(datapath + 'clean_label_kv.txt', 'r') as f:
lines = f.read().splitlines()
for l in lines:
entry = l.split()
img_path = datapath + entry[0]
self.clean_labels[img_path] = int(entry[1])
with open(datapath + 'clean_train_key_list.txt', 'r') as f:
lines = f.read().splitlines()
for i in range(len(lines)):
l = lines[i]
img_path = datapath + l
self.train_imgs.append(img_path)
target = self.clean_labels[img_path]
self.clean_train_list[target].append(i)
self.train_labels.append(target)
with open(datapath + 'noisy_train_key_list.txt', 'r') as f:
lines = f.read().splitlines()
for l in lines:
img_path = datapath + l
self.train_imgs.append(img_path)
target = self.noisy_labels[img_path]
self.train_labels.append(target)
with open(datapath + 'clean_test_key_list.txt', 'r') as f:
lines = f.read().splitlines()
for l in lines:
img_path = datapath + l
self.test_imgs.append(img_path)
target = self.clean_labels[img_path]
self.test_labels.append(target)
with open(datapath + 'clean_val_key_list.txt', 'r') as f:
lines = f.read().splitlines()
for l in lines:
img_path = datapath + l
self.val_imgs.append(img_path)
target = self.clean_labels[img_path]
self.val_labels.append(target)
def __getitem__(self, index):
if self.mode == 'train':
img_path = self.train_imgs[index]
target = self.train_labels[index]
elif self.mode == 'test':
img_path = self.test_imgs[index]
target = self.test_labels[index]
elif self.mode == 'val':
img_path = self.val_imgs[index]
target = self.val_labels[index]
image = Image.open(img_path).convert('RGB')
img = self.transform(image)
return img, target
def __len__(self):
if self.mode == 'train':
return len(self.train_imgs)
elif self.mode == 'test':
return len(self.test_imgs)
elif self.mode == 'val':
return len(self.val_imgs)
class clothing_dataloader():
def __init__(self, batch_size, num_workers, shuffle):
self.batch_size = batch_size
self.num_workers = num_workers
self.shuffle = shuffle
def run(self):
transform_train = transforms.Compose([
transforms.Resize(256),
# transforms.RandomSizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
]) # meanstd transformation
transform_test = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
])
train_dataset = clothing_dataset(transform=transform_train, mode='train')
test_dataset = clothing_dataset(transform=transform_test, mode='test')
val_dataset = clothing_dataset(transform=transform_test, mode='val')
train_loader = DataLoader(
dataset=train_dataset,
batch_size=self.batch_size,
shuffle=self.shuffle)
train_loader_trace = DataLoader(
dataset=train_dataset,
batch_size=self.batch_size,
shuffle=False)
test_loader = DataLoader(
dataset=test_dataset,
batch_size=self.batch_size,
shuffle=False)
val_loader = DataLoader(
dataset=val_dataset,
batch_size=self.batch_size,
shuffle=False)
return train_loader, train_loader_trace, val_loader, test_loader
def clean_train_list(self):
transform_train = transforms.Compose([
transforms.Resize(256),
# transforms.RandomSizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
]) # meanstd transformation
train_dataset = clothing_dataset(transform=transform_train, mode='train')
return train_dataset.clean_train_list
def subset_train_loader(self, train_index, y_slice):
transform_train = transforms.Compose([
transforms.Resize(256),
# transforms.RandomSizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
]) # meanstd transformation
train_dataset = clothing_dataset(transform=transform_train, mode='train')
y_train_new = np.array(train_dataset.train_labels)
y_train_new[train_index] = y_slice
train_dataset.train_labels = y_train_new
subset = Subset(train_dataset, train_index)
train_loader_subset = torch.utils.data.DataLoader(
dataset=subset,
batch_size=self.batch_size,
shuffle=True)
return train_loader_subset
def train_clean_loader(self):
transform_train = transforms.Compose([
transforms.Resize(256),
# transforms.RandomSizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
]) # meanstd transformation
train_dataset = clothing_dataset(transform=transform_train, mode='train')
train_index = self.clean_train_list()
subset = Subset(train_dataset, train_index)
train_loader_subset = torch.utils.data.DataLoader(
dataset=subset,
batch_size=32,
shuffle=True)
return train_loader_subset