-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
150 lines (120 loc) · 5.55 KB
/
dataset.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
import os
import torch
from torchvision import datasets, transforms
from torch.utils.data import Dataset, DataLoader, TensorDataset, ConcatDataset, random_split
from torch.utils.data.sampler import SubsetRandomSampler
import numpy as np
class ClassicDataset(Dataset):
def __init__(self,
x,
y,
transform):
self.xy = TensorDataset(x, y)
self.transform = transform
def __len__(self):
return len(self.xy)
def __getitem__(self, idx):
x, y = self.xy[idx]
if self.transform:
x = self.transform(x)
return x, y
class DataSplit(object):
def __init__(self, dataset):
if dataset == 'mnist':
trva_real = datasets.MNIST(root='./data-mnist', download=True)
tr_real_ds, va_real_ds = random_split(trva_real, [55000, 5000])
xtr_real = trva_real.train_data[tr_real_ds.indices].view(
-1, 1, 28, 28)
ytr_real = trva_real.train_labels[tr_real_ds.indices]
xva_real = trva_real.train_data[va_real_ds.indices].view(
-1, 1, 28, 28)
yva_real = trva_real.train_labels[va_real_ds.indices]
trans = transforms.Compose(
[transforms.ToPILImage(), transforms.ToTensor()]
)
self.train_dataset = ClassicDataset(
x=xtr_real, y=ytr_real, transform=trans)
self.valid_dataset = ClassicDataset(
x=xva_real, y=yva_real, transform=trans)
self.test_dataset = datasets.MNIST(root='./data-mnist', train=False, transform=transforms.Compose([
transforms.ToTensor()
]))
elif dataset == 'fashion-mnist':
trva_real = datasets.FashionMNIST(
root='./data-fashion-mnist', download=True)
tr_real_ds, va_real_ds = random_split(trva_real, [55000, 5000])
xtr_real = trva_real.train_data[tr_real_ds.indices].view(
-1, 1, 28, 28)
ytr_real = trva_real.train_labels[tr_real_ds.indices]
xva_real = trva_real.train_data[va_real_ds.indices].view(
-1, 1, 28, 28)
yva_real = trva_real.train_labels[va_real_ds.indices]
trans = transforms.Compose(
[transforms.ToPILImage(),
transforms.ToTensor(), transforms.Normalize((.2860,), (.3530,))]
)
self.train_dataset = ClassicDataset(
x=xtr_real, y=ytr_real, transform=trans)
self.valid_dataset = ClassicDataset(
x=xva_real, y=yva_real, transform=trans)
self.test_dataset = datasets.FashionMNIST(root='./data-fashion-mnist', train=False, transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((.2860,), (.3530,))
]))
elif dataset == 'cifar10':
trans = transforms.Compose(
[transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)), ]
)
trva_real = datasets.CIFAR10(
root='./data-cifar10', download=True, transform=trans)
tr_real_ds, va_real_ds = random_split(trva_real, [45000, 5000])
'''
tdata = torch.Tensor(trva_real.train_data)
xtr_real = tdata[tr_real_ds.indices].reshape(
-1, 3, 32, 32)
ytr_real = np.array(trva_real.train_labels)[tr_real_ds.indices]
xva_real = tdata[va_real_ds.indices].reshape(
-1, 3, 32, 32)
yva_real = np.array(trva_real.train_labels)[va_real_ds.indices]
'''
trans = transforms.Compose(
[transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)), ]
)
self.train_dataset = tr_real_ds
self.valid_dataset = va_real_ds
self.test_dataset = datasets.CIFAR10(root='./data-cifar10', train=False, transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(
(0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
]))
else:
raise NotImplementedError()
def get_train_loader(self, batch_size, **kwargs):
train_loader = DataLoader(self.train_dataset,
batch_size=batch_size, num_workers=4, shuffle=True, **kwargs)
return train_loader
def get_valid_loader(self, batch_size, **kwargs):
valid_loader = DataLoader(self.valid_dataset,
batch_size=batch_size, shuffle=True, **kwargs)
return valid_loader
def get_test_loader(self, batch_size, **kwargs):
test_loader = DataLoader(self.test_dataset,
batch_size=batch_size, shuffle=False, **kwargs)
return test_loader
if __name__ == '__main__':
from itertools import product
def get_shapes(loader):
return [t.shape for t in next(loader.__iter__())]
dsets = ['mnist', 'fashion-mnist', 'cifar10']
batch_sizes = [1, 3]
for d in dsets:
split = DataSplit(d)
for b in batch_sizes:
for loader in split.get_train_loader(1), split.get_valid_loader(2), split.get_test_loader(3):
print(d, loader, get_shapes(loader))