-
Notifications
You must be signed in to change notification settings - Fork 63
/
dataset.py
524 lines (457 loc) · 19.9 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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
import os
import random
import time
import imagesize
import numpy as np
import pandas as pd
from gm.data.commons import TEMPLATES_FACE, TEMPLATES_OBJECT, TEMPLATES_STYLE
from gm.util import instantiate_from_config
from PIL import Image
from PIL.ImageOps import exif_transpose
class Text2ImageDataset:
def __init__(
self,
data_path,
target_size=(1024, 1024),
transforms=None,
batched_transforms=None,
tokenizer=None,
token_nums=None,
image_filter_size=0,
random_crop=False,
filter_small_size=False,
multi_aspect=None, # for multi_aspect
seed=42, # for multi_aspect
per_batch_size=1, # for multi_aspect
):
super().__init__()
self.tokenizer = tokenizer
self.token_nums = token_nums
self.dataset_column_names = ["samples"]
if self.tokenizer is None:
self.dataset_output_column_names = self.dataset_column_names
else:
assert token_nums is not None and token_nums > 0
self.dataset_output_column_names = [
"image",
] + [f"token{i}" for i in range(token_nums)]
self.target_size = [target_size, target_size] if isinstance(target_size, int) else target_size
self.random_crop = random_crop
self.filter_small_size = filter_small_size
self.multi_aspect = list(multi_aspect) if multi_aspect is not None else None
self.seed = seed
self.per_batch_size = per_batch_size
all_images, all_captions = self.list_image_files_captions_recursively(data_path)
if filter_small_size:
# print(f"Filter small images, filter size: {image_filter_size}")
all_images, all_captions = self.filter_small_image(all_images, all_captions, image_filter_size)
self.local_images = all_images
self.local_captions = all_captions
self.transforms = []
if transforms:
for i, trans_config in enumerate(transforms):
# Mapper
trans = instantiate_from_config(trans_config)
self.transforms.append(trans)
print(f"Adding mapper {trans.__class__.__name__} as transform #{i} " f"to the datapipeline")
self.batched_transforms = []
if batched_transforms:
for i, bs_trans_config in enumerate(batched_transforms):
# Mapper
bs_trans = instantiate_from_config(bs_trans_config)
self.batched_transforms.append(bs_trans)
print(
f"Adding batch mapper {bs_trans.__class__.__name__} as batch transform #{i} " f"to the datapipeline"
)
def __getitem__(self, idx):
# images preprocess
image_path = self.local_images[idx]
image = Image.open(image_path)
image = exif_transpose(image)
if not image.mode == "RGB":
image = image.convert("RGB")
image = np.array(image).astype(np.uint8)
# caption preprocess
caption = self.local_captions[idx]
caption = np.array(caption)
sample = {
"image": image,
"txt": caption,
"original_size_as_tuple": np.array([image.shape[0], image.shape[1]]), # original h, original w
"target_size_as_tuple": np.array([self.target_size[0], self.target_size[1]]), # target h, target w
"crop_coords_top_left": np.array([0, 0]), # crop top, crop left
"aesthetic_score": np.array(
[
6.0,
]
),
}
for trans in self.transforms:
sample = trans(sample)
return sample
def collate_fn(self, samples, batch_info):
new_size = self.target_size
if self.multi_aspect:
epoch_num, batch_num = batch_info.get_epoch_num(), batch_info.get_batch_num()
cur_seed = epoch_num * 10 + batch_num
random.seed(cur_seed)
new_size = random.choice(self.multi_aspect)
for bs_trans in self.batched_transforms:
samples = bs_trans(samples, target_size=new_size)
batch_samples = {k: [] for k in samples[0]}
for s in samples:
for k in s:
batch_samples[k].append(s[k])
data = {k: (np.stack(v, 0) if isinstance(v[0], np.ndarray) else v) for k, v in batch_samples.items()}
if self.tokenizer:
data = {k: (v.tolist() if k == "txt" else v.astype(np.float32)) for k, v in data.items()}
tokens, _ = self.tokenizer(data)
outs = (data["image"],) + tuple(tokens)
else:
outs = data
return outs
def __len__(self):
return len(self.local_images)
@staticmethod
def list_image_files_captions_recursively(data_path):
anno_dir = data_path
anno_list = sorted(
[os.path.join(anno_dir, f) for f in list(filter(lambda x: x.endswith(".csv"), os.listdir(anno_dir)))]
)
db_list = [pd.read_csv(f) for f in anno_list]
all_images = []
all_captions = []
for db in db_list:
all_images.extend(list(db["dir"]))
all_captions.extend(list(db["text"]))
assert len(all_images) == len(all_captions)
all_images = [os.path.join(data_path, f) for f in all_images]
return all_images, all_captions
@staticmethod
def filter_small_image(all_images, all_captions, image_filter_size):
filted_images = []
filted_captions = []
for image, caption in zip(all_images, all_captions):
w, h = imagesize.get(image)
if min(w, h) < image_filter_size:
print(f"The size of image {image}: {w}x{h} < `image_filter_size` and excluded from training.")
continue
else:
filted_images.append(image)
filted_captions.append(caption)
return filted_images, filted_captions
class Text2ImageDatasetDreamBooth:
def __init__(
self,
instance_data_path,
class_data_path,
instance_prompt,
class_prompt,
train_data_repeat=1,
target_size=(1024, 1024),
transforms=None,
batched_transforms=None,
tokenizer=None,
token_nums=None,
image_filter_size=0,
random_crop=False,
filter_small_size=False,
multi_aspect=None, # for multi_aspect
seed=42, # for multi_aspect
per_batch_size=1, # for multi_aspect
):
super().__init__()
self.tokenizer = tokenizer
self.token_nums = token_nums
self.dataset_column_names = ["instance_samples", "class_samples"]
if self.tokenizer is None:
self.dataset_output_column_names = self.dataset_column_names
else:
assert token_nums is not None and token_nums > 0
instance_l = [
"instance_image",
] + [f"instance_token{i}" for i in range(token_nums)]
class_l = [
"class_image",
] + [f"class_token{i}" for i in range(token_nums)]
self.dataset_output_column_names = []
self.dataset_output_column_names.extend(instance_l)
self.dataset_output_column_names.extend(class_l)
self.target_size = [target_size, target_size] if isinstance(target_size, int) else target_size
self.random_crop = random_crop
self.filter_small_size = filter_small_size
self.multi_aspect = list(multi_aspect) if multi_aspect is not None else None
self.seed = seed
self.per_batch_size = per_batch_size
instance_images = self.list_image_files_recursively(instance_data_path)
instance_images = self.repeat_data(instance_images, train_data_repeat)
print(
f"The training data is repeated {train_data_repeat} times, and the total number is {len(instance_images)}"
)
class_images = self.list_image_files_recursively(class_data_path)
if filter_small_size:
instance_images = self.filter_small_image(instance_images, image_filter_size)
class_images = self.filter_small_image(class_images, image_filter_size)
self.instance_images = instance_images
self.class_images = class_images
self.instance_caption = instance_prompt
self.class_caption = class_prompt
self.transforms = []
if transforms:
for i, trans_config in enumerate(transforms):
# Mapper
trans = instantiate_from_config(trans_config)
self.transforms.append(trans)
print(f"Adding mapper {trans.__class__.__name__} as transform #{i} " f"to the datapipeline")
self.batched_transforms = []
if batched_transforms:
for i, bs_trans_config in enumerate(batched_transforms):
# Mapper
bs_trans = instantiate_from_config(bs_trans_config)
self.batched_transforms.append(bs_trans)
print(
f"Adding batch mapper {bs_trans.__class__.__name__} as batch transform #{i} " f"to the datapipeline"
)
def __getitem__(self, idx):
# images preprocess
instance_image_path = self.instance_images[idx]
instance_image = Image.open(instance_image_path)
instance_image = exif_transpose(instance_image)
if not instance_image.mode == "RGB":
instance_image = instance_image.convert("RGB")
instance_image = np.array(instance_image).astype(np.uint8)
class_image_path = random.choice(self.class_images)
class_image = Image.open(class_image_path)
class_image = exif_transpose(class_image)
if not class_image.mode == "RGB":
class_image = class_image.convert("RGB")
class_image = np.array(class_image).astype(np.uint8)
# caption preprocess
instance_caption = np.array(self.instance_caption)
class_caption = np.array(self.class_caption)
instance_sample = {
"image": instance_image,
"txt": instance_caption,
"original_size_as_tuple": np.array(
[instance_image.shape[0], instance_image.shape[1]]
), # original h, original w
"target_size_as_tuple": np.array([self.target_size[0], self.target_size[1]]), # target h, target w
"crop_coords_top_left": np.array([0, 0]), # crop top, crop left
"aesthetic_score": np.array(
[
6.0,
]
),
}
class_sample = {
"image": class_image,
"txt": class_caption,
"original_size_as_tuple": np.array([class_image.shape[0], class_image.shape[1]]), # original h, original w
"target_size_as_tuple": np.array([self.target_size[0], self.target_size[1]]), # target h, target w
"crop_coords_top_left": np.array([0, 0]), # crop top, crop left
"aesthetic_score": np.array(
[
6.0,
]
),
}
for trans in self.transforms:
instance_sample = trans(instance_sample)
class_sample = trans(class_sample)
return instance_sample, class_sample
def collate_fn(self, instance_samples, class_samples, batch_info):
new_size = self.target_size
if self.multi_aspect:
epoch_num, batch_num = batch_info.get_epoch_num(), batch_info.get_batch_num()
cur_seed = epoch_num * 10 + batch_num
random.seed(cur_seed)
new_size = random.choice(self.multi_aspect)
for bs_trans in self.batched_transforms:
instance_samples = bs_trans(instance_samples, target_size=new_size)
class_samples = bs_trans(class_samples, target_size=new_size)
instance_batch_samples = {k: [] for k in instance_samples[0]}
class_batch_samples = {k: [] for k in class_samples[0]}
for s in instance_samples:
for k in s:
instance_batch_samples[k].append(s[k])
for s in class_samples:
for k in s:
class_batch_samples[k].append(s[k])
instance_batch_samples = {
k: (np.stack(v, 0) if isinstance(v[0], np.ndarray) else v) for k, v in instance_batch_samples.items()
}
class_batch_samples = {
k: (np.stack(v, 0) if isinstance(v[0], np.ndarray) else v) for k, v in class_batch_samples.items()
}
if self.tokenizer:
instance_batch_samples = {
k: (v.tolist() if k == "txt" else v.astype(np.float32)) for k, v in instance_batch_samples.items()
}
instance_tokens, _ = self.tokenizer(instance_batch_samples)
instance_outs = (instance_batch_samples["image"],) + tuple(instance_tokens)
class_batch_samples = {
k: (v.tolist() if k == "txt" else v.astype(np.float32)) for k, v in class_batch_samples.items()
}
class_tokens, _ = self.tokenizer(class_batch_samples)
class_outs = (class_batch_samples["image"],) + tuple(class_tokens)
return instance_outs + class_outs
else:
return instance_batch_samples, class_batch_samples
def __len__(self):
return len(self.instance_images)
@staticmethod
def list_image_files_recursively(image_path):
image_path_list = sorted(os.listdir(image_path))
all_images = [os.path.join(image_path, f) for f in image_path_list]
return all_images
@staticmethod
def filter_small_image(all_images, image_filter_size):
filted_images = []
for image in all_images:
w, h = imagesize.get(image)
if min(w, h) < image_filter_size:
print(f"The size of image {image}: {w}x{h} < `image_filter_size` and excluded from training.")
continue
else:
filted_images.append(image)
return filted_images
@staticmethod
def repeat_data(data_list, repeats):
return data_list * repeats
class Text2ImageDatasetTextualInversion(Text2ImageDataset):
def __init__(
self,
data_path,
*args,
learnable_property="object",
placeholder_token="",
templates=None,
image_extensions=[".png", ".jpg", ".jpeg"],
**kwargs,
):
self.placeholder_token = placeholder_token
self.learnable_property = learnable_property
self.image_extensions = image_extensions # recognize images with these extensions only
if templates is not None:
assert (
isinstance(templates, (list, tuple)) and len(templates) > 0
), "Expect to have non-empty templates as list or tuple."
templates = list(templates)
assert all(
["{}" in x for x in templates]
), "Expect to have templates list of strings such as 'a photo of {{}}'"
else:
if learnable_property.lower() == "object":
templates = TEMPLATES_OBJECT
elif learnable_property.lower() == "style":
templates = TEMPLATES_STYLE
elif learnable_property.lower() == "face":
templates = TEMPLATES_FACE
else:
raise ValueError(
f"{learnable_property} learnable property is not supported! Only support ['object', 'style', 'face']"
)
self.templates = templates
super().__init__(data_path, *args, **kwargs)
def __getitem__(self, idx):
# images preprocess
image_path = self.local_images[idx]
image = Image.open(image_path)
image = exif_transpose(image)
if not image.mode == "RGB":
image = image.convert("RGB")
image = np.array(image).astype(np.uint8)
# caption preprocess
placeholder_string = self.placeholder_token
caption = random.choice(self.templates).format(placeholder_string)
caption = caption if self.tokenizer is None else np.array(self.tokenize(caption), dtype=np.int32)
caption = np.array(caption)
sample = {
"image": image,
"txt": caption,
"original_size_as_tuple": np.array([image.shape[0], image.shape[1]]), # original h, original w
"target_size_as_tuple": np.array([self.target_size[0], self.target_size[1]]), # target h, target w
"crop_coords_top_left": np.array([0, 0]), # crop top, crop left
"aesthetic_score": np.array(
[
6.0,
]
),
}
for trans in self.transforms:
sample = trans(sample)
return sample
def list_image_files_captions_recursively(self, image_path):
assert os.path.exists(image_path), f"The given data path {image_path} does not exist!"
image_path_list = sorted(os.listdir(image_path))
img_extensions = self.image_extensions
all_images = [
os.path.join(image_path, f) for f in image_path_list if any([f.lower().endswith(e) for e in img_extensions])
]
return all_images, None
@staticmethod
def filter_small_image(all_images, all_captions, image_filter_size):
assert all_captions is None
filted_images = []
for image in all_images:
w, h = imagesize.get(image)
if min(w, h) < image_filter_size:
print(f"The size of image {image}: {w}x{h} < `image_filter_size` and excluded from training.")
continue
else:
filted_images.append(image)
return filted_images, None
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="check dataset")
parser.add_argument("--target", type=str, default="Text2ImageDataset")
# for Text2ImageDataset
parser.add_argument("--data_path", type=str, default="")
# for Text2ImageDatasetDreamBooth
parser.add_argument("--instance_data_path", type=str, default="")
parser.add_argument("--class_data_path", type=str, default="")
parser.add_argument("--instance_prompt", type=str, default="")
parser.add_argument("--class_prompt", type=str, default="")
args, _ = parser.parse_known_args()
transforms = [
{"target": "gm.data.mappers.Resize", "params": {"size": 1024, "interpolation": 3}},
{"target": "gm.data.mappers.Rescaler", "params": {"isfloat": False}},
{"target": "gm.data.mappers.AddOriginalImageSizeAsTupleAndCropToSquare"},
]
if args.target == "Text2ImageDataset":
dataset = Text2ImageDataset(data_path=args.data_path, target_size=1024, transforms=transforms)
dataset_size = len(dataset)
print(f"dataset size: {dataset_size}")
s_time = time.time()
for i, data in enumerate(dataset):
if i > 9:
break
print(
f"{i}/{dataset_size}, image shape: {data.pop('image')}, {data}, "
f"time cost: {(time.time()-s_time) * 1000} ms"
)
s_time = time.time()
elif args.target == "Text2ImageDatasetDreamBooth":
dataset = Text2ImageDatasetDreamBooth(
instance_data_path=args.instance_data_path,
class_data_path=args.class_data_path,
instance_prompt=args.instance_prompt,
class_prompt=args.class_prompt,
target_size=1024,
transforms=transforms,
)
dataset_size = len(dataset)
print(f"dataset size: {dataset_size}")
for i, data in enumerate(dataset):
print(data)
break
elif args.target == "Text2ImageDatasetTextualInversion":
dataset = Text2ImageDatasetTextualInversion(
data_path=args.data_path,
learnable_property="style",
placeholder_token="<pokemon>",
target_size=1024,
transforms=transforms,
)
else:
ValueError("dataset only support Text2ImageDataset and Text2ImageDatasetDreamBooth")