-
Notifications
You must be signed in to change notification settings - Fork 6
/
preprocess.py
202 lines (170 loc) · 6.88 KB
/
preprocess.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
import torch
from torchvision import datasets, transforms
# NOTE: Mean and std used for normalization are known stats from the distribution of each dataset
def load_data(args):
print('Load Dataset :: {}'.format(args.dataset))
if args.dataset == 'CIFAR10':
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(
mean=(0.4914, 0.4822, 0.4465),
std=(0.2470, 0.2435, 0.2616)
)
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(
mean=(0.4914, 0.4822, 0.4465),
std=(0.2470, 0.2435, 0.2616)
)
])
train_data = datasets.CIFAR10('data', train=True, download=True, transform=transform_train)
train_len = int(len(train_data)*0.9)
val_len = len(train_data) - train_len
print('Len Train: {}, Len Valid: {}'.format(train_len,val_len))
train_set, valid_set = torch.utils.data.random_split(train_data, [train_len, val_len])
valid_set.transform = transform_test #Don't want to apply flips and random crops to this
train_loader = torch.utils.data.DataLoader(
train_set,
batch_size=args.batch_size,
shuffle=True,
num_workers=args.num_workers
)
valid_loader = torch.utils.data.DataLoader(
valid_set,
batch_size=args.batch_size,
shuffle=True,
num_workers=args.num_workers
)
test_loader = torch.utils.data.DataLoader(
datasets.CIFAR10('data', train=False, transform=transform_test),
batch_size=args.batch_size,
shuffle=False,
num_workers=args.num_workers
)
elif args.dataset == 'CIFAR100':
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(
mean=(0.5071, 0.4865, 0.4409),
std=(0.2673, 0.2564, 0.2762)
),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(
mean=(0.5071, 0.4865, 0.4409),
std=(0.2673, 0.2564, 0.2762)
),
])
train_data = datasets.CIFAR100('data', train=True, download=True, transform=transform_train)
actual_to_be_used = int(len(train_data) * args.subset)
train_data, _ = torch.utils.data.random_split(train_data, [actual_to_be_used, len(train_data) - actual_to_be_used])
train_len = int(len(train_data)*0.9)
val_len = len(train_data) - train_len
print('Len Train: {}, Len Valid: {}'.format(train_len,val_len))
train_set, valid_set = torch.utils.data.random_split(train_data, [train_len, val_len])
valid_set.transform = transform_test #Don't want to apply flips and random crops to this
train_loader = torch.utils.data.DataLoader(
train_set,
batch_size=args.batch_size,
shuffle=True,
num_workers=args.num_workers
)
valid_loader = torch.utils.data.DataLoader(
valid_set,
batch_size=args.batch_size,
shuffle=True,
num_workers=args.num_workers
)
test_loader = torch.utils.data.DataLoader(
datasets.CIFAR100('data', train=False, transform=transform_test),
batch_size=args.batch_size,
shuffle=False,
num_workers=args.num_workers
)
elif args.dataset == 'MNIST':
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(
mean=(0.1307,),
std=(0.3081,)
)
])
train_loader = torch.utils.data.DataLoader(
datasets.MNIST('data', train=True, download=True, transform=transform),
batch_size=args.batch_size,
shuffle=True,
num_workers=args.num_workers
)
test_loader = torch.utils.data.DataLoader(
datasets.MNIST('data', train=False, transform=transform),
batch_size=args.batch_size,
shuffle=False,
num_workers=args.num_workers
)
elif args.dataset == 'TinyImageNet':
# We use the normalization stats of the full ImageNet dataset as an estimate for the stats of the
# TinyImageNet dataset
transform_train = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(
mean=(0.485, 0.456, 0.406),
std=(0.229, 0.224, 0.225)
)
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(
mean=(0.485, 0.456, 0.406),
std=(0.229, 0.224, 0.225)
)
])
# Only the training data has labels so we will split it up to make training, testing, and validation sets
train_data = datasets.ImageFolder('./datasets/processed-tiny-imagenet', transform=transform_train)
train_len = int(len(train_data)*0.8)
val_len = int(len(train_data)*0.1)
test_len = int(len(train_data)*0.1)
train_set, valid_set, test_set = torch.utils.data.random_split(train_data, [train_len, val_len, test_len])
# test_len = int(len(train_set)*0.1)
# new_train_len = len(train_set) - test_len
# train_set, test_set = torch.utils.data.random_split(train_set, [new_train_len, test_len])
#Don't want to apply flips and random crops to this
valid_set.transform = transform_test
test_set.transform = transform_test
train_loader = torch.utils.data.DataLoader(
train_set,
batch_size=args.batch_size,
shuffle=True,
num_workers=args.num_workers
)
valid_loader = torch.utils.data.DataLoader(
valid_set,
batch_size=args.batch_size,
shuffle=True,
num_workers=args.num_workers
)
test_loader = torch.utils.data.DataLoader(
test_set,
batch_size=args.batch_size,
shuffle=False,
num_workers=args.num_workers
)
print('TinyImageNet Loader')
print(train_loader)
return train_loader, valid_loader, test_loader
#This is just for testing purposes
class Args:
def __init__(self):
self.batch_size = 32
self.num_workers = 1
self.dataset = 'CIFAR10'
if __name__ == '__main__':
#need to split the training set into train/valid
args = Args()
train, valid, test = load_data(args)