-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdataloader.py
50 lines (41 loc) · 1.69 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
from torchvision.transforms import transforms
from torchvision.datasets.mnist import MNIST
from torchvision.datasets.cifar import CIFAR100, CIFAR10
from torch.utils.data import DataLoader
from torch.utils.data.dataset import random_split
def get_loader(image_size, batch_size, data_set='cifar10'):
transform = transforms.Compose([
transforms.Grayscale(3),
transforms.Resize(image_size),
transforms.ToTensor()
])
if data_set == 'cifar100':
dataset_class = CIFAR100
elif data_set == 'cifar10':
dataset_class = CIFAR10
elif data_set == 'mnist':
dataset_class = MNIST
else:
raise Exception('No matched dataset')
dataset = dataset_class('./dataset', train=True, transform=transform, download=True)
train_length = int(0.9 * len(dataset))
validation_length = len(dataset) - train_length
train_dataset, validation_dataset = random_split(dataset, (train_length, validation_length))
train_loader = DataLoader(train_dataset, batch_size, False)
validation_loader = DataLoader(validation_dataset, batch_size, False)
return train_loader, validation_loader
def get_test_loader(image_size, batch_size, data_set='cifar10'):
transform = transforms.Compose([
transforms.Grayscale(3),
transforms.Resize(image_size),
transforms.ToTensor()
])
if data_set == 'cifar100':
dataset_class = CIFAR100
elif data_set == 'cifar10':
dataset_class = CIFAR10
elif data_set == 'mnist':
dataset_class = MNIST
else:
raise Exception('No matched dataset')
return DataLoader(dataset_class('./dataset', train=False, transform=transform, download=True), batch_size, False)