This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
dataset.py
75 lines (65 loc) · 3.57 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
import torch
import torchvision.transforms as transforms
import torchvision.datasets as dset
def load_dataset(opt):
if opt.dataset in ['imagenet', 'celeba']:
transformations = []
if opt.centerCropSize > opt.imageSize:
transformations.extend([transforms.CenterCrop(opt.centerCropSize),
transforms.Scale(opt.imageSize)])
else:
transformations.extend([transforms.Scale(opt.imageSize),
transforms.CenterCrop(opt.centerCropSize)])
if not opt.useAutoEncoder:
transformations.extend([transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
])
else:
transformations.extend([transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])
# folder dataset
dataset = dset.ImageFolder(root=opt.dataroot,
transform=transforms.Compose(transformations))
elif opt.dataset == 'lsun':
transformations = []
if opt.centerCropSize > opt.imageSize:
transformations.extend([transforms.CenterCrop(opt.centerCropSize),
transforms.Scale(opt.imageSize)])
else:
transformations.extend([transforms.Scale(opt.imageSize),
transforms.CenterCrop(opt.centerCropSize)])
if not opt.useAutoEncoder:
transformations.extend([transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
])
else:
transformations.extend([transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])
dataset = dset.LSUN(opt.dataroot, classes=['bedroom_train'],
transform=transforms.Compose(transformations))
elif opt.dataset == 'cifar10':
transformations = [transforms.Scale(opt.imageSize), transforms.ToTensor()]
if not opt.useAutoEncoder:
transformations.append(transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]))
else:
transformations.append(transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)))
dataset = dset.CIFAR10(root=opt.dataroot, download=True,
transform=transforms.Compose(transformations))
elif opt.dataset == 'stl10':
transformations = [transforms.Scale(opt.imageSize), transforms.ToTensor()]
if not opt.useAutoEncoder:
transformations.append(transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]))
else:
transformations.append(transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)))
dataset = dset.STL10(root=opt.dataroot, split='unlabeled', download=True,
transform=transforms.Compose(transformations))
assert dataset
dataloader = torch.utils.data.DataLoader(dataset, batch_size=opt.batchSize,
shuffle=True, num_workers=int(opt.workers))
return dataloader