-
Notifications
You must be signed in to change notification settings - Fork 83
/
dataloader.py
57 lines (45 loc) · 1.85 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
import torch
import torchvision.transforms as transforms
import torchvision.datasets as dsets
# Directory containing the data.
root = 'data/'
def get_data(dataset, batch_size):
# Get MNIST dataset.
if dataset == 'MNIST':
transform = transforms.Compose([
transforms.Resize(28),
transforms.CenterCrop(28),
transforms.ToTensor()])
dataset = dsets.MNIST(root+'mnist/', train='train',
download=True, transform=transform)
# Get SVHN dataset.
elif dataset == 'SVHN':
transform = transforms.Compose([
transforms.Resize(32),
transforms.CenterCrop(32),
transforms.ToTensor()])
dataset = dsets.SVHN(root+'svhn/', split='train',
download=True, transform=transform)
# Get FashionMNIST dataset.
elif dataset == 'FashionMNIST':
transform = transforms.Compose([
transforms.Resize(28),
transforms.CenterCrop(28),
transforms.ToTensor()])
dataset = dsets.FashionMNIST(root+'fashionmnist/', train='train',
download=True, transform=transform)
# Get CelebA dataset.
# MUST ALREADY BE DOWNLOADED IN THE APPROPRIATE DIRECTOR DEFINED BY ROOT PATH!
elif dataset == 'CelebA':
transform = transforms.Compose([
transforms.Resize(32),
transforms.CenterCrop(32),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5),
(0.5, 0.5, 0.5))])
dataset = dsets.ImageFolder(root=root+'celeba/', transform=transform)
# Create dataloader.
dataloader = torch.utils.data.DataLoader(dataset,
batch_size=batch_size,
shuffle=True)
return dataloader