-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
74 lines (65 loc) · 2.26 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
from sklearn.utils import shuffle
import torch
import torchvision
from torchvision import transforms
# Sample data from the loader
def get_sample(loader):
while True:
for batch in loader:
yield batch
def get_data_loader(datasetname, root, batch_size, transform):
if datasetname == 'LSUN':
dataset = torchvision.datasets.LSUN(
root = root,
classes = ['church_outdoor_train'],
transform=transform
)
dataloader = torch.utils.data.DataLoader(
dataset,
batch_size = batch_size,
num_workers = 2,
pin_memory = True,
shuffle=False
)
elif datasetname=='CELEBA':
dataset = torchvision.datasets.CelebA(
root = root,
transform = transform,
download=True
)
dataloader = torch.utils.data.DataLoader(
dataset,
batch_size = batch_size,
num_workers = 2,
pin_memory = True,
shuffle=False
)
elif datasetname == 'CIFAR-10':
dataset = torchvision.datasets.CIFAR10(
root = root,
train=True,
download=True,
transform = transforms.Compose([
transforms.Resize(32),
transforms.CenterCrop(32),
transforms.ToTensor()
])
)
dataloader = torch.utils.data.DataLoader(
dataset,
batch_size = batch_size,
num_workers = 2,
pin_memory = True
)
else:
raise ValueError(f'No dataset named {datasetname}!')
return dataloader
if '__name__' == '__main__':
# Dataset
datasetname = 'LSUN'
# Data Root
root = './'
# Parameters
batch_size = 256
# Get Dataloader
loader = get_data_loader(datasetname, root, batch_size)