forked from VSehwag/minimal-diffusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
273 lines (259 loc) · 7.84 KB
/
data.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import os
import numpy as np
from PIL import Image
import scipy, scipy.io
from easydict import EasyDict
from collections import OrderedDict
from torch.utils.data import Dataset
from torchvision import datasets, transforms
def get_metadata(name):
if name == "mnist":
metadata = EasyDict(
{
"image_size": 28,
"num_classes": 10,
"train_images": 60000,
"val_images": 10000,
"num_channels": 1,
}
)
elif name == "mnist_m":
metadata = EasyDict(
{
"image_size": 28,
"num_classes": 10,
"train_images": 60000,
"val_images": 10000,
"num_channels": 3,
}
)
elif name == "cifar10":
metadata = EasyDict(
{
"image_size": 32,
"num_classes": 10,
"train_images": 50000,
"val_images": 10000,
"num_channels": 3,
}
)
elif name == "melanoma":
metadata = EasyDict(
{
"image_size": 64,
"num_classes": 2,
"train_images": 33126,
"val_images": 0,
"num_channels": 3,
}
)
elif name == "afhq":
metadata = EasyDict(
{
"image_size": 64,
"num_classes": 3,
"train_images": 14630,
"val_images": 1500,
"num_channels": 3,
}
)
elif name == "celeba":
metadata = EasyDict(
{
"image_size": 64,
"num_classes": 4,
"train_images": 109036,
"val_images": 12376,
"num_channels": 3,
}
)
elif name == "cars":
metadata = EasyDict(
{
"image_size": 64,
"num_classes": 196,
"train_images": 8144,
"val_images": 8041,
"num_channels": 3,
}
)
elif name == "flowers":
metadata = EasyDict(
{
"image_size": 64,
"num_classes": 102,
"train_images": 2040,
"val_images": 6149,
"num_channels": 3,
}
)
elif name == "gtsrb":
metadata = EasyDict(
{
"image_size": 32,
"num_classes": 43,
"train_images": 39252,
"val_images": 12631,
"num_channels": 3,
}
)
else:
raise ValueError(f"{name} dataset nor supported!")
return metadata
class oxford_flowers_dataset(Dataset):
def __init__(self, indexes, labels, root_dir, transform=None):
self.images = []
self.targets = []
self.transform = transform
for i in indexes:
self.images.append(
os.path.join(
root_dir,
"jpg",
"image_" + "".join(["0"] * (5 - len(str(i)))) + str(i) + ".jpg",
)
)
self.targets.append(labels[i - 1] - 1)
def __len__(self):
return len(self.images)
def __getitem__(self, idx):
image = Image.open(self.images[idx]).convert("RGB")
target = self.targets[idx]
if self.transform is not None:
image = self.transform(image)
return image, target
# TODO: Add datasets imagenette/birds/svhn etc etc.
def get_dataset(name, data_dir, metadata):
"""
Return a dataset with the current name. We only support two datasets with
their fixed image resolutions. One can easily add additional datasets here.
Note: To avoid learning the distribution of transformed data, don't use heavy
data augmentation with diffusion models.
"""
if name == "mnist":
transform_train = transforms.Compose(
[
transforms.RandomResizedCrop(
metadata.image_size, scale=(0.8, 1.0), ratio=(0.8, 1.2)
),
transforms.ToTensor(),
]
)
train_set = datasets.MNIST(
root=data_dir,
train=True,
download=True,
transform=transform_train,
)
elif name == "mnist_m":
transform_train = transforms.Compose(
[
transforms.RandomResizedCrop(
metadata.image_size, scale=(0.8, 1.0), ratio=(0.8, 1.2)
),
transforms.ToTensor(),
]
)
train_set = datasets.ImageFolder(
data_dir,
transform=transform_train,
)
elif name == "cifar10":
transform_train = transforms.Compose(
[
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
]
)
train_set = datasets.CIFAR10(
root=data_dir,
train=True,
download=True,
transform=transform_train,
)
elif name in ["imagenette", "melanoma", "afhq"]:
transform_train = transforms.Compose(
[
transforms.Resize(74),
transforms.RandomCrop(64),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
]
)
train_set = datasets.ImageFolder(
data_dir,
transform=transform_train,
)
elif name == "celeba":
# celebA has a large number of images, avoiding randomcropping.
transform_train = transforms.Compose(
[
transforms.Resize(64),
transforms.CenterCrop(64),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
]
)
train_set = datasets.ImageFolder(
data_dir,
transform=transform_train,
)
elif name == "cars":
transform_train = transforms.Compose(
[
transforms.Resize(64),
transforms.RandomCrop(64),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
]
)
train_set = datasets.ImageFolder(
data_dir,
transform=transform_train,
)
elif name == "flowers":
transform_train = transforms.Compose(
[
transforms.Resize(64),
transforms.RandomCrop(64),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
]
)
splits = scipy.io.loadmat(os.path.join(data_dir, "setid.mat"))
labels = scipy.io.loadmat(os.path.join(data_dir, "imagelabels.mat"))
labels = labels["labels"][0]
train_set = oxford_flowers_dataset(
np.concatenate((splits["trnid"][0], splits["valid"][0]), axis=0),
labels,
data_dir,
transform_train,
)
elif name == "gtsrb":
# celebA has a large number of images, avoiding randomcropping.
transform_train = transforms.Compose(
[
transforms.Resize((32, 32)),
transforms.ToTensor(),
]
)
train_set = datasets.ImageFolder(
data_dir,
transform=transform_train,
)
else:
raise ValueError(f"{name} dataset nor supported!")
return train_set
def remove_module(d):
return OrderedDict({(k[len("module.") :], v) for (k, v) in d.items()})
def fix_legacy_dict(d):
keys = list(d.keys())
if "model" in keys:
d = d["model"]
if "state_dict" in keys:
d = d["state_dict"]
keys = list(d.keys())
# remove multi-gpu module.
if "module." in keys[1]:
d = remove_module(d)
return d