|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import logging |
|
import math |
|
import os |
|
|
|
from pathlib import Path |
|
|
|
import accelerate |
|
import datasets |
|
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 ProjectConfiguration, set_seed |
|
from huggingface_hub import create_repo, upload_folder |
|
from packaging import version |
|
from tqdm.auto import tqdm |
|
|
|
import diffusers |
|
from diffusers import AutoencoderKL, DDPMScheduler |
|
from diffusers.optimization import get_scheduler |
|
from diffusers.training_utils import EMAModel |
|
from diffusers.utils import deprecate |
|
from diffusers.utils.import_utils import is_xformers_available |
|
from diffusion_module.utils.Pipline import SDMLDMPipeline |
|
from diffusion_module.unet_2d_sdm import SDMUNet2DModel |
|
from diffusion_module.unet import UNetModel |
|
from diffusers.schedulers import DDIMScheduler,UniPCMultistepScheduler |
|
|
|
|
|
from diffusion_module.utils.loss import get_variance, variance_KL_loss |
|
|
|
from dataset.ade20k import load_data |
|
from crack_config_utils.parse_args_ade import parse_args |
|
from crack_config_utils.utils_ade import log_validation, preprocess_input |
|
import datetime |
|
|
|
|
|
logger = get_logger(__name__, log_level="INFO") |
|
|
|
|
|
def main(): |
|
|
|
args = parse_args() |
|
|
|
if args.non_ema_revision is not None: |
|
deprecate( |
|
"non_ema_revision!=None", |
|
"0.15.0", |
|
message=( |
|
"Downloading 'non_ema' weights from revision branches of the Hub is deprecated. Please make sure to" |
|
" use `--variant=non_ema` instead." |
|
), |
|
) |
|
|
|
current_time = datetime.datetime.now() |
|
timestamp = current_time.strftime("%Y-%m-%d-%H%M") |
|
output_dir = os.path.join(args.output_dir, timestamp) |
|
logging_dir = os.path.join(output_dir, args.logging_dir) |
|
|
|
accelerator_project_config = ProjectConfiguration(project_dir=output_dir, logging_dir=logging_dir, |
|
total_limit=args.checkpoints_total_limit) |
|
|
|
accelerator = Accelerator( |
|
gradient_accumulation_steps=args.gradient_accumulation_steps, |
|
mixed_precision=args.mixed_precision, |
|
log_with=args.report_to, |
|
project_config=accelerator_project_config, |
|
) |
|
|
|
|
|
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: |
|
datasets.utils.logging.set_verbosity_warning() |
|
transformers.utils.logging.set_verbosity_warning() |
|
diffusers.utils.logging.set_verbosity_info() |
|
else: |
|
datasets.utils.logging.set_verbosity_error() |
|
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) |
|
|
|
if args.push_to_hub: |
|
repo_id = create_repo( |
|
repo_id=args.hub_model_id or Path(args.output_dir).name, exist_ok=True, token=args.hub_token |
|
).repo_id |
|
|
|
|
|
|
|
|
|
|
|
noise_scheduler = UniPCMultistepScheduler() |
|
|
|
|
|
|
|
|
|
vae = AutoencoderKL.from_pretrained("runwayml/stable-diffusion-v1-5", subfolder="vae") |
|
|
|
|
|
|
|
|
|
vae.requires_grad_(False) |
|
|
|
|
|
latent_size = (64, 64) |
|
print(latent_size) |
|
unet = UNetModel( |
|
image_size = latent_size, |
|
in_channels=vae.config.latent_channels, |
|
model_channels=256, |
|
|
|
out_channels=vae.config.latent_channels, |
|
num_res_blocks=2, |
|
|
|
attention_resolutions=(2, 4, 8), |
|
dropout=0, |
|
|
|
channel_mult=(1, 2, 3, 4), |
|
num_heads=8, |
|
num_head_channels=-1, |
|
num_heads_upsample=-1, |
|
use_scale_shift_norm=True, |
|
resblock_updown=True, |
|
use_new_attention_order=False, |
|
num_classes=args.segmap_channels, |
|
mask_emb="resize", |
|
use_checkpoint=True, |
|
SPADE_type="spade", |
|
) |
|
|
|
if args.resume_dir is not None: |
|
unet = unet.from_pretrained(args.resume_dir) |
|
|
|
|
|
if args.use_ema: |
|
ema_unet = EMAModel( |
|
unet.parameters(), |
|
decay=args.ema_max_decay, |
|
use_ema_warmup=True, |
|
inv_gamma=args.ema_inv_gamma, |
|
power=args.ema_power, |
|
model_cls=UNetModel, |
|
model_config=unet.config, |
|
) |
|
|
|
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.warn( |
|
"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") |
|
|
|
def compute_snr(timesteps): |
|
""" |
|
Computes SNR as per https://github.com/TiankaiHang/Min-SNR-Diffusion-Training/blob/521b624bd70c67cee4bdf49225915f5945a872e3/guided_diffusion/gaussian_diffusion.py#L847-L849 |
|
""" |
|
alphas_cumprod = noise_scheduler.alphas_cumprod |
|
sqrt_alphas_cumprod = alphas_cumprod**0.5 |
|
sqrt_one_minus_alphas_cumprod = (1.0 - alphas_cumprod) ** 0.5 |
|
|
|
|
|
|
|
sqrt_alphas_cumprod = sqrt_alphas_cumprod.to(device=timesteps.device)[timesteps].float() |
|
while len(sqrt_alphas_cumprod.shape) < len(timesteps.shape): |
|
sqrt_alphas_cumprod = sqrt_alphas_cumprod[..., None] |
|
alpha = sqrt_alphas_cumprod.expand(timesteps.shape) |
|
|
|
sqrt_one_minus_alphas_cumprod = sqrt_one_minus_alphas_cumprod.to(device=timesteps.device)[timesteps].float() |
|
while len(sqrt_one_minus_alphas_cumprod.shape) < len(timesteps.shape): |
|
sqrt_one_minus_alphas_cumprod = sqrt_one_minus_alphas_cumprod[..., None] |
|
sigma = sqrt_one_minus_alphas_cumprod.expand(timesteps.shape) |
|
|
|
|
|
snr = (alpha / sigma) ** 2 |
|
return snr |
|
|
|
|
|
if version.parse(accelerate.__version__) >= version.parse("0.16.0"): |
|
|
|
def save_model_hook(models, weights, output_dir): |
|
if args.use_ema: |
|
ema_unet.save_pretrained(os.path.join(output_dir, "unet_ema")) |
|
|
|
for i, model in enumerate(models): |
|
model.save_pretrained(os.path.join(output_dir, "unet")) |
|
|
|
|
|
weights.pop() |
|
|
|
def load_model_hook(models, input_dir): |
|
if args.use_ema: |
|
load_model = EMAModel.from_pretrained(os.path.join(input_dir, "unet_ema"), SDMUNet2DModel) |
|
ema_unet.load_state_dict(load_model.state_dict()) |
|
ema_unet.to(accelerator.device) |
|
del load_model |
|
|
|
for i in range(len(models)): |
|
|
|
model = models.pop() |
|
|
|
|
|
load_model = UNetModel.from_pretrained(input_dir, subfolder="unet") |
|
model.register_to_config(**load_model.config) |
|
|
|
model.load_state_dict(load_model.state_dict()) |
|
del load_model |
|
|
|
accelerator.register_save_state_pre_hook(save_model_hook) |
|
accelerator.register_load_state_pre_hook(load_model_hook) |
|
|
|
if args.gradient_checkpointing: |
|
unet.enable_gradient_checkpointing() |
|
|
|
if args.scale_lr: |
|
args.learning_rate = ( |
|
args.learning_rate * args.gradient_accumulation_steps * args.train_batch_size * accelerator.num_processes |
|
) |
|
|
|
|
|
optimizer_cls = torch.optim.AdamW |
|
|
|
optimizer = optimizer_cls( |
|
unet.parameters(), |
|
lr=args.learning_rate, |
|
betas=(args.adam_beta1, args.adam_beta2), |
|
weight_decay=args.adam_weight_decay, |
|
eps=args.adam_epsilon, |
|
) |
|
|
|
train_dataloader, train_dataset = load_data( |
|
dataset_mode="ade20k", |
|
data_dir=args.data_root, |
|
batch_size=args.train_batch_size, |
|
image_size= args.resolution, |
|
is_train=True) |
|
|
|
val_dataloader, _ = load_data( |
|
dataset_mode="ade20k", |
|
data_dir=args.data_root, |
|
batch_size=1, |
|
image_size= args.resolution, |
|
is_train=False) |
|
|
|
|
|
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 * args.gradient_accumulation_steps, |
|
num_training_steps=args.max_train_steps * args.gradient_accumulation_steps, |
|
) |
|
|
|
|
|
unet, optimizer, train_dataloader, lr_scheduler = accelerator.prepare( |
|
unet, optimizer, train_dataloader, lr_scheduler |
|
) |
|
|
|
if args.use_ema: |
|
ema_unet.to(accelerator.device) |
|
|
|
|
|
|
|
weight_dtype = torch.float32 |
|
if accelerator.mixed_precision == "fp16": |
|
weight_dtype = torch.float16 |
|
elif accelerator.mixed_precision == "bf16": |
|
weight_dtype = torch.bfloat16 |
|
|
|
|
|
vae.to(accelerator.device, dtype=weight_dtype) |
|
|
|
|
|
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_config = dict(vars(args)) |
|
accelerator.init_trackers(args.tracker_project_name, tracker_config) |
|
|
|
|
|
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 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 |
|
else: |
|
accelerator.print(f"Resuming from checkpoint {path}") |
|
accelerator.load_state(os.path.join(args.output_dir, path)) |
|
global_step = int(path.split("-")[1]) |
|
|
|
resume_global_step = global_step * args.gradient_accumulation_steps |
|
first_epoch = global_step // num_update_steps_per_epoch |
|
resume_step = resume_global_step % (num_update_steps_per_epoch * args.gradient_accumulation_steps) |
|
|
|
|
|
progress_bar = tqdm(range(global_step, args.max_train_steps), disable=not accelerator.is_local_main_process) |
|
progress_bar.set_description("Steps") |
|
|
|
for epoch in range(first_epoch, args.num_train_epochs): |
|
unet.train() |
|
train_loss = 0.0 |
|
for step, batch in enumerate(train_dataloader): |
|
|
|
if args.resume_from_checkpoint and epoch == first_epoch and step < resume_step: |
|
if step % args.gradient_accumulation_steps == 0: |
|
progress_bar.update(1) |
|
continue |
|
|
|
with accelerator.accumulate(unet): |
|
|
|
images =batch[0] |
|
labels = batch[1]['label'] |
|
latents = vae.encode(images.to(weight_dtype)).latent_dist.sample() |
|
latents = latents * vae.config.scaling_factor |
|
segmap = preprocess_input(labels, args.segmap_channels) |
|
|
|
|
|
|
|
noise = torch.randn_like(latents) |
|
|
|
if args.noise_offset: |
|
|
|
noise += args.noise_offset * torch.randn( |
|
(latents.shape[0], latents.shape[1], 1, 1), device=latents.device |
|
) |
|
|
|
bsz = latents.shape[0] |
|
|
|
timesteps = torch.randint(0, noise_scheduler.config.num_train_timesteps, (bsz,), device=latents.device) |
|
timesteps = timesteps.long() |
|
|
|
|
|
|
|
noisy_latents = noise_scheduler.add_noise(latents, noise, timesteps) |
|
|
|
|
|
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}") |
|
|
|
|
|
model_pred = unet(noisy_latents, segmap, timesteps).sample |
|
|
|
if args.snr_gamma is None: |
|
loss = F.mse_loss(model_pred.float(), target.float(), reduction="mean") |
|
|
|
else: |
|
|
|
|
|
|
|
snr = compute_snr(timesteps) |
|
mse_loss_weights = ( |
|
torch.stack([snr, args.snr_gamma * torch.ones_like(timesteps)], dim=1).min(dim=1)[0] / snr |
|
) |
|
|
|
|
|
|
|
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() |
|
|
|
|
|
avg_loss = accelerator.gather(loss.repeat(args.train_batch_size)).mean() |
|
train_loss += avg_loss.item() / args.gradient_accumulation_steps |
|
|
|
|
|
accelerator.backward(loss) |
|
if accelerator.sync_gradients: |
|
accelerator.clip_grad_norm_(unet.parameters(), args.max_grad_norm) |
|
optimizer.step() |
|
lr_scheduler.step() |
|
optimizer.zero_grad() |
|
|
|
|
|
if accelerator.sync_gradients: |
|
if args.use_ema: |
|
ema_unet.step(unet.parameters()) |
|
progress_bar.update(1) |
|
global_step += 1 |
|
log_dic = {"train_loss": train_loss} |
|
accelerator.log(log_dic, step=global_step) |
|
train_loss = 0.0 |
|
|
|
if global_step % args.checkpointing_steps == 0: |
|
if accelerator.is_main_process: |
|
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 = {"step_loss": loss.detach().item(), "lr": lr_scheduler.get_last_lr()[0]} |
|
progress_bar.set_postfix(**logs) |
|
|
|
if global_step >= args.max_train_steps: |
|
break |
|
|
|
if accelerator.is_main_process: |
|
if epoch % args.validation_epochs == 0: |
|
if args.use_ema: |
|
|
|
ema_unet.store(unet.parameters()) |
|
ema_unet.copy_to(unet.parameters()) |
|
log_validation(vae, unet, noise_scheduler, |
|
accelerator, weight_dtype, val_dataloader, |
|
save_dir = args.output_dir,resolution=args.resolution, g_step=global_step) |
|
if args.use_ema: |
|
|
|
ema_unet.restore(unet.parameters()) |
|
|
|
|
|
accelerator.wait_for_everyone() |
|
if accelerator.is_main_process: |
|
unet = accelerator.unwrap_model(unet) |
|
if args.use_ema: |
|
ema_unet.copy_to(unet.parameters()) |
|
|
|
pipeline = SDMLDMPipeline( |
|
vae=vae, |
|
unet=unet, |
|
scheduler=noise_scheduler, |
|
torch_dtype=weight_dtype, |
|
) |
|
pipeline.save_pretrained(args.output_dir) |
|
|
|
if args.push_to_hub: |
|
upload_folder( |
|
repo_id=repo_id, |
|
folder_path=args.output_dir, |
|
commit_message="End of training", |
|
ignore_patterns=["step_*", "epoch_*"], |
|
) |
|
|
|
accelerator.end_training() |
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
main() |
|
|