File size: 89,729 Bytes
3a18eba |
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 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 |
"""
Copyright 2022 HuggingFace, ShivamShrirao
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 keyboard
import gradio as gr
import argparse
import random
import hashlib
import itertools
import json
import math
import os
import copy
from contextlib import nullcontext
from pathlib import Path
import shutil
import torch
import torch.nn.functional as F
import torch.utils.checkpoint
import numpy as np
from accelerate import Accelerator
from accelerate.logging import get_logger
from accelerate.utils import set_seed
from diffusers import AutoencoderKL, DDIMScheduler, DDPMScheduler, DiffusionPipeline, UNet2DConditionModel,DiffusionPipeline, DPMSolverMultistepScheduler,EulerDiscreteScheduler
from diffusers.optimization import get_scheduler
from torchvision.transforms import functional
from tqdm.auto import tqdm
from transformers import CLIPTextModel, CLIPTokenizer
from typing import Dict, List, Generator, Tuple
from PIL import Image, ImageFile
from diffusers.utils.import_utils import is_xformers_available
from trainer_util import *
from dataloaders_util import *
from discriminator import Discriminator2D
from lion_pytorch import Lion
logger = get_logger(__name__)
def parse_args():
parser = argparse.ArgumentParser(description="Simple example of a training script.")
parser.add_argument(
"--revision",
type=str,
default=None,
required=False,
help="Revision of pretrained model identifier from huggingface.co/models.",
)
parser.add_argument(
"--attention",
type=str,
choices=["xformers", "flash_attention"],
default="xformers",
help="Type of attention to use."
)
parser.add_argument(
"--model_variant",
type=str,
default='base',
required=False,
help="Train Base/Inpaint/Depth2Img",
)
parser.add_argument(
"--aspect_mode",
type=str,
default='dynamic',
required=False,
help="Path to pretrained model or model identifier from huggingface.co/models.",
)
parser.add_argument(
"--aspect_mode_action_preference",
type=str,
default='add',
required=False,
help="Path to pretrained model or model identifier from huggingface.co/models.",
)
parser.add_argument('--use_lion',default=False,action="store_true", help='Use the new LION optimizer')
parser.add_argument('--use_ema',default=False,action="store_true", help='Use EMA for finetuning')
parser.add_argument('--clip_penultimate',default=False,action="store_true", help='Use penultimate CLIP layer for text embedding')
parser.add_argument("--conditional_dropout", type=float, default=None,required=False, help="Conditional dropout probability")
parser.add_argument('--disable_cudnn_benchmark', default=False, action="store_true")
parser.add_argument('--use_text_files_as_captions', default=False, action="store_true")
parser.add_argument(
"--sample_from_batch",
type=int,
default=0,
help=("Number of prompts to sample from the batch for inference"),
)
parser.add_argument(
"--flatten_sample_folder",
default=True,
action="store_true",
help="Will save samples in one folder instead of per-epoch",
)
parser.add_argument(
"--stop_text_encoder_training",
type=int,
default=999999999999999,
help=("The epoch at which the text_encoder is no longer trained"),
)
parser.add_argument(
"--use_bucketing",
default=False,
action="store_true",
help="Will save and generate samples before training",
)
parser.add_argument(
"--regenerate_latent_cache",
default=False,
action="store_true",
help="Will save and generate samples before training",
)
parser.add_argument(
"--sample_on_training_start",
default=False,
action="store_true",
help="Will save and generate samples before training",
)
parser.add_argument(
"--add_class_images_to_dataset",
default=False,
action="store_true",
help="will generate and add class images to the dataset without using prior reservation in training",
)
parser.add_argument(
"--auto_balance_concept_datasets",
default=False,
action="store_true",
help="will balance the number of images in each concept dataset to match the minimum number of images in any concept dataset",
)
parser.add_argument(
"--sample_aspect_ratios",
default=False,
action="store_true",
help="sample different aspect ratios for each image",
)
parser.add_argument(
"--dataset_repeats",
type=int,
default=1,
help="repeat the dataset this many times",
)
parser.add_argument(
"--save_every_n_epoch",
type=int,
default=1,
help="save on epoch finished",
)
parser.add_argument(
"--pretrained_model_name_or_path",
type=str,
default=None,
required=True,
help="Path to pretrained model or model identifier from huggingface.co/models.",
)
parser.add_argument(
"--pretrained_vae_name_or_path",
type=str,
default=None,
help="Path to pretrained vae or vae identifier from huggingface.co/models.",
)
parser.add_argument(
"--tokenizer_name",
type=str,
default=None,
help="Pretrained tokenizer name or path if not the same as model_name",
)
parser.add_argument(
"--instance_data_dir",
type=str,
default=None,
help="A folder containing the training data of instance images.",
)
parser.add_argument(
"--class_data_dir",
type=str,
default=None,
help="A folder containing the training data of class images.",
)
parser.add_argument(
"--instance_prompt",
type=str,
default=None,
help="The prompt with identifier specifying the instance",
)
parser.add_argument(
"--class_prompt",
type=str,
default=None,
help="The prompt to specify images in the same class as provided instance images.",
)
parser.add_argument(
"--save_sample_prompt",
type=str,
default=None,
help="The prompt used to generate sample outputs to save.",
)
parser.add_argument(
"--n_save_sample",
type=int,
default=4,
help="The number of samples to save.",
)
parser.add_argument(
"--sample_height",
type=int,
default=512,
help="The number of samples to save.",
)
parser.add_argument(
"--sample_width",
type=int,
default=512,
help="The number of samples to save.",
)
parser.add_argument(
"--save_guidance_scale",
type=float,
default=7.5,
help="CFG for save sample.",
)
parser.add_argument(
"--save_infer_steps",
type=int,
default=30,
help="The number of inference steps for save sample.",
)
parser.add_argument(
"--with_prior_preservation",
default=False,
action="store_true",
help="Flag to add prior preservation loss.",
)
parser.add_argument("--prior_loss_weight", type=float, default=1.0, help="The weight of prior preservation loss.")
parser.add_argument(
"--with_offset_noise",
default=False,
action="store_true",
help="Flag to offset noise applied to latents.",
)
parser.add_argument("--offset_noise_weight", type=float, default=0.1, help="The weight of offset noise applied during training.")
parser.add_argument(
"--num_class_images",
type=int,
default=100,
help=(
"Minimal class images for prior preservation loss. If not have enough images, additional images will be"
" sampled with class_prompt."
),
)
parser.add_argument(
"--output_dir",
type=str,
default="text-inversion-model",
help="The output directory where the model predictions and checkpoints will be written.",
)
parser.add_argument("--seed", type=int, default=None, help="A seed for reproducible training.")
parser.add_argument(
"--resolution",
type=int,
default=512,
help=(
"The resolution for input images, all the images in the train/validation dataset will be resized to this"
" resolution"
),
)
parser.add_argument(
"--center_crop", default=False, action="store_true", help="Whether to center crop images before resizing to resolution"
)
parser.add_argument("--train_text_encoder", default=False, action="store_true", help="Whether to train the text encoder")
parser.add_argument(
"--train_batch_size", type=int, default=4, help="Batch size (per device) for the training dataloader."
)
parser.add_argument(
"--sample_batch_size", type=int, default=4, help="Batch size (per device) for sampling images."
)
parser.add_argument("--num_train_epochs", type=int, default=1)
parser.add_argument(
"--max_train_steps",
type=int,
default=None,
help="Total number of training steps to perform. If provided, overrides num_train_epochs.",
)
parser.add_argument(
"--gradient_accumulation_steps",
type=int,
default=1,
help="Number of updates steps to accumulate before performing a backward/update pass.",
)
parser.add_argument(
"--gradient_checkpointing",
default=False,
action="store_true",
help="Whether or not to use gradient checkpointing to save memory at the expense of slower backward pass.",
)
parser.add_argument(
"--learning_rate",
type=float,
default=5e-6,
help="Initial learning rate (after the potential warmup period) to use.",
)
parser.add_argument(
"--scale_lr",
action="store_true",
default=False,
help="Scale the learning rate by the number of GPUs, gradient accumulation steps, and batch size.",
)
parser.add_argument(
"--lr_scheduler",
type=str,
default="constant",
help=(
'The scheduler type to use. Choose between ["linear", "cosine", "cosine_with_restarts", "polynomial",'
' "constant", "constant_with_warmup"]'
),
)
parser.add_argument(
"--lr_warmup_steps", type=float, default=500, help="Number of steps for the warmup in the lr scheduler."
)
parser.add_argument(
"--use_8bit_adam", default=False, action="store_true", help="Whether or not to use 8-bit Adam from bitsandbytes."
)
parser.add_argument("--adam_beta1", type=float, default=0.9, help="The beta1 parameter for the Adam optimizer.")
parser.add_argument("--adam_beta2", type=float, default=0.999, help="The beta2 parameter for the Adam optimizer.")
parser.add_argument("--adam_weight_decay", type=float, default=1e-2, help="Weight decay to use.")
parser.add_argument("--adam_epsilon", type=float, default=1e-08, help="Epsilon value for the Adam optimizer")
parser.add_argument("--max_grad_norm", default=1.0, type=float, help="Max gradient norm.")
parser.add_argument("--push_to_hub", default=False, action="store_true", help="Whether or not to push the model to the Hub.")
parser.add_argument("--hub_token", type=str, default=None, help="The token to use to push to the Model Hub.")
parser.add_argument(
"--hub_model_id",
type=str,
default=None,
help="The name of the repository to keep in sync with the local `output_dir`.",
)
parser.add_argument(
"--logging_dir",
type=str,
default="logs",
help=(
"[TensorBoard](https://www.tensorflow.org/tensorboard) log directory. Will default to"
" *output_dir/runs/**CURRENT_DATETIME_HOSTNAME***."
),
)
parser.add_argument("--log_interval", type=int, default=10, help="Log every N steps.")
parser.add_argument("--sample_step_interval", type=int, default=100000000000000, help="Sample images every N steps.")
parser.add_argument(
"--mixed_precision",
type=str,
default="no",
choices=["no", "fp16", "bf16","tf32"],
help=(
"Whether to use mixed precision. Choose"
"between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10."
"and an Nvidia Ampere GPU."
),
)
parser.add_argument("--local_rank", type=int, default=-1, help="For distributed training: local_rank")
parser.add_argument(
"--concepts_list",
type=str,
default=None,
help="Path to json containing multiple concepts, will overwrite parameters like instance_prompt, class_prompt, etc.",
)
parser.add_argument("--save_sample_controlled_seed", type=int, action='append', help="Set a seed for an extra sample image to be constantly saved.")
parser.add_argument("--detect_full_drive", default=True, action="store_true", help="Delete checkpoints when the drive is full.")
parser.add_argument("--send_telegram_updates", default=False, action="store_true", help="Send Telegram updates.")
parser.add_argument("--telegram_chat_id", type=str, default="0", help="Telegram chat ID.")
parser.add_argument("--telegram_token", type=str, default="0", help="Telegram token.")
parser.add_argument("--use_deepspeed_adam", default=False, action="store_true", help="Use experimental DeepSpeed Adam 8.")
parser.add_argument('--append_sample_controlled_seed_action', action='append')
parser.add_argument('--add_sample_prompt', type=str, action='append')
parser.add_argument('--use_image_names_as_captions', default=False, action="store_true")
parser.add_argument('--shuffle_captions', default=False, action="store_true")
parser.add_argument("--masked_training", default=False, required=False, action='store_true', help="Whether to mask parts of the image during training")
parser.add_argument("--normalize_masked_area_loss", default=False, required=False, action='store_true', help="Normalize the loss, to make it independent of the size of the masked area")
parser.add_argument("--unmasked_probability", type=float, default=1, required=False, help="Probability of training a step without a mask")
parser.add_argument("--max_denoising_strength", type=float, default=1, required=False, help="Max denoising steps to train on")
parser.add_argument('--add_mask_prompt', type=str, default=None, action="append", dest="mask_prompts", help="Prompt for automatic mask creation")
parser.add_argument('--with_gan', default=False, action="store_true", help="Use GAN (experimental)")
parser.add_argument("--gan_weight", type=float, default=0.2, required=False, help="Strength of effect GAN has on training")
parser.add_argument("--gan_warmup", type=float, default=0, required=False, help="Slowly increases GAN weight from zero over this many steps, useful when initializing a GAN discriminator from scratch")
parser.add_argument('--discriminator_config', default="configs/discriminator_large.json", help="Location of config file to use when initializing a new GAN discriminator")
parser.add_argument('--sample_from_ema', default=True, action="store_true", help="Generate sample images using the EMA model")
parser.add_argument('--run_name', type=str, default=None, help="Adds a custom identifier to the sample and checkpoint directories")
args = parser.parse_args()
env_local_rank = int(os.environ.get("LOCAL_RANK", -1))
if env_local_rank != -1 and env_local_rank != args.local_rank:
args.local_rank = env_local_rank
return args
def main():
print(f" {bcolors.OKBLUE}Booting Up StableTuner{bcolors.ENDC}")
print(f" {bcolors.OKBLUE}Please wait a moment as we load up some stuff...{bcolors.ENDC}")
#torch.cuda.set_per_process_memory_fraction(0.5)
args = parse_args()
#temp arg
args.batch_tokens = None
if args.disable_cudnn_benchmark:
torch.backends.cudnn.benchmark = False
else:
torch.backends.cudnn.benchmark = True
if args.send_telegram_updates:
send_telegram_message(f"Booting up StableTuner!\n", args.telegram_chat_id, args.telegram_token)
logging_dir = Path(args.output_dir, "logs", args.logging_dir)
if args.run_name:
main_sample_dir = os.path.join(args.output_dir, f"samples_{args.run_name}")
else:
main_sample_dir = os.path.join(args.output_dir, "samples")
if os.path.exists(main_sample_dir):
shutil.rmtree(main_sample_dir)
os.makedirs(main_sample_dir)
#create logging directory
if not logging_dir.exists():
logging_dir.mkdir(parents=True)
#create output directory
if not Path(args.output_dir).exists():
Path(args.output_dir).mkdir(parents=True)
accelerator = Accelerator(
gradient_accumulation_steps=args.gradient_accumulation_steps,
mixed_precision=args.mixed_precision if args.mixed_precision != 'tf32' else 'no',
log_with="tensorboard",
logging_dir=logging_dir,
)
# Currently, it's not possible to do gradient accumulation when training two models with accelerate.accumulate
# This will be enabled soon in accelerate. For now, we don't allow gradient accumulation when training two models.
# TODO (patil-suraj): Remove this check when gradient accumulation with two models is enabled in accelerate.
if args.train_text_encoder and args.gradient_accumulation_steps > 1 and accelerator.num_processes > 1:
raise ValueError(
"Gradient accumulation is not supported when training the text encoder in distributed training. "
"Please set gradient_accumulation_steps to 1. This feature will be supported in the future."
)
if args.seed is not None:
set_seed(args.seed)
if args.concepts_list is None:
args.concepts_list = [
{
"instance_prompt": args.instance_prompt,
"class_prompt": args.class_prompt,
"instance_data_dir": args.instance_data_dir,
"class_data_dir": args.class_data_dir
}
]
else:
with open(args.concepts_list, "r") as f:
args.concepts_list = json.load(f)
if args.with_prior_preservation or args.add_class_images_to_dataset:
pipeline = None
for concept in args.concepts_list:
class_images_dir = Path(concept["class_data_dir"])
class_images_dir.mkdir(parents=True, exist_ok=True)
cur_class_images = len(list(class_images_dir.iterdir()))
if cur_class_images < args.num_class_images:
torch_dtype = torch.float16 if accelerator.device.type == "cuda" else torch.float32
if pipeline is None:
pipeline = DiffusionPipeline.from_pretrained(
args.pretrained_model_name_or_path,
safety_checker=None,
vae=AutoencoderKL.from_pretrained(args.pretrained_vae_name_or_path or args.pretrained_model_name_or_path,subfolder=None if args.pretrained_vae_name_or_path else "vae" ,safe_serialization=True),
torch_dtype=torch_dtype,
requires_safety_checker=False,
)
pipeline.set_progress_bar_config(disable=True)
pipeline.to(accelerator.device)
#if args.use_bucketing == False:
num_new_images = args.num_class_images - cur_class_images
logger.info(f"Number of class images to sample: {num_new_images}.")
sample_dataset = PromptDataset(concept["class_prompt"], num_new_images)
sample_dataloader = torch.utils.data.DataLoader(sample_dataset, batch_size=args.sample_batch_size)
sample_dataloader = accelerator.prepare(sample_dataloader)
#else:
#create class images that match up to the concept target buckets
# instance_images_dir = Path(concept["instance_data_dir"])
# cur_instance_images = len(list(instance_images_dir.iterdir()))
#target_wh = min(self.aspects, key=lambda aspects:abs(aspects[0]/aspects[1] - image_aspect))
# num_new_images = cur_instance_images - cur_class_images
with torch.autocast("cuda"):
for example in tqdm(
sample_dataloader, desc="Generating class images", disable=not accelerator.is_local_main_process
):
with torch.autocast("cuda"):
images = pipeline(example["prompt"],height=args.resolution,width=args.resolution).images
for i, image in enumerate(images):
hash_image = hashlib.sha1(image.tobytes()).hexdigest()
image_filename = class_images_dir / f"{example['index'][i] + cur_class_images}-{hash_image}.jpg"
image.save(image_filename)
del pipeline
if torch.cuda.is_available():
torch.cuda.empty_cache()
torch.cuda.ipc_collect()
# Load the tokenizer
if args.tokenizer_name:
tokenizer = CLIPTokenizer.from_pretrained(args.tokenizer_name )
elif args.pretrained_model_name_or_path:
#print(os.getcwd())
tokenizer = CLIPTokenizer.from_pretrained(args.pretrained_model_name_or_path, subfolder="tokenizer" )
# Load models and create wrapper for stable diffusion
#text_encoder = CLIPTextModel.from_pretrained(args.pretrained_model_name_or_path, subfolder="text_encoder" )
text_encoder = CLIPTextModel.from_pretrained(
args.pretrained_model_name_or_path,
subfolder="text_encoder",
revision=args.revision,
)
vae = AutoencoderKL.from_pretrained(
args.pretrained_model_name_or_path,
subfolder="vae",
revision=args.revision,
)
unet = UNet2DConditionModel.from_pretrained(
args.pretrained_model_name_or_path,
subfolder="unet",
revision=args.revision,
torch_dtype=torch.float32
)
if args.with_gan:
if os.path.isdir(os.path.join(args.pretrained_model_name_or_path, "discriminator")):
discriminator = Discriminator2D.from_pretrained(
args.pretrained_model_name_or_path,
subfolder="discriminator",
revision=args.revision,
)
else:
print(f" {bcolors.WARNING}Discriminator network (GAN) not found. Initializing a new network. It may take a very large number of steps to train.{bcolors.ENDC}")
if not args.gan_warmup:
print(f" {bcolors.WARNING}Consider using --gan_warmup to stabilize the model while the discriminator is being trained.{bcolors.ENDC}")
with open(args.discriminator_config, "r") as f:
discriminator_config = json.load(f)
discriminator = Discriminator2D.from_config(discriminator_config)
if is_xformers_available() and args.attention=='xformers':
try:
vae.enable_xformers_memory_efficient_attention()
unet.enable_xformers_memory_efficient_attention()
if args.with_gan:
discriminator.enable_xformers_memory_efficient_attention()
except Exception as e:
logger.warning(
"Could not enable memory efficient attention. Make sure xformers is installed"
f" correctly and a GPU is available: {e}"
)
elif args.attention=='flash_attention':
replace_unet_cross_attn_to_flash_attention()
if args.use_ema == True:
if os.path.isdir(os.path.join(args.pretrained_model_name_or_path, "unet_ema")):
ema_unet = UNet2DConditionModel.from_pretrained(
args.pretrained_model_name_or_path,
subfolder="unet_ema",
revision=args.revision,
torch_dtype=torch.float32
)
else:
ema_unet = copy.deepcopy(unet)
ema_unet.config["step"] = 0
for param in ema_unet.parameters():
param.requires_grad = False
if args.model_variant == "depth2img":
d2i = Depth2Img(unet,text_encoder,args.mixed_precision,args.pretrained_model_name_or_path,accelerator)
vae.requires_grad_(False)
vae.enable_slicing()
if not args.train_text_encoder:
text_encoder.requires_grad_(False)
if args.gradient_checkpointing:
unet.enable_gradient_checkpointing()
if args.train_text_encoder:
text_encoder.gradient_checkpointing_enable()
if args.with_gan:
discriminator.enable_gradient_checkpointing()
if args.scale_lr:
args.learning_rate = (
args.learning_rate * args.gradient_accumulation_steps * args.train_batch_size * accelerator.num_processes
)
# Use 8-bit Adam for lower memory usage or to fine-tune the model in 16GB GPUs
if args.use_8bit_adam and args.use_deepspeed_adam==False and args.use_lion==False:
try:
import bitsandbytes as bnb
except ImportError:
raise ImportError(
"To use 8-bit Adam, please install the bitsandbytes library: `pip install bitsandbytes`."
)
optimizer_class = bnb.optim.AdamW8bit
print("Using 8-bit Adam")
elif args.use_8bit_adam and args.use_deepspeed_adam==True:
try:
from deepspeed.ops.adam import DeepSpeedCPUAdam
except ImportError:
raise ImportError(
"To use 8-bit DeepSpeed Adam, try updating your cuda and deepspeed integrations."
)
optimizer_class = DeepSpeedCPUAdam
elif args.use_lion == True:
print("Using LION optimizer")
optimizer_class = Lion
elif args.use_deepspeed_adam==False and args.use_lion==False and args.use_8bit_adam==False:
optimizer_class = torch.optim.AdamW
params_to_optimize = (
itertools.chain(unet.parameters(), text_encoder.parameters()) if args.train_text_encoder else unet.parameters()
)
if args.use_lion == False:
optimizer = optimizer_class(
params_to_optimize,
lr=args.learning_rate,
betas=(args.adam_beta1, args.adam_beta2),
weight_decay=args.adam_weight_decay,
eps=args.adam_epsilon,
)
if args.with_gan:
optimizer_discriminator = optimizer_class(
discriminator.parameters(),
lr=args.learning_rate,
betas=(args.adam_beta1, args.adam_beta2),
weight_decay=args.adam_weight_decay,
eps=args.adam_epsilon,
)
else:
optimizer = optimizer_class(
params_to_optimize,
lr=args.learning_rate,
betas=(args.adam_beta1, args.adam_beta2),
weight_decay=args.adam_weight_decay,
#eps=args.adam_epsilon,
)
if args.with_gan:
optimizer_discriminator = optimizer_class(
discriminator.parameters(),
lr=args.learning_rate,
betas=(args.adam_beta1, args.adam_beta2),
weight_decay=args.adam_weight_decay,
#eps=args.adam_epsilon,
)
noise_scheduler = DDPMScheduler.from_config(args.pretrained_model_name_or_path, subfolder="scheduler")
if args.use_bucketing:
train_dataset = AutoBucketing(
concepts_list=args.concepts_list,
use_image_names_as_captions=args.use_image_names_as_captions,
shuffle_captions=args.shuffle_captions,
batch_size=args.train_batch_size,
tokenizer=tokenizer,
add_class_images_to_dataset=args.add_class_images_to_dataset,
balance_datasets=args.auto_balance_concept_datasets,
resolution=args.resolution,
with_prior_loss=False,#args.with_prior_preservation,
repeats=args.dataset_repeats,
use_text_files_as_captions=args.use_text_files_as_captions,
aspect_mode=args.aspect_mode,
action_preference=args.aspect_mode_action_preference,
seed=args.seed,
model_variant=args.model_variant,
extra_module=None if args.model_variant != "depth2img" else d2i,
mask_prompts=args.mask_prompts,
load_mask=args.masked_training,
)
else:
train_dataset = NormalDataset(
concepts_list=args.concepts_list,
tokenizer=tokenizer,
with_prior_preservation=args.with_prior_preservation,
size=args.resolution,
center_crop=args.center_crop,
num_class_images=args.num_class_images,
use_image_names_as_captions=args.use_image_names_as_captions,
shuffle_captions=args.shuffle_captions,
repeats=args.dataset_repeats,
use_text_files_as_captions=args.use_text_files_as_captions,
seed = args.seed,
model_variant=args.model_variant,
extra_module=None if args.model_variant != "depth2img" else d2i,
mask_prompts=args.mask_prompts,
load_mask=args.masked_training,
)
def collate_fn(examples):
#print(examples)
#print('test')
input_ids = [example["instance_prompt_ids"] for example in examples]
tokens = input_ids
pixel_values = [example["instance_images"] for example in examples]
mask = None
if "mask" in examples[0]:
mask = [example["mask"] for example in examples]
if args.model_variant == 'depth2img':
depth = [example["instance_depth_images"] for example in examples]
#print('test')
# Concat class and instance examples for prior preservation.
# We do this to avoid doing two forward passes.
if args.with_prior_preservation:
input_ids += [example["class_prompt_ids"] for example in examples]
pixel_values += [example["class_images"] for example in examples]
if "mask" in examples[0]:
mask += [example["class_mask"] for example in examples]
if args.model_variant == 'depth2img':
depth = [example["class_depth_images"] for example in examples]
mask_values = None
if mask is not None:
mask_values = torch.stack(mask)
mask_values = mask_values.to(memory_format=torch.contiguous_format).float()
if args.model_variant == 'depth2img':
depth_values = torch.stack(depth)
depth_values = depth_values.to(memory_format=torch.contiguous_format).float()
### no need to do it now when it's loaded by the multiAspectsDataset
#if args.with_prior_preservation:
# input_ids += [example["class_prompt_ids"] for example in examples]
# pixel_values += [example["class_images"] for example in examples]
#print(pixel_values)
#unpack the pixel_values from tensor to list
pixel_values = torch.stack(pixel_values)
pixel_values = pixel_values.to(memory_format=torch.contiguous_format).float()
input_ids = tokenizer.pad(
{"input_ids": input_ids},
padding="max_length",
max_length=tokenizer.model_max_length,
return_tensors="pt",\
).input_ids
extra_values = None
if args.model_variant == 'depth2img':
extra_values = depth_values
return {
"input_ids": input_ids,
"pixel_values": pixel_values,
"extra_values": extra_values,
"mask_values": mask_values,
"tokens": tokens
}
train_dataloader = torch.utils.data.DataLoader(
train_dataset, batch_size=args.train_batch_size, shuffle=False, collate_fn=collate_fn, pin_memory=True
)
#get the length of the dataset
train_dataset_length = len(train_dataset)
#code to check if latent cache needs to be resaved
#check if last_run.json file exists in logging_dir
if os.path.exists(logging_dir / "last_run.json"):
#if it exists, load it
with open(logging_dir / "last_run.json", "r") as f:
last_run = json.load(f)
last_run_batch_size = last_run["batch_size"]
last_run_dataset_length = last_run["dataset_length"]
if last_run_batch_size != args.train_batch_size:
print(f" {bcolors.WARNING}The batch_size has changed since the last run. Regenerating Latent Cache.{bcolors.ENDC}")
args.regenerate_latent_cache = True
#save the new batch_size and dataset_length to last_run.json
if last_run_dataset_length != train_dataset_length:
print(f" {bcolors.WARNING}The dataset length has changed since the last run. Regenerating Latent Cache.{bcolors.ENDC}")
args.regenerate_latent_cache = True
#save the new batch_size and dataset_length to last_run.json
with open(logging_dir / "last_run.json", "w") as f:
json.dump({"batch_size": args.train_batch_size, "dataset_length": train_dataset_length}, f)
else:
#if it doesn't exist, create it
last_run = {"batch_size": args.train_batch_size, "dataset_length": train_dataset_length}
#create the file
with open(logging_dir / "last_run.json", "w") as f:
json.dump(last_run, f)
weight_dtype = torch.float32
if accelerator.mixed_precision == "fp16":
print("Using fp16")
weight_dtype = torch.float16
elif accelerator.mixed_precision == "bf16":
print("Using bf16")
weight_dtype = torch.bfloat16
elif args.mixed_precision == "tf32":
torch.backends.cuda.matmul.allow_tf32 = True
#torch.set_float32_matmul_precision("medium")
# Move text_encode and vae to gpu.
# For mixed precision training we cast the text_encoder and vae weights to half-precision
# as these models are only used for inference, keeping weights in full precision is not required.
vae.to(accelerator.device, dtype=weight_dtype)
if args.use_ema == True:
ema_unet.to(accelerator.device)
if not args.train_text_encoder:
text_encoder.to(accelerator.device, dtype=weight_dtype)
if args.use_bucketing:
wh = set([tuple(x.target_wh) for x in train_dataset.image_train_items])
else:
wh = set([tuple([args.resolution, args.resolution]) for x in train_dataset.image_paths])
full_mask_by_aspect = {shape: vae.encode(torch.zeros(1, 3, shape[1], shape[0]).to(accelerator.device, dtype=weight_dtype)).latent_dist.mean * 0.18215 for shape in wh}
cached_dataset = CachedLatentsDataset(batch_size=args.train_batch_size,
text_encoder=text_encoder,
tokenizer=tokenizer,
dtype=weight_dtype,
model_variant=args.model_variant,
shuffle_per_epoch="False",
args = args,)
gen_cache = False
data_len = len(train_dataloader)
latent_cache_dir = Path(args.output_dir, "logs", "latent_cache")
#check if latents_cache.pt exists in the output_dir
if not os.path.exists(latent_cache_dir):
os.makedirs(latent_cache_dir)
for i in range(0,data_len-1):
if not os.path.exists(os.path.join(latent_cache_dir, f"latents_cache_{i}.pt")):
gen_cache = True
break
if args.regenerate_latent_cache == True:
files = os.listdir(latent_cache_dir)
gen_cache = True
for file in files:
os.remove(os.path.join(latent_cache_dir,file))
if gen_cache == False :
print(f" {bcolors.OKGREEN}Loading Latent Cache from {latent_cache_dir}{bcolors.ENDC}")
del vae
if not args.train_text_encoder:
del text_encoder
if torch.cuda.is_available():
torch.cuda.empty_cache()
torch.cuda.ipc_collect()
#load all the cached latents into a single dataset
for i in range(0,data_len-1):
cached_dataset.add_pt_cache(os.path.join(latent_cache_dir,f"latents_cache_{i}.pt"))
if gen_cache == True:
#delete all the cached latents if they exist to avoid problems
print(f" {bcolors.WARNING}Generating latents cache...{bcolors.ENDC}")
train_dataset = LatentsDataset([], [], [], [], [], [])
counter = 0
ImageFile.LOAD_TRUNCATED_IMAGES = True
with torch.no_grad():
for batch in tqdm(train_dataloader, desc="Caching latents", bar_format='%s{l_bar}%s%s{bar}%s%s{r_bar}%s'%(bcolors.OKBLUE,bcolors.ENDC, bcolors.OKBLUE, bcolors.ENDC,bcolors.OKBLUE,bcolors.ENDC,)):
cached_extra = None
cached_mask = None
batch["pixel_values"] = batch["pixel_values"].to(accelerator.device, non_blocking=True, dtype=weight_dtype)
batch["input_ids"] = batch["input_ids"].to(accelerator.device, non_blocking=True)
cached_latent = vae.encode(batch["pixel_values"]).latent_dist
if batch["mask_values"] is not None:
cached_mask = functional.resize(batch["mask_values"], size=cached_latent.mean.shape[2:])
if batch["mask_values"] is not None and args.model_variant == "inpainting":
batch["mask_values"] = batch["mask_values"].to(accelerator.device, non_blocking=True, dtype=weight_dtype)
cached_extra = vae.encode(batch["pixel_values"] * (1 - batch["mask_values"])).latent_dist
if args.model_variant == "depth2img":
batch["extra_values"] = batch["extra_values"].to(accelerator.device, non_blocking=True, dtype=weight_dtype)
cached_extra = functional.resize(batch["extra_values"], size=cached_latent.mean.shape[2:])
if args.train_text_encoder:
cached_text_enc = batch["input_ids"]
else:
cached_text_enc = text_encoder(batch["input_ids"])[0]
train_dataset.add_latent(cached_latent, cached_text_enc, cached_mask, cached_extra, batch["tokens"])
del batch
del cached_latent
del cached_text_enc
del cached_mask
del cached_extra
torch.save(train_dataset, os.path.join(latent_cache_dir,f"latents_cache_{counter}.pt"))
cached_dataset.add_pt_cache(os.path.join(latent_cache_dir,f"latents_cache_{counter}.pt"))
counter += 1
train_dataset = LatentsDataset([], [], [], [], [], [])
#if counter % 300 == 0:
#train_dataloader = torch.utils.data.DataLoader(train_dataset, batch_size=1, collate_fn=lambda x: x, shuffle=False)
# gc.collect()
# torch.cuda.empty_cache()
# accelerator.free_memory()
#clear vram after caching latents
del vae
if not args.train_text_encoder:
del text_encoder
if torch.cuda.is_available():
torch.cuda.empty_cache()
torch.cuda.ipc_collect()
#load all the cached latents into a single dataset
train_dataloader = torch.utils.data.DataLoader(cached_dataset, batch_size=1, collate_fn=lambda x: x, shuffle=False)
print(f" {bcolors.OKGREEN}Latents are ready.{bcolors.ENDC}")
# Scheduler and math around the number of training steps.
overrode_max_train_steps = False
num_update_steps_per_epoch = len(train_dataloader)
if args.max_train_steps is None:
args.max_train_steps = args.num_train_epochs * num_update_steps_per_epoch
overrode_max_train_steps = True
if args.lr_warmup_steps < 1:
args.lr_warmup_steps = math.floor(args.lr_warmup_steps * args.max_train_steps / args.gradient_accumulation_steps)
lr_scheduler = get_scheduler(
args.lr_scheduler,
optimizer=optimizer,
num_warmup_steps=args.lr_warmup_steps * args.gradient_accumulation_steps,
num_training_steps=args.max_train_steps,
)
if args.train_text_encoder and not args.use_ema:
unet, text_encoder, optimizer, train_dataloader, lr_scheduler = accelerator.prepare(
unet, text_encoder, optimizer, train_dataloader, lr_scheduler
)
elif args.train_text_encoder and args.use_ema:
unet, text_encoder, ema_unet, optimizer, train_dataloader, lr_scheduler = accelerator.prepare(
unet, text_encoder, ema_unet, optimizer, train_dataloader, lr_scheduler
)
elif not args.train_text_encoder and args.use_ema:
unet, ema_unet, optimizer, train_dataloader, lr_scheduler = accelerator.prepare(
unet, ema_unet, optimizer, train_dataloader, lr_scheduler
)
elif not args.train_text_encoder and not args.use_ema:
unet, optimizer, train_dataloader, lr_scheduler = accelerator.prepare(
unet, optimizer, train_dataloader, lr_scheduler
)
if args.with_gan:
lr_scheduler_discriminator = get_scheduler(
args.lr_scheduler,
optimizer=optimizer_discriminator,
num_warmup_steps=args.lr_warmup_steps * args.gradient_accumulation_steps,
num_training_steps=args.max_train_steps,
)
discriminator, optimizer_discriminator, lr_scheduler_discriminator = accelerator.prepare(discriminator, optimizer_discriminator, lr_scheduler_discriminator)
# We need to recalculate our total training steps as the size of the training dataloader may have changed.
num_update_steps_per_epoch = len(train_dataloader)
if overrode_max_train_steps:
args.max_train_steps = args.num_train_epochs * num_update_steps_per_epoch
#print(args.max_train_steps, num_update_steps_per_epoch)
# Afterwards we recalculate our number of training epochs
#print(args.max_train_steps, num_update_steps_per_epoch)
args.num_train_epochs = math.ceil(args.max_train_steps / num_update_steps_per_epoch)
# We need to initialize the trackers we use, and also store our configuration.
# The trackers initializes automatically on the main process.
if accelerator.is_main_process:
accelerator.init_trackers("dreambooth")
# Train!
total_batch_size = args.train_batch_size * accelerator.num_processes * args.gradient_accumulation_steps
logger.info("***** Running training *****")
logger.info(f" Num examples = {len(train_dataset)}")
logger.info(f" Num batches each epoch = {len(train_dataloader)}")
logger.info(f" Num Epochs = {args.num_train_epochs}")
logger.info(f" Instantaneous batch size per device = {args.train_batch_size}")
logger.info(f" Total train batch size (w. parallel, distributed & accumulation) = {total_batch_size}")
logger.info(f" Gradient Accumulation steps = {args.gradient_accumulation_steps}")
logger.info(f" Total optimization steps = {args.max_train_steps}")
def mid_train_playground(step):
tqdm.write(f"{bcolors.WARNING} Booting up GUI{bcolors.ENDC}")
epoch = step // num_update_steps_per_epoch
if args.train_text_encoder and args.stop_text_encoder_training == True:
text_enc_model = accelerator.unwrap_model(text_encoder,True)
elif args.train_text_encoder and args.stop_text_encoder_training > epoch:
text_enc_model = accelerator.unwrap_model(text_encoder,True)
elif args.train_text_encoder == False:
text_enc_model = CLIPTextModel.from_pretrained(args.pretrained_model_name_or_path, subfolder="text_encoder" )
elif args.train_text_encoder and args.stop_text_encoder_training <= epoch:
if 'frozen_directory' in locals():
text_enc_model = CLIPTextModel.from_pretrained(frozen_directory, subfolder="text_encoder")
else:
text_enc_model = accelerator.unwrap_model(text_encoder,True)
scheduler = DPMSolverMultistepScheduler.from_pretrained(args.pretrained_model_name_or_path, subfolder="scheduler")
unwrapped_unet = accelerator.unwrap_model(ema_unet if args.use_ema else unet,True)
pipeline = DiffusionPipeline.from_pretrained(
args.pretrained_model_name_or_path,
unet=unwrapped_unet,
text_encoder=text_enc_model,
vae=AutoencoderKL.from_pretrained(args.pretrained_vae_name_or_path or args.pretrained_model_name_or_path,subfolder=None if args.pretrained_vae_name_or_path else "vae", safe_serialization=True),
safety_checker=None,
torch_dtype=weight_dtype,
local_files_only=False,
requires_safety_checker=False,
)
pipeline.scheduler = scheduler
if is_xformers_available() and args.attention=='xformers':
try:
vae.enable_xformers_memory_efficient_attention()
unet.enable_xformers_memory_efficient_attention()
except Exception as e:
logger.warning(
"Could not enable memory efficient attention. Make sure xformers is installed"
f" correctly and a GPU is available: {e}"
)
elif args.attention=='flash_attention':
replace_unet_cross_attn_to_flash_attention()
pipeline = pipeline.to(accelerator.device)
def inference(prompt, negative_prompt, num_samples, height=512, width=512, num_inference_steps=50,seed=-1,guidance_scale=7.5):
with torch.autocast("cuda"), torch.inference_mode():
if seed != -1:
if g_cuda is None:
g_cuda = torch.Generator(device='cuda')
else:
g_cuda.manual_seed(int(seed))
else:
seed = random.randint(0, 100000)
g_cuda = torch.Generator(device='cuda')
g_cuda.manual_seed(seed)
return pipeline(
prompt, height=int(height), width=int(width),
negative_prompt=negative_prompt,
num_images_per_prompt=int(num_samples),
num_inference_steps=int(num_inference_steps), guidance_scale=guidance_scale,
generator=g_cuda).images, seed
with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
prompt = gr.Textbox(label="Prompt", value="photo of zwx dog in a bucket")
negative_prompt = gr.Textbox(label="Negative Prompt", value="")
run = gr.Button(value="Generate")
with gr.Row():
num_samples = gr.Number(label="Number of Samples", value=4)
guidance_scale = gr.Number(label="Guidance Scale", value=7.5)
with gr.Row():
height = gr.Number(label="Height", value=512)
width = gr.Number(label="Width", value=512)
with gr.Row():
num_inference_steps = gr.Slider(label="Steps", value=25)
seed = gr.Number(label="Seed", value=-1)
with gr.Column():
gallery = gr.Gallery()
seedDisplay = gr.Number(label="Used Seed:", value=0)
run.click(inference, inputs=[prompt, negative_prompt, num_samples, height, width, num_inference_steps,seed, guidance_scale], outputs=[gallery,seedDisplay])
demo.launch(share=True,prevent_thread_lock=True)
tqdm.write(f"{bcolors.WARNING}Gradio Session is active, Press 'F12' to resume training{bcolors.ENDC}")
keyboard.wait('f12')
demo.close()
del demo
del text_enc_model
del unwrapped_unet
del pipeline
return
def save_and_sample_weights(step,context='checkpoint',save_model=True):
try:
#check how many folders are in the output dir
#if there are more than 5, delete the oldest one
#save the model
#save the optimizer
#save the lr_scheduler
#save the args
height = args.sample_height
width = args.sample_width
batch_prompts = []
if args.sample_from_batch > 0:
num_samples = args.sample_from_batch if args.sample_from_batch < args.train_batch_size else args.train_batch_size
batch_prompts = []
tokens = args.batch_tokens
if tokens != None:
allPrompts = list(set([tokenizer.decode(p).replace('<|endoftext|>','').replace('<|startoftext|>', '') for p in tokens]))
if len(allPrompts) < num_samples:
num_samples = len(allPrompts)
batch_prompts = random.sample(allPrompts, num_samples)
if args.sample_aspect_ratios:
#choose random aspect ratio from ASPECTS
aspect_ratio = random.choice(ASPECTS)
height = aspect_ratio[0]
width = aspect_ratio[1]
if os.path.exists(args.output_dir):
if args.detect_full_drive==True:
folders = os.listdir(args.output_dir)
#check how much space is left on the drive
total, used, free = shutil.disk_usage("/")
if (free // (2**30)) < 4:
#folders.remove("0")
#get the folder with the lowest number
#oldest_folder = min(folder for folder in folders if folder.isdigit())
tqdm.write(f"{bcolors.FAIL}Drive is almost full, Please make some space to continue training.{bcolors.ENDC}")
if args.send_telegram_updates:
try:
send_telegram_message(f"Drive is almost full, Please make some space to continue training.", args.telegram_chat_id, args.telegram_token)
except:
pass
#count time
import time
start_time = time.time()
import platform
while input("Press Enter to continue... if you're on linux we'll wait 5 minutes for you to make space and continue"):
#check if five minutes have passed
#check if os is linux
if 'Linux' in platform.platform():
if time.time() - start_time > 300:
break
#oldest_folder_path = os.path.join(args.output_dir, oldest_folder)
#shutil.rmtree(oldest_folder_path)
# Create the pipeline using using the trained modules and save it.
if accelerator.is_main_process:
if 'step' in context:
#what is the current epoch
epoch = step // num_update_steps_per_epoch
else:
epoch = step
if args.train_text_encoder and args.stop_text_encoder_training == True:
text_enc_model = accelerator.unwrap_model(text_encoder,True)
elif args.train_text_encoder and args.stop_text_encoder_training > epoch:
text_enc_model = accelerator.unwrap_model(text_encoder,True)
elif args.train_text_encoder == False:
text_enc_model = CLIPTextModel.from_pretrained(args.pretrained_model_name_or_path, subfolder="text_encoder" )
elif args.train_text_encoder and args.stop_text_encoder_training <= epoch:
if 'frozen_directory' in locals():
text_enc_model = CLIPTextModel.from_pretrained(frozen_directory, subfolder="text_encoder")
else:
text_enc_model = accelerator.unwrap_model(text_encoder,True)
#scheduler = DDIMScheduler(beta_start=0.00085, beta_end=0.012, beta_schedule="scaled_linear", clip_sample=False, set_alpha_to_one=False)
#scheduler = EulerDiscreteScheduler.from_pretrained(args.pretrained_model_name_or_path, subfolder="scheduler", prediction_type="v_prediction")
scheduler = DPMSolverMultistepScheduler.from_pretrained(args.pretrained_model_name_or_path, subfolder="scheduler")
unwrapped_unet = accelerator.unwrap_model(unet,True)
pipeline = DiffusionPipeline.from_pretrained(
args.pretrained_model_name_or_path,
unet=unwrapped_unet,
text_encoder=text_enc_model,
vae=AutoencoderKL.from_pretrained(args.pretrained_vae_name_or_path or args.pretrained_model_name_or_path,subfolder=None if args.pretrained_vae_name_or_path else "vae",),
safety_checker=None,
torch_dtype=weight_dtype,
local_files_only=False,
requires_safety_checker=False,
)
pipeline.scheduler = scheduler
if is_xformers_available() and args.attention=='xformers':
try:
unet.enable_xformers_memory_efficient_attention()
except Exception as e:
logger.warning(
"Could not enable memory efficient attention. Make sure xformers is installed"
f" correctly and a GPU is available: {e}"
)
elif args.attention=='flash_attention':
replace_unet_cross_attn_to_flash_attention()
if args.run_name:
save_dir = os.path.join(args.output_dir, f"{context}_{step}_{args.run_name}")
else:
save_dir = os.path.join(args.output_dir, f"{context}_{step}")
if args.flatten_sample_folder:
sample_dir = main_sample_dir
else:
sample_dir = os.path.join(main_sample_dir, f"{context}_{step}")
#if sample dir path does not exist, create it
if args.stop_text_encoder_training == True:
save_dir = frozen_directory
if save_model:
pipeline.save_pretrained(save_dir,safe_serialization=True)
if args.with_gan:
discriminator.save_pretrained(os.path.join(save_dir, "discriminator"), safe_serialization=True)
if args.use_ema:
ema_unet.save_pretrained(os.path.join(save_dir, "unet_ema"), safe_serialization=True)
with open(os.path.join(save_dir, "args.json"), "w") as f:
json.dump(args.__dict__, f, indent=2)
if args.stop_text_encoder_training == True:
#delete every folder in frozen_directory but the text encoder
for folder in os.listdir(save_dir):
if folder != "text_encoder" and os.path.isdir(os.path.join(save_dir, folder)):
shutil.rmtree(os.path.join(save_dir, folder))
imgs = []
if args.use_ema and args.sample_from_ema:
pipeline.unet = ema_unet
for param in unet.parameters():
param.requires_grad = False
if torch.cuda.is_available():
torch.cuda.empty_cache()
torch.cuda.ipc_collect()
if args.add_sample_prompt is not None or batch_prompts != [] and args.stop_text_encoder_training != True:
prompts = []
if args.add_sample_prompt is not None:
for prompt in args.add_sample_prompt:
prompts.append(prompt)
if batch_prompts != []:
for prompt in batch_prompts:
prompts.append(prompt)
pipeline = pipeline.to(accelerator.device)
pipeline.set_progress_bar_config(disable=True)
#sample_dir = os.path.join(save_dir, "samples")
#if sample_dir exists, delete it
if os.path.exists(sample_dir):
if not args.flatten_sample_folder:
shutil.rmtree(sample_dir)
os.makedirs(sample_dir, exist_ok=True)
with torch.autocast("cuda"), torch.inference_mode():
if args.send_telegram_updates:
try:
send_telegram_message(f"Generating samples for <b>{step}</b> {context}", args.telegram_chat_id, args.telegram_token)
except:
pass
n_sample = args.n_save_sample
if args.save_sample_controlled_seed:
n_sample += len(args.save_sample_controlled_seed)
progress_bar_sample = tqdm(total=len(prompts)*n_sample,desc="Generating samples")
for samplePrompt in prompts:
sampleIndex = prompts.index(samplePrompt)
#convert sampleIndex to number in words
# Data to be written
sampleProperties = {
"samplePrompt" : samplePrompt
}
# Serializing json
json_object = json.dumps(sampleProperties, indent=4)
if args.flatten_sample_folder:
sampleName = f"{context}_{step}_prompt_{sampleIndex+1}"
else:
sampleName = f"prompt_{sampleIndex+1}"
if not args.flatten_sample_folder:
os.makedirs(os.path.join(sample_dir,sampleName), exist_ok=True)
if args.model_variant == 'inpainting':
conditioning_image = torch.zeros(1, 3, height, width)
mask = torch.ones(1, 1, height, width)
if args.model_variant == 'depth2img':
#pil new white image
test_image = Image.new('RGB', (width, height), (255, 255, 255))
depth_image = Image.new('RGB', (width, height), (255, 255, 255))
depth = np.array(depth_image.convert("L"))
depth = depth.astype(np.float32) / 255.0
depth = depth[None, None]
depth = torch.from_numpy(depth)
for i in range(n_sample):
#check if the sample is controlled by a seed
if i < args.n_save_sample:
if args.model_variant == 'inpainting':
images = pipeline(samplePrompt, conditioning_image, mask, height=height,width=width, guidance_scale=args.save_guidance_scale, num_inference_steps=args.save_infer_steps).images
if args.model_variant == 'depth2img':
images = pipeline(samplePrompt,image=test_image, guidance_scale=args.save_guidance_scale, num_inference_steps=args.save_infer_steps,strength=1.0).images
elif args.model_variant == 'base':
images = pipeline(samplePrompt,height=height,width=width, guidance_scale=args.save_guidance_scale, num_inference_steps=args.save_infer_steps).images
if not args.flatten_sample_folder:
images[0].save(os.path.join(sample_dir,sampleName, f"{sampleName}_{i}.png"))
else:
images[0].save(os.path.join(sample_dir, f"{sampleName}_{i}.png"))
else:
seed = args.save_sample_controlled_seed[i - args.n_save_sample]
generator = torch.Generator("cuda").manual_seed(seed)
if args.model_variant == 'inpainting':
images = pipeline(samplePrompt,conditioning_image, mask,height=height,width=width, guidance_scale=args.save_guidance_scale, num_inference_steps=args.save_infer_steps, generator=generator).images
if args.model_variant == 'depth2img':
images = pipeline(samplePrompt,image=test_image, guidance_scale=args.save_guidance_scale, num_inference_steps=args.save_infer_steps,generator=generator,strength=1.0).images
elif args.model_variant == 'base':
images = pipeline(samplePrompt,height=height,width=width, guidance_scale=args.save_guidance_scale, num_inference_steps=args.save_infer_steps, generator=generator).images
if not args.flatten_sample_folder:
images[0].save(os.path.join(sample_dir,sampleName, f"{sampleName}_controlled_seed_{str(seed)}.png"))
else:
images[0].save(os.path.join(sample_dir, f"{sampleName}_controlled_seed_{str(seed)}.png"))
progress_bar_sample.update(1)
if args.send_telegram_updates:
imgs = []
#get all the images from the sample folder
if not args.flatten_sample_folder:
dir = os.listdir(os.path.join(sample_dir,sampleName))
else:
dir = sample_dir
for file in dir:
if file.endswith(".png"):
#open the image with pil
img = Image.open(os.path.join(sample_dir,sampleName,file))
imgs.append(img)
try:
send_media_group(args.telegram_chat_id,args.telegram_token,imgs, caption=f"Samples for the <b>{step}</b> {context} using the prompt:\n\n<b>{samplePrompt}</b>")
except:
pass
del pipeline
del unwrapped_unet
for param in unet.parameters():
param.requires_grad = True
if torch.cuda.is_available():
torch.cuda.empty_cache()
torch.cuda.ipc_collect()
if save_model == True:
tqdm.write(f"{bcolors.OKGREEN}Weights saved to {save_dir}{bcolors.ENDC}")
elif save_model == False and len(imgs) > 0:
del imgs
tqdm.write(f"{bcolors.OKGREEN}Samples saved to {sample_dir}{bcolors.ENDC}")
except Exception as e:
tqdm.write(e)
tqdm.write(f"{bcolors.FAIL} Error occured during sampling, skipping.{bcolors.ENDC}")
pass
@torch.no_grad()
def update_ema(ema_model, model):
ema_step = ema_model.config["step"]
decay = min((ema_step + 1) / (ema_step + 10), 0.9999)
ema_model.config["step"] += 1
for (s_param, param) in zip(ema_model.parameters(), model.parameters()):
if param.requires_grad:
s_param.add_((1 - decay) * (param - s_param))
else:
s_param.copy_(param)
# Only show the progress bar once on each machine.
progress_bar = tqdm(range(args.max_train_steps),bar_format='%s{l_bar}%s%s{bar}%s%s{r_bar}%s'%(bcolors.OKBLUE,bcolors.ENDC, bcolors.OKBLUE, bcolors.ENDC,bcolors.OKBLUE,bcolors.ENDC,), disable=not accelerator.is_local_main_process)
progress_bar_inter_epoch = tqdm(range(num_update_steps_per_epoch),bar_format='%s{l_bar}%s%s{bar}%s%s{r_bar}%s'%(bcolors.OKBLUE,bcolors.ENDC, bcolors.OKGREEN, bcolors.ENDC,bcolors.OKBLUE,bcolors.ENDC,), disable=not accelerator.is_local_main_process)
progress_bar_e = tqdm(range(args.num_train_epochs),bar_format='%s{l_bar}%s%s{bar}%s%s{r_bar}%s'%(bcolors.OKBLUE,bcolors.ENDC, bcolors.OKGREEN, bcolors.ENDC,bcolors.OKBLUE,bcolors.ENDC,), disable=not accelerator.is_local_main_process)
progress_bar.set_description("Overall Steps")
progress_bar_inter_epoch.set_description("Steps To Epoch")
progress_bar_e.set_description("Overall Epochs")
global_step = 0
loss_avg = AverageMeter("loss_avg", max_eta=0.999)
gan_loss_avg = AverageMeter("gan_loss_avg", max_eta=0.999)
text_enc_context = nullcontext() if args.train_text_encoder else torch.no_grad()
if args.send_telegram_updates:
try:
send_telegram_message(f"Starting training with the following settings:\n\n{format_dict(args.__dict__)}", args.telegram_chat_id, args.telegram_token)
except:
pass
try:
tqdm.write(f"{bcolors.OKBLUE}Starting Training!{bcolors.ENDC}")
try:
def toggle_gui(event=None):
if keyboard.is_pressed("ctrl") and keyboard.is_pressed("shift") and keyboard.is_pressed("g"):
tqdm.write(f"{bcolors.WARNING}GUI will boot as soon as the current step is done.{bcolors.ENDC}")
nonlocal mid_generation
if mid_generation == True:
mid_generation = False
tqdm.write(f"{bcolors.WARNING}Cancelled GUI.{bcolors.ENDC}")
else:
mid_generation = True
def toggle_checkpoint(event=None):
if keyboard.is_pressed("ctrl") and keyboard.is_pressed("shift") and keyboard.is_pressed("s") and not keyboard.is_pressed("alt"):
tqdm.write(f"{bcolors.WARNING}Saving the model as soon as this epoch is done.{bcolors.ENDC}")
nonlocal mid_checkpoint
if mid_checkpoint == True:
mid_checkpoint = False
tqdm.write(f"{bcolors.WARNING}Cancelled Checkpointing.{bcolors.ENDC}")
else:
mid_checkpoint = True
def toggle_sample(event=None):
if keyboard.is_pressed("ctrl") and keyboard.is_pressed("shift") and keyboard.is_pressed("p") and not keyboard.is_pressed("alt"):
tqdm.write(f"{bcolors.WARNING}Sampling will begin as soon as this epoch is done.{bcolors.ENDC}")
nonlocal mid_sample
if mid_sample == True:
mid_sample = False
tqdm.write(f"{bcolors.WARNING}Cancelled Sampling.{bcolors.ENDC}")
else:
mid_sample = True
def toggle_checkpoint_step(event=None):
if keyboard.is_pressed("ctrl") and keyboard.is_pressed("shift") and keyboard.is_pressed("alt") and keyboard.is_pressed("s"):
tqdm.write(f"{bcolors.WARNING}Saving the model as soon as this step is done.{bcolors.ENDC}")
nonlocal mid_checkpoint_step
if mid_checkpoint_step == True:
mid_checkpoint_step = False
tqdm.write(f"{bcolors.WARNING}Cancelled Checkpointing.{bcolors.ENDC}")
else:
mid_checkpoint_step = True
def toggle_sample_step(event=None):
if keyboard.is_pressed("ctrl") and keyboard.is_pressed("shift") and keyboard.is_pressed("alt") and keyboard.is_pressed("p"):
tqdm.write(f"{bcolors.WARNING}Sampling will begin as soon as this step is done.{bcolors.ENDC}")
nonlocal mid_sample_step
if mid_sample_step == True:
mid_sample_step = False
tqdm.write(f"{bcolors.WARNING}Cancelled Sampling.{bcolors.ENDC}")
else:
mid_sample_step = True
def toggle_quit_and_save_epoch(event=None):
if keyboard.is_pressed("ctrl") and keyboard.is_pressed("shift") and keyboard.is_pressed("q") and not keyboard.is_pressed("alt"):
tqdm.write(f"{bcolors.WARNING}Quitting and saving the model as soon as this epoch is done.{bcolors.ENDC}")
nonlocal mid_quit
if mid_quit == True:
mid_quit = False
tqdm.write(f"{bcolors.WARNING}Cancelled Quitting.{bcolors.ENDC}")
else:
mid_quit = True
def toggle_quit_and_save_step(event=None):
if keyboard.is_pressed("ctrl") and keyboard.is_pressed("shift") and keyboard.is_pressed("alt") and keyboard.is_pressed("q"):
tqdm.write(f"{bcolors.WARNING}Quitting and saving the model as soon as this step is done.{bcolors.ENDC}")
nonlocal mid_quit_step
if mid_quit_step == True:
mid_quit_step = False
tqdm.write(f"{bcolors.WARNING}Cancelled Quitting.{bcolors.ENDC}")
else:
mid_quit_step = True
def help(event=None):
if keyboard.is_pressed("ctrl") and keyboard.is_pressed("h"):
print_instructions()
keyboard.on_press_key("g", toggle_gui)
keyboard.on_press_key("s", toggle_checkpoint)
keyboard.on_press_key("p", toggle_sample)
keyboard.on_press_key("s", toggle_checkpoint_step)
keyboard.on_press_key("p", toggle_sample_step)
keyboard.on_press_key("q", toggle_quit_and_save_epoch)
keyboard.on_press_key("q", toggle_quit_and_save_step)
keyboard.on_press_key("h", help)
print_instructions()
except Exception as e:
pass
mid_generation = False
mid_checkpoint = False
mid_sample = False
mid_checkpoint_step = False
mid_sample_step = False
mid_quit = False
mid_quit_step = False
#lambda set mid_generation to true
if args.run_name:
frozen_directory = os.path.join(args.output_dir, f"frozen_text_encoder_{args.run_name}")
else:
frozen_directory = os.path.join(args.output_dir, "frozen_text_encoder")
unet_stats = {}
discriminator_stats = {}
os.makedirs(main_sample_dir, exist_ok=True)
with open(os.path.join(main_sample_dir, "args.json"), "w") as f:
json.dump(args.__dict__, f, indent=2)
if args.with_gan:
with open(os.path.join(main_sample_dir, "discriminator_config.json"), "w") as f:
json.dump(discriminator.config, f, indent=2)
for epoch in range(args.num_train_epochs):
#every 10 epochs print instructions
unet.train()
if args.train_text_encoder:
text_encoder.train()
#save initial weights
if args.sample_on_training_start==True and epoch==0:
save_and_sample_weights(epoch,'start',save_model=False)
if args.train_text_encoder and args.stop_text_encoder_training == epoch:
args.stop_text_encoder_training = True
if accelerator.is_main_process:
tqdm.write(f"{bcolors.WARNING} Stopping text encoder training{bcolors.ENDC}")
current_percentage = (epoch/args.num_train_epochs)*100
#round to the nearest whole number
current_percentage = round(current_percentage,0)
try:
send_telegram_message(f"Text encoder training stopped at epoch {epoch} which is {current_percentage}% of training. Freezing weights and saving.", args.telegram_chat_id, args.telegram_token)
except:
pass
if os.path.exists(frozen_directory):
#delete the folder if it already exists
shutil.rmtree(frozen_directory)
os.mkdir(frozen_directory)
save_and_sample_weights(epoch,'epoch')
args.stop_text_encoder_training = epoch
progress_bar_inter_epoch.reset(total=num_update_steps_per_epoch)
for step, batch in enumerate(train_dataloader):
with accelerator.accumulate(unet):
# Convert images to latent space
with torch.no_grad():
latent_dist = batch[0][0]
latents = latent_dist.sample() * 0.18215
if args.model_variant == 'inpainting':
mask = batch[0][2]
mask_mean = batch[0][3]
conditioning_latent_dist = batch[0][4]
conditioning_latents = conditioning_latent_dist.sample() * 0.18215
if args.model_variant == 'depth2img':
depth = batch[0][4]
if args.sample_from_batch > 0:
args.batch_tokens = batch[0][5]
# Sample noise that we'll add to the latents
# and some extra bits to make it so that the model learns to change the zero-frequency of the component freely
# https://www.crosslabs.org/blog/diffusion-with-offset-noise
if (args.with_offset_noise == True):
noise = torch.randn_like(latents) + (args.offset_noise_weight * torch.randn(latents.shape[0], latents.shape[1], 1, 1).to(accelerator.device))
else:
noise = torch.randn_like(latents)
bsz = latents.shape[0]
# Sample a random timestep for each image
timesteps = torch.randint(0, int(noise_scheduler.config.num_train_timesteps * args.max_denoising_strength), (bsz,), device=latents.device)
timesteps = timesteps.long()
# Add noise to the latents according to the noise magnitude at each timestep
# (this is the forward diffusion process)
noisy_latents = noise_scheduler.add_noise(latents, noise, timesteps)
# Get the text embedding for conditioning
with text_enc_context:
if args.train_text_encoder:
if args.clip_penultimate == True:
encoder_hidden_states = text_encoder(batch[0][1],output_hidden_states=True)
encoder_hidden_states = text_encoder.text_model.final_layer_norm(encoder_hidden_states['hidden_states'][-2])
else:
encoder_hidden_states = text_encoder(batch[0][1])[0]
else:
encoder_hidden_states = batch[0][1]
# Predict the noise residual
mask=None
if args.model_variant == 'inpainting':
if mask is not None and random.uniform(0, 1) < args.unmasked_probability:
# for some steps, predict the unmasked image
conditioning_latents = torch.stack([full_mask_by_aspect[tuple([latents.shape[3]*8, latents.shape[2]*8])].squeeze()] * bsz)
mask = torch.ones(bsz, 1, latents.shape[2], latents.shape[3]).to(accelerator.device, dtype=weight_dtype)
noisy_inpaint_latents = torch.concat([noisy_latents, mask, conditioning_latents], 1)
model_pred = unet(noisy_inpaint_latents, timesteps, encoder_hidden_states).sample
elif args.model_variant == 'depth2img':
noisy_depth_latents = torch.cat([noisy_latents, depth], dim=1)
model_pred = unet(noisy_depth_latents, timesteps, encoder_hidden_states, depth).sample
elif args.model_variant == "base":
model_pred = unet(noisy_latents, timesteps, encoder_hidden_states).sample
# Get the target for loss depending on the prediction type
if noise_scheduler.config.prediction_type == "epsilon":
target = noise
elif noise_scheduler.config.prediction_type == "v_prediction":
target = noise_scheduler.get_velocity(latents, noise, timesteps)
else:
raise ValueError(f"Unknown prediction type {noise_scheduler.config.prediction_type}")
# GAN stuff
# Input: noisy_latents
# True output: target
# Fake output: model_pred
if args.with_gan:
# Turn on learning for the discriminator, and do an optimization step
for param in discriminator.parameters():
param.requires_grad = True
pred_fake = discriminator(torch.cat((noisy_latents, model_pred), 1).detach(), encoder_hidden_states)
pred_real = discriminator(torch.cat((noisy_latents, target), 1), encoder_hidden_states)
discriminator_loss = F.mse_loss(pred_fake, torch.zeros_like(pred_fake), reduction="mean") + F.mse_loss(pred_real, torch.ones_like(pred_real), reduction="mean")
if discriminator_loss.isnan():
tqdm.write(f"{bcolors.WARNING}Discriminator loss is NAN, skipping GAN update.{bcolors.ENDC}")
else:
accelerator.backward(discriminator_loss)
if accelerator.sync_gradients:
accelerator.clip_grad_norm_(discriminator.parameters(), args.max_grad_norm)
optimizer_discriminator.step()
lr_scheduler_discriminator.step()
# Hack to fix NaNs caused by GAN training
for name, p in discriminator.named_parameters():
if p.isnan().any():
fix_nans_(p, name, discriminator_stats[name])
else:
(std, mean) = torch.std_mean(p)
discriminator_stats[name] = (std.item(), mean.item())
del std, mean
optimizer_discriminator.zero_grad()
del pred_real, pred_fake, discriminator_loss
# Turn off learning for the discriminator for the generator optimization step
for param in discriminator.parameters():
param.requires_grad = False
if args.with_prior_preservation:
# Chunk the noise and noise_pred into two parts and compute the loss on each part separately.
"""
noise_pred, noise_pred_prior = torch.chunk(noise_pred, 2, dim=0)
noise, noise_prior = torch.chunk(noise, 2, dim=0)
# Compute instance loss
loss = F.mse_loss(noise_pred.float(), noise.float(), reduction="none").mean([1, 2, 3]).mean()
# Compute prior loss
prior_loss = F.mse_loss(noise_pred_prior.float(), noise_prior.float(), reduction="mean")
# Add the prior loss to the instance loss.
loss = loss + args.prior_loss_weight * prior_loss
"""
# Chunk the noise and model_pred into two parts and compute the loss on each part separately.
model_pred, model_pred_prior = torch.chunk(model_pred, 2, dim=0)
target, target_prior = torch.chunk(target, 2, dim=0)
if mask is not None and args.model_variant != "inpainting":
loss = masked_mse_loss(model_pred.float(), target.float(), mask, reduction="none").mean([1, 2, 3]).mean()
prior_loss = masked_mse_loss(model_pred_prior.float(), target_prior.float(), mask, reduction="mean")
else:
loss = F.mse_loss(model_pred.float(), target.float(), reduction="none").mean([1, 2, 3]).mean()
prior_loss = F.mse_loss(model_pred_prior.float(), target_prior.float(), reduction="mean")
# Add the prior loss to the instance loss.
loss = loss + args.prior_loss_weight * prior_loss
if mask is not None and args.normalize_masked_area_loss:
loss = loss / mask_mean
else:
if mask is not None and args.model_variant != "inpainting":
loss = masked_mse_loss(model_pred.float(), target.float(), mask, reduction="none").mean([1, 2, 3])
loss = loss.mean()
else:
loss = F.mse_loss(model_pred.float(), target.float(), reduction="mean")
if mask is not None and args.normalize_masked_area_loss:
loss = loss / mask_mean
base_loss = loss
if args.with_gan:
# Add loss from the GAN
pred_fake = discriminator(torch.cat((noisy_latents, model_pred), 1), encoder_hidden_states)
gan_loss = F.mse_loss(pred_fake, torch.ones_like(pred_fake), reduction="mean")
if gan_loss.isnan():
tqdm.write(f"{bcolors.WARNING}GAN loss is NAN, skipping GAN loss.{bcolors.ENDC}")
else:
gan_weight = args.gan_weight
if args.gan_warmup and global_step < args.gan_warmup:
gan_weight *= global_step / args.gan_warmup
loss += gan_weight * gan_loss
del pred_fake
accelerator.backward(loss)
if accelerator.sync_gradients:
params_to_clip = (
itertools.chain(unet.parameters(), text_encoder.parameters())
if args.train_text_encoder
else unet.parameters()
)
accelerator.clip_grad_norm_(params_to_clip, args.max_grad_norm)
optimizer.step()
lr_scheduler.step()
# Hack to fix NaNs caused by GAN training
for name, p in unet.named_parameters():
if p.isnan().any():
fix_nans_(p, name, unet_stats[name])
else:
(std, mean) = torch.std_mean(p)
unet_stats[name] = (std.item(), mean.item())
del std, mean
optimizer.zero_grad()
loss_avg.update(base_loss.detach_())
if args.with_gan and not gan_loss.isnan():
gan_loss_avg.update(gan_loss.detach_())
if args.use_ema == True:
update_ema(ema_unet, unet)
del loss, model_pred
if args.with_prior_preservation:
del model_pred_prior
logs = {"loss": loss_avg.avg.item(), "lr": lr_scheduler.get_last_lr()[0]}
if args.with_gan:
logs["gan_loss"] = gan_loss_avg.avg.item()
progress_bar.set_postfix(**logs)
if not global_step % args.log_interval:
accelerator.log(logs, step=global_step)
if global_step > 0 and not global_step % args.sample_step_interval:
save_and_sample_weights(global_step,'step',save_model=False)
progress_bar.update(1)
progress_bar_inter_epoch.update(1)
progress_bar_e.refresh()
global_step += 1
if mid_quit_step==True:
accelerator.wait_for_everyone()
save_and_sample_weights(global_step,'quit_step')
quit()
if mid_generation==True:
mid_train_playground(global_step)
mid_generation=False
if mid_checkpoint_step == True:
save_and_sample_weights(global_step,'step',save_model=True)
mid_checkpoint_step=False
mid_sample_step=False
elif mid_sample_step == True:
save_and_sample_weights(global_step,'step',save_model=False)
mid_sample_step=False
if global_step >= args.max_train_steps:
break
progress_bar_e.update(1)
if mid_quit==True:
accelerator.wait_for_everyone()
save_and_sample_weights(epoch,'quit_epoch')
quit()
if epoch == args.num_train_epochs - 1:
save_and_sample_weights(epoch,'epoch',True)
elif args.save_every_n_epoch and (epoch + 1) % args.save_every_n_epoch == 0:
save_and_sample_weights(epoch,'epoch',True)
elif mid_checkpoint==True:
save_and_sample_weights(epoch,'epoch',True)
mid_checkpoint=False
mid_sample=False
elif mid_sample==True:
save_and_sample_weights(epoch,'epoch',False)
mid_sample=False
accelerator.wait_for_everyone()
except Exception:
try:
send_telegram_message("Something went wrong while training! :(", args.telegram_chat_id, args.telegram_token)
#save_and_sample_weights(global_step,'checkpoint')
send_telegram_message(f"Saved checkpoint {global_step} on exit", args.telegram_chat_id, args.telegram_token)
except Exception:
pass
raise
except KeyboardInterrupt:
send_telegram_message("Training stopped", args.telegram_chat_id, args.telegram_token)
try:
send_telegram_message("Training finished!", args.telegram_chat_id, args.telegram_token)
except:
pass
accelerator.end_training()
if __name__ == "__main__":
main()
|