-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataloader.py
184 lines (151 loc) · 7.79 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
172
173
174
175
176
177
178
179
180
181
182
183
184
import numpy as np
import torch
import torchvision
'''
Dataloaders. We use the image datasets Cifar10, Cifar100, MNIST and SVHN
(like Entezari et al. https://arxiv.org/pdf/2110.06296.pdf, but omit the imagenet dataset).
'''
# SET ALL SEEDS
def get_dataloader_from_name(name: str,
batch_size: int,
num_workers: int = 0,
root: str = "data",
validation_fraction: float = 0.1,
model_name: str = None) -> (
torch.utils.data.DataLoader):
'''
Downloads the datasets given a name to the "data" folder and returns train,valid and test data loaders.
'''
train_dataset, valid_dataset, test_dataset = None, None, None
if name == "cifar10":
# analogous to https://github.com/sidak/otfusion/blob/master/data.py
transforms = torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
# see https: // github.com / soapisnotfat / pytorch - cifar10 / blob / master / main.py for alternative
# or this:
# transforms = torchvision.transforms.Compose([
# torchvision.transforms.Resize((70, 70)),
# torchvision.transforms.RandomCrop((64, 64)),
# torchvision.transforms.ToTensor(),
# torchvision.transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
# ])
train_dataset = torchvision.datasets.CIFAR10(root=root, train=True, transform=transforms, download=True)
valid_dataset = torchvision.datasets.CIFAR10(root=root, train=True, transform=transforms, download=True)
test_dataset = torchvision.datasets.CIFAR10(root=root, train=False, transform=transforms, download=True)
if name == "cifar100":
# from https://github.com/solangii/CIFAR10-CIFAR100/blob/master/data.py
transform_train = torchvision.transforms.Compose([
torchvision.transforms.RandomCrop(32, padding=4),
torchvision.transforms.RandomHorizontalFlip(),
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)),
])
transform_test = torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)),
])
train_dataset = torchvision.datasets.CIFAR100(root=root, train=True, transform=transform_train, download=True)
valid_dataset = torchvision.datasets.CIFAR100(root=root, train=True, transform=transform_train, download=True)
test_dataset = torchvision.datasets.CIFAR100(root=root, train=False, transform=transform_test, download=True)
if name == "mnist":
# see https://github.com/sidak/otfusion/blob/master/mnist.py (ot-fusion paper)
transforms = torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize((0.1307,), (0.3081,))
])
train_dataset = torchvision.datasets.MNIST(root=root, train=True, transform=transforms, download=True)
valid_dataset = torchvision.datasets.MNIST(root=root, train=True, transform=transforms, download=True)
test_dataset = torchvision.datasets.MNIST(root=root, train=False, transform=transforms, download=True)
if name == "svhn":
# analogue to here: https://jovian.com/proprincekush/svhn-cnn
# todo: look at papers to see what they used here
# todo: get format 1 (not mnist-style but with bounding boxes)
transforms = torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
])
train_dataset = torchvision.datasets.SVHN(root=root, split="train", transform=transforms, download=True)
valid_dataset = torchvision.datasets.SVHN(root=root, split="train", transform=transforms, download=True)
test_dataset = torchvision.datasets.SVHN(root=root, split="test", transform=transforms, download=True)
if name == "places365":
pass
if name == "places205":
pass
# Perform index-based train-validation split of original training data.
total = len(train_dataset) # Get overall number of samples in original training data.
idx = list(range(total)) # Make index list.
np.random.shuffle(idx) # Shuffle indices.
vnum = int(validation_fraction * total) # Determine number of validation samples from validation split.
train_indices, valid_indices = idx[vnum:], idx[0:vnum] # Extract train and validation indices.
# Get samplers.
train_sampler = torch.utils.data.SubsetRandomSampler(train_indices)
valid_sampler = torch.utils.data.SubsetRandomSampler(valid_indices)
# Get data loaders.
valid_loader = torch.utils.data.DataLoader(dataset=valid_dataset, batch_size=batch_size,
num_workers=num_workers, sampler=valid_sampler)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=batch_size,
num_workers=num_workers, drop_last=True, sampler=train_sampler)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=batch_size,
num_workers=num_workers, shuffle=False)
return train_loader, valid_loader, test_loader
# GET DATALOADERS (NON-PARALLEL)
def get_dataloaders_cifar10(batch_size: int,
num_workers: int = 0,
root: str = 'cifar10',
validation_fraction: float = 0.1,
train_transforms: torchvision.transforms.Compose = None,
test_transforms: torchvision.transforms.Compose = None) \
-> torch.utils.data.DataLoader:
if train_transforms is None:
train_transforms = torchvision.transforms.ToTensor()
if test_transforms is None:
test_transforms = torchvision.transforms.ToTensor()
# Load training data.
train_dataset = torchvision.datasets.CIFAR10(
root=root,
train=True,
transform=train_transforms,
download=True
)
# Load validation data.
valid_dataset = torchvision.datasets.CIFAR10(
root=root,
train=True,
transform=test_transforms
)
# Load test data.
test_dataset = torchvision.datasets.CIFAR10(
root=root,
train=False,
transform=test_transforms
)
# Perform index-based train-validation split of original training data.
total = len(train_dataset) # Get overall number of samples in original training data.
idx = list(range(total)) # Make index list.
np.random.shuffle(idx) # Shuffle indices.
vnum = int(validation_fraction * total) # Determine number of validation samples from validation split.
train_indices, valid_indices = idx[vnum:], idx[0:vnum] # Extract train and validation indices.
# Get samplers.
train_sampler = torch.utils.data.SubsetRandomSampler(train_indices)
valid_sampler = torch.utils.data.SubsetRandomSampler(valid_indices)
# Get data loaders.
valid_loader = torch.utils.data.DataLoader(
dataset=valid_dataset,
batch_size=batch_size,
num_workers=num_workers,
sampler=valid_sampler
)
train_loader = torch.utils.data.DataLoader(
dataset=train_dataset,
batch_size=batch_size,
num_workers=num_workers,
drop_last=True,
sampler=train_sampler
)
test_loader = torch.utils.data.DataLoader(
dataset=test_dataset,
batch_size=batch_size,
num_workers=num_workers,
shuffle=False
)
return train_loader, valid_loader, test_loader