-
Notifications
You must be signed in to change notification settings - Fork 201
/
datasets.py
executable file
·192 lines (165 loc) · 9.23 KB
/
datasets.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
185
186
187
188
189
190
191
192
# -*- coding: utf-8 -*-
# @Date : 2019-07-25
# @Author : Xinyu Gong ([email protected])
# @Link : None
# @Version : 0.0
import torch
import torchvision.datasets as datasets
import torchvision.transforms as transforms
from torch.utils.data import Dataset
from celeba import CelebA, FFHQ
class ImageDataset(object):
def __init__(self, args, cur_img_size=None, bs=None):
bs = args.dis_batch_size if bs == None else bs
img_size = cur_img_size if args.fade_in > 0 else args.img_size
if args.dataset.lower() == 'cifar10':
Dt = datasets.CIFAR10
transform = transforms.Compose([
transforms.Resize(size=(img_size, img_size)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])
args.n_classes = 0
train_dataset = Dt(root=args.data_path, train=True, transform=transform, download=True)
val_dataset = Dt(root=args.data_path, train=False, transform=transform)
train_sampler = torch.utils.data.distributed.DistributedSampler(train_dataset)
val_sampler = torch.utils.data.distributed.DistributedSampler(val_dataset)
self.train_sampler = train_sampler
self.train = torch.utils.data.DataLoader(
train_dataset,
batch_size=args.dis_batch_size, shuffle=(train_sampler is None),
num_workers=args.num_workers, pin_memory=True, sampler=train_sampler)
self.valid = torch.utils.data.DataLoader(
val_dataset,
batch_size=args.dis_batch_size, shuffle=False,
num_workers=args.num_workers, pin_memory=True, sampler=val_sampler)
self.test = self.valid
elif args.dataset.lower() == 'stl10':
Dt = datasets.STL10
transform = transforms.Compose([
transforms.Resize(img_size),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])
train_dataset = Dt(root=args.data_path, split='train+unlabeled', transform=transform, download=True)
val_dataset = Dt(root=args.data_path, split='test', transform=transform)
if args.distributed:
train_sampler = torch.utils.data.distributed.DistributedSampler(train_dataset)
val_sampler = torch.utils.data.distributed.DistributedSampler(val_dataset)
else:
train_sampler = None
val_sampler = None
self.train_sampler = train_sampler
self.train = torch.utils.data.DataLoader(
train_dataset,
batch_size=args.dis_batch_size, shuffle=(train_sampler is None),
num_workers=args.num_workers, pin_memory=True, sampler=train_sampler)
self.valid = torch.utils.data.DataLoader(
val_dataset,
batch_size=args.dis_batch_size, shuffle=False,
num_workers=args.num_workers, pin_memory=True, sampler=val_sampler)
self.test = self.valid
elif args.dataset.lower() == 'celeba':
Dt = CelebA
transform = transforms.Compose([
transforms.Resize(size=(img_size, img_size)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])
train_dataset = Dt(root=args.data_path, transform=transform)
val_dataset = Dt(root=args.data_path, transform=transform)
train_sampler = torch.utils.data.distributed.DistributedSampler(train_dataset)
val_sampler = torch.utils.data.distributed.DistributedSampler(val_dataset)
self.train_sampler = train_sampler
self.train = torch.utils.data.DataLoader(
train_dataset,
batch_size=args.dis_batch_size, shuffle=(train_sampler is None),
num_workers=args.num_workers, pin_memory=True, drop_last=True, sampler=train_sampler)
self.valid = torch.utils.data.DataLoader(
val_dataset,
batch_size=args.dis_batch_size, shuffle=False,
num_workers=args.num_workers, pin_memory=True, sampler=val_sampler)
self.test = torch.utils.data.DataLoader(
val_dataset,
batch_size=args.dis_batch_size, shuffle=False,
num_workers=args.num_workers, pin_memory=True, sampler=val_sampler)
elif args.dataset.lower() == 'ffhq':
Dt = FFHQ
transform = transforms.Compose([
transforms.Resize(size=(img_size, img_size)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])
train_dataset = Dt(root=args.data_path, transform=transform)
val_dataset = Dt(root=args.data_path, transform=transform)
train_sampler = torch.utils.data.distributed.DistributedSampler(train_dataset)
val_sampler = torch.utils.data.distributed.DistributedSampler(val_dataset)
self.train_sampler = train_sampler
self.train = torch.utils.data.DataLoader(
train_dataset,
batch_size=args.dis_batch_size, shuffle=(train_sampler is None),
num_workers=args.num_workers, pin_memory=True, drop_last=True, sampler=train_sampler)
self.valid = torch.utils.data.DataLoader(
val_dataset,
batch_size=args.dis_batch_size, shuffle=False,
num_workers=args.num_workers, pin_memory=True, sampler=val_sampler)
self.test = torch.utils.data.DataLoader(
val_dataset,
batch_size=args.dis_batch_size, shuffle=False,
num_workers=args.num_workers, pin_memory=True, sampler=val_sampler)
elif args.dataset.lower() == 'bedroom':
Dt = datasets.LSUN
transform = transforms.Compose([
transforms.Resize(size=(img_size, img_size)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])
train_dataset = Dt(root=args.data_path, classes=["bedroom_train"], transform=transform)
val_dataset = Dt(root=args.data_path, classes=["bedroom_val"], transform=transform)
train_sampler = torch.utils.data.distributed.DistributedSampler(train_dataset)
val_sampler = torch.utils.data.distributed.DistributedSampler(val_dataset)
self.train_sampler = train_sampler
self.train = torch.utils.data.DataLoader(
train_dataset,
batch_size=args.dis_batch_size, shuffle=(train_sampler is None),
num_workers=args.num_workers, pin_memory=True, drop_last=True, sampler=train_sampler)
self.valid = torch.utils.data.DataLoader(
val_dataset,
batch_size=args.dis_batch_size, shuffle=False,
num_workers=args.num_workers, pin_memory=True, sampler=val_sampler)
self.test = torch.utils.data.DataLoader(
val_dataset,
batch_size=args.dis_batch_size, shuffle=False,
num_workers=args.num_workers, pin_memory=True, sampler=val_sampler)
elif args.dataset.lower() == 'church':
Dt = datasets.LSUN
transform = transforms.Compose([
transforms.Resize(size=(img_size, img_size)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])
train_dataset = Dt(root=args.data_path, classes=["church_outdoor_train"], transform=transform)
val_dataset = Dt(root=args.data_path, classes=["church_outdoor_val"], transform=transform)
train_sampler = torch.utils.data.distributed.DistributedSampler(train_dataset)
val_sampler = torch.utils.data.distributed.DistributedSampler(val_dataset)
self.train_sampler = train_sampler
self.train = torch.utils.data.DataLoader(
train_dataset,
batch_size=args.dis_batch_size, shuffle=(train_sampler is None),
num_workers=args.num_workers, pin_memory=True, drop_last=True, sampler=train_sampler)
self.valid = torch.utils.data.DataLoader(
val_dataset,
batch_size=args.dis_batch_size, shuffle=False,
num_workers=args.num_workers, pin_memory=True, sampler=val_sampler)
self.test = torch.utils.data.DataLoader(
val_dataset,
batch_size=args.dis_batch_size, shuffle=False,
num_workers=args.num_workers, pin_memory=True, sampler=val_sampler)
else:
raise NotImplementedError('Unknown dataset: {}'.format(args.dataset))