-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataset.py
More file actions
345 lines (276 loc) · 11.6 KB
/
Copy pathdataset.py
File metadata and controls
345 lines (276 loc) · 11.6 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import os
import json
import random
import glob
import torch
from torch.utils.data import Dataset
import numpy as np
import einops
from PIL import Image
import PIL.Image
try:
import pyspng
except ImportError:
pyspng = None
from torchvision import transforms
from torchvision.transforms import InterpolationMode
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])
class CustomDataset_512(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-512')
self.features_dir = os.path.join(data_dir, 'vae-sd-512')
# 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))
feature_path = os.path.join(self.features_dir, feature_fname)
try:
features = np.load(feature_path)
except EOFError:
print(f"[EOFError] Empty or corrupted file: {feature_path}")
raise
except Exception as e:
print(f"[Error] Failed to load feature file {feature_path}: {e}")
raise
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):
def __init__(self, root, image_size=256):
self.root = root
self.num_data, self.n_captions = get_feature_dir_info(root)
self.image_size = image_size
# newly added
self.transform = transforms.Compose([
transforms.Resize(image_size, interpolation=transforms.InterpolationMode.LANCZOS),
transforms.CenterCrop(image_size),
transforms.ToTensor()
])
def __len__(self):
return self.num_data
def __getitem__(self, index):
with open(os.path.join(self.root, f'{index}.png'), 'rb') as f:
img = PIL.Image.open(f).convert('RGB')
x = self.transform(img)
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'))
# NEW: read the k-th line text from {index}.txt
txt_path = os.path.join(self.root, f'{index}.txt')
with open(txt_path, 'r', encoding='utf-8') as f:
lines = f.read().splitlines() # 去掉每行末尾的 \n
if k >= len(lines):
raise IndexError(
f"Index {index}: k={k} but {txt_path} only has {len(lines)} lines."
)
t = lines[k] # 第k行(0-based)
return x, z, c, t
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
def center_crop(width, height, img):
resample = {'box': Image.BOX, 'lanczos': Image.LANCZOS}['lanczos']
crop = np.min(img.shape[:2])
img = img[(img.shape[0] - crop) // 2: (img.shape[0] + crop) // 2,
(img.shape[1] - crop) // 2: (img.shape[1] + crop) // 2]
try:
img = Image.fromarray(img, 'RGB')
except:
img = Image.fromarray(img)
img = img.resize((width, height), resample)
return np.array(img).astype(np.uint8)
class MSCOCODatabase(Dataset):
def __init__(self, root, annFile, size=None):
from pycocotools.coco import COCO
self.root = root
self.height = self.width = size
self.coco = COCO(annFile)
self.keys = list(sorted(self.coco.imgs.keys()))
def _load_image(self, key: int):
path = self.coco.loadImgs(key)[0]["file_name"]
return Image.open(os.path.join(self.root, path)).convert("RGB")
def _load_target(self, key: int):
return self.coco.loadAnns(self.coco.getAnnIds(key))
def __len__(self):
return len(self.keys)
def __getitem__(self, index):
key = self.keys[index]
image = self._load_image(key)
image = np.array(image).astype(np.uint8)
image = center_crop(self.width, self.height, image).astype(np.float32)
image = (image / 127.5 - 1.0).astype(np.float32)
image = einops.rearrange(image, 'h w c -> c h w')
anns = self._load_target(key)
target = []
for ann in anns:
target.append(ann['caption'])
return image, target
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'.../fid_stats/fid_stats_mscoco256_val.npz'