text stringlengths 0 5.54k |
|---|
... clean_images = batch["images"] |
... # Sample noise to add to the images |
... noise = torch.randn(clean_images.shape).to(clean_images.device) |
... bs = clean_images.shape[0] |
... # Sample a random timestep for each image |
... timesteps = torch.randint( |
... 0, noise_scheduler.config.num_train_timesteps, (bs,), device=clean_images.device |
... ).long() |
... # Add noise to 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, timesteps) |
... with accelerator.accumulate(model): |
... # Predict the noise residual |
... noise_pred = model(noisy_images, timesteps, return_dict=False)[0] |
... loss = F.mse_loss(noise_pred, noise) |
... accelerator.backward(loss) |
... accelerator.clip_grad_norm_(model.parameters(), 1.0) |
... optimizer.step() |
... lr_scheduler.step() |
... optimizer.zero_grad() |
... progress_bar.update(1) |
... logs = {"loss": loss.detach().item(), "lr": lr_scheduler.get_last_lr()[0], "step": global_step} |
... progress_bar.set_postfix(**logs) |
... accelerator.log(logs, step=global_step) |
... global_step += 1 |
... # After each epoch you optionally sample some demo images with evaluate() and save the model |
... if accelerator.is_main_process: |
... pipeline = DDPMPipeline(unet=accelerator.unwrap_model(model), scheduler=noise_scheduler) |
... if (epoch + 1) % config.save_image_epochs == 0 or epoch == config.num_epochs - 1: |
... evaluate(config, epoch, pipeline) |
... if (epoch + 1) % config.save_model_epochs == 0 or epoch == config.num_epochs - 1: |
... if config.push_to_hub: |
... repo.push_to_hub(commit_message=f"Epoch {epoch}", blocking=True) |
... else: |
... pipeline.save_pretrained(config.output_dir) |
Phew, that was quite a bit of code! But you’re finally ready to launch the training with 🤗 Accelerate’s notebook_launcher function. Pass the function the training loop, all the training arguments, and the number of processes (you can change this value to the number of GPUs available to you) to use for training: |
Copied |
>>> from accelerate import notebook_launcher |
>>> args = (config, model, noise_scheduler, optimizer, train_dataloader, lr_scheduler) |
>>> notebook_launcher(train_loop, args, num_processes=1) |
Once training is complete, take a look at the final 🦋 images 🦋 generated by your diffusion model! |
Copied |
>>> import glob |
>>> sample_images = sorted(glob.glob(f"{config.output_dir}/samples/*.png")) |
>>> Image.open(sample_images[-1]) |
Next steps |
Unconditional image generation is one example of a task that can be trained. You can explore other tasks and training techniques by visiting the 🧨 Diffusers Training Examples page. Here are some examples of what you can learn: |
Textual Inversion, an algorithm that teaches a model a specific visual concept and integrates it into the generated image. |
DreamBooth, a technique for generating personalized images of a subject given several input images of the subject. |
Guide to finetuning a Stable Diffusion model on your own dataset. |
Guide to using LoRA, a memory-efficient technique for finetuning really large models faster. |
Image-to-image Image-to-image is similar to text-to-image, but in addition to a prompt, you can also pass an initial image as a starting point for the diffusion process. The initial image is encoded to latent space and noise is added to it. Then the latent diffusion model takes a prompt and the noisy latent ... |
from diffusers import AutoPipelineForImage2Image |
from diffusers.utils import load_image, make_image_grid |
pipeline = AutoPipelineForImage2Image.from_pretrained( |
"kandinsky-community/kandinsky-2-2-decoder", torch_dtype=torch.float16, use_safetensors=True |
) |
pipeline.enable_model_cpu_offload() |
# remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed |
pipeline.enable_xformers_memory_efficient_attention() You’ll notice throughout the guide, we use enable_model_cpu_offload() and enable_xformers_memory_efficient_attention(), to save memory and increase inference speed. If you’re using PyTorch 2.0, then you don’t need to call enable_xformers_memory_efficient_attention()... |
image = pipeline(prompt, image=init_image).images[0] |
make_image_grid([init_image, image], rows=1, cols=2) initial image generated image Popular models The most popular image-to-image models are Stable Diffusion v1.5, Stable Diffusion XL (SDXL), and Kandinsky 2.2. The results from the Stable Diffusion and Kandinsky models vary due to their architecture differences and ... |
from diffusers import AutoPipelineForImage2Image |
from diffusers.utils import make_image_grid, load_image |
pipeline = AutoPipelineForImage2Image.from_pretrained( |
"runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16, variant="fp16", use_safetensors=True |
) |
pipeline.enable_model_cpu_offload() |
# remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed |
pipeline.enable_xformers_memory_efficient_attention() |
# prepare image |
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img-init.png" |
init_image = load_image(url) |
prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" |
# pass prompt and image to pipeline |
image = pipeline(prompt, image=init_image).images[0] |
make_image_grid([init_image, image], rows=1, cols=2) initial image generated image Stable Diffusion XL (SDXL) SDXL is a more powerful version of the Stable Diffusion model. It uses a larger base model, and an additional refiner model to increase the quality of the base model’s output. Read the SDXL guide for a more ... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.