Views
No views yet
train_text_to_image.py script shows how to fine-tune stable diffusion model on your own dataset.1git clone https://github.com/huggingface/diffusers
2cd diffusers
3pip install .pip install -r requirements.txtaccelerate configpeft>=0.6.0 installed in your environment.v1-4, so you'll need to visit its card, read the license and tick the checkbox if you agree.hf auth logingradient_checkpointing and mixed_precision it should be possible to fine tune the model on a single 24GB GPU. For higher batch_size and faster training it's better to use GPUs with >30GB memory.resolution to 768 if you are using the stable-diffusion-2 768x768 model.1export MODEL_NAME="CompVis/stable-diffusion-v1-4"
2export DATASET_NAME="lambdalabs/naruto-blip-captions"
3
4accelerate launch --mixed_precision="fp16" train_text_to_image.py \
5 --pretrained_model_name_or_path=$MODEL_NAME \
6 --dataset_name=$DATASET_NAME \
7 --use_ema \
8 --resolution=512 --center_crop --random_flip \
9 --train_batch_size=1 \
10 --gradient_accumulation_steps=4 \
11 --gradient_checkpointing \
12 --max_train_steps=15000 \
13 --learning_rate=1e-05 \
14 --max_grad_norm=1 \
15 --lr_scheduler="constant" --lr_warmup_steps=0 \
16 --output_dir="sd-naruto-model"datasets, you can find the instructions for how to do that in this document.
If you wish to use custom loading logic, you should modify the script, we have left pointers for that in the training script.1export MODEL_NAME="CompVis/stable-diffusion-v1-4"
2export TRAIN_DIR="path_to_your_dataset"
3
4accelerate launch --mixed_precision="fp16" train_text_to_image.py \
5 --pretrained_model_name_or_path=$MODEL_NAME \
6 --train_data_dir=$TRAIN_DIR \
7 --use_ema \
8 --resolution=512 --center_crop --random_flip \
9 --train_batch_size=1 \
10 --gradient_accumulation_steps=4 \
11 --gradient_checkpointing \
12 --max_train_steps=15000 \
13 --learning_rate=1e-05 \
14 --max_grad_norm=1 \
15 --lr_scheduler="constant" --lr_warmup_steps=0 \
16 --output_dir="sd-naruto-model"output_dir specified in the command. In this example it's sd-naruto-model. To load the fine-tuned model for inference just pass that path to StableDiffusionPipeline1import torch
2from diffusers import StableDiffusionPipeline
3
4model_path = "path_to_saved_model"
5pipe = StableDiffusionPipeline.from_pretrained(model_path, torch_dtype=torch.float16)
6pipe.to("cuda")
7
8image = pipe(prompt="yoda").images[0]
9image.save("yoda-naruto.png")1import torch
2from diffusers import StableDiffusionPipeline, UNet2DConditionModel
3
4model_path = "path_to_saved_model"
5unet = UNet2DConditionModel.from_pretrained(model_path + "/checkpoint-<N>/unet", torch_dtype=torch.float16)
6
7pipe = StableDiffusionPipeline.from_pretrained("<initial model>", unet=unet, torch_dtype=torch.float16)
8pipe.to("cuda")
9
10image = pipe(prompt="yoda").images[0]
11image.save("yoda-naruto.png")accelerate allows for seamless multi-GPU training. Follow the instructions here
for running distributed training with accelerate. Here is an example command:1export MODEL_NAME="CompVis/stable-diffusion-v1-4"
2export DATASET_NAME="lambdalabs/naruto-blip-captions"
3
4accelerate launch --mixed_precision="fp16" --multi_gpu train_text_to_image.py \
5 --pretrained_model_name_or_path=$MODEL_NAME \
6 --dataset_name=$DATASET_NAME \
7 --use_ema \
8 --resolution=512 --center_crop --random_flip \
9 --train_batch_size=1 \
10 --gradient_accumulation_steps=4 \
11 --gradient_checkpointing \
12 --max_train_steps=15000 \
13 --learning_rate=1e-05 \
14 --max_grad_norm=1 \
15 --lr_scheduler="constant" --lr_warmup_steps=0 \
16 --output_dir="sd-naruto-model"--snr_gamma argument. The recommended
value when using it is 5.0.snr_gamma set to 5.0)snr_gamma set to 1.0)epsilon (i.e., the noise) or the v_prediction. For both of these cases, the formulation of the Min-SNR weighting strategy that we have used holds.EMAModel class, we support a convenient method of tracking an exponential moving average of model parameters. This helps to smooth out noise in model parameter updates and generally improves model performance. If enabled with the --use_ema argument, the final model checkpoint that is saved at the end of training will use the EMA weights.--foreach_ema can be used to further reduce the overhead. If you are short on VRAM and still want to use EMA weights, you can store them in CPU RAM by using the --offload_ema argument. This will keep the EMA weights in pinned CPU memory during the training step. Then, once every model parameter update, it will transfer the EMA weights back to the GPU which can then update the parameters on the GPU, before sending them back to the CPU. Both of these transfers are set up as non-blocking, so CUDA devices should be able to overlap this transfer with other computations. With sufficient bandwidth between the host and device and a sufficiently long gap between model parameter updates, storing EMA weights in CPU RAM should have no additional performance overhead, as long as no other calls force synchronization.forward step in the training loop. You can turn on DREAM training by using the --dream_training argument. The --dream_detail_preservation argument controls the detail preservation variable p and is the default of 1 from the paper.scale parameter.MODEL_NAME and DATASET_NAME environment variables. Here, we will use Stable Diffusion v1-4 and the Narutos dataset.resolution to 768 if you are using the stable-diffusion-2 768x768 model.pip install wandb before training to automatically log images.1export MODEL_NAME="CompVis/stable-diffusion-v1-4"
2export DATASET_NAME="lambdalabs/naruto-blip-captions"--push_to_hub flag.hf auth login1accelerate launch --mixed_precision="fp16" train_text_to_image_lora.py \
2 --pretrained_model_name_or_path=$MODEL_NAME \
3 --dataset_name=$DATASET_NAME --caption_column="text" \
4 --resolution=512 --random_flip \
5 --train_batch_size=1 \
6 --num_train_epochs=100 --checkpointing_steps=5000 \
7 --learning_rate=1e-04 --lr_scheduler="constant" --lr_warmup_steps=0 \
8 --seed=42 \
9 --output_dir="sd-naruto-model-lora" \
10 --validation_prompt="cute dragon creature" --report_to="wandb"train_text_to_image_lora.py in consumer GPUs like T4 or V100.StableDiffusionPipeline after loading the trained LoRA weights. You
need to pass the output_dir for loading the LoRA weights which, in this case, is sd-naruto-model-lora.1from diffusers import StableDiffusionPipeline
2import torch
3
4model_path = "sayakpaul/sd-model-finetuned-lora-t4"
5pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", torch_dtype=torch.float16)
6pipe.unet.load_attn_procs(model_path)
7pipe.to("cuda")
8
9prompt = "A naruto with green eyes and red legs."
10image = pipe(prompt, num_inference_steps=30, guidance_scale=7.5).images[0]
11image.save("naruto.png")base_model tag (such as this), then
you can do:1from huggingface_hub.repocard import RepoCard
2
3lora_model_id = "sayakpaul/sd-model-finetuned-lora-t4"
4card = RepoCard.load(lora_model_id)
5base_model_id = card.data.to_dict()["base_model"]
6
7pipe = StableDiffusionPipeline.from_pretrained(base_model_id, torch_dtype=torch.float16)
8...pip install -U -r requirements_flax.txt1export MODEL_NAME="duongna/stable-diffusion-v1-4-flax"
2export DATASET_NAME="lambdalabs/naruto-blip-captions"
3
4python train_text_to_image_flax.py \
5 --pretrained_model_name_or_path=$MODEL_NAME \
6 --dataset_name=$DATASET_NAME \
7 --resolution=512 --center_crop --random_flip \
8 --train_batch_size=1 \
9 --mixed_precision="fp16" \
10 --max_train_steps=15000 \
11 --learning_rate=1e-05 \
12 --max_grad_norm=1 \
13 --output_dir="sd-naruto-model"datasets, you can find the instructions for how to do that in this document.
If you wish to use custom loading logic, you should modify the script, we have left pointers for that in the training script.1export MODEL_NAME="duongna/stable-diffusion-v1-4-flax"
2export TRAIN_DIR="path_to_your_dataset"
3
4python train_text_to_image_flax.py \
5 --pretrained_model_name_or_path=$MODEL_NAME \
6 --train_data_dir=$TRAIN_DIR \
7 --resolution=512 --center_crop --random_flip \
8 --train_batch_size=1 \
9 --mixed_precision="fp16" \
10 --max_train_steps=15000 \
11 --learning_rate=1e-05 \
12 --max_grad_norm=1 \
13 --output_dir="sd-naruto-model"--enable_xformers_memory_efficient_attention argument to the script.v0.0.16 cannot be used for training in some GPUs. If you observe that problem, please install a development version as indicated in that comment.train_text_to_image_sdxl.py script. Please refer to the docs here.train_text_to_image_lora_sdxl.py script. Please refer to the docs here.