diffusers/examples/train_unconditional.py

225 lines
9.2 KiB
Python
Raw Normal View History

2022-06-15 03:21:02 -06:00
import argparse
2022-06-14 10:25:22 -06:00
import os
2022-06-13 08:50:30 -06:00
import torch
import torch.nn.functional as F
from accelerate import Accelerator
from accelerate.logging import get_logger
2022-06-13 08:50:30 -06:00
from datasets import load_dataset
from diffusers import DDIMPipeline, DDIMScheduler, UNetModel
2022-06-21 02:43:40 -06:00
from diffusers.hub_utils import init_git_repo, push_to_hub
from diffusers.optimization import get_scheduler
2022-06-27 07:23:01 -06:00
from diffusers.training_utils import EMAModel
2022-06-14 03:33:24 -06:00
from torchvision.transforms import (
2022-06-15 07:52:23 -06:00
CenterCrop,
2022-06-14 03:33:24 -06:00
Compose,
InterpolationMode,
2022-06-27 07:23:01 -06:00
Normalize,
2022-06-14 03:33:24 -06:00
RandomHorizontalFlip,
Resize,
ToTensor,
)
2022-06-13 08:50:30 -06:00
from tqdm.auto import tqdm
2022-06-21 02:38:34 -06:00
logger = get_logger(__name__)
2022-06-13 08:50:30 -06:00
2022-06-14 10:25:22 -06:00
def main(args):
logging_dir = os.path.join(args.output_dir, args.logging_dir)
accelerator = Accelerator(mixed_precision=args.mixed_precision, log_with="tensorboard", logging_dir=logging_dir)
2022-06-14 10:25:22 -06:00
model = UNetModel(
attn_resolutions=(16,),
ch=128,
ch_mult=(1, 2, 4, 8),
dropout=0.0,
num_res_blocks=2,
resamp_with_conv=True,
2022-06-15 03:21:02 -06:00
resolution=args.resolution,
2022-06-14 10:25:22 -06:00
)
noise_scheduler = DDIMScheduler(timesteps=1000, tensor_format="pt")
optimizer = torch.optim.AdamW(
model.parameters(),
lr=args.learning_rate,
betas=(args.adam_beta1, args.adam_beta2),
weight_decay=args.adam_weight_decay,
eps=args.adam_epsilon,
)
2022-06-14 10:25:22 -06:00
augmentations = Compose(
[
2022-06-15 03:21:02 -06:00
Resize(args.resolution, interpolation=InterpolationMode.BILINEAR),
2022-06-15 06:36:43 -06:00
CenterCrop(args.resolution),
2022-06-14 10:25:22 -06:00
RandomHorizontalFlip(),
ToTensor(),
2022-06-27 07:23:01 -06:00
Normalize([0.5], [0.5]),
2022-06-14 10:25:22 -06:00
]
)
2022-06-15 03:21:02 -06:00
dataset = load_dataset(args.dataset, split="train")
2022-06-14 10:25:22 -06:00
def transforms(examples):
images = [augmentations(image.convert("RGB")) for image in examples["image"]]
return {"input": images}
dataset.set_transform(transforms)
train_dataloader = torch.utils.data.DataLoader(dataset, batch_size=args.train_batch_size, shuffle=True)
2022-06-14 10:25:22 -06:00
2022-06-21 03:35:56 -06:00
lr_scheduler = get_scheduler(
args.lr_scheduler,
2022-06-14 10:25:22 -06:00
optimizer=optimizer,
num_warmup_steps=args.lr_warmup_steps,
2022-06-15 03:21:02 -06:00
num_training_steps=(len(train_dataloader) * args.num_epochs) // args.gradient_accumulation_steps,
2022-06-14 10:25:22 -06:00
)
model, optimizer, train_dataloader, lr_scheduler = accelerator.prepare(
model, optimizer, train_dataloader, lr_scheduler
)
ema_model = EMAModel(model, inv_gamma=args.ema_inv_gamma, power=args.ema_power, max_value=args.ema_max_decay)
2022-06-27 07:23:01 -06:00
2022-06-21 02:38:34 -06:00
if args.push_to_hub:
repo = init_git_repo(args, at_init=True)
if accelerator.is_main_process:
run = os.path.split(__file__)[-1].split(".")[0]
accelerator.init_trackers(run)
2022-06-21 02:38:34 -06:00
# Train!
2022-06-21 03:21:10 -06:00
is_distributed = torch.distributed.is_available() and torch.distributed.is_initialized()
world_size = torch.distributed.get_world_size() if is_distributed else 1
total_train_batch_size = args.train_batch_size * args.gradient_accumulation_steps * world_size
2022-06-21 02:38:34 -06:00
max_steps = len(train_dataloader) // args.gradient_accumulation_steps * args.num_epochs
logger.info("***** Running training *****")
logger.info(f" Num examples = {len(train_dataloader.dataset)}")
logger.info(f" Num Epochs = {args.num_epochs}")
logger.info(f" Instantaneous batch size per device = {args.train_batch_size}")
2022-06-21 02:38:34 -06:00
logger.info(f" Total train batch size (w. parallel, distributed & accumulation) = {total_train_batch_size}")
logger.info(f" Gradient Accumulation steps = {args.gradient_accumulation_steps}")
logger.info(f" Total optimization steps = {max_steps}")
2022-06-27 07:23:01 -06:00
global_step = 0
2022-06-15 03:21:02 -06:00
for epoch in range(args.num_epochs):
2022-06-14 10:25:22 -06:00
model.train()
progress_bar = tqdm(total=len(train_dataloader), disable=not accelerator.is_local_main_process)
progress_bar.set_description(f"Epoch {epoch}")
for step, batch in enumerate(train_dataloader):
clean_images = batch["input"]
noise_samples = torch.randn(clean_images.shape).to(clean_images.device)
bsz = clean_images.shape[0]
timesteps = torch.randint(0, noise_scheduler.timesteps, (bsz,), device=clean_images.device).long()
# add noise onto the clean images according to the noise magnitude at each timestep
# (this is the forward diffusion process)
noisy_images = noise_scheduler.add_noise(clean_images, noise_samples, timesteps)
if step % args.gradient_accumulation_steps != 0:
with accelerator.no_sync(model):
2022-06-14 10:25:22 -06:00
output = model(noisy_images, timesteps)
2022-06-15 03:21:02 -06:00
# predict the noise residual
2022-06-14 10:25:22 -06:00
loss = F.mse_loss(output, noise_samples)
2022-06-21 03:35:56 -06:00
loss = loss / args.gradient_accumulation_steps
2022-06-14 10:25:22 -06:00
accelerator.backward(loss)
else:
output = model(noisy_images, timesteps)
# predict the noise residual
loss = F.mse_loss(output, noise_samples)
loss = loss / args.gradient_accumulation_steps
accelerator.backward(loss)
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
optimizer.step()
lr_scheduler.step()
ema_model.step(model, global_step)
optimizer.zero_grad()
progress_bar.update(1)
progress_bar.set_postfix(
loss=loss.detach().item(), lr=optimizer.param_groups[0]["lr"], ema_decay=ema_model.decay
)
accelerator.log(
{
"train_loss": loss.detach().item(),
"epoch": epoch,
"ema_decay": ema_model.decay,
"step": global_step,
},
step=global_step,
)
global_step += 1
progress_bar.close()
2022-06-14 10:25:22 -06:00
2022-06-27 07:23:01 -06:00
accelerator.wait_for_everyone()
2022-06-14 10:25:22 -06:00
2022-06-15 03:21:02 -06:00
# Generate a sample image for visual inspection
2022-06-27 07:23:01 -06:00
if accelerator.is_main_process:
2022-06-14 10:25:22 -06:00
with torch.no_grad():
pipeline = DDIMPipeline(
unet=accelerator.unwrap_model(ema_model.averaged_model),
noise_scheduler=noise_scheduler,
2022-06-27 07:23:01 -06:00
)
2022-06-15 03:21:02 -06:00
generator = torch.manual_seed(0)
2022-06-14 10:25:22 -06:00
# run pipeline in inference (sample random noise and denoise)
images = pipeline(generator=generator, batch_size=args.eval_batch_size, num_inference_steps=50)
2022-06-14 10:25:22 -06:00
# denormalize the images and save to tensorboard
images_processed = (images.cpu() + 1.0) * 127.5
images_processed = images_processed.clamp(0, 255).type(torch.uint8).numpy()
2022-06-21 03:21:10 -06:00
accelerator.trackers[0].writer.add_images("test_samples", images_processed, epoch)
2022-06-14 10:25:22 -06:00
2022-06-21 03:21:10 -06:00
# save the model
if args.push_to_hub:
push_to_hub(args, pipeline, repo, commit_message=f"Epoch {epoch}", blocking=False)
else:
pipeline.save_pretrained(args.output_dir)
2022-06-27 07:23:01 -06:00
accelerator.wait_for_everyone()
2022-06-14 10:25:22 -06:00
accelerator.end_training()
2022-06-14 10:25:22 -06:00
if __name__ == "__main__":
2022-06-15 03:21:02 -06:00
parser = argparse.ArgumentParser(description="Simple example of a training script.")
2022-06-21 02:38:34 -06:00
parser.add_argument("--local_rank", type=int, default=-1)
2022-06-15 03:21:02 -06:00
parser.add_argument("--dataset", type=str, default="huggan/flowers-102-categories")
2022-06-21 03:21:10 -06:00
parser.add_argument("--output_dir", type=str, default="ddpm-model")
parser.add_argument("--overwrite_output_dir", action="store_true")
2022-06-15 03:21:02 -06:00
parser.add_argument("--resolution", type=int, default=64)
parser.add_argument("--train_batch_size", type=int, default=16)
parser.add_argument("--eval_batch_size", type=int, default=16)
2022-06-15 03:21:02 -06:00
parser.add_argument("--num_epochs", type=int, default=100)
2022-06-15 06:36:43 -06:00
parser.add_argument("--gradient_accumulation_steps", type=int, default=1)
parser.add_argument("--learning_rate", type=float, default=1e-4)
parser.add_argument("--lr_scheduler", type=str, default="cosine")
parser.add_argument("--lr_warmup_steps", type=int, default=500)
parser.add_argument("--adam_beta1", type=float, default=0.95)
parser.add_argument("--adam_beta2", type=float, default=0.999)
parser.add_argument("--adam_weight_decay", type=float, default=1e-6)
parser.add_argument("--adam_epsilon", type=float, default=1e-3)
parser.add_argument("--ema_inv_gamma", type=float, default=1.0)
2022-06-27 09:39:41 -06:00
parser.add_argument("--ema_power", type=float, default=3 / 4)
parser.add_argument("--ema_max_decay", type=float, default=0.9999)
2022-06-21 02:38:34 -06:00
parser.add_argument("--push_to_hub", action="store_true")
2022-06-21 03:21:10 -06:00
parser.add_argument("--hub_token", type=str, default=None)
parser.add_argument("--hub_model_id", type=str, default=None)
parser.add_argument("--hub_private_repo", action="store_true")
parser.add_argument("--logging_dir", type=str, default="logs")
2022-06-14 10:25:22 -06:00
parser.add_argument(
"--mixed_precision",
type=str,
default="no",
choices=["no", "fp16", "bf16"],
2022-06-15 04:35:47 -06:00
help=(
"Whether to use mixed precision. Choose"
"between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10."
"and an Nvidia Ampere GPU."
),
2022-06-14 10:25:22 -06:00
)
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
main(args)