This repository implements Stable Diffusion. As of today the repo provides code to do the following:
For autoencoder I provide code for vae as well as vqvae. But both the stages of training use VQVAE only. One can easily change that to vae if needed
For diffusion part, as of now it only implements DDPM with linear schedule.
Image - Top, Reconstructions - Below
conda activate <environment_name>git clone https://github.com/explainingai-code/StableDiffusion-PyTorch.gitcd StableDiffusion-PyTorchpip install -r requirements.txtmodels/weights/v0.1/vgg.pthFor setting up the mnist dataset follow - https://github.com/explainingai-code/Pytorch-VAE#data-preparation
Ensure directory structure is following
StableDiffusion-PyTorch
-> data
-> mnist
-> train
-> images
-> *.png
-> test
-> images
-> *.png
For setting up on CelebHQ for unconditional, simply download the images from the official repo of CelebMASK HQ here.
Ensure directory structure is the following
StableDiffusion-PyTorch
-> data
-> CelebAMask-HQ
-> CelebA-HQ-img
-> *.jpg
For CelebHQ for mask conditional LDM additionally do the following:
Ensure directory structure is the following
StableDiffusion-PyTorch
-> data
-> CelebAMask-HQ
-> CelebA-HQ-img
-> *.jpg
-> CelebAMask-HQ-mask-anno
-> 0/1/2/3.../14
-> *.png
python -m utils.create_celeb_mask from repo root to create the mask images from mask annotationsEnsure directory structure is the following
StableDiffusion-PyTorch
-> data
-> CelebAMask-HQ
-> CelebA-HQ-img
-> *.jpg
-> CelebAMask-HQ-mask-anno
-> 0/1/2/3.../14
-> *.png
-> CelebAMask-HQ-mask
-> *.png
For CelebHQ for text conditional LDM additionally do the following:
text link provided in the repo - https://github.com/IIGROUP/MM-CelebA-HQ-Dataset?tab=readme-ov-file#overviewceleba-captions folder, simply move this inside the data/CelebAMask-HQ folder as that is where the dataset class expects it to be.Ensure directory structure is the following
StableDiffusion-PyTorch
-> data
-> CelebAMask-HQ
-> CelebA-HQ-img
-> *.jpg
-> CelebAMask-HQ-mask-anno
-> 0/1/2/3.../14
-> *.png
-> CelebAMask-HQ-mask
-> *.png
-> celeba-caption
-> *.txt
Allows you to play with different components of ddpm and autoencoder training
config/mnist.yaml - Small autoencoder and ldm can even be trained on CPUconfig/celebhq.yaml - Configuration used for celebhq datasetRelevant configuration parameters
Most parameters are self explanatory but below I mention couple which are specific to this repo.
autoencoder_acc_steps : For accumulating gradients if image size is too large for larger batch sizessave_latents : Enable this to save the latents , during inference of autoencoder. That way ddpm training will be fasterThe repo provides training and inference for Mnist(Unconditional and Class Conditional) and CelebHQ (Unconditional, Text and/or Mask Conditional).
For working on your own dataset:
celebhq.yaml for guidance)mnist_dataset.py or celeb_dataset.py for guidanceOnce the config and dataset is setup:
mnist.yamlcelebhq.yamlpython -m tools.train_vqvae --config config/mnist.yaml for training vqvae with the desire config filepython -m tools.infer_vqvae --config config/mnist.yaml for generating reconstructions with right config file. Use save_latent in config to save the latent filesTrain the autoencoder first and setup dataset accordingly.
For training unconditional LDM map the dataset to the right class in train_ddpm_vqvae.py
python -m tools.train_ddpm_vqvae --config config/mnist.yaml for training unconditional ddpm using right configpython -m tools.sample_ddpm_vqvae --config config/mnist.yaml for generating images using trained ddpmFor training conditional models we need two changes:
Specifically the dataset getitem will return the following:
image_tensor for unconditional training(image_tensor, cond_input ) for conditional training where cond_input is a dictionary consisting of keys {class/text/image}The repo provides class conditional latent diffusion model training code for mnist dataset, so one can use that to follow the same for their own dataset
mnist_class_cond.yaml config file as a guide to create your class conditional config file.
Specifically following new keys need to be modified according to your dataset within ldm_params.condition_config:
condition_types: ['class']
class_condition_config :
num_classes : <number of classes: 10 for mnist>
cond_drop_prob : <probability of dropping class labels>
(image_tensor, {
'class' : {0/1/.../num_classes}
})
For training class conditional LDM map the dataset to the right class in train_ddpm_cond and run the below commands using desired config
python -m tools.train_ddpm_cond --config config/mnist_class_cond.yaml for training class conditional on mnistpython -m tools.sample_ddpm_class_cond --config config/mnist.yaml for generating images using class conditional trained ddpmThe repo provides text conditional latent diffusion model training code for celebhq dataset, so one can use that to follow the same for their own dataset
celebhq_text_cond.yaml config file as a guide to create your config file.
Specifically following new keys need to be modified according to your dataset within ldm_params. condition_config:
condition_types: [ 'text' ]
text_condition_config:
text_embed_model: 'clip' or 'bert'
text_embed_dim: 512 or 768
cond_drop_prob: 0.1
(image_tensor, {
'text' : 'a sample caption for image_tensor'
})
For training text conditional LDM map the dataset to the right class in train_ddpm_cond and run the below commands using desired config
python -m tools.train_ddpm_cond --config config/celebhq_text_cond.yaml for training text conditioned ldm on celebhqpython -m tools.sample_ddpm_text_cond --config config/celebhq_text_cond.yaml for generating images using text conditional trained ddpmThe repo provides text and mask conditional latent diffusion model training code for celebhq dataset, so one can use that to follow the same for their own dataset and can even use that train a mask only conditional ldm
celebhq_text_image_cond.yaml config file as a guide to create your config file.
Specifically following new keys need to be modified according to your dataset within ldm_params. condition_config:
condition_types: [ 'text', 'image' ]
text_condition_config:
text_embed_model: 'clip' or 'bert
text_embed_dim: 512 or 768
cond_drop_prob: 0.1
image_condition_config:
image_condition_input_channels: 18
image_condition_output_channels: 3
image_condition_h : 512
image_condition_w : 512
cond_drop_prob: 0.1
(image_tensor, {
'text' : 'a sample caption for image_tensor',
'image' : NUM_CLASSES x MASK_H x MASK_W
})
For training text unconditional LDM map the dataset to the right class in train_ddpm_cond and run the below commands using desired config
python -m tools.train_ddpm_cond --config config/celebhq_text_image_cond.yaml for training text and mask conditioned ldm on celebhqpython -m tools.sample_ddpm_text_image_cond --config config/celebhq_text_image_cond.yaml for generating images using text and mask conditional trained ddpmOutputs will be saved according to the configuration present in yaml files.
For every run a folder of task_name key in config will be created
During training of autoencoder the following output will be saved
task_name directorytask_name/vqvae_autoencoder_samplesDuring inference of autoencoder the following output will be saved
task_nametask_name/vqvae_latent_dir_name if mentioned in configDuring training and inference of ddpm following output will be saved
task_name directorytask_name/samples/*.png . The final decoded generated image will be x0_0.png. Images from x0_999.png to x0_1.png will be latent image predictions of denoising process from T=999 to T=1. Generated Image is at T=0task_name/cond_class_samples/*.png . The final decoded generated image will be x0_0.png. Images from x0_999.png to x0_1.png will be latent image predictions of denoising process from T=999 to T=1. Generated Image is at T=0task_name/cond_text_samples/*.png . The final decoded generated image will be x0_0.png . Images from x0_999.png to x0_1.png will be latent image predictions of denoising process from T=999 to T=1. Generated Image is at T=0task_name/cond_text_image_samples/*.png . The final decoded generated image will be x0_0.png. Images from x0_999.png to x0_1.png will be latent image predictions of denoising process from T=999 to T=1. Generated Image is at T=028 commits
2 commits
Python
100.0%
This repository implements Stable Diffusion. As of today the repo provides code to do the following:
For autoencoder I provide code for vae as well as vqvae. But both the stages of training use VQVAE only. One can easily change that to vae if needed
For diffusion part, as of now it only implements DDPM with linear schedule.
Image - Top, Reconstructions - Below
conda activate <environment_name>git clone https://github.com/explainingai-code/StableDiffusion-PyTorch.gitcd StableDiffusion-PyTorchpip install -r requirements.txtmodels/weights/v0.1/vgg.pthFor setting up the mnist dataset follow - https://github.com/explainingai-code/Pytorch-VAE#data-preparation
Ensure directory structure is following
StableDiffusion-PyTorch
-> data
-> mnist
-> train
-> images
-> *.png
-> test
-> images
-> *.png
For setting up on CelebHQ for unconditional, simply download the images from the official repo of CelebMASK HQ here.
Ensure directory structure is the following
StableDiffusion-PyTorch
-> data
-> CelebAMask-HQ
-> CelebA-HQ-img
-> *.jpg
For CelebHQ for mask conditional LDM additionally do the following:
Ensure directory structure is the following
StableDiffusion-PyTorch
-> data
-> CelebAMask-HQ
-> CelebA-HQ-img
-> *.jpg
-> CelebAMask-HQ-mask-anno
-> 0/1/2/3.../14
-> *.png
python -m utils.create_celeb_mask from repo root to create the mask images from mask annotationsEnsure directory structure is the following
StableDiffusion-PyTorch
-> data
-> CelebAMask-HQ
-> CelebA-HQ-img
-> *.jpg
-> CelebAMask-HQ-mask-anno
-> 0/1/2/3.../14
-> *.png
-> CelebAMask-HQ-mask
-> *.png
For CelebHQ for text conditional LDM additionally do the following:
text link provided in the repo - https://github.com/IIGROUP/MM-CelebA-HQ-Dataset?tab=readme-ov-file#overviewceleba-captions folder, simply move this inside the data/CelebAMask-HQ folder as that is where the dataset class expects it to be.Ensure directory structure is the following
StableDiffusion-PyTorch
-> data
-> CelebAMask-HQ
-> CelebA-HQ-img
-> *.jpg
-> CelebAMask-HQ-mask-anno
-> 0/1/2/3.../14
-> *.png
-> CelebAMask-HQ-mask
-> *.png
-> celeba-caption
-> *.txt
Allows you to play with different components of ddpm and autoencoder training
config/mnist.yaml - Small autoencoder and ldm can even be trained on CPUconfig/celebhq.yaml - Configuration used for celebhq datasetRelevant configuration parameters
Most parameters are self explanatory but below I mention couple which are specific to this repo.
autoencoder_acc_steps : For accumulating gradients if image size is too large for larger batch sizessave_latents : Enable this to save the latents , during inference of autoencoder. That way ddpm training will be fasterThe repo provides training and inference for Mnist(Unconditional and Class Conditional) and CelebHQ (Unconditional, Text and/or Mask Conditional).
For working on your own dataset:
celebhq.yaml for guidance)mnist_dataset.py or celeb_dataset.py for guidanceOnce the config and dataset is setup:
mnist.yamlcelebhq.yamlpython -m tools.train_vqvae --config config/mnist.yaml for training vqvae with the desire config filepython -m tools.infer_vqvae --config config/mnist.yaml for generating reconstructions with right config file. Use save_latent in config to save the latent filesTrain the autoencoder first and setup dataset accordingly.
For training unconditional LDM map the dataset to the right class in train_ddpm_vqvae.py
python -m tools.train_ddpm_vqvae --config config/mnist.yaml for training unconditional ddpm using right configpython -m tools.sample_ddpm_vqvae --config config/mnist.yaml for generating images using trained ddpmFor training conditional models we need two changes:
Specifically the dataset getitem will return the following:
image_tensor for unconditional training(image_tensor, cond_input ) for conditional training where cond_input is a dictionary consisting of keys {class/text/image}The repo provides class conditional latent diffusion model training code for mnist dataset, so one can use that to follow the same for their own dataset
mnist_class_cond.yaml config file as a guide to create your class conditional config file.
Specifically following new keys need to be modified according to your dataset within ldm_params.condition_config:
condition_types: ['class']
class_condition_config :
num_classes : <number of classes: 10 for mnist>
cond_drop_prob : <probability of dropping class labels>
(image_tensor, {
'class' : {0/1/.../num_classes}
})
For training class conditional LDM map the dataset to the right class in train_ddpm_cond and run the below commands using desired config
python -m tools.train_ddpm_cond --config config/mnist_class_cond.yaml for training class conditional on mnistpython -m tools.sample_ddpm_class_cond --config config/mnist.yaml for generating images using class conditional trained ddpmThe repo provides text conditional latent diffusion model training code for celebhq dataset, so one can use that to follow the same for their own dataset
celebhq_text_cond.yaml config file as a guide to create your config file.
Specifically following new keys need to be modified according to your dataset within ldm_params. condition_config:
condition_types: [ 'text' ]
text_condition_config:
text_embed_model: 'clip' or 'bert'
text_embed_dim: 512 or 768
cond_drop_prob: 0.1
(image_tensor, {
'text' : 'a sample caption for image_tensor'
})
For training text conditional LDM map the dataset to the right class in train_ddpm_cond and run the below commands using desired config
python -m tools.train_ddpm_cond --config config/celebhq_text_cond.yaml for training text conditioned ldm on celebhqpython -m tools.sample_ddpm_text_cond --config config/celebhq_text_cond.yaml for generating images using text conditional trained ddpmThe repo provides text and mask conditional latent diffusion model training code for celebhq dataset, so one can use that to follow the same for their own dataset and can even use that train a mask only conditional ldm
celebhq_text_image_cond.yaml config file as a guide to create your config file.
Specifically following new keys need to be modified according to your dataset within ldm_params. condition_config:
condition_types: [ 'text', 'image' ]
text_condition_config:
text_embed_model: 'clip' or 'bert
text_embed_dim: 512 or 768
cond_drop_prob: 0.1
image_condition_config:
image_condition_input_channels: 18
image_condition_output_channels: 3
image_condition_h : 512
image_condition_w : 512
cond_drop_prob: 0.1
(image_tensor, {
'text' : 'a sample caption for image_tensor',
'image' : NUM_CLASSES x MASK_H x MASK_W
})
For training text unconditional LDM map the dataset to the right class in train_ddpm_cond and run the below commands using desired config
python -m tools.train_ddpm_cond --config config/celebhq_text_image_cond.yaml for training text and mask conditioned ldm on celebhqpython -m tools.sample_ddpm_text_image_cond --config config/celebhq_text_image_cond.yaml for generating images using text and mask conditional trained ddpmOutputs will be saved according to the configuration present in yaml files.
For every run a folder of task_name key in config will be created
During training of autoencoder the following output will be saved
task_name directorytask_name/vqvae_autoencoder_samplesDuring inference of autoencoder the following output will be saved
task_nametask_name/vqvae_latent_dir_name if mentioned in configDuring training and inference of ddpm following output will be saved
task_name directorytask_name/samples/*.png . The final decoded generated image will be x0_0.png. Images from x0_999.png to x0_1.png will be latent image predictions of denoising process from T=999 to T=1. Generated Image is at T=0task_name/cond_class_samples/*.png . The final decoded generated image will be x0_0.png. Images from x0_999.png to x0_1.png will be latent image predictions of denoising process from T=999 to T=1. Generated Image is at T=0task_name/cond_text_samples/*.png . The final decoded generated image will be x0_0.png . Images from x0_999.png to x0_1.png will be latent image predictions of denoising process from T=999 to T=1. Generated Image is at T=0task_name/cond_text_image_samples/*.png . The final decoded generated image will be x0_0.png. Images from x0_999.png to x0_1.png will be latent image predictions of denoising process from T=999 to T=1. Generated Image is at T=028 commits
2 commits
Python
100.0%