-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils_data.py
209 lines (181 loc) · 7.82 KB
/
utils_data.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
203
204
205
206
207
208
209
import numpy as np
import os
import torch
import torch.utils.data
import torchvision.datasets as datasets
import torchvision.transforms as transforms
from sklearn.datasets import make_blobs
from sklearn.model_selection import train_test_split
from torch.utils.data import DataLoader, SubsetRandomSampler
DATA_ROOT = os.environ['DATA_ROOT']
def get_gauss2d_data(args):
if args.data == "crossed_gaussians":
data = make_blobs(n_samples=args.n_samples, n_features=2, centers=[args.mu_1, args.mu_2],
cluster_std=[args.std_1, args.std_2], random_state=args.data_seed)
else:
raise NotImplementedError
return data
def get_gauss2d_loaders(args):
x, y = get_gauss2d_data(args)
train_x, test_x, train_y, test_y = train_test_split(x, y, train_size=args.train_size, random_state=args.data_seed)
test_x, val_x, test_y, val_y = train_test_split(test_x, test_y, test_size=0.5, random_state=args.data_seed)
trainset = torch.utils.data.TensorDataset(torch.FloatTensor(train_x),
torch.LongTensor(train_y))
valset = torch.utils.data.TensorDataset(torch.FloatTensor(val_x),
torch.LongTensor(val_y))
testset = torch.utils.data.TensorDataset(torch.FloatTensor(test_x),
torch.LongTensor(test_y))
train_loader = torch.utils.data.DataLoader(trainset, batch_size=args.train_bs, shuffle=True,
num_workers=1, pin_memory=True, drop_last=False)
val_loader = torch.utils.data.DataLoader(valset, batch_size=args.val_bs, shuffle=True,
num_workers=1, pin_memory=True, drop_last=False)
test_loader = torch.utils.data.DataLoader(testset, batch_size=args.test_bs, shuffle=False,
num_workers=1, pin_memory=True, drop_last=False)
return train_loader, val_loader, test_loader
def get_cifar100_loaders(batch_size=128, test_batch_size=1000, val_size=0.2, data_root=DATA_ROOT, limit=None,
verbose=False, augmentation=True):
if augmentation:
train_transform = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
])
else:
train_transform = transforms.Compose([
transforms.ToTensor(),
])
test_transform = transforms.Compose([
transforms.ToTensor(),
])
data_root = os.path.join(data_root, 'cifar100')
train_dataset = datasets.CIFAR100(
root=data_root, train=True,
download=True, transform=train_transform,
)
val_dataset = datasets.CIFAR100(
root=data_root, train=True,
download=True, transform=test_transform,
)
test_dataset = datasets.CIFAR100(
root=data_root, train=False,
download=True, transform=test_transform,
)
num_train = len(train_dataset)
if limit:
num_train = limit
indices = list(range(num_train))
split = int(np.floor(val_size * num_train))
if verbose:
print("train size: {}\nsplit: {}\n".format(num_train, split))
# random_seed = 30
# # np.random.seed(random_seed)
np.random.shuffle(indices)
train_idx, valid_idx = indices[split:], indices[:split]
train_sampler = SubsetRandomSampler(train_idx)
valid_sampler = SubsetRandomSampler(valid_idx)
train_loader = DataLoader(
train_dataset, batch_size=batch_size, sampler=train_sampler, num_workers=4
)
valid_loader = DataLoader(
val_dataset, batch_size=test_batch_size, sampler=valid_sampler, num_workers=4
)
test_loader = DataLoader(
test_dataset, batch_size=test_batch_size, shuffle=False, num_workers=4
)
return train_loader, valid_loader, test_loader
def get_cifar10_loaders(batch_size=128, test_batch_size=1000, val_size=0.2, data_root=DATA_ROOT, limit=None,
verbose=False, augmentation=True):
if augmentation:
train_transform = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
])
else:
train_transform = transforms.Compose([
transforms.ToTensor(),
])
test_transform = transforms.Compose([
transforms.ToTensor(),
])
data_root = os.path.join(data_root, 'cifar10')
train_dataset = datasets.CIFAR10(
root=data_root, train=True,
download=True, transform=train_transform,
)
val_dataset = datasets.CIFAR10(
root=data_root, train=True,
download=True, transform=test_transform,
)
test_dataset = datasets.CIFAR10(
root=data_root, train=False,
download=True, transform=test_transform,
)
num_train = len(train_dataset)
if limit:
num_train = limit
indices = list(range(num_train))
split = int(np.floor(val_size * num_train))
if verbose:
print("train size: {}\nsplit: {}\n".format(num_train, split))
# random_seed = 30
# # np.random.seed(random_seed)
np.random.shuffle(indices)
train_idx, valid_idx = indices[split:], indices[:split]
train_sampler = SubsetRandomSampler(train_idx)
valid_sampler = SubsetRandomSampler(valid_idx)
train_loader = DataLoader(
train_dataset, batch_size=batch_size, sampler=train_sampler, num_workers=4
)
valid_loader = DataLoader(
val_dataset, batch_size=test_batch_size, sampler=valid_sampler, num_workers=4
)
test_loader = DataLoader(
test_dataset, batch_size=test_batch_size, shuffle=False, num_workers=4
)
return train_loader, valid_loader, test_loader
def get_tiny_imagenet_loaders(train_batch_size=128, test_batch_size=128, data_root=DATA_ROOT,
augmentation=True, num_workers=0, val_size=0.2, verbose=False):
if augmentation:
train_transform = transforms.Compose([
transforms.RandomCrop(64, padding=8),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
])
else:
train_transform = transforms.Compose([
transforms.ToTensor(),
])
test_transform = transforms.Compose([
transforms.ToTensor(),
])
data_transforms = {
'train': train_transform,
'val': test_transform,
'test': test_transform
}
data_dir = os.path.join(data_root, 'tiny-imagenet-200')
paths = {
'train': os.path.join(data_dir, 'train'),
'val': os.path.join(data_dir, 'val'),
'test': os.path.join(data_dir, 'test')
}
train_dataset = datasets.ImageFolder(paths['train'], data_transforms['train'])
val_dataset = datasets.ImageFolder(paths['train'], data_transforms['val'])
test_dataset = datasets.ImageFolder(paths['val'], data_transforms['test'])
num_train = len(train_dataset)
indices = list(range(num_train))
split = int(np.floor(val_size * num_train))
if verbose:
print("train size: {}\nsplit: {}\n".format(num_train, split))
np.random.shuffle(indices)
train_idx, val_idx = indices[split:], indices[:split]
train_sampler = torch.utils.data.SubsetRandomSampler(train_idx)
val_sampler = torch.utils.data.SubsetRandomSampler(val_idx)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=train_batch_size, sampler=train_sampler,
num_workers=num_workers)
val_loader = torch.utils.data.DataLoader(val_dataset, batch_size=test_batch_size, sampler=val_sampler,
num_workers=num_workers)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=test_batch_size,
shuffle=False, num_workers=num_workers)
return train_loader, val_loader, test_loader