File size: 21,174 Bytes
9c6594c |
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 |
# coding=utf-8
# Copyright 2025 MMaDA Team
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import itertools
import json
import math
import os
import random
import re
import pandas as pd
from functools import partial
from typing import List, Optional, Union
from PIL import Image
Image.warnings.simplefilter('error', Image.DecompressionBombWarning)
import webdataset as wds
import yaml
from braceexpand import braceexpand
from torch.utils.data import default_collate
from torchvision import transforms
from transformers import PreTrainedTokenizer
from webdataset.tariterators import (
base_plus_ext,
tar_file_expander,
url_opener,
valid_sample,
)
person_token = ["a person", "someone", "somebody"]
def replace_person_token(t):
"Used for CC12M - handles all case variations of <person> tag"
t = re.sub(r"<person>([,\s]*(and)*[,\s]*<person>)+", " people ", t, flags=re.IGNORECASE)
person_pattern = re.compile(r"<person>", re.IGNORECASE)
while person_pattern.search(t):
match = person_pattern.search(t)
t = t[:match.start()] + f" {random.choice(person_token)} " + t[match.end():]
return t
def filter_keys(key_set):
def _f(dictionary):
return {k: v for k, v in dictionary.items() if k in key_set}
return _f
def group_by_keys_nothrow(data, keys=base_plus_ext, lcase=True, suffixes=None, handler=None, src=None):
"""Return function over iterator that groups key, value pairs into samples.
:param keys: function that splits the key into key and extension (base_plus_ext)
:param lcase: convert suffixes to lower case (Default value = True)
"""
current_sample = None
for filesample in data:
assert isinstance(filesample, dict)
if "fname" not in filesample.keys():
print(f"fname not in filesample.keys(): {filesample}")
print(f"src: {src}")
continue
fname, value = filesample["fname"], filesample["data"]
prefix, suffix = keys(fname)
if prefix is None:
continue
if lcase:
suffix = suffix.lower()
if current_sample is None or prefix != current_sample["__key__"] or suffix in current_sample:
if valid_sample(current_sample):
yield current_sample
current_sample = dict(__key__=prefix, __url__=filesample["__url__"])
if suffixes is None or suffix in suffixes:
current_sample[suffix] = value
if valid_sample(current_sample):
yield current_sample
def tarfile_to_samples_nothrow(src, handler=wds.warn_and_continue):
# NOTE this is a re-impl of the webdataset impl with group_by_keys that doesn't throw
streams = url_opener(src, handler=handler)
files = tar_file_expander(streams, handler=handler) # [{fname,data,__url__}, ...] __url__ ๅญๆฎตๆ ่ฏๅฝๅ่ฏปๅ็ๆไปถๆฅ่ชๅชไธช tar ๅ
samples = group_by_keys_nothrow(files, handler=handler, src=src)
return samples
def image_transform(sample, resolution=256):
image = sample["images"]
image = transforms.Resize(resolution, interpolation=transforms.InterpolationMode.BICUBIC)(image)
image = transforms.CenterCrop((resolution, resolution))(image)
image = transforms.ToTensor()(image)
image = transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5], inplace=True)(image)
sample["images"] = image
return sample
def image_transform_squash(sample, resolution=256):
image = sample["images"]
image = transforms.Resize((resolution, resolution), interpolation=transforms.InterpolationMode.BICUBIC)(image)
image = transforms.ToTensor()(image)
image = transforms.Normalize(mean=[0.5, 0.5, 0.5],std=[0.5, 0.5, 0.5])(image)
sample["images"] = image
return sample
def conditional_image_transform(sample, resolution=256):
url = sample.get("__url__", "")
special_datasets = ['ai2d', 'clevr', 'docvqa', 'geo']
use_squash = False
for keyword in special_datasets:
if keyword in url:
use_squash = True
break
if use_squash:
return image_transform_squash(sample, resolution)
else:
return image_transform(sample, resolution)
def remove_prefix(caption):
caption = caption.replace('The image features ', '').replace('The image presents ', '').replace(
"The image you've sent is, ", '').replace("In the center of the image, ", '').replace(
"The image showcases ", '').replace("The image is ", '').replace(
"The image captures ", '').replace("In the given image ", '').replace(
"The image portrays ", '').replace("In the image, ", '').replace("In this image, we see ", '').replace(
"The image depicts ", '').replace("This is ", '').replace("In this image, ", '').replace(
"This image captures ", '')
return caption
def filter_long_samples(sample):
return sample.get('input_ids') is not None
class Text2ImageDataset:
def __init__(
self,
train_shards_path_or_url: Union[str, List[str]],
tokenizer: PreTrainedTokenizer,
max_seq_length: int,
num_train_examples: int,
per_gpu_batch_size: int,
global_batch_size: int,
num_workers: int,
resolution: int = 256,
shuffle_buffer_size: int = 1000,
pin_memory: bool = False,
persistent_workers: bool = False,
external_caption_path: Optional[str] = '',
external_journeydb_caption_path: Optional[str] = '',
external_laion12m_caption_path: Optional[str] = '',
external_cc12m_caption_path: Optional[str] = '',
external_text_to_image_2M_512_caption_path: Optional[str] = '',
external_ai2d_caption_path: Optional[str] = '',
external_clevr_caption_path: Optional[str] = '',
external_docvqa_caption_path: Optional[str] = '',
external_geo_caption_path: Optional[str] = '',
is_captioning: bool = False,
add_caption_prompt: bool = False,
long_caption: bool = True,
shuffle: bool = True,
):
if f"{train_shards_path_or_url}.yaml" in os.listdir('./configs'):
with open(f"./configs/{train_shards_path_or_url}.yaml") as f:
train_shards_path_or_url = yaml.safe_load(f)
self.long_caption = long_caption
self.external_caption_path = external_caption_path
self.external_journeydb_caption_path = external_journeydb_caption_path
self.external_laion12m_caption_path = external_laion12m_caption_path
self.external_cc12m_caption_path = external_cc12m_caption_path
self.external_text_to_image_2M_512_caption_path = external_text_to_image_2M_512_caption_path
self.is_captioning = is_captioning
self.add_caption_prompt = add_caption_prompt
if self.add_caption_prompt:
with open("./training/questions.json") as f:
self.caption_prompt = json.load(f)
# self.caption_prompt = ['USER: \n' + prompt + ' ASSISTANT:' for prompt in self.caption_prompt]
self.caption_prompt = ['<|start_header_id|>user<|end_header_id|>\n' + prompt + '<eot_id><|start_header_id|>assistant<|end_header_id|>\n' for prompt in self.caption_prompt]
else:
self.caption_prompt = None
if external_journeydb_caption_path != '':
with open(external_journeydb_caption_path) as file:
self.journeydb_caption = json.load(file)
else:
self.journeydb_caption = None
if external_ai2d_caption_path!= '':
self.ai2d_caption = pd.read_csv(external_ai2d_caption_path)
if external_clevr_caption_path!= '':
self.clevr_caption = pd.read_csv(external_clevr_caption_path)
if external_docvqa_caption_path!= '':
self.docvqa_caption = pd.read_csv(external_docvqa_caption_path)
if external_geo_caption_path!= '':
self.geo_caption = pd.read_csv(external_geo_caption_path)
def tokenize(text):
if tokenizer is not None:
text = replace_person_token(text)
encoding = tokenizer(
text,
truncation=True,
max_length=2 * max_seq_length,
padding=False,
return_tensors="pt"
)
full_input_ids = encoding.input_ids[0]
if len(full_input_ids) > max_seq_length:
return None
else:
return text
else:
return text
if not isinstance(train_shards_path_or_url, str):
train_shards_path_or_url = [list(braceexpand(urls)) for urls in train_shards_path_or_url]
# flatten list using itertools
train_shards_path_or_url = list(itertools.chain.from_iterable(train_shards_path_or_url))
if external_caption_path != '':
processing_pipeline = [
wds.decode("pil", handler=wds.ignore_and_continue),
wds.map(self.load_external_caption, handler=wds.ignore_and_continue),
wds.rename(
images="jpg;png;jpeg;webp",
input_ids="text;txt;caption",
handler=wds.warn_and_continue,
),
wds.map(partial(conditional_image_transform, resolution=resolution), handler=wds.warn_and_continue),
wds.map(filter_keys(set(["images", "input_ids"]))),
wds.map_dict(
input_ids=tokenize,
handler=wds.warn_and_continue,
),
wds.select(filter_long_samples),
]
else:
processing_pipeline = [
wds.decode("pil", handler=wds.ignore_and_continue),
wds.rename(
images="jpg;png;jpeg;webp",
input_ids="text;txt;caption",
handler=wds.warn_and_continue,
),
wds.map(partial(conditional_image_transform, resolution=resolution), handler=wds.warn_and_continue),
wds.map(filter_keys(set(["images", "input_ids"]))),
wds.map_dict(
input_ids=tokenize,
handler=wds.warn_and_continue,
),
wds.select(filter_long_samples),
]
pipeline = [
wds.ResampledShards(train_shards_path_or_url),
tarfile_to_samples_nothrow,
wds.shuffle(shuffle_buffer_size),
*processing_pipeline,
wds.batched(per_gpu_batch_size, partial=False, collation_fn=default_collate),
]
num_batches = math.ceil(num_train_examples / global_batch_size)
num_worker_batches = math.ceil(num_train_examples / (global_batch_size * num_workers)) # per dataloader worker
num_batches = num_worker_batches * num_workers
num_samples = num_batches * global_batch_size
self._train_dataset = wds.DataPipeline(*pipeline).with_epoch(num_worker_batches)
self._train_dataloader = wds.WebLoader(
self._train_dataset,
batch_size=None,
shuffle=False,
num_workers=num_workers,
pin_memory=pin_memory,
persistent_workers=persistent_workers,
)
# add meta-data to dataloader instance for convenience
self._train_dataloader.num_batches = num_batches
self._train_dataloader.num_samples = num_samples
def load_external_caption(self, sample):
if 'SA1B' in sample['__key__'] or 'sa' in sample['__key__']:
captionf = f"{self.external_caption_path}/{sample['__key__'].split('/')[-1]}.txt"
if os.path.exists(captionf):
with open(captionf, "r") as reader:
captions = reader.readlines()[0].replace('\n', '')
else:
captions = ""
# for captioning
if self.is_captioning:
if self.add_caption_prompt is not None:
prompt = random.sample(self.caption_prompt, 1)[0]
sample['txt'] = prompt + captions
else:
sample['txt'] = captions
# for generation
else:
# randomly choose short and long captions
if random.random() < 0.5:
sample['txt'] = captions.split('.')[0]
else:
sample['txt'] = captions
sample['txt'] = remove_prefix(sample['txt'])
return sample
elif 'laion' in sample['__url__']:
url_part = sample['__url__'].split('/')[-1].split('.')[0]
key = sample['__key__'].split('/')[-1]
captionf = os.path.join(self.external_laion12m_caption_path, url_part, f"{key}.caption")
if os.path.exists(captionf):
with open(captionf, "r") as reader:
captions = reader.read().strip()
else:
captions = ""
# for captioning
if self.is_captioning:
if self.add_caption_prompt is not None:
prompt = random.sample(self.caption_prompt, 1)[0]
sample['txt'] = prompt + captions
else:
sample['txt'] = captions
# for generation
else:
# randomly choose short and long captions
if random.random() < 0.5:
sample['txt'] = captions.split('.')[0]
else:
sample['txt'] = captions
sample['txt'] = remove_prefix(sample['txt'])
return sample
elif 'cc12m' in sample['__url__']:
url_part = sample['__url__'].split('/')[-1].split('.')[0]
key = sample['__key__'].split('/')[-1]
captionf = os.path.join(self.external_cc12m_caption_path, url_part, f"{key}.caption")
if os.path.exists(captionf):
with open(captionf, "r") as reader:
captions = reader.read().strip()
else:
captions = ""
# for captioning
if self.is_captioning:
if self.add_caption_prompt is not None:
prompt = random.sample(self.caption_prompt, 1)[0]
sample['txt'] = prompt + captions
else:
sample['txt'] = captions
# for generation
else:
# randomly choose short and long captions
if random.random() < 0.5:
sample['txt'] = captions.split('.')[0]
else:
sample['txt'] = captions
sample['txt'] = remove_prefix(sample['txt'])
return sample
elif "text-to-image-2M" in sample['__url__']:
if "json" in sample and "prompt" in sample["json"]:
captions = sample["json"]["prompt"]
else:
print(f"sample has no json or prompt: {sample}")
captions = ""
sample['txt'] = captions
return sample
elif 'ai2d' in sample['__url__']:
key = sample['__key__'].split('/')[-1]
df_row = self.ai2d_caption[self.ai2d_caption['image'].astype(str) == key + '.png']
if len(df_row) == 0:
print(f"No captions available for key {sample['__key__']}")
return sample
elif len(df_row) > 1:
# print(f"Multiple captions available for key {sample['__key__']}")
df_row = df_row.sample(1)
question = df_row['question'].values[0]
solution = df_row['solution'].values[0]
caption = (
'<|start_header_id|>user<|end_header_id|>\n'
"You should first think about the reasoning process in the mind and then provide the user with the answer. The reasoning process is enclosed within <think> </think> tags, i.e. <think> reasoning process here </think> answer here\n"
f"{question}\n"
'<eot_id><|start_header_id|>assistant<|end_header_id|>\n'
f"{solution}"
)
sample['txt'] = caption
return sample
elif 'clevr' in sample['__url__']:
key = sample['__key__'].split('/')[-1]
df_row = self.clevr_caption[self.clevr_caption['image'].astype(str) == key + ".jpg"]
if len(df_row) == 0:
print(f"No captions available for key {sample['__key__']}")
return sample
elif len(df_row) > 1:
# print(f"Multiple captions available for key {sample['__key__']}")
df_row = df_row.sample(1)
question = df_row['question'].values[0]
solution = df_row['solution'].values[0]
caption = (
'<|start_header_id|>user<|end_header_id|>\n'
"You should first think about the reasoning process in the mind and then provide the user with the answer. The reasoning process is enclosed within <think> </think> tags, i.e. <think> reasoning process here </think> answer here\n"
f"{question}\n"
'<eot_id><|start_header_id|>assistant<|end_header_id|>\n'
f"{solution}"
)
sample['txt'] = caption
return sample
elif 'docvqa' in sample['__url__']:
key = sample['__key__'].split('/')[-1]
df_row = self.docvqa_caption[self.docvqa_caption['image'].astype(str) == key + ".png"]
if len(df_row) == 0:
print(f"No captions available for key {sample['__key__']}")
return sample
elif len(df_row) > 1:
# print(f"Multiple captions available for key {sample['__key__']}")
df_row = df_row.sample(1)
question = df_row['question'].values[0]
solution = df_row['solution'].values[0]
caption = (
'<|start_header_id|>user<|end_header_id|>\n'
"You should first think about the reasoning process in the mind and then provide the user with the answer. The reasoning process is enclosed within <think> </think> tags, i.e. <think> reasoning process here </think> answer here\n"
f"{question}\n"
'<eot_id><|start_header_id|>assistant<|end_header_id|>\n'
f"{solution}"
)
sample['txt'] = caption
return sample
elif 'geo' in sample['__url__']:
key = sample['__key__'].split('/')[-1]
df_row = self.geo_caption[self.geo_caption['image'].astype(str) == key + ".jpg"]
if len(df_row) == 0:
print(f"No captions available for key {sample['__key__']}")
return sample
elif len(df_row) > 1:
# print(f"Multiple captions available for key {sample['__key__']}")
df_row = df_row.sample(1)
question = df_row['question'].values[0]
solution = df_row['solution'].values[0]
caption = (
'<|start_header_id|>user<|end_header_id|>\n'
"You should first think about the reasoning process in the mind and then provide the user with the answer. The reasoning process is enclosed within <think> </think> tags, i.e. <think> reasoning process here </think> answer here\n"
f"{question}\n"
'<eot_id><|start_header_id|>assistant<|end_header_id|>\n'
f"{solution}"
)
sample['txt'] = caption
return sample
elif self.journeydb_caption is not None and sample['__key__'] in self.journeydb_caption:
captions_list = self.journeydb_caption[sample['__key__']]
if len(captions_list) == 0:
print(f"No captions available for key {sample['__key__']}")
return sample
sample['txt'] = random.sample(captions_list, 1)[0]
return sample
else:
print(f"none exist sample: {sample}")
return sample
@property
def train_dataset(self):
return self._train_dataset
@property
def train_dataloader(self):
return self._train_dataloader
if __name__ == '__main__':
pass
|