Datasets:
Languages:
Portuguese
File size: 27,072 Bytes
bde6c13 099270a 5c95ca9 099270a bde6c13 d554bd9 bde6c13 c047202 bde6c13 589f00d bde6c13 589f00d bde6c13 d3ab746 bde6c13 0334894 d019aa0 4e25450 bde6c13 a409df7 bde6c13 4e25450 bde6c13 4e25450 bde6c13 4e25450 bde6c13 f5a18d0 bde6c13 4e25450 bde6c13 |
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 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import zipfile\n",
"import shutil\n",
"from subprocess import getoutput\n",
"from IPython.utils import capture\n",
"import random\n",
"import concurrent.futures\n",
"from tqdm import tqdm\n",
"from PIL import Image\n",
"import time\n",
"import re\n",
"import json\n",
"import glob\n",
"import gdown\n",
"import requests\n",
"import subprocess\n",
"from urllib.parse import urlparse, unquote\n",
"from pathlib import Path\n",
"import toml\n",
"\n",
"#root_dir\n",
"root_dir = \"/content\"\n",
"deps_dir = os.path.join(root_dir,\"deps\")\n",
"repo_dir = os.path.join(root_dir,\"Kohya-Colab\")\n",
"training_dir = os.path.join(root_dir,\"Dreamboot-Config\")\n",
"pretrained_model = os.path.join(root_dir,\"pretrained_model\")\n",
"vae_dir = os.path.join(root_dir,\"vae\")\n",
"config_dir = os.path.join(training_dir,\"config\")\n",
"\n",
"#repo_dir\n",
"accelerate_config = os.path.join(repo_dir, \"accelerate_config/config.yaml\")\n",
"tools_dir = os.path.join(repo_dir,\"tools\")\n",
"finetune_dir = os.path.join(repo_dir,\"finetune\")\n",
"\n",
"for store in [\"root_dir\", \"deps_dir\", \"repo_dir\", \"training_dir\", \"pretrained_model\", \"vae_dir\", \"accelerate_config\", \"tools_dir\", \"finetune_dir\", \"config_dir\"]:\n",
" with capture.capture_output() as cap:\n",
" %store {store}\n",
" del cap\n",
"\n",
"repo_url = \"https://github.com/phamhungd/Kohya-Colab\"\n",
"bitsandytes_main_py = \"/usr/local/lib/python3.10/dist-packages/bitsandbytes/cuda_setup/main.py\"\n",
"branch = \"\"\n",
"verbose = False\n",
"\n",
"def read_file(filename):\n",
" with open(filename, \"r\") as f:\n",
" contents = f.read()\n",
" return contents\n",
"\n",
"\n",
"def write_file(filename, contents):\n",
" with open(filename, \"w\") as f:\n",
" f.write(contents)\n",
"\n",
"\n",
"def clone_repo(url):\n",
" if not os.path.exists(repo_dir):\n",
" os.chdir(root_dir)\n",
" !git clone {url} {repo_dir}\n",
" else:\n",
" os.chdir(repo_dir)\n",
" !git pull origin {branch} if branch else !git pull\n",
"\n",
"\n",
"def install_dependencies():\n",
" s = getoutput('nvidia-smi')\n",
"\n",
" if 'T4' in s:\n",
" !sed -i \"s@cpu@cuda@\" library/model_util.py\n",
"\n",
" !pip install {'-q' if not verbose else ''} --upgrade -r requirements.txt\n",
"\n",
" from accelerate.utils import write_basic_config\n",
"\n",
" if not os.path.exists(accelerate_config):\n",
" write_basic_config(save_location=accelerate_config)\n",
"\n",
"\n",
"def remove_bitsandbytes_message(filename):\n",
" welcome_message = \"\"\"\n",
"def evaluate_cuda_setup():\n",
" print('')\n",
" print('='*35 + 'BUG REPORT' + '='*35)\n",
" print('Welcome to bitsandbytes. For bug reports, please submit your error trace to: https://github.com/TimDettmers/bitsandbytes/issues')\n",
" print('For effortless bug reporting copy-paste your error into this form: https://docs.google.com/forms/d/e/1FAIpQLScPB8emS3Thkp66nvqwmjTEgxp8Y9ufuWTzFyr9kJ5AoI47dQ/viewform?usp=sf_link')\n",
" print('='*80)\"\"\"\n",
"\n",
" new_welcome_message = \"\"\"\n",
"def evaluate_cuda_setup():\n",
" import os\n",
" if 'BITSANDBYTES_NOWELCOME' not in os.environ or str(os.environ['BITSANDBYTES_NOWELCOME']) == '0':\n",
" print('')\n",
" print('=' * 35 + 'BUG REPORT' + '=' * 35)\n",
" print('Welcome to bitsandbytes. For bug reports, please submit your error trace to: https://github.com/TimDettmers/bitsandbytes/issues')\n",
" print('For effortless bug reporting copy-paste your error into this form: https://docs.google.com/forms/d/e/1FAIpQLScPB8emS3Thkp66nvqwmjTEgxp8Y9ufuWTzFyr9kJ5AoI47dQ/viewform?usp=sf_link')\n",
" print('To hide this message, set the BITSANDBYTES_NOWELCOME variable like so: export BITSANDBYTES_NOWELCOME=1')\n",
" print('=' * 80)\"\"\"\n",
"\n",
" contents = read_file(filename)\n",
" new_contents = contents.replace(welcome_message, new_welcome_message)\n",
" write_file(filename, new_contents)\n",
"\n",
"\n",
"def main():\n",
" os.chdir(root_dir)\n",
"\n",
" for dir in [\n",
" deps_dir,\n",
" training_dir,\n",
" config_dir,\n",
" pretrained_model,\n",
" vae_dir\n",
" ]:\n",
" os.makedirs(dir, exist_ok=True)\n",
"\n",
" clone_repo(repo_url)\n",
"\n",
" if branch:\n",
" os.chdir(repo_dir)\n",
" status = os.system(f\"git checkout {branch}\")\n",
" if status != 0:\n",
" raise Exception(\"Failed to checkout branch or commit\")\n",
"\n",
" os.chdir(repo_dir)\n",
"\n",
" !apt install aria2 {'-qq' if not verbose else ''}\n",
"\n",
" install_dependencies()\n",
" time.sleep(3)\n",
"\n",
" remove_bitsandbytes_message(bitsandytes_main_py)\n",
"\n",
" os.environ[\"TF_CPP_MIN_LOG_LEVEL\"] = \"3\"\n",
" os.environ[\"BITSANDBYTES_NOWELCOME\"] = \"1\"\n",
" os.environ[\"SAFETENSORS_FAST_GPU\"] = \"1\"\n",
"\n",
" cuda_path = \"/usr/local/cuda-11.8/targets/x86_64-linux/lib/\"\n",
" ld_library_path = os.environ.get(\"LD_LIBRARY_PATH\", \"\")\n",
" os.environ[\"LD_LIBRARY_PATH\"] = f\"{ld_library_path}:{cuda_path}\"\n",
"\n",
"main()\n",
"\n",
"\n",
"print(f\"Your train data directory : {train_data_dir}\")\n",
"\n",
"os.chdir(finetune_dir)\n",
"\n",
"config = {\n",
" \"_train_data_dir\": train_data_dir,\n",
" \"batch_size\": 8,\n",
" \"repo_id\": \"SmilingWolf/wd-v1-4-convnextv2-tagger-v2\",\n",
" \"recursive\": True,\n",
" \"remove_underscore\": True,\n",
" \"general_threshold\": 0.75,\n",
" \"character_threshold\": 0.25,\n",
" \"caption_extension\": \".txt\",\n",
" \"max_data_loader_n_workers\": 2,\n",
" \"debug\": True,\n",
"}\n",
"\n",
"args = \"\"\n",
"for k, v in config.items():\n",
" if k.startswith(\"_\"):\n",
" args += f'\"{v}\" '\n",
" elif isinstance(v, str):\n",
" args += f'--{k}=\"{v}\" '\n",
" elif isinstance(v, bool) and v:\n",
" args += f\"--{k} \"\n",
" elif isinstance(v, float) and not isinstance(v, bool):\n",
" args += f\"--{k}={v} \"\n",
" elif isinstance(v, int) and not isinstance(v, bool):\n",
" args += f\"--{k}={v} \"\n",
"\n",
"final_args = f\"python tag_images_by_wd14_tagger.py {args}\"\n",
"if not NoAutoCaption :\n",
" !{final_args}\n",
"\n",
"os.chdir(root_dir)\n",
"\n",
"extension = \".txt\"\n",
"custom_tag = CustomCaption\n",
"\n",
"def read_file(filename):\n",
" with open(filename, \"r\") as f:\n",
" contents = f.read()\n",
" return contents\n",
"\n",
"def write_file(filename, contents):\n",
" with open(filename, \"w\") as f:\n",
" f.write(contents)\n",
"\n",
"def process_tags(filename, custom_tag, append, remove_tag):\n",
" contents = read_file(filename)\n",
" tags = [tag.strip() for tag in contents.split(',')]\n",
" custom_tags = [tag.strip() for tag in custom_tag.split(',')]\n",
"\n",
" for custom_tag in custom_tags:\n",
" custom_tag = custom_tag.replace(\"_\", \" \")\n",
" if remove_tag:\n",
" while custom_tag in tags:\n",
" tags.remove(custom_tag)\n",
" else:\n",
" if custom_tag not in tags:\n",
" if append:\n",
" tags.append(custom_tag)\n",
" else:\n",
" tags.insert(0, custom_tag)\n",
"\n",
" contents = ', '.join(tags)\n",
" write_file(filename, contents)\n",
"\n",
"def process_directory(train_data_dir, tag, append, remove_tag, recursive):\n",
" for filename in os.listdir(train_data_dir):\n",
" file_path = os.path.join(train_data_dir, filename)\n",
" if os.path.isdir(file_path) and recursive:\n",
" process_directory(file_path, tag, append, remove_tag, recursive)\n",
" elif filename.endswith(extension):\n",
" process_tags(file_path, tag, append, remove_tag)\n",
"\n",
"if not any(\n",
" [filename.endswith(extension) for filename in os.listdir(train_data_dir)]\n",
"):\n",
" for filename in os.listdir(train_data_dir):\n",
" if filename.endswith((\".png\", \".jpg\", \".jpeg\", \".webp\", \".bmp\")):\n",
" open(\n",
" os.path.join(train_data_dir, filename.split(\".\")[0] + extension),\n",
" \"w\",\n",
" ).close()\n",
"if not NoAutoCaption :\n",
" process_directory(train_data_dir, custom_tag, False, False, True)\n",
"\n",
"#3.Setting\n",
"\n",
"MODEL_URLS = {\n",
" \"GSMaletoPhotoreal_v4\" : \"https://civitai.com/api/download/models/164715\",\n",
" \"GSMaletoFusion_v1\" : \"https://civitai.com/api/download/models/138518\",\n",
" \"GSMaletoAnime_v1\" : \"https://civitai.com/api/download/models/503605\",\n",
"}\n",
"MODEL_URL = MODEL_URLS.get(Model, Model)\n",
"drive_dir = os.path.join(root_dir, \"drive/MyDrive\")\n",
"def get_supported_extensions():\n",
" return tuple([\".ckpt\", \".safetensors\", \".pt\", \".pth\"])\n",
"\n",
"def get_filename(url, quiet=True):\n",
" extensions = get_supported_extensions()\n",
"\n",
" if url.startswith(drive_dir) or url.endswith(tuple(extensions)):\n",
" filename = os.path.basename(url)\n",
" else:\n",
" response = requests.get(url, stream=True)\n",
" response.raise_for_status()\n",
"\n",
" if 'content-disposition' in response.headers:\n",
" content_disposition = response.headers['content-disposition']\n",
" filename = re.findall('filename=\"?([^\"]+)\"?', content_disposition)[0]\n",
" else:\n",
" url_path = urlparse(url).path\n",
" filename = unquote(os.path.basename(url_path))\n",
"\n",
" if filename.endswith(tuple(get_supported_extensions())):\n",
" return filename\n",
" else:\n",
" return None\n",
"\n",
"def get_most_recent_file(directory):\n",
" files = glob.glob(os.path.join(directory, \"*\"))\n",
" if not files:\n",
" return None\n",
" most_recent_file = max(files, key=os.path.getmtime)\n",
" basename = os.path.basename(most_recent_file)\n",
"\n",
" return most_recent_file\n",
"\n",
"def parse_args(config):\n",
" args = []\n",
"\n",
" for k, v in config.items():\n",
" if k.startswith(\"_\"):\n",
" args.append(f\"{v}\")\n",
" elif isinstance(v, str) and v is not None:\n",
" args.append(f'--{k}={v}')\n",
" elif isinstance(v, bool) and v:\n",
" args.append(f\"--{k}\")\n",
" elif isinstance(v, float) and not isinstance(v, bool):\n",
" args.append(f\"--{k}={v}\")\n",
" elif isinstance(v, int) and not isinstance(v, bool):\n",
" args.append(f\"--{k}={v}\")\n",
"\n",
" return args\n",
"def aria2_download(dir, filename, url):\n",
" aria2_config = {\n",
" \"console-log-level\" : \"error\",\n",
" \"summary-interval\" : 10,\n",
" \"continue\" : True,\n",
" \"max-connection-per-server\" : 16,\n",
" \"min-split-size\" : \"1M\",\n",
" \"split\" : 16,\n",
" \"dir\" : dir,\n",
" \"out\" : filename,\n",
" \"_url\" : url,\n",
" }\n",
" aria2_args = parse_args(aria2_config)\n",
" subprocess.run([\"aria2c\", *aria2_args])\n",
"\n",
"def gdown_download(url, dst, filepath):\n",
" if \"/uc?id/\" in url:\n",
" return gdown.download(url, filepath, quiet=False)\n",
" elif \"/file/d/\" in url:\n",
" return gdown.download(url=url, output=filepath, quiet=False, fuzzy=True)\n",
" elif \"/drive/folders/\" in url:\n",
" os.chdir(dst)\n",
" return gdown.download_folder(url, quiet=True, use_cookies=False)\n",
"\n",
"def download(url, dst):\n",
" print(f\"Starting downloading from {url}\")\n",
" filename = get_filename(url)\n",
" filepath = os.path.join(dst, filename)\n",
"\n",
" if \"drive.google.com\" in url:\n",
" gdown = gdown_download(url, dst, filepath)\n",
" else:\n",
" if \"huggingface.co\" in url and \"/blob/\" in url:\n",
" url = url.replace(\"/blob/\", \"/resolve/\")\n",
" aria2_download(dst, filename, url)\n",
"\n",
" print(f\"Download finished: {filepath}\")\n",
" return filepath\n",
"\n",
"def get_gpu_name():\n",
" try:\n",
" return subprocess.check_output(\"nvidia-smi --query-gpu=name --format=csv,noheader,nounits\", shell=True).decode('ascii').strip()\n",
" except:\n",
" return None\n",
"\n",
"def main():\n",
" global model_path, vae_path\n",
" model_path, vae_path = None, None\n",
" download_targets = {\n",
" \"model\": (MODEL_URL, pretrained_model),\n",
" }\n",
" for target, (url, dst) in download_targets.items():\n",
" if url and not url.startswith(f\"PASTE {target.upper()} URL OR GDRIVE PATH HERE\"):\n",
" filepath = download(url, dst)\n",
" if target == \"model\":\n",
" model_path = filepath\n",
" print()\n",
" if model_path:\n",
" print(f\"Selected model: {model_path}\")\n",
"\n",
"if Model.startswith(\"/content/drive/\"):\n",
" model_path = Model\n",
" print(f\"Diffusers model is loaded : {Model}\")\n",
"else:\n",
" main()\n",
"\n",
"!aria2c --console-log-level=error --summary-interval=10 -c -x 16 -k 1M -s 16 https://huggingface.co/stabilityai/sd-vae-ft-mse-original/resolve/main/vae-ft-mse-840000-ema-pruned.ckpt -d /content/VAE -o VAE84EMA.vae.pt\n",
"vae = \"/content/VAE/VAE84EMA.vae.pt\"\n",
"\n",
"#Dataset Config\n",
"\n",
"activation_word = \"sd vn\"\n",
"caption_extension = \".txt\"\n",
"token_to_captions = False\n",
"dataset_repeats = Repeats\n",
"keep_tokens = 0\n",
"flip_aug = False\n",
"\n",
"if ',' in activation_word or ' ' in activation_word:\n",
" words = activation_word.replace(',', ' ').split()\n",
" class_token = words[-1]\n",
"\n",
"\n",
"def read_file(filename):\n",
" with open(filename, \"r\") as f:\n",
" contents = f.read()\n",
" return contents\n",
"\n",
"\n",
"def write_file(filename, contents):\n",
" with open(filename, \"w\") as f:\n",
" f.write(contents)\n",
"\n",
"\n",
"def get_supported_images(folder):\n",
" supported_extensions = (\".png\", \".jpg\", \".jpeg\", \".webp\", \".bmp\")\n",
" return [file for ext in supported_extensions for file in glob.glob(f\"{folder}/*{ext}\")]\n",
"\n",
"\n",
"def get_subfolders_with_supported_images(folder):\n",
" subfolders = [os.path.join(folder, subfolder) for subfolder in os.listdir(folder) if os.path.isdir(os.path.join(folder, subfolder))]\n",
" return [subfolder for subfolder in subfolders if len(get_supported_images(subfolder)) > 0]\n",
"\n",
"\n",
"def process_tags(filename, custom_tag, remove_tag):\n",
" contents = read_file(filename)\n",
" tags = [tag.strip() for tag in contents.split(',')]\n",
" custom_tags = [tag.strip() for tag in custom_tag.split(',')]\n",
"\n",
" for custom_tag in custom_tags:\n",
" custom_tag = custom_tag.replace(\"_\", \" \")\n",
" # if remove_tag:\n",
" # while custom_tag in tags:\n",
" # tags.remove(custom_tag)\n",
" # else:\n",
" if custom_tag not in tags:\n",
" tags.insert(0, custom_tag)\n",
"\n",
" contents = ', '.join(tags)\n",
" write_file(filename, contents)\n",
"\n",
"\n",
"def process_folder_recursively(folder):\n",
" for root, _, files in os.walk(folder):\n",
" for file in files:\n",
" if file.endswith(caption_extension):\n",
" file_path = os.path.join(root, file)\n",
" extracted_class_token = get_class_token_from_folder_name(root, folder)\n",
" train_supported_images = get_supported_images(train_data_dir)\n",
" tag = extracted_class_token if extracted_class_token else activation_word if train_supported_images else \"\"\n",
" if not tag == \"\":\n",
" process_tags(file_path, tag, remove_tag=(not token_to_captions))\n",
"\n",
"\n",
"def get_num_repeats(folder):\n",
" folder_name = os.path.basename(folder)\n",
" try:\n",
" repeats, _ = folder_name.split('_', 1)\n",
" num_repeats = int(repeats)\n",
" except ValueError:\n",
" num_repeats = dataset_repeats\n",
"\n",
" return num_repeats\n",
"\n",
"\n",
"def get_class_token_from_folder_name(folder, parent_folder):\n",
" if folder == parent_folder:\n",
" return class_token\n",
"\n",
" folder_name = os.path.basename(folder)\n",
" try:\n",
" _, concept = folder_name.split('_', 1)\n",
" return concept\n",
" except ValueError:\n",
" return \"\"\n",
"\n",
"train_supported_images = get_supported_images(train_data_dir)\n",
"train_subfolders = get_subfolders_with_supported_images(train_data_dir)\n",
"\n",
"subsets = []\n",
"config = {\n",
" \"general\": {\n",
" \"enable_bucket\": True,\n",
" \"caption_extension\": caption_extension,\n",
" \"shuffle_caption\": True,\n",
" \"keep_tokens\": keep_tokens,\n",
" \"bucket_reso_steps\": 64,\n",
" \"bucket_no_upscale\": False,\n",
" },\n",
" \"datasets\": [\n",
" {\n",
" \"resolution\": resolution,\n",
" \"min_bucket_reso\": 320 if resolution > 640 else 256,\n",
" \"max_bucket_reso\": 1280 if resolution > 640 else 1024,\n",
" \"caption_dropout_rate\": 0,\n",
" \"caption_tag_dropout_rate\": 0,\n",
" \"caption_dropout_every_n_epochs\": 0,\n",
" \"flip_aug\": flip_aug,\n",
" \"color_aug\": False,\n",
" \"face_crop_aug_range\": None,\n",
" \"subsets\": subsets,\n",
" }\n",
" ],\n",
"}\n",
"\n",
"if token_to_captions and keep_tokens < 2:\n",
" keep_tokens = 1\n",
"\n",
"process_folder_recursively(train_data_dir)\n",
"\n",
"if train_supported_images:\n",
" subsets.append({\n",
" \"image_dir\": train_data_dir,\n",
" \"class_tokens\": activation_word,\n",
" \"num_repeats\": dataset_repeats,\n",
" })\n",
"\n",
"for subfolder in train_subfolders:\n",
" num_repeats = get_num_repeats(subfolder)\n",
" extracted_class_token = get_class_token_from_folder_name(subfolder, train_data_dir)\n",
" subsets.append({\n",
" \"image_dir\": subfolder,\n",
" \"class_tokens\": extracted_class_token if extracted_class_token else None,\n",
" \"num_repeats\": num_repeats,\n",
" })\n",
"\n",
"for subset in subsets:\n",
" if not glob.glob(f\"{subset['image_dir']}/*.txt\"):\n",
" subset[\"class_tokens\"] = activation_word\n",
"\n",
"dataset_config = os.path.join(config_dir, \"dataset_config.toml\")\n",
"\n",
"for key in config:\n",
" if isinstance(config[key], dict):\n",
" for sub_key in config[key]:\n",
" if config[key][sub_key] == \"\":\n",
" config[key][sub_key] = None\n",
" elif config[key] == \"\":\n",
" config[key] = None\n",
"\n",
"config_str = toml.dumps(config)\n",
"\n",
"with open(dataset_config, \"w\") as f:\n",
" f.write(config_str)\n",
"\n",
"print(config_str)\n",
"\n",
"#Config\n",
"optimizer_args = False\n",
"conv_dim = 128\n",
"conv_alpha = 128\n",
"\n",
"network_module = \"networks.lora\"\n",
"network_args = \"\"\n",
"\n",
"config = {\n",
" \"model_arguments\": {\n",
" \"v2\": False,\n",
" \"v_parameterization\": False,\n",
" \"pretrained_model_name_or_path\": model_path,\n",
" \"vae\": vae,\n",
" },\n",
" \"additional_network_arguments\": {\n",
" \"no_metadata\": False,\n",
" \"unet_lr\": float(unet_lr),\n",
" \"text_encoder_lr\": float(text_encoder_lr),\n",
" \"network_module\": network_module,\n",
" \"network_dim\": 48,\n",
" \"network_alpha\": 24,\n",
" \"training_comment\": \"GSGI Trainer\",\n",
" },\n",
" \"optimizer_arguments\": {\n",
" \"optimizer_type\": \"AdamW8bit\",\n",
" \"optimizer_args\": eval(optimizer_args) if optimizer_args else None,\n",
" \"learning_rate\": unet_lr,\n",
" \"max_grad_norm\": 1.0,\n",
" \"lr_scheduler\": \"cosine_with_restarts\",\n",
" \"lr_scheduler_num_cycles\": 4,\n",
" },\n",
" \"dataset_arguments\": {\n",
" \"cache_latents\": True,\n",
" \"debug_dataset\": False,\n",
" \"vae_batch_size\": Batch_size,\n",
" },\n",
" \"training_arguments\": {\n",
" \"output_dir\": output_dir,\n",
" \"output_name\": Loraname,\n",
" \"save_precision\": \"fp16\",\n",
" \"save_every_n_epochs\": save_n_epochs_type_value,\n",
" \"train_batch_size\": Batch_size,\n",
" \"max_token_length\": 225,\n",
" \"mem_eff_attn\": False,\n",
" \"xformers\": True,\n",
" \"max_train_epochs\": num_epochs,\n",
" \"max_data_loader_n_workers\": 8,\n",
" \"persistent_data_loader_workers\": True,\n",
" \"gradient_checkpointing\": False,\n",
" \"gradient_accumulation_steps\": 1,\n",
" \"mixed_precision\": \"fp16\",\n",
" \"clip_skip\": 1,\n",
" \"logging_dir\": \"/content/Dreamboot-Config/logs\",\n",
" \"log_prefix\": Loraname,\n",
" \"lowram\": True,\n",
" \"training_comment\" : \"train by GSGI Trainer\",\n",
" },\n",
" \"sample_prompt_arguments\": {\n",
" \"sample_every_n_steps\": 200,\n",
" \"sample_every_n_epochs\": 1,\n",
" \"sample_sampler\": \"euler\",\n",
" },\n",
" \"dreambooth_arguments\": {\n",
" \"prior_loss_weight\": 1,\n",
" },\n",
" \"saving_arguments\": {\n",
" \"save_model_as\": \"safetensors\",\n",
" },\n",
"}\n",
"SamplePrompt = f\"{Loraname},front view, masterpiece,best quality\"\n",
"sample_str = f\"\"\"\n",
" {SamplePrompt}\\\n",
" --n lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry \\\n",
" --w 512 \\\n",
" --h 768 \\\n",
" --l 7 \\\n",
" --s 30\n",
"\"\"\"\n",
"config_path = os.path.join(config_dir, \"config_file.toml\")\n",
"prompt_path = os.path.join(config_dir, \"sample_prompt.txt\")\n",
"\n",
"for key in config:\n",
" if isinstance(config[key], dict):\n",
" for sub_key in config[key]:\n",
" if config[key][sub_key] == \"\":\n",
" config[key][sub_key] = None\n",
" elif config[key] == \"\":\n",
" config[key] = None\n",
"\n",
"config_str = toml.dumps(config)\n",
"\n",
"def write_file(filename, contents):\n",
" with open(filename, \"w\") as f:\n",
" f.write(contents)\n",
"\n",
"write_file(config_path, config_str)\n",
"write_file(prompt_path, sample_str)\n",
"\n",
"print(config_str)\n",
"\n",
"os.chdir(repo_dir)\n",
"\n",
"\n",
"train_file = \"train_network.py\"\n",
"ConfigFolder = \"/content/Dreamboot-Config/config\"\n",
"sample_prompt = f\"{ConfigFolder}/sample_prompt.txt\"\n",
"config_file = f\"{ConfigFolder}/config_file.toml\"\n",
"dataset_config = f\"{ConfigFolder}/dataset_config.toml\"\n",
"accelerate_conf = {\n",
" \"config_file\" : accelerate_config,\n",
" \"num_cpu_threads_per_process\" : 1,\n",
"}\n",
"\n",
"train_conf = {\n",
" \"sample_prompts\" : sample_prompt,\n",
" \"dataset_config\" : dataset_config,\n",
" \"config_file\" : config_file\n",
"}\n",
"\n",
"def train(config):\n",
" args = \"\"\n",
" for k, v in config.items():\n",
" if k.startswith(\"_\"):\n",
" args += f'\"{v}\" '\n",
" elif isinstance(v, str):\n",
" args += f'--{k}=\"{v}\" '\n",
" elif isinstance(v, bool) and v:\n",
" args += f\"--{k} \"\n",
" elif isinstance(v, float) and not isinstance(v, bool):\n",
" args += f\"--{k}={v} \"\n",
" elif isinstance(v, int) and not isinstance(v, bool):\n",
" args += f\"--{k}={v} \"\n",
"\n",
" return args\n",
"\n",
"accelerate_args = train(accelerate_conf)\n",
"train_args = train(train_conf)\n",
"final_args = f\"accelerate launch {accelerate_args} {train_file} {train_args}\"\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|