-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdataset_util.py
336 lines (285 loc) · 10.4 KB
/
dataset_util.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
# Custom dataloader implementation for Stable Cascade, based on StableTuner's
import warnings
import os
import random
import math
import numpy as np
from tqdm import tqdm
from PIL import Image, ImageFile
from PIL import UnidentifiedImageError
class BucketWalker():
def __init__(
self,
reject_aspects=1000,
path=None,
tokenizer=None
):
self.images = []
self.reject_aspects=reject_aspects
self.reject_count = 0
self.interrupted = False
self.final_dataset = []
self.buckets = {}
self.tokenizer = tokenizer
# Optionally provide a path so you can manually walk folders later.
if path is not None:
self.walk_dataset_folders(self, path)
def bucketize(self, batch_size):
all_aspects = self.buckets.keys()
# Make all buckets divisible by batch size
original_count = 0
for aspect in all_aspects:
aspect_len = len(self.buckets[aspect])
original_count += aspect_len
if aspect_len > 0:
remain = batch_size - aspect_len % batch_size
if remain > 0 and remain != batch_size:
for i in range(remain):
self.buckets[aspect].extend(random.sample(self.buckets[aspect], 1))
#print(f"Bucket {aspect} has {aspect_len} images, duplicated {remain} images to fit batch size.")
# else:
# print(f"Bucket {aspect} has {aspect_len} images, duplicates not required, nice!")
random.shuffle(self.buckets[aspect])
# Finally
self.final_dataset.extend(self.buckets[aspect])
total_count = len(self.final_dataset)
print(f"Original Image Count: {original_count}")
print(f"Total Image Count: {total_count}")
print(f"Total Step Count: {total_count // batch_size}")
@staticmethod
def walk_dataset_folders(self, path):
if self.interrupted:
return
with warnings.catch_warnings():
warnings.simplefilter("ignore")
sub_dirs = []
path_list = os.listdir(path)
pbar = tqdm(path_list, desc=f"* Processing: {path}")
for f in path_list:
current = os.path.join(path, f)
if os.path.isfile(current):
ext = os.path.splitext(f)[1].lower()
if ext in [".jpg", ".jpeg", ".png", ".bmp", ".webp"]:
try:
# Converting to RGB will ensure no truncated or malformed images pass into the training set
image = Image.open(current).convert("RGB")
width, height = image.size
aspect = width / height
# Only allow aspect ratios above this, a ratio of 6 would allow all aspects less than 1:6
# The image is always oriented "vertically" for this check to ensure consistency against all cases.
se = min(width, height)
le = max(width, height)
alt_aspect = le/se
if alt_aspect <= self.reject_aspects:
trimmed_aspect = f"{aspect:.2f}"
txt_file = os.path.splitext(current)[0] + ".txt"
caption = ""
if os.path.exists(txt_file):
with open(txt_file, "r", encoding="utf-8") as txt:
caption = txt.readline().strip()
if len(caption) < 1:
raise ValueError(f"Could not find valid text in: {txt_file}")
#Should it succeed in finding captions add it:
file_dict = {"path": current, "width": width, "height": height, "aspect": trimmed_aspect, "caption": caption}
if trimmed_aspect not in self.buckets:
self.buckets[trimmed_aspect] = []
self.buckets[trimmed_aspect].append(file_dict)
else:
tqdm.write(f"No text file found for: {txt_file}; skipping.")
self.reject_count += 1
except UnidentifiedImageError as e:
tqdm.write(f"Cannot load {current}, file may be broken or corrupt.")
self.reject_count += 1
except Image.DecompressionBombWarning as e:
tqdm.write(f"Cannot load {current}, file is too large.")
self.reject_count += 1
except ValueError as e:
tqdm.write("Cannot load {current}, file is either too large or a ValueError occurred.")
self.reject_count += 1
except KeyboardInterrupt:
self.interrupted = True
except:
tqdm.write(f"Cannot load {current}, file may be broken or corrupt.")
self.reject_count += 1
if os.path.isdir(current):
sub_dirs.append(current)
pbar.update(1)
for dir in sub_dirs:
self.walk_dataset_folders(self=self, path=dir)
def scan_folder(self, path):
self.walk_dataset_folders(self, path)
def get_rejects(self):
return self.reject_count
def get_buckets(self):
# Deduplicate buckets with a > 1 aspect ratio
aspects = self.buckets.keys()
buckets = {}
for aspect in aspects:
actual_aspect = aspect
if actual_aspect not in buckets:
buckets[actual_aspect] = True
all_aspects = buckets.keys()
output_buckets = []
for aspect in all_aspects:
output_buckets.append(float(aspect))
return output_buckets
def get_final_dataset(self):
return self.final_dataset
def __len__(self):
return len(self.final_dataset)
def __getitem__(self, i):
idx = i % len(self.final_dataset)
item = self.final_dataset[idx]
tokens = self.tokenizer(
item["caption"],
padding=False,
add_special_tokens=False,
verbose=False
).input_ids
return {"images": item["path"], "caption": item["caption"], "tokens": tokens, "aspects": item["aspect"]}
from torch.utils.data import Dataset
import torch
# This is known to work on multi-GPU setups
class CachedLatents(Dataset):
def __init__(self, accelerator, tokenizer=None, tag_shuffle=False, retokenise=False):
self.cache_paths = []
self.accelerator = accelerator
self.tokenizer = tokenizer
self.tag_shuffle = tag_shuffle
self.retokenise = retokenise
if tag_shuffle:
print("Will shuffle captions in Latent Caches.")
if retokenise:
print("Will retokenise captions in Latent Caches.")
def __len__(self):
return len(self.cache_paths)
def __getitem__(self, index):
if index == 0:
random.shuffle(self.cache_paths)
cache = torch.load(self.cache_paths[index][0], map_location=self.accelerator.device)
if self.cache_paths[index][1]:
cache["dropout"] = True
if self.tag_shuffle:
del cache["tokens"]
del cache["att_mask"]
shuffled_captions = []
for caption in cache["captions"]:
shuffled_caption = ""
tags = caption.split(",")
random.shuffle(tags)
for tag in tags:
t = tag.strip()
shuffled_caption += f"{t}, "
shuffled_captions.append(shuffled_caption)
raw_tokens = self.tokenizer(
shuffled_captions,
padding=False,
add_special_tokens=False,
verbose=False
).input_ids
# Get total number of chunks
max_len = max(len(x) for x in raw_tokens)
num_chunks = math.ceil(max_len / (self.tokenizer.model_max_length - 2))
if num_chunks < 1:
num_chunks = 1
# Get the true padded length of the tokens
len_input = self.tokenizer.model_max_length - 2
if num_chunks > 1:
len_input = (self.tokenizer.model_max_length * num_chunks) - (num_chunks * 2)
# Tokenize!
tokens = self.tokenizer.pad(
{"input_ids": raw_tokens},
padding="max_length",
max_length=len_input,
return_tensors="pt",
).to("cpu")
b_tokens = tokens["input_ids"]
b_att_mask = tokens["attention_mask"]
max_standard_tokens = self.tokenizer.model_max_length - 2
true_len = max(len(x) for x in b_tokens)
n_chunks = np.ceil(true_len / max_standard_tokens).astype(int)
max_len = n_chunks.item() * max_standard_tokens
# Properly manage memory here - don't bother loading tokens onto GPU.
# Should prevent an OOM scenario on the GPU.
cropped_tokens = [b_tokens[:, i:i + max_standard_tokens].clone().detach().to("cpu") for i in range(0, max_len, max_standard_tokens)]
cropped_attn = [b_att_mask[:, i:i + max_standard_tokens].clone().detach().to("cpu") for i in range(0, max_len, max_standard_tokens)]
cache["tokens"] = cropped_tokens
cache["att_mask"] = cropped_attn
del tokens
del b_tokens
del b_att_mask
if self.retokenise:
del cache["tokens"]
del cache["att_mask"]
raw_tokens = self.tokenizer(
cache["captions"],
padding=False,
add_special_tokens=False,
verbose=False
).input_ids
# Get total number of chunks
max_len = max(len(x) for x in raw_tokens)
num_chunks = math.ceil(max_len / (self.tokenizer.model_max_length - 2))
if num_chunks < 1:
num_chunks = 1
# Get the true padded length of the tokens
len_input = self.tokenizer.model_max_length - 2
if num_chunks > 1:
len_input = (self.tokenizer.model_max_length * num_chunks) - (num_chunks * 2)
# Tokenize!
tokens = self.tokenizer.pad(
{"input_ids": raw_tokens},
padding="max_length",
max_length=len_input,
return_tensors="pt",
).to("cpu")
b_tokens = tokens["input_ids"]
b_att_mask = tokens["attention_mask"]
max_standard_tokens = self.tokenizer.model_max_length - 2
true_len = max(len(x) for x in b_tokens)
n_chunks = np.ceil(true_len / max_standard_tokens).astype(int)
max_len = n_chunks.item() * max_standard_tokens
# Properly manage memory here - don't bother loading tokens onto GPU.
# Should prevent an OOM scenario on the GPU.
cropped_tokens = [b_tokens[:, i:i + max_standard_tokens].clone().detach().to("cpu") for i in range(0, max_len, max_standard_tokens)]
cropped_attn = [b_att_mask[:, i:i + max_standard_tokens].clone().detach().to("cpu") for i in range(0, max_len, max_standard_tokens)]
cache["tokens"] = cropped_tokens
cache["att_mask"] = cropped_attn
del tokens
del b_tokens
del b_att_mask
return cache
def get_cache_list(self):
return self.cache_paths
def add_cache_location(self, cache_path, dropout):
self.cache_paths.append((cache_path, dropout))
# Work in progress
class RegularLatents(Dataset):
def __init__(self, bucketer, accelerator):
self.batches = []
self.bucketer = bucketer
self.accelerator = accelerator
def __len__(self):
return len(self.batches)
def __getitem__(self, index):
if index == 0:
random.shuffle(self.batches)
images = []
for i in range(0, len(self.batches[index][0]["images"])):
images.append(self.bucketer.load_and_resize(self.batches[index][0][0]["images"][i]), float(self.batches[index][0][0]["aspect"][i]))
images = torch.stack(images)
images = images.to(memory_format=torch.contiguous_format)
images = images.to(self.accelerator.device)
batch = {
"aspects": self.batches[0][0]["aspects"],
"images": images,
"tokens": None,
"att_mask": None,
"captions": self.batches[0][0]["captions"],
"dropout": False
}
return batch
def get_batch_list(self):
return self.batches
def add_latent_batch(self, batch, dropout):
self.batches.append((batch, dropout))