text stringlengths 0 5.54k |
|---|
prompt = "sailing ship in storm by Leonardo da Vinci" |
image = pipeline(prompt).images[0] To export the pipeline in the ONNX format and use it later for inference, use the optimum-cli export command: Copied optimum-cli export onnx --model stabilityai/stable-diffusion-xl-base-1.0 --task stable-diffusion-xl sd_xl_onnx/ SDXL in the ONNX format is supported for text-to-image... |
Latent Consistency Distillation Latent Consistency Models (LCMs) are able to generate high-quality images in just a few steps, representing a big leap forward because many pipelines require at least 25+ steps. LCMs are produced by applying the latent consistency distillation method to any Stable Diffusion model. This m... |
cd diffusers |
pip install . Then navigate to the example folder containing the training script and install the required dependencies for the script you’re using: Copied cd examples/consistency_distillation |
pip install -r requirements.txt 🤗 Accelerate is a library for helping you train on multiple GPUs/TPUs or with mixed-precision. It’ll automatically configure your training setup based on your hardware and environment. Take a look at the 🤗 Accelerate Quick tour to learn more. Initialize an 🤗 Accelerate environment (tr... |
write_basic_config() Lastly, if you want to train a model on your own dataset, take a look at the Create a dataset for training guide to learn how to create a dataset that works with the training script. Script parameters The following sections highlight parts of the training script that are important for understandin... |
--mixed_precision="fp16" Most of the parameters are identical to the parameters in the Text-to-image training guide, so you’ll focus on the parameters that are relevant to latent consistency distillation in this guide. --pretrained_teacher_model: the path to a pretrained latent diffusion model to use as the teacher m... |
image = example["image"] |
image = TF.resize(image, resolution, interpolation=transforms.InterpolationMode.BILINEAR) |
c_top, c_left, _, _ = transforms.RandomCrop.get_params(image, output_size=(resolution, resolution)) |
image = TF.crop(image, c_top, c_left, resolution, resolution) |
image = TF.to_tensor(image) |
image = TF.normalize(image, [0.5], [0.5]) |
example["image"] = image |
return example For improved performance on reading and writing large datasets stored in the cloud, this script uses the WebDataset format to create a preprocessing pipeline to apply transforms and create a dataset and dataloader for training. Images are processed and fed to the training loop without having to downl... |
wds.decode("pil", handler=wds.ignore_and_continue), |
wds.rename(image="jpg;png;jpeg;webp", text="text;txt;caption", handler=wds.warn_and_continue), |
wds.map(filter_keys({"image", "text"})), |
wds.map(transform), |
wds.to_tuple("image", "text"), |
] In the main() function, all the necessary components like the noise scheduler, tokenizers, text encoders, and VAE are loaded. The teacher UNet is also loaded here and then you can create a student UNet from the teacher UNet. The student UNet is updated by the optimizer during training. Copied teacher_unet = UNet2DC... |
args.pretrained_teacher_model, subfolder="unet", revision=args.teacher_revision |
) |
unet = UNet2DConditionModel(**teacher_unet.config) |
unet.load_state_dict(teacher_unet.state_dict(), strict=False) |
unet.train() Now you can create the optimizer to update the UNet parameters: Copied optimizer = optimizer_class( |
unet.parameters(), |
lr=args.learning_rate, |
betas=(args.adam_beta1, args.adam_beta2), |
weight_decay=args.adam_weight_decay, |
eps=args.adam_epsilon, |
) Create the dataset: Copied dataset = Text2ImageDataset( |
train_shards_path_or_url=args.train_shards_path_or_url, |
num_train_examples=args.max_train_samples, |
per_gpu_batch_size=args.train_batch_size, |
global_batch_size=args.train_batch_size * accelerator.num_processes, |
num_workers=args.dataloader_num_workers, |
resolution=args.resolution, |
shuffle_buffer_size=1000, |
pin_memory=True, |
persistent_workers=True, |
) |
train_dataloader = dataset.train_dataloader Next, you’re ready to setup the training loop and implement the latent consistency distillation method (see Algorithm 1 in the paper for more details). This section of the script takes care of adding noise to the latents, sampling and creating a guidance scale embedding, and ... |
noise_pred, |
start_timesteps, |
noisy_model_input, |
noise_scheduler.config.prediction_type, |
alpha_schedule, |
sigma_schedule, |
) |
model_pred = c_skip_start * noisy_model_input + c_out_start * pred_x_0 It gets the teacher model predictions and the LCM predictions next, calculates the loss, and then backpropagates it to the LCM. Copied if args.loss_type == "l2": |
loss = F.mse_loss(model_pred.float(), target.float(), reduction="mean") |
elif args.loss_type == "huber": |
loss = torch.mean( |
torch.sqrt((model_pred.float() - target.float()) ** 2 + args.huber_c**2) - args.huber_c |
) If you want to learn more about how the training loop works, check out the Understanding pipelines, models and schedulers tutorial which breaks down the basic pattern of the denoising process. Launch the script Now you’re ready to launch the training script and start distilling! For this guide, you’ll use the --... |
export OUTPUT_DIR="path/to/saved/model" |
accelerate launch train_lcm_distill_sd_wds.py \ |
--pretrained_teacher_model=$MODEL_DIR \ |
--output_dir=$OUTPUT_DIR \ |
--mixed_precision=fp16 \ |
--resolution=512 \ |
--learning_rate=1e-6 --loss_type="huber" --ema_decay=0.95 --adam_weight_decay=0.0 \ |
--max_train_steps=1000 \ |
--max_train_samples=4000000 \ |
--dataloader_num_workers=8 \ |
--train_shards_path_or_url="pipe:curl -L -s https://huggingface.co/datasets/laion/conceptual-captions-12m-webdataset/resolve/main/data/{00000..01099}.tar?download=true" \ |
--validation_steps=200 \ |
--checkpointing_steps=200 --checkpoints_total_limit=10 \ |
--train_batch_size=12 \ |
--gradient_checkpointing --enable_xformers_memory_efficient_attention \ |
--gradient_accumulation_steps=1 \ |
--use_8bit_adam \ |
--resume_from_checkpoint=latest \ |
--report_to=wandb \ |
--seed=453645634 \ |
--push_to_hub Once training is complete, you can use your new LCM for inference. Copied from diffusers import UNet2DConditionModel, DiffusionPipeline, LCMScheduler |
import torch |
unet = UNet2DConditionModel.from_pretrained("your-username/your-model", torch_dtype=torch.float16, variant="fp16") |
pipeline = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", unet=unet, torch_dtype=torch.float16, variant="fp16") |
pipeline.scheduler = LCMScheduler.from_config(pipe.scheduler.config) |
pipeline.to("cuda") |
prompt = "sushi rolls in the form of panda heads, sushi platter" |
image = pipeline(prompt, num_inference_steps=4, guidance_scale=1.0).images[0] LoRA LoRA is a training technique for significantly reducing the number of trainable parameters. As a result, training is faster and it is easier to store the resulting weights because they are a lot smaller (~100MBs). Use the train_lcm_dist... |
Prior Transformer The Prior Transformer was originally introduced in Hierarchical Text-Conditional Image Generation with CLIP Latents by Ramesh et al. It is used to predict CLIP image embeddings from CLIP text embeddings; image embeddings are predicted through a denoising diffusion process. The abstract from the paper ... |
The number of embeddings of the model input hidden_states additional_embeddings (int, optional, defaults to 4) — The number of additional tokens appended to the |
projected hidden_states. The actual length of the used hidden_states is num_embeddings + additional_embeddings. dropout (float, optional, defaults to 0.0) — The dropout probability to use. time_embed_act_fn (str, optional, defaults to ‘silu’) — |
The activation function to use to create timestep embeddings. norm_in_type (str, optional, defaults to None) — The normalization layer to apply on hidden states before |
passing to Transformer blocks. Set it to None if normalization is not needed. embedding_proj_norm_type (str, optional, defaults to None) — |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.