-
Notifications
You must be signed in to change notification settings - Fork 1
/
DataLoader.py
171 lines (152 loc) · 7.02 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
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
import torch
import torchvision
import torchvision.transforms as transforms
import torchvision.datasets as datasets
from torch.utils.data import ConcatDataset, random_split
import numpy as np
import torch
from torch.utils.data import Subset, DataLoader
from torch.utils.data import Dataset
def get_data(args):
torch.manual_seed(args.seed)
if args.dataset == 'cifar10':
args.mean = (0.4914, 0.4822, 0.4465)
args.std = (0.2023, 0.1994, 0.2010)
if args.sota:
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(args.mean, args.std),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(args.mean, args.std),
])
else:
if 'mlp' not in args.model:
transform_train = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(args.mean, args.std),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(args.mean, args.std),
])
else:
transform_train = transforms.Compose([
transforms.ToTensor(),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
])
trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform_train)
testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform_test)
elif args.dataset == 'imagenet':
args.mean = (0.485, 0.456, 0.406)
args.std = (0.229, 0.224, 0.225)
if args.sota:
transform_train = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomResizedCrop((64, 64)),
transforms.ToTensor(),
transforms.Normalize(args.mean, args.std),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(args.mean, args.std),
])
else:
transform_train = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(args.mean, args.std),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(args.mean, args.std),
])
trainset = datasets.ImageFolder('data/tiny-imagenet-200/train/', transform_train)
testset = datasets.ImageFolder('data/tiny-imagenet-200/val/images/', transform_test)
elif args.dataset == 'cifar100':
trainset = torchvision.datasets.CIFAR100(root='./data', train=True, download=True)
x = np.concatenate([np.asarray(trainset[i][0]) for i in range(len(trainset))])
args.mean = (np.mean(x, axis=(0, 1))/255).tolist()
args.std = (np.std(x, axis=(0, 1))/255).tolist()
if args.sota:
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(15),
transforms.ToTensor(),
transforms.Normalize(args.mean, args.std),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(args.mean, args.std),
])
else:
if 'mlp' not in args.model:
transform_train = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(args.mean, args.std),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(args.mean, args.std),
])
else:
transform_train = transforms.Compose([
transforms.ToTensor(),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
])
trainset = torchvision.datasets.CIFAR100(root='./data', train=True, download=True, transform=transform_train)
testset = torchvision.datasets.CIFAR100(root='./data', train=False, download=True, transform=transform_test)
elif args.dataset == 'svhn':
trainset = torchvision.datasets.SVHN(root='./data', split='train', download=True)
x = np.concatenate([np.asarray(trainset[i][0]) for i in range(len(trainset))])
args.mean = (np.mean(x, axis=(0, 1))/255).tolist()
args.std = (np.std(x, axis=(0, 1))/255).tolist()
if 'mlp' not in args.model:
trans = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(args.mean, args.std)
])
else:
trans = transforms.Compose([
transforms.ToTensor(),
])
trainset = torchvision.datasets.SVHN(root='./data', split='train', download=True, transform=trans)
testset = torchvision.datasets.SVHN(root='./data', split='test', download=True, transform=trans)
trainset, _ = torch.utils.data.random_split(trainset, [50000, len(trainset) - 50000])
elif args.dataset == 'fashion_mnist':
trainset = torchvision.datasets.FashionMNIST(root='./data', train=True, download=True)
x = np.concatenate([np.asarray(trainset[i][0]) for i in range(len(trainset))])
args.mean = (np.mean(x, axis=(0, 1))/255).tolist()
args.std = (np.std(x, axis=(0, 1))/255).tolist()
if 'mlp' not in args.model:
trans = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(args.mean, args.std),
])
else:
trans = transforms.Compose([
transforms.ToTensor(),
])
trainset = torchvision.datasets.FashionMNIST(root='./data', train=True, download=True, transform=trans)
testset = torchvision.datasets.FashionMNIST(root='./data', train=False, download=True, transform=trans)
train_loader = torch.utils.data.DataLoader(trainset, batch_size=args.batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(testset, batch_size=args.test_batch_size, shuffle=False)
return trainset, train_loader, testset, test_loader
class Data_transform(Dataset):
def __init__(self, subset, transform=None):
self.subset = subset
self.transform = transform
def __getitem__(self, index):
x, y = self.subset[index]
if self.transform:
x = self.transform(x)
return x, y
def __len__(self):
return len(self.subset)