-
Notifications
You must be signed in to change notification settings - Fork 5
/
datasets.py
162 lines (147 loc) · 7.45 KB
/
datasets.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
import os
import pickle
import msgpack
import cv2
from torchvision import datasets, transforms
import torch
from torch.utils.data.dataset import Dataset
from torch.utils.data.sampler import SubsetRandomSampler
import numpy as np
from PIL import Image
class InMemoryImageNet(Dataset):
'''
For systems with slow disk but large RAM (>250 GB). This loads the full
ImageNet dataset into RAM before starting training, to avoid disk access.
'''
def __init__(self, path, num_samples, transforms):
self.path = path
self.num_samples = num_samples
self.transforms = transforms
self.samples = []
f = open(self.path, "rb")
for i, sample in enumerate(msgpack.Unpacker(f, use_list=False, raw=True)):
self.samples.append(sample)
if i == self.num_samples - 1:
break
f.close()
def __getitem__(self, index):
x, y = self.samples[index]
x = self.transforms(x)
return (x, y)
def __len__(self):
return self.num_samples
def get_dataset(dataset_root, dataset, batch_size, is_cuda=True, aug='+',
val_only=False, input_size=224, sample_ratio=1):
if dataset == 'mnist':
return get_mnist(dataset_root, batch_size, is_cuda, aug)
elif dataset == 'cifar10':
return get_cifar10(dataset_root, batch_size, is_cuda, aug, sample_ratio)
elif dataset == 'imagenet':
return get_imagenet(dataset_root, batch_size, is_cuda, val_only=val_only,
input_size=input_size)
else:
raise ValueError('Dataset `{}` not found'.format(dataset))
return train, train_loader, test, test_loader
def get_mnist(dataset_root, batch_size, is_cuda=True, aug='+'):
kwargs = {'num_workers': 12, 'pin_memory': True} if is_cuda else {}
train = datasets.MNIST(os.path.join(dataset_root, 'mnist'), train=True, download=True,
transform=transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,)),
]))
test = datasets.MNIST(os.path.join(dataset_root, 'mnist'), train=False, download=True,
transform=transforms.Compose([
transforms.Pad(2),
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,)),
]))
train_loader = torch.utils.data.DataLoader(train, batch_size=batch_size,
shuffle=True, drop_last=False, **kwargs)
test_loader = torch.utils.data.DataLoader(test, batch_size=batch_size,
shuffle=False, drop_last=False, **kwargs)
return train, train_loader, test, test_loader
def get_cifar10(dataset_root, batch_size, is_cuda=True, aug='+', sample_ratio=1):
kwargs = {'num_workers': 16, 'pin_memory': True} if is_cuda else {}
stds = (0.247, 0.243, 0.261)
if aug == '-':
transform = [
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), stds),
]
elif aug == '+':
transform = [
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), stds),
]
else:
raise ValueError('Invalid Augmentation setting `{}` not found'.format(aug))
train = datasets.CIFAR10(os.path.join(dataset_root, 'cifar10'), train=True, download=True,
transform=transforms.Compose(transform))
test = datasets.CIFAR10(os.path.join(dataset_root, 'cifar10'), train=False, download=True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), stds),
]))
if sample_ratio == 1:
train_loader = torch.utils.data.DataLoader(train, batch_size=batch_size,
shuffle=True, drop_last=False, **kwargs)
else:
idxs = []
train.train_labels = np.array(train.train_labels)
for label in range(10):
label_idxs = np.where(train.train_labels == label)[0]
num_keep = int(sample_ratio*len(label_idxs))
idxs.extend(label_idxs[:num_keep])
train_loader = torch.utils.data.DataLoader(train, batch_size=batch_size,
sampler=SubsetRandomSampler(idxs),
shuffle=False, drop_last=False, **kwargs)
test_loader = torch.utils.data.DataLoader(test, batch_size=batch_size,
shuffle=False, drop_last=False, **kwargs)
train_loader.num_samples = len(train)
test_loader.num_samples = len(test)
return train, train_loader, test, test_loader
def get_imagenet(dataset_root, batch_size, is_cuda=True, num_workers=32,
val_only=False, input_size=224):
train_path = os.path.join(dataset_root, 'imagenet-msgpack', 'ILSVRC-train.bin')
val_path = os.path.join(dataset_root, 'imagenet-msgpack', 'ILSVRC-val.bin')
kwargs = {'num_workers': num_workers, 'pin_memory': True} if is_cuda else {}
num_train = 1281167
num_val = 50000
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
if not val_only:
train = InMemoryImageNet(train_path, num_train,
transforms=transforms.Compose([
pickle.loads,
lambda x: cv2.imdecode(x, cv2.IMREAD_COLOR),
lambda x: cv2.cvtColor(x, cv2.COLOR_BGR2RGB),
transforms.ToPILImage(),
transforms.RandomResizedCrop(input_size, scale=(0.2, 1.0)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
]))
train_loader = torch.utils.data.DataLoader(train, batch_size=batch_size,
shuffle=True, drop_last=False, **kwargs)
train_loader.num_samples = num_train
else:
train, train_loader = None, None
test = InMemoryImageNet(val_path, num_val,
transforms=transforms.Compose([
pickle.loads,
lambda x: cv2.imdecode(x, cv2.IMREAD_COLOR),
lambda x: cv2.cvtColor(x, cv2.COLOR_BGR2RGB),
transforms.ToPILImage(),
transforms.Resize(int(input_size / 0.875)),
transforms.CenterCrop(input_size),
transforms.ToTensor(),
normalize,
]))
test_loader = torch.utils.data.DataLoader(test, batch_size=batch_size,
shuffle=False, drop_last=False, **kwargs)
test_loader.num_samples = num_val
return train, train_loader, test, test_loader