-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdata.py
179 lines (167 loc) · 5.15 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
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
from celeba import CelebA
import torchvision.transforms.functional as F
class Crop(object):
def __init__(self, x1, x2, y1, y2):
self.x1 = x1
self.x2 = x2
self.y1 = y1
self.y2 = y2
def __call__(self, img):
return F.crop(img, self.x1, self.y1, self.x2 - self.x1, self.y2 - self.y1)
def __repr__(self):
return self.__class__.__name__ + "(x1={}, x2={}, y1={}, y2={})".format(
self.x1, self.x2, self.y1, self.y2
)
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
def get_dataset(name, data_dir):
"""
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.ToTensor(),
]
)
train_set = datasets.MNIST(
root=data_dir,
train=True,
download=True,
transform=transform_train,
)
elif name == "mnist_m":
transform_train = transforms.Compose(
[
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 == "celeba64":
# celebA has a large number of images, avoiding randomcropping.
cx = 89
cy = 121
x1 = cy - 64
x2 = cy + 64
y1 = cx - 64
y2 = cx + 64
train_set = CelebA(
root=os.path.join(data_dir),
split="train",
transform=transforms.Compose(
[
Crop(x1, x2, y1, y2),
transforms.Resize(64),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
]
),
download = True
)
# 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