Skip to content
Snippets Groups Projects
Commit 136605a9 authored by Dellandrea Emmanuel's avatar Dellandrea Emmanuel
Browse files

Create Subject_9_Diffusion_Models.ipynb

parent 494c2c34
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
### **_Deep Learning - Bsc Data Science for Responsible Business - Centrale Lyon_**
2024-2025
Emmanuel Dellandréa
%% Cell type:markdown id: tags:
# Practical Session 9 - Diffusion Models
Subject written by Bruno Machado
<p align="center">
<img height=300px src="https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2f5d7da7-52db-4104-9742-a0b4555d8dd6_1300x387.png"/></p>
<p align="center"></p>
%% Cell type:markdown id: tags:
The objective of this tutorial is to discover Diffusion Models that are probabilistic generative models which learn to generate data by iteratively refining random noise through a reverse diffusion process. Given a sample of data, noise is progressively added in small steps until it becomes pure noise. Then, a neural network is trained to reverse this process and generate realistic data from noise.
Diffusion models have gained popularity due to their ability to generate high-quality, diverse, and detailed content, surpassing GANs in the quality of the generated images.
In this assignment we will focus on DDPMs, which were introduced in this [paper](https://arxiv.org/abs/2006.11239) and laid the foundation for generative diffusion models.
The notebook contains code cells with the **"# TO DO"** comments. Your goal is to complete these cells and run the proposed experiments.
As the computation is heavy, particularly during training, we encourage you to use a GPU. If your laptob is not equiped, you may use one of these remote jupyter servers, where you can select the execution on GPU :
1) [jupyter.mi90.ec-lyon.fr](https://jupyter.mi90.ec-lyon.fr/)
This server is accessible within the campus network. If outside, you need to use a VPN. Before executing the notebook, select the kernel "Python PyTorch" to run it on GPU and have access to PyTorch module.
2) [Google Colaboratory](https://colab.research.google.com/)
Before executing the notebook, select the execution on GPU : "Exécution" Menu -> "Modifier le type d'exécution" and select "T4 GPU".
%% Cell type:markdown id: tags:
# Part1: Diffusion
%% Cell type:code id: tags:
``` python
# First import useful libraries
import torch
import numpy as np
import matplotlib.pyplot as plt
import torchvision.transforms as transforms
```
%% Cell type:code id: tags:
``` python
# device = torch.device("mps")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
```
%% Cell type:markdown id: tags:
As in the previous session, we will use the MNIST dataset.
%% Cell type:code id: tags:
``` python
# TO DO: your code here to load the MNIST dataset. The size of the images should be set to 64x64.
mnist_dataset =
mnist_dataloader =
```
%% Cell type:markdown id: tags:
Auxiliary functions for plotting images
%% Cell type:code id: tags:
``` python
def reverse_transform(image):
image = image.numpy().transpose((1, 2, 0))
image = np.clip(image, 0, 1)
image = (image * 255).astype(np.uint8)
return image
def plot1xNArray(images, labels):
f, axarr = plt.subplots(1, len(images))
for image, ax, label in zip(images, axarr, labels):
ax.imshow(image, cmap='gray')
ax.axis('off')
ax.set_title(label)
```
%% Cell type:markdown id: tags:
In order to train the model with the diffusion process, we will use a noise scheduler, which will be in charge of the forward diffusion process. The scheduler takes an image, a sample of random noise and a timestep, and return a noisy image for the corresponding timestep. Noise is progressively added to the image at each timestep, therefore a noisy image at timestep 0 will have barely any noise while a noisy image at the maximum timestep will be basically just noise.
Let's create a noise scheduler with 1000 max timesteps and visualize some noise images.
We will use the diffusers library from Hugging Face, which provides several tools for training and using diffusion models.
%% Cell type:code id: tags:
``` python
from diffusers import DDPMScheduler
# TO DO: Create the scheduler
noise_scheduler =
image, _ = mnist_dataset[0]
# TO DO: Create a noise tensor sampled from a normal distribution with the same shape as the image
noise =
images, labels = [reverse_transform(image)], ["Original"]
for i in [100, 250, 400, 900]:
timestep = torch.LongTensor([i])
noisy_image = noise_scheduler.add_noise(image, noise, timestep)
images.append(reverse_transform(noisy_image))
labels.append(f"t={i}")
plot1xNArray(images, labels)
```
%% Cell type:markdown id: tags:
For the reverse diffusion process we will use a neural network. Given a noisy image and the corresponding timestep, the goal of the neural network is to predict the noise, which allows for the denoising.
For the model, we will have a similar architecture as we used for the cGAN generator, a 2D UNet with a few modifications. The main difference will be that we have to indicate to the model which timestep is currently being denoised. For that purpose, a timestep embedding is added, therefore the model has 2 inputs, the noisy image and the corresponding timestep.
In this exercise, we will use an UNet implementation from the diffusers library, which already has the timestep embedding included.
%% Cell type:code id: tags:
``` python
from diffusers import UNet2DModel
# TO DO: Complete the parameters
diffusion_backbone = UNet2DModel(
block_out_channels=(64, 128, 256, 512),
down_block_types=("DownBlock2D", "DownBlock2D", "DownBlock2D", "DownBlock2D"),
up_block_types=("UpBlock2D", "UpBlock2D", "UpBlock2D", "UpBlock2D"),
sample_size=,
in_channels=,
out_channels=,
).to(device)
# Optimizer
optimizer = torch.optim.AdamW(diffusion_backbone.parameters(), lr=1e-4)
print(diffusion_backbone)
```
%% Cell type:markdown id: tags:
### Now, let's train the model.
%% Cell type:code id: tags:
``` python
# ----------------
# Training Loop
# ----------------
torch.backends.cudnn.deterministic = True
losses = []
num_epochs = 5
print_every = 100
diffusion_backbone.train()
for epoch in range(num_epochs):
for i, batch in enumerate(mnist_dataloader):
# Zero the gradients
optimizer.zero_grad()
# Send input to device
images = batch[0].to(device)
# Generate noisy images, different timestep for each image in the batch
timesteps = torch.randint(noise_scheduler.config.num_train_timesteps, (images.size(0),), device=device)
# TO DO: Complete the code
noise =
noisy_images =
# Forward pass
residual = diffusion_backbone(noisy_images, timesteps).sample
# TO DO: Compute the loss
loss =
loss.backward()
optimizer.step()
# Print stats
if i % print_every == 0:
print(f'Epoch [{epoch+1}/{num_epochs}][{i}/{len(mnist_dataloader)}] | loss: {loss.item():6.4f}')
losses.append(loss.item())
torch.save(diffusion_backbone.state_dict(), f"diffusion_{epoch+1}.pth")
```
%% Cell type:markdown id: tags:
If the training takes too long, you can download an already trained model from [this link](https://partage.liris.cnrs.fr/index.php/s/AP2t6b3w8SM4Bp5) and use it for inference.
%% Cell type:code id: tags:
``` python
# TO DO: Add the path to the model checkpoint for loading the model
diffusion_backbone.load_state_dict(torch.load())
diffusion_backbone.eval()
```
%% Cell type:markdown id: tags:
### Time to generate some images.
During training, for each data sample, we take a random timestep and the corresponding noisy image to give it as input to our model. With sufficient training, the model should learn how to predict the noise in a noisy image for all possible timesteps.
During inference, to generate an image, we will start from pure noise and step by step predict the noise to go from one noisy image to the next, progressively denoising the image until we reach the timestep 0, in which we should have an image without any noise.
%% Cell type:code id: tags:
``` python
from tqdm import tqdm
# Start the image as random noise
image = torch.randn((10, 1, 64, 64)).to(device)
# Create a list of images and labels for visualization
images, labels = [(image / 2 + 0.5).clamp(0, 1).cpu().permute(0, 2, 3, 1).numpy()], ["Original"]
# Use the scheduler to iterate over timesteps
noise_scheduler.set_timesteps(1000)
for timestep in tqdm(noise_scheduler.timesteps):
with torch.no_grad():
residual = diffusion_backbone(image, timestep).sample
image = noise_scheduler.step(residual, timestep, image).prev_sample
if timestep.item() % 200 == 0:
images.append((image / 2 + 0.5).clamp(0, 1).cpu().permute(0, 2, 3, 1).numpy())
labels.append(f"t={timestep.item()}")
for i in range(images[0].shape[0]):
plot1xNArray([img[i] for img in images], labels)
```
%% Cell type:markdown id: tags:
The diffusers library also provides *Pipeline* classes, which are wrappers around the model that abstracts the inference loop implemented above.
We can create a pipeline, giving it the trained model and the noise scheduler, and use it to generate images. In this case, we will only have access to the final image, generated on the last timestep, but not the intermediary images from the denoising process.
%% Cell type:code id: tags:
``` python
from diffusers import DDPMPipeline
pipeline = DDPMPipeline(diffusion_backbone, noise_scheduler)
generated_images = pipeline(10, output_type="np")
f, axarr = plt.subplots(1, len(generated_images["images"]))
for image, ax in zip(generated_images["images"], axarr):
ax.imshow(image, cmap='gray')
ax.axis('off')
```
%% Cell type:markdown id: tags:
# Part 2: What about those beautiful images ?
%% Cell type:markdown id: tags:
<p align="center">
<img height=300px src="https://huggingface.co/stabilityai/stable-diffusion-3.5-large/media/main/sd3.5_large_demo.png"/></p>
<p align="center"></p>
In this exercise we achieved decent results for very simple datasets. But we are quite far from those beautiful AI generated images we can find online. That is for 2 main reasons:
- Model size: due to the computation and time constrains, we can't really train very large models
- Dataset size: due to the same constrains, we can't use very complex and large datasets, which requires larger models and longer training times.
Fortunately, even though we can't train those large models with the available hardware and time, we can at least use them for inference !
The goal of this part is to learn how to retrieve and use a pre-trained diffusion models and also to get creative to come up with some nice prompts to generate outstanding images.
We are going to use Stable Diffusion 3.5, which is a state of the art open-source text conditioned model. It takes a prompt in natural language and use it to guide the diffusion process. This type of models are trained with image-text pairs, but can generalize beyond the pairs seen during training, being able to mix several different concepts into a single image.
In order to save memory, we will use quantization, which consists into converting the model weights types from float16 into float4. That simply means that each model weight will be stored using only 4 bits instead of 16 bits. That allows us to run the model in GPUs with less VRAM and have faster inference, with a small drop in the quality of the results.
%% Cell type:markdown id: tags:
For this part of the assignment restart the notebook kernel to be sure your GPU memory is empty. The memory usage can be verified using the command `nvidia-smi` in a terminal. If your GPU has 2GB of VRAM or less, the model will probably not fit into memory even with quantization. In that case, use Google Colab for this part or use the smaller model indicated below. If you are not happy with the results and have plenty of VRAM available, feel free to increase the quantization to 8 bits or even load the model without quantization.
%% Cell type:code id: tags:
``` python
from diffusers import BitsAndBytesConfig, SD3Transformer2DModel
from diffusers import StableDiffusion3Pipeline
model_id = "stabilityai/stable-diffusion-3.5-medium"
nf4_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.float16
)
model_nf4 = SD3Transformer2DModel.from_pretrained(
model_id,
subfolder="transformer",
quantization_config=nf4_config,
torch_dtype=torch.float16
)
pipeline = StableDiffusion3Pipeline.from_pretrained(
model_id,
transformer=model_nf4,
torch_dtype=torch.float16
)
pipeline.enable_model_cpu_offload()
```
%% Cell type:code id: tags:
``` python
# TO DO: test different prompts and visualize the generated images
# Once you are happy with the results, you can save 3 different images as png file with the correspondent prompts in a text file
prompt =
image = pipeline(
prompt=prompt,
num_inference_steps=40,
guidance_scale=4.5,
max_sequence_length=512
).images[0]
image.save("generated_image.png")
```
%% Cell type:markdown id: tags:
If even with the quantization you still run out of GPU memory and you can't use Google Colab, you can use the following code instead, which uses a much smaller model (the results won't be as near as impressive, but it should be able to run even on a CPU, if you have a little patience)
%% Cell type:code id: tags:
``` python
from diffusers import DiffusionPipeline
pipe = DiffusionPipeline.from_pretrained("OFA-Sys/small-stable-diffusion-v0").to(device)
```
%% Cell type:code id: tags:
``` python
# TO DO: test different prompts and visualize the generated images
prompt =
image = pipe(prompt).images[0]
image.save("not_as_good_generated_image.png")
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment