forked from timgaripov/dnn-mode-connectivity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
79 lines (64 loc) · 2.72 KB
/
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
import os
import torch
import torchvision
import torchvision.transforms as transforms
class Transforms:
class CIFAR10:
class VGG:
train = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(32, padding=4),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
class ResNet:
train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(mean=[0.4914, 0.4822, 0.4465], std=[0.2023, 0.1994, 0.2010]),
])
test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=[0.4914, 0.4822, 0.4465], std=[0.2023, 0.1994, 0.2010]),
])
CIFAR100 = CIFAR10
def loaders(dataset, path, batch_size, num_workers, transform_name, use_test=False,
shuffle_train=True):
ds = getattr(torchvision.datasets, dataset)
path = os.path.join(path, dataset.lower())
transform = getattr(getattr(Transforms, dataset), transform_name)
train_set = ds(path, train=True, download=True, transform=transform.train)
if use_test:
print('You are going to run models on the test set. Are you sure?')
test_set = ds(path, train=False, download=True, transform=transform.test)
else:
print("Using train (45000) + validation (5000)")
train_set.train_data = train_set.train_data[:-5000]
train_set.train_labels = train_set.train_labels[:-5000]
test_set = ds(path, train=True, download=True, transform=transform.test)
test_set.train = False
test_set.test_data = test_set.train_data[-5000:]
test_set.test_labels = test_set.train_labels[-5000:]
delattr(test_set, 'train_data')
delattr(test_set, 'train_labels')
return {
'train': torch.utils.data.DataLoader(
train_set,
batch_size=batch_size,
shuffle=shuffle_train,
num_workers=num_workers,
pin_memory=True
),
'test': torch.utils.data.DataLoader(
test_set,
batch_size=batch_size,
shuffle=False,
num_workers=num_workers,
pin_memory=True
),
}, max(train_set.train_labels) + 1