-
Notifications
You must be signed in to change notification settings - Fork 14
/
data_loader.py
133 lines (110 loc) · 5.04 KB
/
data_loader.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
import numpy as np
import torch
from torchvision import datasets
from torchvision import transforms
from torch.utils.data import Subset
from norb import smallNORBViewPoint, smallNORB
def get_train_valid_loader(data_dir,
dataset,
batch_size,
random_seed,
exp='azimuth',
valid_size=0.1,
shuffle=True,
num_workers=4,
pin_memory=False):
data_dir = data_dir + '/' + dataset
if dataset == "cifar10":
trans = [transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(0.5),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))]
dataset = datasets.CIFAR10(data_dir, train=True, download=True,
transform=transforms.Compose(trans))
elif dataset == "svhn":
normalize = transforms.Normalize(mean=[x / 255.0 for x in[109.9, 109.7, 113.8]],
std=[x / 255.0 for x in [50.1, 50.6, 50.8]])
trans = [transforms.RandomCrop(32, padding=4),
transforms.ToTensor(),
normalize]
dataset = datasets.SVHN(data_dir, split='train', download=True,
transform=transforms.Compose(trans))
elif dataset == "smallnorb":
trans = [transforms.Resize(48),
transforms.RandomCrop(32),
transforms.ColorJitter(brightness=32./255, contrast=0.3),
transforms.ToTensor(),
#transforms.Normalize((0.7199,), (0.117,))
]
if exp in VIEWPOINT_EXPS:
train_set = smallNORBViewPoint(data_dir, exp=exp, train=True, download=True,
transform=transforms.Compose(trans))
trans = trans[:1] + [transforms.CenterCrop(32)] +trans[3:]
valid_set = smallNORBViewPoint(data_dir, exp=exp, train=False, familiar=False, download=False,
transform=transforms.Compose(trans))
elif exp == "full":
dataset = smallNORB(data_dir, train=True, download=True,
transform = transforms.Compose(trans))
if exp not in VIEWPOINT_EXPS:
num_train = len(dataset)
indices = list(range(num_train))
split = int(np.floor(valid_size * num_train))
if shuffle:
np.random.seed(random_seed)
np.random.shuffle(indices)
train_idx = indices[split:]
valid_idx = indices[:split]
train_set = Subset(dataset, train_idx)
valid_set = Subset(dataset, valid_idx)
train_loader = torch.utils.data.DataLoader(
train_set, batch_size=batch_size, shuffle=True,
num_workers=num_workers, pin_memory=pin_memory,
)
valid_loader = torch.utils.data.DataLoader(
valid_set, batch_size=batch_size, shuffle=False,
num_workers=num_workers, pin_memory=pin_memory,
)
return train_loader, valid_loader
def get_test_loader(data_dir,
dataset,
batch_size,
exp='azimuth', # smallnorb only
familiar=True, # smallnorb only
num_workers=4,
pin_memory=False):
data_dir = data_dir + '/' + dataset
if dataset == "cifar10":
trans = [transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))]
dataset = datasets.CIFAR10(data_dir, train=False, download=False,
transform=transforms.Compose(trans))
elif dataset == "svhn":
normalize = transforms.Normalize(mean=[x / 255.0 for x in[109.9, 109.7, 113.8]],
std=[x / 255.0 for x in [50.1, 50.6, 50.8]])
trans = [transforms.ToTensor(),
normalize]
dataset = datasets.SVHN(data_dir, split='test', download=True,
transform=transforms.Compose(trans))
elif dataset == "smallnorb":
trans = [transforms.Resize(48),
transforms.CenterCrop(32),
transforms.ToTensor(),
#transforms.Normalize((0.7199,), (0.117,))
]
if exp in VIEWPOINT_EXPS:
dataset = smallNORBViewPoint(data_dir, exp=exp, familiar=familiar, train=False, download=True,
transform=transforms.Compose(trans))
elif exp == "full":
dataset = smallNORB(data_dir, train=False, download=True,
transform=transforms.Compose(trans))
data_loader = torch.utils.data.DataLoader(
dataset, batch_size=batch_size, shuffle=False,
num_workers=num_workers, pin_memory=pin_memory,
)
return data_loader
DATASET_CONFIGS = {
'cifar10': {'size': 32, 'channels': 3, 'classes': 10},
'svhn': {'size': 32, 'channels': 3, 'classes': 10},
'smallnorb': {'size': 32, 'channels': 1, 'classes': 5},
}
VIEWPOINT_EXPS = ['azimuth', 'elevation']