-
Notifications
You must be signed in to change notification settings - Fork 2
/
dataloader.py
141 lines (124 loc) · 5.2 KB
/
dataloader.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
import os
import argparse
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
import torchvision.datasets as datasets
import torchvision.transforms as transforms
from utils import subset_sampler
def get_mnist_loaders(data_aug=False, batch_size=128, test_batch_size=1000, perc=1.0, path="./data/mnist") :
if data_aug :
transform_train = transforms.Compose([
transforms.RandomCrop(28, padding=4),
transforms.ToTensor(),
])
else :
transform_train = transforms.Compose([
transforms.ToTensor(),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
])
train_loader = DataLoader(
datasets.MNIST(root=path, train=True, download=True, transform=transform_train),
batch_size=batch_size, shuffle=True, num_workers=2, drop_last=True
)
train_eval_loader = DataLoader(
datasets.MNIST(root=path, train=True, download=True, transform=transform_test),
batch_size=batch_size, shuffle=False, num_workers=2, drop_last=True
)
test_loader = DataLoader(
datasets.MNIST(root=path, train=False, download=True, transform=transform_test),
batch_size=test_batch_size, shuffle=False, num_workers=2, drop_last=True
)
return train_loader, test_loader, train_eval_loader
def get_cifar10_loaders(data_aug=True, batch_size=128, test_batch_size=500, perc=1.0, path="./data/cifar10") :
mean = (0.4914, 0.4822, 0.2265)
std = (0.2023, 0.1994, 0.2010)
if data_aug :
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(mean, std),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean, std)
])
else :
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(mean, std),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean, std),
])
train_loader = DataLoader(
datasets.CIFAR10(root=path, train=True, download=True, transform=transform_train),
batch_size=batch_size, shuffle=True, num_workers=2, drop_last=True
)
eval_loader = DataLoader(
datasets.CIFAR10(root=path, train=True, download=True, transform=transform_test),
batch_size=batch_size, shuffle=False, num_workers=2, drop_last=True
)
test_loader = DataLoader(
datasets.CIFAR10(root=path, train=False, download=True, transform=transform_test),
batch_size=test_batch_size, shuffle=False, num_workers=2, drop_last=True
)
return train_loader, test_loader, eval_loader
def get_fmnist_loaders(data_aug=True, batch_size=128, test_batch_size=500, path="./data/fmnist") :
transform_train = transforms.Compose([
transforms.RandomCrop(28, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor()
])
transform_test = transforms.ToTensor()
train_loader = DataLoader(
datasets.FashionMNIST(root=path, train=True, download=True, transform=transform_train),
batch_size=batch_size, shuffle=True, num_workers=2, drop_last=True
)
eval_loader = DataLoader(
datasets.FashionMNIST(root=path, train=True, download=True, transform=transform_test),
batch_size=test_batch_size, shuffle=False, num_workers=2, drop_last=True
)
test_loader = DataLoader(
datasets.FashionMNIST(root=path, train=True, download=True, transform=transform_test),
batch_size=test_batch_size, shuffle=False, num_workers=2, drop_last=True
)
return train_loader, test_loader, eval_loader
def normalize(image) :
# This function is only used for CIFAR10.
image = image.clone()
mean = (0.4914, 0.4822, 0.2265)
std = (0.2023, 0.1994, 0.2010)
if len(image.shape) == 3 :
image = image.reshape(1, *image.size())
image[:,0,:,:] = (image[:,0,:,:] - mean[0]) / std[0]
image[:,1,:,:] = (image[:,1,:,:] - mean[1]) / std[1]
image[:,2,:,:] = (image[:,2,:,:] - mean[2]) / std[2]
if image.size(0) == 1 :
return image.reshape(*image.size()[1:])
return image
def inverse_normalize(image) :
image = image.clone()
mean = (0.4914, 0.4822, 0.2265)
std = (0.2023, 0.1994, 0.2010)
if len(image.shape) == 3 :
image = image.reshape(1, *image.size())
image[:,0,:,:] = image[:,0,:,:] * std[0] + mean[0]
image[:,1,:,:] = image[:,1,:,:] * std[1] + mean[1]
image[:,2,:,:] = image[:,2,:,:] * std[2] + mean[2]
if image.size(0) == 1 :
return image.reshape(*image.size()[1:])
return image
def inf_generator(iterable) :
iterator = iterable.__iter__()
while True :
try :
yield iterator.__next__()
except StopIteration:
iterator = iterable.__iter__()