This repository has been archived by the owner on May 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 799
/
data_loaders.py
executable file
·196 lines (154 loc) · 7.8 KB
/
data_loaders.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
#
# Copyright (c) 2018 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Helper code for data loading.
This code will help with the image classification datasets: ImageNet and CIFAR10
"""
import os
import torch
import torchvision.transforms as transforms
import torchvision.datasets as datasets
from torch.utils.data.sampler import SubsetRandomSampler
import numpy as np
DATASETS_NAMES = ['imagenet', 'cifar10']
def load_data(dataset, data_dir, batch_size, workers, valid_size=0.1, deterministic=False):
"""Load a dataset.
Args:
dataset: a string with the name of the dataset to load (cifar10/imagenet)
data_dir: the directory where the datset resides
batch_size: the batch size
workers: the number of worker threads to use for loading the data
valid_size: portion of training dataset to set aside for validation
deterministic: set to True if you want the data loading process to be deterministic.
Note that deterministic data loading suffers from poor performance.
"""
assert dataset in DATASETS_NAMES
if dataset == 'cifar10':
return cifar10_load_data(data_dir, batch_size, workers, valid_size=valid_size, deterministic=deterministic)
if dataset == 'imagenet':
return imagenet_load_data(data_dir, batch_size, workers, valid_size=valid_size, deterministic=deterministic)
print("FATAL ERROR: load_data does not support dataset %s" % dataset)
exit(1)
def __image_size(dataset):
# un-squeeze is used here to add the batch dimension (value=1), which is missing
return dataset[0][0].unsqueeze(0).size()
def __deterministic_worker_init_fn(worker_id, seed=0):
import random
import numpy
random.seed(seed)
numpy.random.seed(seed)
torch.manual_seed(seed)
def cifar10_load_data(data_dir, batch_size, num_workers, valid_size=0.1, deterministic=False):
"""Load the CIFAR10 dataset.
The original training dataset is split into training and validation sets (code is
inspired by https://gist.github.com/kevinzakka/d33bf8d6c7f06a9d8c76d97a7879f5cb).
By default we use a 90:10 (45K:5K) training:validation split.
The output of torchvision datasets are PIL Image images of range [0, 1].
We transform them to Tensors of normalized range [-1, 1]
https://github.com/pytorch/tutorials/blob/master/beginner_source/blitz/cifar10_tutorial.py
Data augmentation: 4 pixels are padded on each side, and a 32x32 crop is randomly sampled
from the padded image or its horizontal flip.
This is similar to [1] and some other work that use CIFAR10.
[1] C.-Y. Lee, S. Xie, P. Gallagher, Z. Zhang, and Z. Tu. Deeply Supervised Nets.
arXiv:1409.5185, 2014
"""
transform = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
train_dataset = datasets.CIFAR10(root=data_dir, train=True,
download=True, transform=transform)
num_train = len(train_dataset)
indices = list(range(num_train))
split = int(np.floor(valid_size * num_train))
np.random.shuffle(indices)
train_idx, valid_idx = indices[split:], indices[:split]
train_sampler = SubsetRandomSampler(train_idx)
worker_init_fn = __deterministic_worker_init_fn if deterministic else None
train_loader = torch.utils.data.DataLoader(train_dataset,
batch_size=batch_size, sampler=train_sampler,
num_workers=num_workers, pin_memory=True,
worker_init_fn=worker_init_fn)
valid_loader = None
if split > 0:
valid_sampler = SubsetRandomSampler(valid_idx)
valid_loader = torch.utils.data.DataLoader(train_dataset,
batch_size=batch_size, sampler=valid_sampler,
num_workers=num_workers, pin_memory=True,
worker_init_fn=worker_init_fn)
testset = datasets.CIFAR10(root=data_dir, train=False,
download=True, transform=transform_test)
test_loader = torch.utils.data.DataLoader(
testset, batch_size=batch_size, shuffle=False,
num_workers=num_workers, pin_memory=True)
input_shape = __image_size(train_dataset)
# If validation split was 0 we use the test set as the validation set
return train_loader, valid_loader or test_loader, test_loader, input_shape
def imagenet_load_data(data_dir, batch_size, num_workers, valid_size=0.1, deterministic=False):
"""Load the ImageNet dataset.
Somewhat unconventionally, we use the ImageNet validation dataset as our test dataset,
and split the training dataset for training and validation (90/10 by default).
"""
train_dir = os.path.join(data_dir, 'train')
test_dir = os.path.join(data_dir, 'val')
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
train_dataset = datasets.ImageFolder(
train_dir,
transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
]))
num_train = len(train_dataset)
indices = list(range(num_train))
split = int(np.floor(valid_size * num_train))
# Note! We must shuffle the imagenet data because the files are ordered
# by class. If we don't shuffle, the train and validation datasets will
# by mutually-exclusive
np.random.shuffle(indices)
train_idx, valid_idx = indices[split:], indices[:split]
train_sampler = SubsetRandomSampler(train_idx)
input_shape = __image_size(train_dataset)
worker_init_fn = __deterministic_worker_init_fn if deterministic else None
train_loader = torch.utils.data.DataLoader(train_dataset,
batch_size=batch_size, sampler=train_sampler,
num_workers=num_workers, pin_memory=True,
worker_init_fn=worker_init_fn)
valid_loader = None
if split > 0:
valid_sampler = SubsetRandomSampler(valid_idx)
valid_loader = torch.utils.data.DataLoader(train_dataset,
batch_size=batch_size, sampler=valid_sampler,
num_workers=num_workers, pin_memory=True,
worker_init_fn=worker_init_fn)
test_loader = torch.utils.data.DataLoader(
datasets.ImageFolder(test_dir, transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
normalize,
])),
batch_size=batch_size, shuffle=False,
num_workers=num_workers, pin_memory=True)
# If validation split was 0 we use the test set as the validation set
return train_loader, valid_loader or test_loader, test_loader, input_shape