|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import argparse |
|
import gc |
|
import itertools |
|
import json |
|
import logging |
|
import math |
|
import os |
|
import random |
|
import shutil |
|
import warnings |
|
from contextlib import nullcontext |
|
from pathlib import Path |
|
|
|
import numpy as np |
|
import torch |
|
import torch.nn.functional as F |
|
import torch.utils.checkpoint |
|
import transformers |
|
from accelerate import Accelerator |
|
from accelerate.logging import get_logger |
|
from accelerate.utils import ( |
|
DistributedDataParallelKwargs, |
|
ProjectConfiguration, |
|
set_seed, |
|
) |
|
from huggingface_hub import create_repo, hf_hub_download, upload_folder |
|
from huggingface_hub.utils import insecure_hashlib |
|
from packaging import version |
|
from peft import LoraConfig, set_peft_model_state_dict |
|
from peft.utils import get_peft_model_state_dict |
|
from PIL import Image |
|
from PIL.ImageOps import exif_transpose |
|
from safetensors.torch import load_file, save_file |
|
from torch.utils.data import Dataset |
|
from torchvision import transforms |
|
from torchvision.transforms.functional import crop |
|
from tqdm.auto import tqdm |
|
from transformers import AutoTokenizer, PretrainedConfig |
|
|
|
import diffusers |
|
from diffusers import ( |
|
AutoencoderKL, |
|
DDPMScheduler, |
|
DPMSolverMultistepScheduler, |
|
EDMEulerScheduler, |
|
EulerDiscreteScheduler, |
|
StableDiffusionXLPipeline, |
|
UNet2DConditionModel, |
|
) |
|
from diffusers.loaders import LoraLoaderMixin |
|
from diffusers.optimization import get_scheduler |
|
from diffusers.training_utils import ( |
|
_set_state_dict_into_text_encoder, |
|
cast_training_params, |
|
compute_snr, |
|
) |
|
from diffusers.utils import ( |
|
check_min_version, |
|
convert_all_state_dict_to_peft, |
|
convert_state_dict_to_diffusers, |
|
convert_state_dict_to_kohya, |
|
convert_unet_state_dict_to_peft, |
|
is_wandb_available, |
|
) |
|
from diffusers.utils.hub_utils import load_or_create_model_card, populate_model_card |
|
from diffusers.utils.import_utils import is_xformers_available |
|
from diffusers.utils.torch_utils import is_compiled_module |
|
|
|
|
|
if is_wandb_available(): |
|
import wandb |
|
|
|
|
|
|
|
|
|
logger = get_logger(__name__) |
|
|
|
from utils import extend_mask_with_bezier, mask_paint2bbox |
|
|
|
|
|
def determine_scheduler_type(pretrained_model_name_or_path, revision): |
|
model_index_filename = "model_index.json" |
|
if os.path.isdir(pretrained_model_name_or_path): |
|
model_index = os.path.join(pretrained_model_name_or_path, model_index_filename) |
|
else: |
|
model_index = hf_hub_download( |
|
repo_id=pretrained_model_name_or_path, |
|
filename=model_index_filename, |
|
revision=revision, |
|
) |
|
|
|
with open(model_index, "r") as f: |
|
scheduler_type = json.load(f)["scheduler"][1] |
|
return scheduler_type |
|
|
|
|
|
def parse_args(input_args=None): |
|
parser = argparse.ArgumentParser(description="Simple example of a training script.") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
parser.add_argument( |
|
"--category", |
|
type=str, |
|
default="", |
|
help="the category of the image, for decompose", |
|
) |
|
parser.add_argument( |
|
"--regular_prob", |
|
type=float, |
|
default=0.5, |
|
help="probability of using regular image", |
|
) |
|
|
|
parser.add_argument( |
|
"--regular_dir", |
|
type=str, |
|
default=None, |
|
required=True, |
|
help="Path to regular img and mask", |
|
) |
|
|
|
parser.add_argument( |
|
"--loss_reweight_object", |
|
type=float, |
|
default=1, |
|
) |
|
|
|
parser.add_argument( |
|
"--loss_reweight_background", |
|
type=float, |
|
default=1, |
|
) |
|
|
|
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_model_name_or_path", |
|
type=str, |
|
default=None, |
|
help="Path to pretrained VAE model with better numerical stability. More details: https://github.com/huggingface/diffusers/pull/4038.", |
|
) |
|
parser.add_argument( |
|
"--revision", |
|
type=str, |
|
default=None, |
|
required=False, |
|
help="Revision of pretrained model identifier from huggingface.co/models.", |
|
) |
|
parser.add_argument( |
|
"--variant", |
|
type=str, |
|
default=None, |
|
help="Variant of the model files of the pretrained model identifier from huggingface.co/models, 'e.g.' fp16", |
|
) |
|
parser.add_argument( |
|
"--dataset_name", |
|
type=str, |
|
default=None, |
|
help=( |
|
"The name of the Dataset (from the HuggingFace hub) containing the training data of instance images (could be your own, possibly private," |
|
" dataset). It can also be a path pointing to a local copy of a dataset in your filesystem," |
|
" or to a folder containing files that π€ Datasets can understand." |
|
), |
|
) |
|
parser.add_argument( |
|
"--dataset_config_name", |
|
type=str, |
|
default=None, |
|
help="The config of the Dataset, leave as None if there's only one config.", |
|
) |
|
parser.add_argument( |
|
"--instance_data_dir", |
|
type=str, |
|
default=None, |
|
help=("A folder containing the training data. "), |
|
) |
|
|
|
parser.add_argument( |
|
"--cache_dir", |
|
type=str, |
|
default=None, |
|
help="The directory where the downloaded models and datasets will be stored.", |
|
) |
|
|
|
parser.add_argument( |
|
"--image_column", |
|
type=str, |
|
default="image", |
|
help="The column of the dataset containing the target image. By " |
|
"default, the standard Image Dataset maps out 'file_name' " |
|
"to 'image'.", |
|
) |
|
parser.add_argument( |
|
"--caption_column", |
|
type=str, |
|
default=None, |
|
help="The column of the dataset containing the instance prompt for each image", |
|
) |
|
|
|
parser.add_argument( |
|
"--repeats", |
|
type=int, |
|
default=1, |
|
help="How many times to repeat the training data.", |
|
) |
|
|
|
parser.add_argument( |
|
"--class_data_dir", |
|
type=str, |
|
default=None, |
|
required=False, |
|
help="A folder containing the training data of class images.", |
|
) |
|
parser.add_argument( |
|
"--instance_prompt", |
|
type=str, |
|
default=None, |
|
required=True, |
|
help="The prompt with identifier specifying the instance, e.g. 'photo of a TOK dog', 'in the style of TOK'", |
|
) |
|
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( |
|
"--validation_prompt", |
|
type=str, |
|
default=None, |
|
help="A prompt that is used during validation to verify that the model is learning.", |
|
) |
|
parser.add_argument( |
|
"--num_validation_images", |
|
type=int, |
|
default=4, |
|
help="Number of images that should be generated during validation with `validation_prompt`.", |
|
) |
|
parser.add_argument( |
|
"--validation_epochs", |
|
type=int, |
|
default=50, |
|
help=( |
|
"Run dreambooth validation every X epochs. Dreambooth validation consists of running the prompt" |
|
" `args.validation_prompt` multiple times: `args.num_validation_images`." |
|
), |
|
) |
|
parser.add_argument( |
|
"--do_edm_style_training", |
|
default=False, |
|
action="store_true", |
|
help="Flag to conduct training using the EDM formulation as introduced in https://arxiv.org/abs/2206.00364.", |
|
) |
|
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( |
|
"--num_class_images", |
|
type=int, |
|
default=100, |
|
help=( |
|
"Minimal class images for prior preservation loss. If there are not enough images already present in" |
|
" class_data_dir, additional images will be sampled with class_prompt." |
|
), |
|
) |
|
parser.add_argument( |
|
"--output_dir", |
|
type=str, |
|
default="lora-dreambooth-model", |
|
help="The output directory where the model predictions and checkpoints will be written.", |
|
) |
|
parser.add_argument( |
|
"--output_kohya_format", |
|
action="store_true", |
|
help="Flag to additionally generate final state dict in the Kohya format so that it becomes compatible with A111, Comfy, Kohya, etc.", |
|
) |
|
parser.add_argument( |
|
"--seed", type=int, default=None, help="A seed for reproducible training." |
|
) |
|
parser.add_argument( |
|
"--resolution", |
|
type=int, |
|
default=1024, |
|
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 the input images to the resolution. If not set, the images will be randomly" |
|
" cropped. The images will be resized to the resolution first before cropping." |
|
), |
|
) |
|
parser.add_argument( |
|
"--random_flip", |
|
action="store_true", |
|
help="whether to randomly flip images horizontally", |
|
) |
|
parser.add_argument( |
|
"--train_text_encoder", |
|
action="store_true", |
|
help="Whether to train the text encoder. If set, the text encoder should be float32 precision.", |
|
) |
|
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( |
|
"--checkpointing_steps", |
|
type=int, |
|
default=500, |
|
help=( |
|
"Save a checkpoint of the training state every X updates. These checkpoints can be used both as final" |
|
" checkpoints in case they are better than the last checkpoint, and are also suitable for resuming" |
|
" training using `--resume_from_checkpoint`." |
|
), |
|
) |
|
parser.add_argument( |
|
"--checkpoints_total_limit", |
|
type=int, |
|
default=None, |
|
help=("Max number of checkpoints to store."), |
|
) |
|
parser.add_argument( |
|
"--resume_from_checkpoint", |
|
type=str, |
|
default=None, |
|
help=( |
|
"Whether training should be resumed from a previous checkpoint. Use a path saved by" |
|
' `--checkpointing_steps`, or `"latest"` to automatically select the last available checkpoint.' |
|
), |
|
) |
|
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", |
|
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=1e-4, |
|
help="Initial learning rate (after the potential warmup period) to use.", |
|
) |
|
|
|
parser.add_argument( |
|
"--text_encoder_lr", |
|
type=float, |
|
default=5e-6, |
|
help="Text encoder learning rate 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( |
|
"--snr_gamma", |
|
type=float, |
|
default=None, |
|
help="SNR weighting gamma to be used if rebalancing the loss. Recommended value is 5.0. " |
|
"More details here: https://arxiv.org/abs/2303.09556.", |
|
) |
|
parser.add_argument( |
|
"--lr_warmup_steps", |
|
type=int, |
|
default=500, |
|
help="Number of steps for the warmup in the lr scheduler.", |
|
) |
|
parser.add_argument( |
|
"--lr_num_cycles", |
|
type=int, |
|
default=1, |
|
help="Number of hard resets of the lr in cosine_with_restarts scheduler.", |
|
) |
|
parser.add_argument( |
|
"--lr_power", |
|
type=float, |
|
default=1.0, |
|
help="Power factor of the polynomial scheduler.", |
|
) |
|
parser.add_argument( |
|
"--dataloader_num_workers", |
|
type=int, |
|
default=0, |
|
help=( |
|
"Number of subprocesses to use for data loading. 0 means that the data will be loaded in the main process." |
|
), |
|
) |
|
|
|
parser.add_argument( |
|
"--optimizer", |
|
type=str, |
|
default="AdamW", |
|
help=('The optimizer type to use. Choose between ["AdamW", "prodigy"]'), |
|
) |
|
|
|
parser.add_argument( |
|
"--use_8bit_adam", |
|
action="store_true", |
|
help="Whether or not to use 8-bit Adam from bitsandbytes. Ignored if optimizer is not set to AdamW", |
|
) |
|
|
|
parser.add_argument( |
|
"--adam_beta1", |
|
type=float, |
|
default=0.9, |
|
help="The beta1 parameter for the Adam and Prodigy optimizers.", |
|
) |
|
parser.add_argument( |
|
"--adam_beta2", |
|
type=float, |
|
default=0.999, |
|
help="The beta2 parameter for the Adam and Prodigy optimizers.", |
|
) |
|
parser.add_argument( |
|
"--prodigy_beta3", |
|
type=float, |
|
default=None, |
|
help="coefficients for computing the Prodidy stepsize using running averages. If set to None, " |
|
"uses the value of square root of beta2. Ignored if optimizer is adamW", |
|
) |
|
parser.add_argument( |
|
"--prodigy_decouple", |
|
type=bool, |
|
default=True, |
|
help="Use AdamW style decoupled weight decay", |
|
) |
|
parser.add_argument( |
|
"--adam_weight_decay", |
|
type=float, |
|
default=1e-04, |
|
help="Weight decay to use for unet params", |
|
) |
|
parser.add_argument( |
|
"--adam_weight_decay_text_encoder", |
|
type=float, |
|
default=1e-03, |
|
help="Weight decay to use for text_encoder", |
|
) |
|
|
|
parser.add_argument( |
|
"--adam_epsilon", |
|
type=float, |
|
default=1e-08, |
|
help="Epsilon value for the Adam optimizer and Prodigy optimizers.", |
|
) |
|
|
|
parser.add_argument( |
|
"--prodigy_use_bias_correction", |
|
type=bool, |
|
default=True, |
|
help="Turn on Adam's bias correction. True by default. Ignored if optimizer is adamW", |
|
) |
|
parser.add_argument( |
|
"--prodigy_safeguard_warmup", |
|
type=bool, |
|
default=True, |
|
help="Remove lr from the denominator of D estimate to avoid issues during warm-up stage. True by default. " |
|
"Ignored if optimizer is adamW", |
|
) |
|
parser.add_argument( |
|
"--max_grad_norm", default=1.0, type=float, help="Max gradient norm." |
|
) |
|
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( |
|
"--allow_tf32", |
|
action="store_true", |
|
help=( |
|
"Whether or not to allow TF32 on Ampere GPUs. Can be used to speed up training. For more information, see" |
|
" https://pytorch.org/docs/stable/notes/cuda.html#tensorfloat-32-tf32-on-ampere-devices" |
|
), |
|
) |
|
parser.add_argument( |
|
"--report_to", |
|
type=str, |
|
default="tensorboard", |
|
help=( |
|
'The integration to report the results and logs to. Supported platforms are `"tensorboard"`' |
|
' (default), `"wandb"` and `"comet_ml"`. Use `"all"` to report to all integrations.' |
|
), |
|
) |
|
parser.add_argument( |
|
"--mixed_precision", |
|
type=str, |
|
default=None, |
|
choices=["no", "fp16", "bf16"], |
|
help=( |
|
"Whether to use mixed precision. Choose between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >=" |
|
" 1.10.and an Nvidia Ampere GPU. Default to the value of accelerate config of the current system or the" |
|
" flag passed with the `accelerate.launch` command. Use this argument to override the accelerate config." |
|
), |
|
) |
|
parser.add_argument( |
|
"--prior_generation_precision", |
|
type=str, |
|
default=None, |
|
choices=["no", "fp32", "fp16", "bf16"], |
|
help=( |
|
"Choose prior generation precision between fp32, fp16 and bf16 (bfloat16). Bf16 requires PyTorch >=" |
|
" 1.10.and an Nvidia Ampere GPU. Default to fp16 if a GPU is available else fp32." |
|
), |
|
) |
|
parser.add_argument( |
|
"--local_rank", |
|
type=int, |
|
default=-1, |
|
help="For distributed training: local_rank", |
|
) |
|
parser.add_argument( |
|
"--enable_xformers_memory_efficient_attention", |
|
action="store_true", |
|
help="Whether or not to use xformers.", |
|
) |
|
parser.add_argument( |
|
"--rank", |
|
type=int, |
|
default=4, |
|
help=("The dimension of the LoRA update matrices."), |
|
) |
|
parser.add_argument( |
|
"--use_dora", |
|
action="store_true", |
|
default=False, |
|
help=( |
|
"Wether to train a DoRA as proposed in- DoRA: Weight-Decomposed Low-Rank Adaptation https://arxiv.org/abs/2402.09353. " |
|
"Note: to use DoRA you need to install peft from main, `pip install git+https://github.com/huggingface/peft.git`" |
|
), |
|
) |
|
|
|
if input_args is not None: |
|
args = parser.parse_args(input_args) |
|
else: |
|
args = parser.parse_args() |
|
|
|
if args.dataset_name is None and args.instance_data_dir is None: |
|
raise ValueError("Specify either `--dataset_name` or `--instance_data_dir`") |
|
|
|
if args.dataset_name is not None and args.instance_data_dir is not None: |
|
raise ValueError( |
|
"Specify only one of `--dataset_name` or `--instance_data_dir`" |
|
) |
|
|
|
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 |
|
|
|
if args.with_prior_preservation: |
|
if args.class_data_dir is None: |
|
raise ValueError("You must specify a data directory for class images.") |
|
if args.class_prompt is None: |
|
raise ValueError("You must specify prompt for class images.") |
|
else: |
|
|
|
if args.class_data_dir is not None: |
|
warnings.warn( |
|
"You need not use --class_data_dir without --with_prior_preservation." |
|
) |
|
if args.class_prompt is not None: |
|
warnings.warn( |
|
"You need not use --class_prompt without --with_prior_preservation." |
|
) |
|
|
|
return args |
|
|
|
|
|
class DreamBoothDataset(Dataset): |
|
""" |
|
A dataset to prepare the instance and class images with the prompts for fine-tuning the model. |
|
It pre-processes the images. |
|
""" |
|
|
|
def __init__( |
|
self, |
|
instance_data_root, |
|
instance_prompt, |
|
class_prompt, |
|
class_data_root=None, |
|
class_num=None, |
|
size=1024, |
|
repeats=1, |
|
center_crop=False, |
|
): |
|
self.size = size |
|
self.center_crop = center_crop |
|
|
|
self.instance_prompt = instance_prompt |
|
self.custom_instance_prompts = None |
|
self.class_prompt = class_prompt |
|
|
|
|
|
import modules.inpaint_worker as inpaint_worker |
|
from utils.add_fooocus_inpaint_head_patch import load_inpaint_head |
|
|
|
global pipe |
|
_device, _dtype = pipe.vae.device, pipe.vae.dtype |
|
inpaint_head_model = load_inpaint_head( |
|
"./models/fooocus_inpaint/fooocus_inpaint_head.pth", _device |
|
).to(device=_device, dtype=_dtype) |
|
|
|
print("Processing image and mask to latent") |
|
|
|
train_resize = transforms.Resize( |
|
size, interpolation=transforms.InterpolationMode.BILINEAR |
|
) |
|
train_crop = ( |
|
transforms.CenterCrop(size) if center_crop else transforms.RandomCrop(size) |
|
) |
|
train_flip = transforms.RandomHorizontalFlip(p=1.0) |
|
train_transforms = transforms.Compose( |
|
[ |
|
transforms.ToTensor(), |
|
transforms.Normalize([0.5], [0.5]), |
|
] |
|
) |
|
|
|
INSTANCE_REPEAT_NUM = 10 |
|
REGULAR_REPEAT_NUM = 3 |
|
USE_BBOX_MASK = False |
|
|
|
|
|
|
|
|
|
self.original_sizes = [] |
|
self.crop_top_lefts = [] |
|
self.pixel_values = [] |
|
|
|
if True: |
|
self.instance_data_root = Path(instance_data_root) |
|
if not self.instance_data_root.exists(): |
|
raise ValueError("Instance images root doesn't exists.") |
|
|
|
self.instance_images = [] |
|
self.instance_masks = [] |
|
|
|
|
|
for path in list(Path(instance_data_root).iterdir()): |
|
image = Image.open(path) |
|
mask_path = str(path).replace("/image", "/mask").replace(".jpg", ".png") |
|
if not Path(mask_path).exists(): |
|
print(f"Warning! mask of image {path} not exist! Ignore..") |
|
continue |
|
mask_img = Image.open(mask_path).resize(image.size) |
|
self.instance_images.extend(itertools.repeat(image, repeats)) |
|
self.instance_masks.extend(itertools.repeat(mask_img, repeats)) |
|
|
|
|
|
with torch.no_grad(): |
|
for _ in tqdm( |
|
range(INSTANCE_REPEAT_NUM) |
|
): |
|
for image, mask in zip(self.instance_images, self.instance_masks): |
|
|
|
image = exif_transpose(image) |
|
mask = exif_transpose(mask) |
|
if not image.mode == "RGB": |
|
image = image.convert("RGB") |
|
|
|
self.original_sizes.append((image.height, image.width)) |
|
image = train_resize(image) |
|
mask = train_resize(mask) |
|
if args.random_flip and random.random() < 0.5: |
|
|
|
image = train_flip(image) |
|
mask = train_flip(mask) |
|
if args.center_crop: |
|
y1 = max( |
|
0, int(round((image.height - args.resolution) / 2.0)) |
|
) |
|
x1 = max( |
|
0, int(round((image.width - args.resolution) / 2.0)) |
|
) |
|
image = train_crop(image) |
|
mask = train_crop(mask) |
|
else: |
|
y1, x1, h, w = train_crop.get_params( |
|
image, (args.resolution, args.resolution) |
|
) |
|
image = crop(image, y1, x1, h, w) |
|
mask = crop(mask, y1, x1, h, w) |
|
crop_top_left = (y1, x1) |
|
self.crop_top_lefts.append(crop_top_left) |
|
|
|
mask = extend_mask_with_bezier( |
|
( |
|
np.asarray(mask)[:, :, 0] |
|
if mask.mode == "RGB" |
|
else np.asarray(mask) |
|
), |
|
extend_ratio=random.randint(1, 10) / 10.0, |
|
random_width=random.randint(1, 20), |
|
) |
|
|
|
if USE_BBOX_MASK: |
|
mask = mask_paint2bbox(mask) |
|
|
|
|
|
mask = Image.fromarray(mask) |
|
|
|
for interesting_are in [1]: |
|
inpaint_work = inpaint_worker.InpaintWorker( |
|
image=np.asarray(image), |
|
mask=( |
|
np.asarray(mask)[:, :, 0] |
|
if mask.mode == "RGB" |
|
else np.asarray(mask) |
|
), |
|
use_fill=True, |
|
k=interesting_are, |
|
path_upscale_models="./models/upscale_models/fooocus_upscaler_s409985e5.bin", |
|
) |
|
|
|
pixel_fill = pipe.image_processor.preprocess( |
|
Image.fromarray(inpaint_work.interested_fill) |
|
) |
|
pixel_image = pipe.image_processor.preprocess( |
|
Image.fromarray(inpaint_work.interested_image) |
|
) |
|
pixel_mask = pipe.mask_processor.preprocess( |
|
Image.fromarray(inpaint_work.interested_mask) |
|
) |
|
|
|
pixel_fill = pixel_fill.to(device=_device, dtype=_dtype) |
|
pixel_image = pixel_image.to(device=_device, dtype=_dtype) |
|
pixel_mask = pixel_mask.to( |
|
device=_device, dtype=torch.float32 |
|
) |
|
|
|
masked_image = pixel_image * (pixel_mask < 0.5) |
|
latent_fill = pipe._encode_vae_image( |
|
image=pixel_fill, generator=None |
|
) |
|
latent_masked_image = pipe._encode_vae_image( |
|
image=masked_image, generator=None |
|
) |
|
latent_image = pipe._encode_vae_image( |
|
image=pixel_image, generator=None |
|
) |
|
B, C, H, W = latent_fill.shape |
|
latent_mask = torch.nn.functional.interpolate( |
|
pixel_mask, size=(H * 8, W * 8), mode="bilinear" |
|
).round() |
|
latent_mask = ( |
|
torch.nn.functional.max_pool2d(latent_mask, (8, 8)) |
|
.round() |
|
.to(latent_fill.device) |
|
) |
|
|
|
feed = torch.cat( |
|
[latent_mask, latent_masked_image], |
|
dim=1, |
|
) |
|
inpaint_head_feature = inpaint_head_model(feed).to("cpu") |
|
|
|
|
|
self.pixel_values.append( |
|
( |
|
inpaint_head_feature, |
|
latent_image, |
|
latent_mask, |
|
"instance", |
|
) |
|
) |
|
|
|
self.num_instance_images = len(self.pixel_values) |
|
|
|
|
|
global regular_prompts_list |
|
print("Processing regular image and mask to latent") |
|
regular_data_root = args.regular_dir |
|
regular_prompts_list = [name for name in os.listdir(regular_data_root)] |
|
|
|
if args.regular_prob > 0: |
|
with torch.no_grad(): |
|
|
|
for prompt in tqdm(regular_prompts_list): |
|
all_images_and_masks = os.listdir( |
|
os.path.join(regular_data_root, prompt) |
|
) |
|
files_name = set( |
|
[name.split("-")[0] for name in all_images_and_masks] |
|
) |
|
|
|
for file_name in tqdm(files_name): |
|
_image = Image.open( |
|
f"{regular_data_root}/{prompt}/{file_name}-image.png" |
|
) |
|
_mask = Image.open( |
|
f"{regular_data_root}/{prompt}/{file_name}-mask.png" |
|
) |
|
for _ in range(REGULAR_REPEAT_NUM): |
|
image = exif_transpose(_image) |
|
mask = exif_transpose(_mask) |
|
if not image.mode == "RGB": |
|
image = image.convert("RGB") |
|
|
|
self.original_sizes.append((image.height, image.width)) |
|
image = train_resize(image) |
|
mask = train_resize(mask) |
|
if args.random_flip and random.random() < 0.5: |
|
|
|
image = train_flip(image) |
|
mask = train_flip(mask) |
|
if args.center_crop: |
|
y1 = max( |
|
0, |
|
int( |
|
round( |
|
(image.height - args.resolution) / 2.0 |
|
) |
|
), |
|
) |
|
x1 = max( |
|
0, |
|
int( |
|
round((image.width - args.resolution) / 2.0) |
|
), |
|
) |
|
image = train_crop(image) |
|
mask = train_crop(mask) |
|
else: |
|
y1, x1, h, w = train_crop.get_params( |
|
image, (args.resolution, args.resolution) |
|
) |
|
image = crop(image, y1, x1, h, w) |
|
mask = crop(mask, y1, x1, h, w) |
|
crop_top_left = (y1, x1) |
|
self.crop_top_lefts.append(crop_top_left) |
|
|
|
mask = extend_mask_with_bezier( |
|
( |
|
np.asarray(mask)[:, :, 0] |
|
if mask.mode == "RGB" |
|
else np.asarray(mask) |
|
), |
|
extend_ratio=random.randint(1, 10) / 10.0, |
|
random_width=random.randint(1, 20), |
|
) |
|
|
|
if USE_BBOX_MASK: |
|
mask = mask_paint2bbox(mask) |
|
|
|
|
|
mask = Image.fromarray(mask) |
|
|
|
interesting_are = 1 |
|
inpaint_work = inpaint_worker.InpaintWorker( |
|
image=np.asarray(image), |
|
mask=( |
|
np.asarray(mask)[:, :, 0] |
|
if mask.mode == "RGB" |
|
else np.asarray(mask) |
|
), |
|
use_fill=True, |
|
k=interesting_are, |
|
path_upscale_models="./models/upscale_models/fooocus_upscaler_s409985e5.bin", |
|
) |
|
|
|
pixel_fill = pipe.image_processor.preprocess( |
|
Image.fromarray(inpaint_work.interested_fill) |
|
) |
|
pixel_image = pipe.image_processor.preprocess( |
|
Image.fromarray(inpaint_work.interested_image) |
|
) |
|
pixel_mask = pipe.mask_processor.preprocess( |
|
Image.fromarray(inpaint_work.interested_mask) |
|
) |
|
|
|
pixel_fill = pixel_fill.to(device=_device, dtype=_dtype) |
|
pixel_image = pixel_image.to( |
|
device=_device, dtype=_dtype |
|
) |
|
pixel_mask = pixel_mask.to( |
|
device=_device, dtype=torch.float32 |
|
) |
|
|
|
masked_image = pixel_image * (pixel_mask < 0.5) |
|
latent_fill = pipe._encode_vae_image( |
|
image=pixel_fill, generator=None |
|
) |
|
latent_masked_image = pipe._encode_vae_image( |
|
image=masked_image, generator=None |
|
) |
|
latent_image = pipe._encode_vae_image( |
|
image=pixel_image, generator=None |
|
) |
|
B, C, H, W = latent_fill.shape |
|
latent_mask = torch.nn.functional.interpolate( |
|
pixel_mask, size=(H * 8, W * 8), mode="bilinear" |
|
).round() |
|
latent_mask = ( |
|
torch.nn.functional.max_pool2d(latent_mask, (8, 8)) |
|
.round() |
|
.to(latent_fill.device) |
|
) |
|
|
|
feed = torch.cat( |
|
[latent_mask, latent_masked_image], |
|
dim=1, |
|
) |
|
inpaint_head_feature = inpaint_head_model(feed).to( |
|
"cpu" |
|
) |
|
|
|
self.pixel_values.append( |
|
( |
|
inpaint_head_feature, |
|
latent_image, |
|
latent_mask, |
|
prompt, |
|
) |
|
) |
|
|
|
inpaint_head_model.to("cpu") |
|
del inpaint_head_model |
|
if torch.cuda.is_available(): |
|
torch.cuda.empty_cache() |
|
|
|
self.num_all_images = len(self.pixel_values) |
|
self.num_regular_images = self.num_all_images - self.num_instance_images |
|
self._length = self.num_all_images |
|
|
|
self.class_data_root = None |
|
|
|
def __len__(self): |
|
return self._length |
|
|
|
def __getitem__(self, index): |
|
example = {} |
|
|
|
self.num_regular_images |
|
self.num_all_images |
|
self.num_instance_images |
|
|
|
regular_prob = args.regular_prob |
|
if random.random() < regular_prob and self.num_regular_images > 0: |
|
img_id = self.num_instance_images + index % self.num_regular_images |
|
else: |
|
img_id = index % self.num_instance_images |
|
|
|
instance_image = self.pixel_values[img_id] |
|
original_size = self.original_sizes[img_id] |
|
crop_top_left = self.crop_top_lefts[img_id] |
|
|
|
example["instance_images"] = instance_image |
|
example["original_size"] = original_size |
|
example["crop_top_left"] = crop_top_left |
|
|
|
example["instance_prompt"] = self.instance_prompt |
|
|
|
return example |
|
|
|
|
|
def collate_fn(examples, with_prior_preservation=False): |
|
pixel_values = [example["instance_images"] for example in examples] |
|
prompts = [example["instance_prompt"] for example in examples] |
|
original_sizes = [example["original_size"] for example in examples] |
|
crop_top_lefts = [example["crop_top_left"] for example in examples] |
|
|
|
|
|
|
|
if with_prior_preservation: |
|
pixel_values += [example["class_images"] for example in examples] |
|
prompts += [example["class_prompt"] for example in examples] |
|
original_sizes += [example["original_size"] for example in examples] |
|
crop_top_lefts += [example["crop_top_left"] for example in examples] |
|
|
|
|
|
|
|
|
|
batch = { |
|
"pixel_values": pixel_values, |
|
"prompts": prompts, |
|
"original_sizes": original_sizes, |
|
"crop_top_lefts": crop_top_lefts, |
|
} |
|
return batch |
|
|
|
|
|
class PromptDataset(Dataset): |
|
"A simple dataset to prepare the prompts to generate class images on multiple GPUs." |
|
|
|
def __init__(self, prompt, num_samples): |
|
self.prompt = prompt |
|
self.num_samples = num_samples |
|
|
|
def __len__(self): |
|
return self.num_samples |
|
|
|
def __getitem__(self, index): |
|
example = {} |
|
example["prompt"] = self.prompt |
|
example["index"] = index |
|
return example |
|
|
|
|
|
def tokenize_prompt(tokenizer, prompt): |
|
text_inputs = tokenizer( |
|
prompt, |
|
padding="max_length", |
|
max_length=tokenizer.model_max_length, |
|
truncation=True, |
|
return_tensors="pt", |
|
) |
|
text_input_ids = text_inputs.input_ids |
|
return text_input_ids |
|
|
|
|
|
|
|
def encode_prompt(text_encoders, tokenizers, prompt, text_input_ids_list=None): |
|
prompt_embeds_list = [] |
|
|
|
for i, text_encoder in enumerate(text_encoders): |
|
if tokenizers is not None: |
|
tokenizer = tokenizers[i] |
|
text_input_ids = tokenize_prompt(tokenizer, prompt) |
|
else: |
|
assert text_input_ids_list is not None |
|
text_input_ids = text_input_ids_list[i] |
|
|
|
prompt_embeds = text_encoder( |
|
text_input_ids.to(text_encoder.device), |
|
output_hidden_states=True, |
|
return_dict=False, |
|
) |
|
|
|
|
|
pooled_prompt_embeds = prompt_embeds[0] |
|
prompt_embeds = prompt_embeds[-1][-2] |
|
bs_embed, seq_len, _ = prompt_embeds.shape |
|
prompt_embeds = prompt_embeds.view(bs_embed, seq_len, -1) |
|
prompt_embeds_list.append(prompt_embeds) |
|
|
|
prompt_embeds = torch.concat(prompt_embeds_list, dim=-1) |
|
pooled_prompt_embeds = pooled_prompt_embeds.view(bs_embed, -1) |
|
return prompt_embeds, pooled_prompt_embeds |
|
|
|
|
|
def main(args): |
|
if args.report_to == "wandb" and args.hub_token is not None: |
|
raise ValueError( |
|
"You cannot use both --report_to=wandb and --hub_token due to a security risk of exposing your token." |
|
" Please use `huggingface-cli login` to authenticate with the Hub." |
|
) |
|
|
|
if args.do_edm_style_training and args.snr_gamma is not None: |
|
raise ValueError( |
|
"Min-SNR formulation is not supported when conducting EDM-style training." |
|
) |
|
|
|
if torch.backends.mps.is_available() and args.mixed_precision == "bf16": |
|
|
|
raise ValueError( |
|
"Mixed precision training with bfloat16 is not supported on MPS. Please use fp16 (recommended) or fp32 instead." |
|
) |
|
|
|
logging_dir = Path(args.output_dir, args.logging_dir) |
|
|
|
accelerator_project_config = ProjectConfiguration( |
|
project_dir=args.output_dir, logging_dir=logging_dir |
|
) |
|
kwargs = DistributedDataParallelKwargs(find_unused_parameters=True) |
|
accelerator = Accelerator( |
|
gradient_accumulation_steps=args.gradient_accumulation_steps, |
|
mixed_precision=args.mixed_precision, |
|
log_with=args.report_to, |
|
project_config=accelerator_project_config, |
|
kwargs_handlers=[kwargs], |
|
) |
|
|
|
|
|
if torch.backends.mps.is_available(): |
|
accelerator.native_amp = False |
|
|
|
if args.report_to == "wandb": |
|
if not is_wandb_available(): |
|
raise ImportError( |
|
"Make sure to install wandb if you want to use it for logging during training." |
|
) |
|
|
|
|
|
logging.basicConfig( |
|
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s", |
|
datefmt="%m/%d/%Y %H:%M:%S", |
|
level=logging.INFO, |
|
) |
|
logger.info(accelerator.state, main_process_only=False) |
|
if accelerator.is_local_main_process: |
|
transformers.utils.logging.set_verbosity_warning() |
|
diffusers.utils.logging.set_verbosity_info() |
|
else: |
|
transformers.utils.logging.set_verbosity_error() |
|
diffusers.utils.logging.set_verbosity_error() |
|
|
|
|
|
if args.seed is not None: |
|
set_seed(args.seed) |
|
|
|
|
|
if accelerator.is_main_process: |
|
if args.output_dir is not None: |
|
os.makedirs(args.output_dir, exist_ok=True) |
|
|
|
print("Loading sdxl pipeline") |
|
from FooocusSDXLInpaintAllInOnePipeline import FooocusSDXLInpaintPipeline |
|
|
|
global pipe |
|
pipe = FooocusSDXLInpaintPipeline.from_pretrained( |
|
"frankjoshua/juggernautXL_v8Rundiffusion", |
|
|
|
|
|
use_safetensors=True, |
|
).to("cuda") |
|
|
|
|
|
|
|
print("Loading fooocus unet") |
|
|
|
pipe.only_load_fooocus_unet_and_cover_pipe_unet_for_train( |
|
fooocus_model_path="./models/fooocus_inpaint/inpaint_v26.fooocus.patch", |
|
) |
|
|
|
|
|
tokenizer_one = pipe.tokenizer |
|
tokenizer_two = pipe.tokenizer_2 |
|
|
|
|
|
scheduler_type = determine_scheduler_type( |
|
args.pretrained_model_name_or_path, args.revision |
|
) |
|
if "EDM" in scheduler_type: |
|
args.do_edm_style_training = True |
|
noise_scheduler = EDMEulerScheduler.from_pretrained( |
|
args.pretrained_model_name_or_path, subfolder="scheduler" |
|
) |
|
logger.info("Performing EDM-style training!") |
|
elif args.do_edm_style_training: |
|
noise_scheduler = EulerDiscreteScheduler.from_pretrained( |
|
args.pretrained_model_name_or_path, subfolder="scheduler" |
|
) |
|
logger.info("Performing EDM-style training!") |
|
else: |
|
noise_scheduler = DDPMScheduler.from_pretrained( |
|
args.pretrained_model_name_or_path, subfolder="scheduler" |
|
) |
|
|
|
text_encoder_one = pipe.text_encoder |
|
|
|
text_encoder_two = pipe.text_encoder_2 |
|
|
|
vae = pipe.vae |
|
|
|
latents_mean = latents_std = None |
|
if hasattr(vae.config, "latents_mean") and vae.config.latents_mean is not None: |
|
latents_mean = torch.tensor(vae.config.latents_mean).view(1, 4, 1, 1) |
|
if hasattr(vae.config, "latents_std") and vae.config.latents_std is not None: |
|
latents_std = torch.tensor(vae.config.latents_std).view(1, 4, 1, 1) |
|
|
|
unet = pipe.unet |
|
|
|
|
|
vae.requires_grad_(False) |
|
text_encoder_one.requires_grad_(False) |
|
text_encoder_two.requires_grad_(False) |
|
unet.requires_grad_(False) |
|
|
|
|
|
|
|
weight_dtype = torch.float32 |
|
if accelerator.mixed_precision == "fp16": |
|
weight_dtype = torch.float16 |
|
elif accelerator.mixed_precision == "bf16": |
|
weight_dtype = torch.bfloat16 |
|
|
|
if torch.backends.mps.is_available() and weight_dtype == torch.bfloat16: |
|
|
|
raise ValueError( |
|
"Mixed precision training with bfloat16 is not supported on MPS. Please use fp16 (recommended) or fp32 instead." |
|
) |
|
|
|
|
|
unet.to(accelerator.device, dtype=weight_dtype) |
|
|
|
|
|
vae.to(accelerator.device, dtype=torch.float32) |
|
|
|
text_encoder_one.to(accelerator.device, dtype=weight_dtype) |
|
text_encoder_two.to(accelerator.device, dtype=weight_dtype) |
|
|
|
if args.enable_xformers_memory_efficient_attention: |
|
if is_xformers_available(): |
|
import xformers |
|
|
|
xformers_version = version.parse(xformers.__version__) |
|
if xformers_version == version.parse("0.0.16"): |
|
logger.warning( |
|
"xFormers 0.0.16 cannot be used for training in some GPUs. If you observe problems during training, " |
|
"please update xFormers to at least 0.0.17. See https://huggingface.co/docs/diffusers/main/en/optimization/xformers for more details." |
|
) |
|
unet.enable_xformers_memory_efficient_attention() |
|
else: |
|
raise ValueError( |
|
"xformers is not available. Make sure it is installed correctly" |
|
) |
|
|
|
if args.gradient_checkpointing: |
|
unet.enable_gradient_checkpointing() |
|
if args.train_text_encoder: |
|
text_encoder_one.gradient_checkpointing_enable() |
|
text_encoder_two.gradient_checkpointing_enable() |
|
|
|
|
|
unet_lora_config = LoraConfig( |
|
r=args.rank, |
|
use_dora=args.use_dora, |
|
lora_alpha=args.rank, |
|
init_lora_weights="gaussian", |
|
target_modules=[ |
|
"to_k", |
|
|
|
"to_v", |
|
"to_out.0", |
|
|
|
|
|
|
|
|
|
], |
|
) |
|
unet.add_adapter(unet_lora_config) |
|
|
|
|
|
|
|
|
|
def unwrap_model(model): |
|
model = accelerator.unwrap_model(model) |
|
model = model._orig_mod if is_compiled_module(model) else model |
|
return model |
|
|
|
|
|
def save_model_hook(models, weights, output_dir): |
|
if accelerator.is_main_process: |
|
|
|
|
|
unet_lora_layers_to_save = None |
|
text_encoder_one_lora_layers_to_save = None |
|
text_encoder_two_lora_layers_to_save = None |
|
|
|
for model in models: |
|
if isinstance(model, type(unwrap_model(unet))): |
|
unet_lora_layers_to_save = convert_state_dict_to_diffusers( |
|
get_peft_model_state_dict(model) |
|
) |
|
elif isinstance(model, type(unwrap_model(text_encoder_one))): |
|
text_encoder_one_lora_layers_to_save = ( |
|
convert_state_dict_to_diffusers( |
|
get_peft_model_state_dict(model) |
|
) |
|
) |
|
elif isinstance(model, type(unwrap_model(text_encoder_two))): |
|
text_encoder_two_lora_layers_to_save = ( |
|
convert_state_dict_to_diffusers( |
|
get_peft_model_state_dict(model) |
|
) |
|
) |
|
else: |
|
raise ValueError(f"unexpected save model: {model.__class__}") |
|
|
|
|
|
weights.pop() |
|
|
|
StableDiffusionXLPipeline.save_lora_weights( |
|
output_dir, |
|
unet_lora_layers=unet_lora_layers_to_save, |
|
text_encoder_lora_layers=text_encoder_one_lora_layers_to_save, |
|
text_encoder_2_lora_layers=text_encoder_two_lora_layers_to_save, |
|
) |
|
|
|
def load_model_hook(models, input_dir): |
|
unet_ = None |
|
text_encoder_one_ = None |
|
text_encoder_two_ = None |
|
|
|
while len(models) > 0: |
|
model = models.pop() |
|
|
|
if isinstance(model, type(unwrap_model(unet))): |
|
unet_ = model |
|
elif isinstance(model, type(unwrap_model(text_encoder_one))): |
|
text_encoder_one_ = model |
|
elif isinstance(model, type(unwrap_model(text_encoder_two))): |
|
text_encoder_two_ = model |
|
else: |
|
raise ValueError(f"unexpected save model: {model.__class__}") |
|
|
|
lora_state_dict, network_alphas = LoraLoaderMixin.lora_state_dict(input_dir) |
|
|
|
unet_state_dict = { |
|
f'{k.replace("unet.", "")}': v |
|
for k, v in lora_state_dict.items() |
|
if k.startswith("unet.") |
|
} |
|
unet_state_dict = convert_unet_state_dict_to_peft(unet_state_dict) |
|
incompatible_keys = set_peft_model_state_dict( |
|
unet_, unet_state_dict, adapter_name="default" |
|
) |
|
if incompatible_keys is not None: |
|
|
|
unexpected_keys = getattr(incompatible_keys, "unexpected_keys", None) |
|
if unexpected_keys: |
|
logger.warning( |
|
f"Loading adapter weights from state_dict led to unexpected keys not found in the model: " |
|
f" {unexpected_keys}. " |
|
) |
|
|
|
if args.train_text_encoder: |
|
|
|
_set_state_dict_into_text_encoder( |
|
lora_state_dict, prefix="text_encoder.", text_encoder=text_encoder_one_ |
|
) |
|
|
|
_set_state_dict_into_text_encoder( |
|
lora_state_dict, |
|
prefix="text_encoder_2.", |
|
text_encoder=text_encoder_two_, |
|
) |
|
|
|
|
|
|
|
|
|
if args.mixed_precision == "fp16": |
|
models = [unet_] |
|
if args.train_text_encoder: |
|
models.extend([text_encoder_one_, text_encoder_two_]) |
|
|
|
cast_training_params(models) |
|
|
|
accelerator.register_save_state_pre_hook(save_model_hook) |
|
accelerator.register_load_state_pre_hook(load_model_hook) |
|
|
|
|
|
|
|
if args.allow_tf32 and torch.cuda.is_available(): |
|
torch.backends.cuda.matmul.allow_tf32 = True |
|
|
|
if args.scale_lr: |
|
args.learning_rate = ( |
|
args.learning_rate |
|
* args.gradient_accumulation_steps |
|
* args.train_batch_size |
|
* accelerator.num_processes |
|
) |
|
|
|
|
|
if args.mixed_precision == "fp16": |
|
models = [unet] |
|
if args.train_text_encoder: |
|
models.extend([text_encoder_one, text_encoder_two]) |
|
|
|
|
|
cast_training_params(models, dtype=torch.float32) |
|
|
|
unet_lora_parameters = list(filter(lambda p: p.requires_grad, unet.parameters())) |
|
|
|
if args.train_text_encoder: |
|
text_lora_parameters_one = list( |
|
filter(lambda p: p.requires_grad, text_encoder_one.parameters()) |
|
) |
|
text_lora_parameters_two = list( |
|
filter(lambda p: p.requires_grad, text_encoder_two.parameters()) |
|
) |
|
|
|
|
|
unet_lora_parameters_with_lr = { |
|
"params": unet_lora_parameters, |
|
"lr": args.learning_rate, |
|
} |
|
if args.train_text_encoder: |
|
|
|
text_lora_parameters_one_with_lr = { |
|
"params": text_lora_parameters_one, |
|
"weight_decay": args.adam_weight_decay_text_encoder, |
|
"lr": args.text_encoder_lr if args.text_encoder_lr else args.learning_rate, |
|
} |
|
text_lora_parameters_two_with_lr = { |
|
"params": text_lora_parameters_two, |
|
"weight_decay": args.adam_weight_decay_text_encoder, |
|
"lr": args.text_encoder_lr if args.text_encoder_lr else args.learning_rate, |
|
} |
|
params_to_optimize = [ |
|
unet_lora_parameters_with_lr, |
|
text_lora_parameters_one_with_lr, |
|
text_lora_parameters_two_with_lr, |
|
] |
|
else: |
|
params_to_optimize = [unet_lora_parameters_with_lr] |
|
|
|
|
|
if not (args.optimizer.lower() == "prodigy" or args.optimizer.lower() == "adamw"): |
|
logger.warning( |
|
f"Unsupported choice of optimizer: {args.optimizer}.Supported optimizers include [adamW, prodigy]." |
|
"Defaulting to adamW" |
|
) |
|
args.optimizer = "adamw" |
|
|
|
if args.use_8bit_adam and not args.optimizer.lower() == "adamw": |
|
logger.warning( |
|
f"use_8bit_adam is ignored when optimizer is not set to 'AdamW'. Optimizer was " |
|
f"set to {args.optimizer.lower()}" |
|
) |
|
|
|
if args.optimizer.lower() == "adamw": |
|
if args.use_8bit_adam: |
|
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 |
|
else: |
|
optimizer_class = torch.optim.AdamW |
|
|
|
optimizer = optimizer_class( |
|
params_to_optimize, |
|
betas=(args.adam_beta1, args.adam_beta2), |
|
weight_decay=args.adam_weight_decay, |
|
eps=args.adam_epsilon, |
|
) |
|
|
|
if args.optimizer.lower() == "prodigy": |
|
try: |
|
import prodigyopt |
|
except ImportError: |
|
raise ImportError( |
|
"To use Prodigy, please install the prodigyopt library: `pip install prodigyopt`" |
|
) |
|
|
|
optimizer_class = prodigyopt.Prodigy |
|
|
|
if args.learning_rate <= 0.1: |
|
logger.warning( |
|
"Learning rate is too low. When using prodigy, it's generally better to set learning rate around 1.0" |
|
) |
|
if args.train_text_encoder and args.text_encoder_lr: |
|
logger.warning( |
|
f"Learning rates were provided both for the unet and the text encoder- e.g. text_encoder_lr:" |
|
f" {args.text_encoder_lr} and learning_rate: {args.learning_rate}. " |
|
f"When using prodigy only learning_rate is used as the initial learning rate." |
|
) |
|
|
|
|
|
params_to_optimize[1]["lr"] = args.learning_rate |
|
params_to_optimize[2]["lr"] = args.learning_rate |
|
|
|
optimizer = optimizer_class( |
|
params_to_optimize, |
|
lr=args.learning_rate, |
|
betas=(args.adam_beta1, args.adam_beta2), |
|
beta3=args.prodigy_beta3, |
|
weight_decay=args.adam_weight_decay, |
|
eps=args.adam_epsilon, |
|
decouple=args.prodigy_decouple, |
|
use_bias_correction=args.prodigy_use_bias_correction, |
|
safeguard_warmup=args.prodigy_safeguard_warmup, |
|
) |
|
|
|
|
|
train_dataset = DreamBoothDataset( |
|
instance_data_root=args.instance_data_dir, |
|
instance_prompt=args.instance_prompt, |
|
class_prompt=args.class_prompt, |
|
class_data_root=args.class_data_dir if args.with_prior_preservation else None, |
|
class_num=args.num_class_images, |
|
size=args.resolution, |
|
repeats=args.repeats, |
|
center_crop=args.center_crop, |
|
) |
|
|
|
train_dataloader = torch.utils.data.DataLoader( |
|
train_dataset, |
|
batch_size=args.train_batch_size, |
|
shuffle=True, |
|
collate_fn=lambda examples: collate_fn(examples, args.with_prior_preservation), |
|
num_workers=args.dataloader_num_workers, |
|
) |
|
|
|
del vae |
|
del pipe.vae |
|
gc.collect() |
|
if torch.cuda.is_available(): |
|
torch.cuda.empty_cache() |
|
|
|
|
|
|
|
|
|
|
|
def compute_time_ids(original_size, crops_coords_top_left): |
|
|
|
target_size = (args.resolution, args.resolution) |
|
add_time_ids = list(original_size + crops_coords_top_left + target_size) |
|
add_time_ids = torch.tensor([add_time_ids]) |
|
add_time_ids = add_time_ids.to(accelerator.device, dtype=weight_dtype) |
|
return add_time_ids |
|
|
|
if not args.train_text_encoder: |
|
tokenizers = [tokenizer_one, tokenizer_two] |
|
text_encoders = [text_encoder_one, text_encoder_two] |
|
|
|
def compute_text_embeddings(prompt, text_encoders, tokenizers): |
|
with torch.no_grad(): |
|
prompt_embeds, pooled_prompt_embeds = encode_prompt( |
|
text_encoders, tokenizers, prompt |
|
) |
|
prompt_embeds = prompt_embeds.to(accelerator.device) |
|
pooled_prompt_embeds = pooled_prompt_embeds.to(accelerator.device) |
|
return prompt_embeds, pooled_prompt_embeds |
|
|
|
|
|
|
|
|
|
if not args.train_text_encoder and not train_dataset.custom_instance_prompts: |
|
instance_prompt_hidden_states, instance_pooled_prompt_embeds = ( |
|
compute_text_embeddings(args.instance_prompt, text_encoders, tokenizers) |
|
) |
|
|
|
global prompt_embeds_dict, unet_add_text_embeds_dict |
|
prompt_embeds_dict = {"instance": instance_prompt_hidden_states.to("cpu")} |
|
unet_add_text_embeds_dict = { |
|
"instance": instance_pooled_prompt_embeds.to("cpu") |
|
} |
|
|
|
print("Encoding regular prompts") |
|
global regular_prompts_list |
|
|
|
for _prompt in tqdm(regular_prompts_list): |
|
prompt = _prompt.replace("_", " ") |
|
prompt_embeds, pooled_prompt_embeds = compute_text_embeddings( |
|
prompt, text_encoders, tokenizers |
|
) |
|
|
|
prompt_embeds_dict[_prompt] = prompt_embeds.to("cpu") |
|
unet_add_text_embeds_dict[_prompt] = pooled_prompt_embeds.to("cpu") |
|
|
|
|
|
if args.with_prior_preservation: |
|
if not args.train_text_encoder: |
|
class_prompt_hidden_states, class_pooled_prompt_embeds = ( |
|
compute_text_embeddings(args.class_prompt, text_encoders, tokenizers) |
|
) |
|
|
|
|
|
if not args.train_text_encoder and not train_dataset.custom_instance_prompts: |
|
del tokenizers, text_encoders |
|
gc.collect() |
|
if torch.cuda.is_available(): |
|
torch.cuda.empty_cache() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
overrode_max_train_steps = False |
|
num_update_steps_per_epoch = math.ceil( |
|
len(train_dataloader) / args.gradient_accumulation_steps |
|
) |
|
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 |
|
|
|
lr_scheduler = get_scheduler( |
|
args.lr_scheduler, |
|
optimizer=optimizer, |
|
num_warmup_steps=args.lr_warmup_steps * accelerator.num_processes, |
|
num_training_steps=args.max_train_steps * accelerator.num_processes, |
|
num_cycles=args.lr_num_cycles, |
|
power=args.lr_power, |
|
) |
|
|
|
|
|
if args.train_text_encoder: |
|
( |
|
unet, |
|
text_encoder_one, |
|
text_encoder_two, |
|
optimizer, |
|
train_dataloader, |
|
lr_scheduler, |
|
) = accelerator.prepare( |
|
unet, |
|
text_encoder_one, |
|
text_encoder_two, |
|
optimizer, |
|
train_dataloader, |
|
lr_scheduler, |
|
) |
|
else: |
|
unet, optimizer, train_dataloader, lr_scheduler = accelerator.prepare( |
|
unet, optimizer, train_dataloader, lr_scheduler |
|
) |
|
|
|
|
|
num_update_steps_per_epoch = math.ceil( |
|
len(train_dataloader) / args.gradient_accumulation_steps |
|
) |
|
if overrode_max_train_steps: |
|
args.max_train_steps = args.num_train_epochs * num_update_steps_per_epoch |
|
|
|
args.num_train_epochs = math.ceil(args.max_train_steps / num_update_steps_per_epoch) |
|
|
|
|
|
|
|
if accelerator.is_main_process: |
|
tracker_name = ( |
|
"dreambooth-lora-sd-xl" |
|
if "playground" not in args.pretrained_model_name_or_path |
|
else "dreambooth-lora-playground" |
|
) |
|
accelerator.init_trackers(tracker_name, config=vars(args)) |
|
|
|
|
|
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}") |
|
global_step = 0 |
|
first_epoch = 0 |
|
|
|
|
|
if args.resume_from_checkpoint: |
|
if args.resume_from_checkpoint != "latest": |
|
path = os.path.basename(args.resume_from_checkpoint) |
|
else: |
|
|
|
dirs = os.listdir(args.output_dir) |
|
dirs = [d for d in dirs if d.startswith("checkpoint")] |
|
dirs = sorted(dirs, key=lambda x: int(x.split("-")[1])) |
|
path = dirs[-1] if len(dirs) > 0 else None |
|
|
|
if path is None: |
|
accelerator.print( |
|
f"Checkpoint '{args.resume_from_checkpoint}' does not exist. Starting a new training run." |
|
) |
|
args.resume_from_checkpoint = None |
|
initial_global_step = 0 |
|
else: |
|
accelerator.print(f"Resuming from checkpoint {path}") |
|
accelerator.load_state(os.path.join(args.output_dir, path)) |
|
global_step = int(path.split("-")[1]) |
|
|
|
initial_global_step = global_step |
|
first_epoch = global_step // num_update_steps_per_epoch |
|
|
|
else: |
|
initial_global_step = 0 |
|
|
|
progress_bar = tqdm( |
|
range(0, args.max_train_steps), |
|
initial=initial_global_step, |
|
desc="Steps", |
|
|
|
disable=not accelerator.is_local_main_process, |
|
) |
|
|
|
def get_sigmas(timesteps, n_dim=4, dtype=torch.float32): |
|
sigmas = noise_scheduler.sigmas.to(device=accelerator.device, dtype=dtype) |
|
schedule_timesteps = noise_scheduler.timesteps.to(accelerator.device) |
|
timesteps = timesteps.to(accelerator.device) |
|
|
|
step_indices = [(schedule_timesteps == t).nonzero().item() for t in timesteps] |
|
|
|
sigma = sigmas[step_indices].flatten() |
|
while len(sigma.shape) < n_dim: |
|
sigma = sigma.unsqueeze(-1) |
|
return sigma |
|
|
|
del text_encoder_one, text_encoder_two |
|
del pipe.text_encoder |
|
del pipe.text_encoder_2 |
|
gc.collect() |
|
if torch.cuda.is_available(): |
|
torch.cuda.empty_cache() |
|
|
|
from utils import inject_fooocus_inpaint_head |
|
|
|
for epoch in range(first_epoch, args.num_train_epochs): |
|
unet.train() |
|
if args.train_text_encoder: |
|
text_encoder_one.train() |
|
text_encoder_two.train() |
|
|
|
|
|
accelerator.unwrap_model( |
|
text_encoder_one |
|
).text_model.embeddings.requires_grad_(True) |
|
accelerator.unwrap_model( |
|
text_encoder_two |
|
).text_model.embeddings.requires_grad_(True) |
|
|
|
for step, batch in enumerate(train_dataloader): |
|
with accelerator.accumulate(unet): |
|
|
|
assert len(batch["pixel_values"]) == 1, "only support batch size 1" |
|
|
|
|
|
(inpaint_head_feature, model_input, latent_mask, prompt) = batch[ |
|
"pixel_values" |
|
][0] |
|
|
|
inject_fooocus_inpaint_head( |
|
unet, inpaint_head_feature, parallel_train=True |
|
) |
|
|
|
prompt_embeds = prompt_embeds_dict[prompt].to(accelerator.device) |
|
|
|
unet_add_text_embeds = unet_add_text_embeds_dict[prompt].to( |
|
accelerator.device |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
noise = torch.randn_like(model_input) |
|
bsz = model_input.shape[0] |
|
|
|
|
|
if not args.do_edm_style_training: |
|
timesteps = torch.randint( |
|
0, |
|
noise_scheduler.config.num_train_timesteps, |
|
(bsz,), |
|
device=model_input.device, |
|
) |
|
timesteps = timesteps.long() |
|
else: |
|
|
|
|
|
|
|
indices = torch.randint( |
|
0, noise_scheduler.config.num_train_timesteps, (bsz,) |
|
) |
|
timesteps = noise_scheduler.timesteps[indices].to( |
|
device=model_input.device |
|
) |
|
|
|
|
|
|
|
noisy_model_input = noise_scheduler.add_noise( |
|
model_input, noise, timesteps |
|
) |
|
|
|
|
|
|
|
if args.do_edm_style_training: |
|
sigmas = get_sigmas( |
|
timesteps, len(noisy_model_input.shape), noisy_model_input.dtype |
|
) |
|
if "EDM" in scheduler_type: |
|
inp_noisy_latents = noise_scheduler.precondition_inputs( |
|
noisy_model_input, sigmas |
|
) |
|
else: |
|
inp_noisy_latents = noisy_model_input / ((sigmas**2 + 1) ** 0.5) |
|
|
|
|
|
add_time_ids = torch.cat( |
|
[ |
|
compute_time_ids(original_size=s, crops_coords_top_left=c) |
|
for s, c in zip( |
|
batch["original_sizes"], batch["crop_top_lefts"] |
|
) |
|
] |
|
) |
|
|
|
|
|
if not train_dataset.custom_instance_prompts: |
|
elems_to_repeat_text_embeds = ( |
|
bsz // 2 if args.with_prior_preservation else bsz |
|
) |
|
else: |
|
elems_to_repeat_text_embeds = 1 |
|
|
|
|
|
if not args.train_text_encoder: |
|
unet_added_conditions = { |
|
"time_ids": add_time_ids, |
|
"text_embeds": unet_add_text_embeds.repeat( |
|
elems_to_repeat_text_embeds, 1 |
|
), |
|
} |
|
prompt_embeds_input = prompt_embeds.repeat( |
|
elems_to_repeat_text_embeds, 1, 1 |
|
) |
|
model_pred = unet( |
|
( |
|
inp_noisy_latents |
|
if args.do_edm_style_training |
|
else noisy_model_input |
|
), |
|
timesteps, |
|
prompt_embeds_input, |
|
added_cond_kwargs=unet_added_conditions, |
|
return_dict=False, |
|
)[0] |
|
else: |
|
unet_added_conditions = {"time_ids": add_time_ids} |
|
prompt_embeds, pooled_prompt_embeds = encode_prompt( |
|
text_encoders=[text_encoder_one, text_encoder_two], |
|
tokenizers=None, |
|
prompt=None, |
|
text_input_ids_list=[tokens_one, tokens_two], |
|
) |
|
unet_added_conditions.update( |
|
{ |
|
"text_embeds": pooled_prompt_embeds.repeat( |
|
elems_to_repeat_text_embeds, 1 |
|
) |
|
} |
|
) |
|
prompt_embeds_input = prompt_embeds.repeat( |
|
elems_to_repeat_text_embeds, 1, 1 |
|
) |
|
model_pred = unet( |
|
( |
|
inp_noisy_latents |
|
if args.do_edm_style_training |
|
else noisy_model_input |
|
), |
|
timesteps, |
|
prompt_embeds_input, |
|
added_cond_kwargs=unet_added_conditions, |
|
return_dict=False, |
|
)[0] |
|
|
|
weighting = None |
|
if args.do_edm_style_training: |
|
|
|
|
|
|
|
if "EDM" in scheduler_type: |
|
model_pred = noise_scheduler.precondition_outputs( |
|
noisy_model_input, model_pred, sigmas |
|
) |
|
else: |
|
if noise_scheduler.config.prediction_type == "epsilon": |
|
model_pred = model_pred * (-sigmas) + noisy_model_input |
|
elif noise_scheduler.config.prediction_type == "v_prediction": |
|
model_pred = model_pred * ( |
|
-sigmas / (sigmas**2 + 1) ** 0.5 |
|
) + (noisy_model_input / (sigmas**2 + 1)) |
|
|
|
|
|
|
|
|
|
if "EDM" not in scheduler_type: |
|
weighting = (sigmas**-2.0).float() |
|
|
|
|
|
if noise_scheduler.config.prediction_type == "epsilon": |
|
target = model_input if args.do_edm_style_training else noise |
|
elif noise_scheduler.config.prediction_type == "v_prediction": |
|
target = ( |
|
model_input |
|
if args.do_edm_style_training |
|
else noise_scheduler.get_velocity(model_input, noise, timesteps) |
|
) |
|
else: |
|
raise ValueError( |
|
f"Unknown prediction type {noise_scheduler.config.prediction_type}" |
|
) |
|
|
|
if args.with_prior_preservation: |
|
|
|
model_pred, model_pred_prior = torch.chunk(model_pred, 2, dim=0) |
|
target, target_prior = torch.chunk(target, 2, dim=0) |
|
|
|
|
|
if weighting is not None: |
|
prior_loss = torch.mean( |
|
( |
|
weighting.float() |
|
* (model_pred_prior.float() - target_prior.float()) ** 2 |
|
).reshape(target_prior.shape[0], -1), |
|
1, |
|
) |
|
prior_loss = prior_loss.mean() |
|
else: |
|
prior_loss = F.mse_loss( |
|
model_pred_prior.float(), |
|
target_prior.float(), |
|
reduction="mean", |
|
) |
|
|
|
if args.snr_gamma is None: |
|
if weighting is not None: |
|
loss = torch.mean( |
|
( |
|
weighting.float() |
|
* (model_pred.float() - target.float()) ** 2 |
|
).reshape(target.shape[0], -1), |
|
1, |
|
) |
|
loss = loss.mean() |
|
else: |
|
obj_loss_weight = args.loss_reweight_object |
|
bg_loss_weight = args.loss_reweight_background |
|
mask_reweight = ( |
|
latent_mask * (obj_loss_weight - bg_loss_weight) |
|
+ bg_loss_weight |
|
) |
|
model_pred = model_pred.float() * mask_reweight |
|
target = target.float() * mask_reweight |
|
loss = F.mse_loss(model_pred, target, reduction="mean") |
|
else: |
|
|
|
|
|
|
|
snr = compute_snr(noise_scheduler, timesteps) |
|
base_weight = ( |
|
torch.stack( |
|
[snr, args.snr_gamma * torch.ones_like(timesteps)], dim=1 |
|
).min(dim=1)[0] |
|
/ snr |
|
) |
|
|
|
if noise_scheduler.config.prediction_type == "v_prediction": |
|
|
|
mse_loss_weights = base_weight + 1 |
|
else: |
|
|
|
mse_loss_weights = base_weight |
|
|
|
loss = F.mse_loss( |
|
model_pred.float(), target.float(), reduction="none" |
|
) |
|
loss = ( |
|
loss.mean(dim=list(range(1, len(loss.shape)))) |
|
* mse_loss_weights |
|
) |
|
loss = loss.mean() |
|
|
|
if args.with_prior_preservation: |
|
|
|
loss = loss + args.prior_loss_weight * prior_loss |
|
|
|
accelerator.backward(loss) |
|
if accelerator.sync_gradients: |
|
params_to_clip = ( |
|
itertools.chain( |
|
unet_lora_parameters, |
|
text_lora_parameters_one, |
|
text_lora_parameters_two, |
|
) |
|
if args.train_text_encoder |
|
else unet_lora_parameters |
|
) |
|
accelerator.clip_grad_norm_(params_to_clip, args.max_grad_norm) |
|
|
|
optimizer.step() |
|
lr_scheduler.step() |
|
optimizer.zero_grad() |
|
|
|
|
|
if accelerator.sync_gradients: |
|
progress_bar.update(1) |
|
global_step += 1 |
|
|
|
if accelerator.is_main_process: |
|
if global_step % args.checkpointing_steps == 0: |
|
save_path = os.path.join( |
|
args.output_dir, f"checkpoint-{global_step}" |
|
) |
|
accelerator.save_state(save_path) |
|
logger.info(f"Saved state to {save_path}") |
|
|
|
logs = {"loss": loss.detach().item(), "lr": lr_scheduler.get_last_lr()[0]} |
|
progress_bar.set_postfix(**logs) |
|
accelerator.log(logs, step=global_step) |
|
|
|
if global_step >= args.max_train_steps: |
|
break |
|
|
|
|
|
accelerator.wait_for_everyone() |
|
if accelerator.is_main_process: |
|
unet = unwrap_model(unet) |
|
unet = unet.to(torch.float32) |
|
unet_lora_layers = convert_state_dict_to_diffusers( |
|
get_peft_model_state_dict(unet) |
|
) |
|
|
|
if args.train_text_encoder: |
|
text_encoder_one = unwrap_model(text_encoder_one) |
|
text_encoder_lora_layers = convert_state_dict_to_diffusers( |
|
get_peft_model_state_dict(text_encoder_one.to(torch.float32)) |
|
) |
|
text_encoder_two = unwrap_model(text_encoder_two) |
|
text_encoder_2_lora_layers = convert_state_dict_to_diffusers( |
|
get_peft_model_state_dict(text_encoder_two.to(torch.float32)) |
|
) |
|
else: |
|
text_encoder_lora_layers = None |
|
text_encoder_2_lora_layers = None |
|
|
|
StableDiffusionXLPipeline.save_lora_weights( |
|
save_directory=args.output_dir, |
|
unet_lora_layers=unet_lora_layers, |
|
text_encoder_lora_layers=text_encoder_lora_layers, |
|
text_encoder_2_lora_layers=text_encoder_2_lora_layers, |
|
) |
|
if args.output_kohya_format: |
|
lora_state_dict = load_file( |
|
f"{args.output_dir}/pytorch_lora_weights.safetensors" |
|
) |
|
peft_state_dict = convert_all_state_dict_to_peft(lora_state_dict) |
|
kohya_state_dict = convert_state_dict_to_kohya(peft_state_dict) |
|
save_file( |
|
kohya_state_dict, |
|
f"{args.output_dir}/pytorch_lora_weights_kohya.safetensors", |
|
) |
|
|
|
accelerator.end_training() |
|
|
|
|
|
if __name__ == "__main__": |
|
args = parse_args() |
|
main(args) |
|
|