-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathdataset.py
195 lines (160 loc) · 6.39 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
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
import os
import json
import random
import glob
import torch
from torch.utils.data import Dataset
import numpy as np
from PIL import Image
import PIL.Image
try:
import pyspng
except ImportError:
pyspng = None
class CustomDataset(Dataset):
def __init__(self, data_dir):
PIL.Image.init()
supported_ext = PIL.Image.EXTENSION.keys() | {'.npy'}
self.images_dir = os.path.join(data_dir, 'images')
self.features_dir = os.path.join(data_dir, 'vae-sd')
# images
self._image_fnames = {
os.path.relpath(os.path.join(root, fname), start=self.images_dir)
for root, _dirs, files in os.walk(self.images_dir) for fname in files
}
self.image_fnames = sorted(
fname for fname in self._image_fnames if self._file_ext(fname) in supported_ext
)
# features
self._feature_fnames = {
os.path.relpath(os.path.join(root, fname), start=self.features_dir)
for root, _dirs, files in os.walk(self.features_dir) for fname in files
}
self.feature_fnames = sorted(
fname for fname in self._feature_fnames if self._file_ext(fname) in supported_ext
)
# labels
fname = 'dataset.json'
with open(os.path.join(self.features_dir, fname), 'rb') as f:
labels = json.load(f)['labels']
labels = dict(labels)
labels = [labels[fname.replace('\\', '/')] for fname in self.feature_fnames]
labels = np.array(labels)
self.labels = labels.astype({1: np.int64, 2: np.float32}[labels.ndim])
def _file_ext(self, fname):
return os.path.splitext(fname)[1].lower()
def __len__(self):
assert len(self.image_fnames) == len(self.feature_fnames), \
"Number of feature files and label files should be same"
return len(self.feature_fnames)
def __getitem__(self, idx):
image_fname = self.image_fnames[idx]
feature_fname = self.feature_fnames[idx]
image_ext = self._file_ext(image_fname)
with open(os.path.join(self.images_dir, image_fname), 'rb') as f:
if image_ext == '.npy':
image = np.load(f)
image = image.reshape(-1, *image.shape[-2:])
elif image_ext == '.png' and pyspng is not None:
image = pyspng.load(f.read())
image = image.reshape(*image.shape[:2], -1).transpose(2, 0, 1)
else:
image = np.array(PIL.Image.open(f))
image = image.reshape(*image.shape[:2], -1).transpose(2, 0, 1)
features = np.load(os.path.join(self.features_dir, feature_fname))
return torch.from_numpy(image), torch.from_numpy(features), torch.tensor(self.labels[idx])
def get_feature_dir_info(root):
files = glob.glob(os.path.join(root, '*.npy'))
files_caption = glob.glob(os.path.join(root, '*_*.npy'))
num_data = len(files) - len(files_caption)
n_captions = {k: 0 for k in range(num_data)}
for f in files_caption:
name = os.path.split(f)[-1]
k1, k2 = os.path.splitext(name)[0].split('_')
n_captions[int(k1)] += 1
return num_data, n_captions
class DatasetFactory(object):
def __init__(self):
self.train = None
self.test = None
def get_split(self, split, labeled=False):
if split == "train":
dataset = self.train
elif split == "test":
dataset = self.test
else:
raise ValueError
if self.has_label:
return dataset #if labeled else UnlabeledDataset(dataset)
else:
assert not labeled
return dataset
def unpreprocess(self, v): # to B C H W and [0, 1]
v = 0.5 * (v + 1.)
v.clamp_(0., 1.)
return v
@property
def has_label(self):
return True
@property
def data_shape(self):
raise NotImplementedError
@property
def data_dim(self):
return int(np.prod(self.data_shape))
@property
def fid_stat(self):
return None
def sample_label(self, n_samples, device):
raise NotImplementedError
def label_prob(self, k):
raise NotImplementedError
class MSCOCOFeatureDataset(Dataset):
# the image features are got through sample
def __init__(self, root):
self.root = root
self.num_data, self.n_captions = get_feature_dir_info(root)
def __len__(self):
return self.num_data
def __getitem__(self, index):
with open(os.path.join(self.root, f'{index}.png'), 'rb') as f:
x = np.array(PIL.Image.open(f))
x = x.reshape(*x.shape[:2], -1).transpose(2, 0, 1)
z = np.load(os.path.join(self.root, f'{index}.npy'))
k = random.randint(0, self.n_captions[index] - 1)
c = np.load(os.path.join(self.root, f'{index}_{k}.npy'))
return x, z, c
class CFGDataset(Dataset): # for classifier free guidance
def __init__(self, dataset, p_uncond, empty_token):
self.dataset = dataset
self.p_uncond = p_uncond
self.empty_token = empty_token
def __len__(self):
return len(self.dataset)
def __getitem__(self, item):
x, z, y = self.dataset[item]
if random.random() < self.p_uncond:
y = self.empty_token
return x, z, y
class MSCOCO256Features(DatasetFactory): # the moments calculated by Stable Diffusion image encoder & the contexts calculated by clip
def __init__(self, path, cfg=True, p_uncond=0.1, mode='train'):
super().__init__()
print('Prepare dataset...')
if mode == 'val':
self.test = MSCOCOFeatureDataset(os.path.join(path, 'val'))
assert len(self.test) == 40504
self.empty_context = np.load(os.path.join(path, 'empty_context.npy'))
else:
self.train = MSCOCOFeatureDataset(os.path.join(path, 'train'))
assert len(self.train) == 82783
self.empty_context = np.load(os.path.join(path, 'empty_context.npy'))
if cfg: # classifier free guidance
assert p_uncond is not None
print(f'prepare the dataset for classifier free guidance with p_uncond={p_uncond}')
self.train = CFGDataset(self.train, p_uncond, self.empty_context)
@property
def data_shape(self):
return 4, 32, 32
@property
def fid_stat(self):
return f'assets/fid_stats/fid_stats_mscoco256_val.npz'