import{S as Tn,i as qn,s as In,e as o,k as c,w as u,t as n,M as Nn,c as i,d as t,m as f,a as r,x as d,h as l,b as h,N as Cn,G as a,g as p,y as m,q as _,o as g,B as v,v as Rn}from"../../chunks/vendor-hf-doc-builder.js";import{T as jn}from"../../chunks/Tip-hf-doc-builder.js";import{I as P}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as y}from"../../chunks/CodeBlock-hf-doc-builder.js";function Un(lt){let b,$,w,k,N;return{c(){b=o("p"),$=n("Dreambooth fine-tuning is very sensitive to hyperparameters and easy to overfit. We recommend you take a look at our "),w=o("a"),k=n("in-depth analysis"),N=n(" with recommended settings for different subjects, and go from there."),this.h()},l(E){b=i(E,"P",{});var S=r(b);$=l(S,"Dreambooth fine-tuning is very sensitive to hyperparameters and easy to overfit. We recommend you take a look at our "),w=i(S,"A",{href:!0,rel:!0});var W=r(w);k=l(W,"in-depth analysis"),W.forEach(t),N=l(S," with recommended settings for different subjects, and go from there."),S.forEach(t),this.h()},h(){h(w,"href","https://huggingface.co/blog/dreambooth"),h(w,"rel","nofollow")},m(E,S){p(E,b,S),a(b,$),a(b,w),a(w,k),a(b,N)},d(E){E&&t(b)}}}function On(lt){let b;return{c(){b=n("Training the text encoder requires additional memory, so training won't fit on a 16GB GPU. You'll need at least 24GB VRAM to use this option.")},l($){b=l($,"Training the text encoder requires additional memory, so training won't fit on a 16GB GPU. You'll need at least 24GB VRAM to use this option.")},m($,w){p($,b,w)},d($){$&&t(b)}}}function Mn(lt){let b,$,w,k,N,E,S,W,Vs,_a,De,xe,Hs,Ys,ga,J,pt,rr,Fs,Ae,Ws,Pe,Js,Zs,va,Z,Ks,Se,Qs,Xs,ba,K,wa,C,Q,$t,je,eo,Et,to,$a,R,X,yt,Te,ao,kt,so,Ea,j,oo,Dt,io,ro,xt,no,lo,ya,qe,ka,ee,po,ht,ho,co,Da,te,fo,Ie,uo,mo,xa,Ne,Aa,T,_o,At,go,vo,Ce,bo,wo,Pa,ae,$o,Pt,Eo,yo,Sa,U,se,St,Re,ko,jt,Do,ja,oe,xo,Ue,Ao,Po,Ta,ct,So,qa,Oe,Ia,O,ie,Tt,Me,jo,qt,To,Na,ft,qo,Ca,re,Io,It,No,Co,Ra,Le,Ua,M,ne,Nt,Ge,Ro,Ct,Uo,Oa,ut,Oo,Ma,Be,La,D,Mo,Rt,Lo,Go,Ut,Bo,zo,Ot,Vo,Ho,Ga,L,le,Mt,ze,Yo,Lt,Fo,Ba,q,Wo,Gt,Jo,Zo,Bt,Ko,Qo,za,Ve,Va,dt,Xo,Ha,G,pe,zt,He,ei,Vt,ti,Ya,mt,ai,Fa,B,Ht,si,oi,Yt,ii,ri,Wa,Ye,Ja,he,ni,Ft,li,pi,Za,Fe,Ka,z,ce,Wt,We,hi,Jt,ci,Qa,fe,fi,Je,ui,di,Xa,Ze,es,ue,mi,Zt,_i,gi,ts,Ke,as,V,de,Kt,Qe,vi,Qt,bi,ss,x,wi,Xt,$i,Ei,ea,yi,ki,Xe,Di,xi,os,me,Ai,ta,Pi,Si,is,_e,rs,et,ns,H,ge,aa,tt,ji,sa,Ti,ls,ve,qi,at,Ii,Ni,ps,I,Ci,oa,Ri,Ui,st,Oi,Mi,hs,be,Li,ia,Gi,Bi,cs,ot,fs,Y,we,ra,it,zi,na,Vi,us,A,Hi,la,Yi,Fi,pa,Wi,Ji,ha,Zi,Ki,ds,F,ca,Qi,Xi,fa,er,tr,ms,rt,_s,$e,ar,_t,sr,or,gs;return E=new P({}),K=new jn({props:{warning:!0,$$slots:{default:[Un]},$$scope:{ctx:lt}}}),je=new P({}),Te=new P({}),qe=new y({props:{code:`pip install git+https://github.com/huggingface/diffusers
pip install -U -r diffusers/examples/dreambooth/requirements.txt`,highlighted:`pip install git+https://github.com/huggingface/diffusers
pip install -U -r diffusers/examples/dreambooth/requirements.txt`}}),Ne=new y({props:{code:"accelerate config",highlighted:"accelerate config"}}),Re=new P({}),Oe=new y({props:{code:`export MODEL_NAME="CompVis/stable-diffusion-v1-4"
export INSTANCE_DIR="path_to_training_images"
export OUTPUT_DIR="path_to_saved_model"

accelerate launch train_dreambooth.py \\
  --pretrained_model_name_or_path=$MODEL_NAME  \\
  --instance_data_dir=$INSTANCE_DIR \\
  --output_dir=$OUTPUT_DIR \\
  --instance_prompt="a photo of sks dog" \\
  --resolution=512 \\
  --train_batch_size=1 \\
  --gradient_accumulation_steps=1 \\
  --learning_rate=5e-6 \\
  --lr_scheduler="constant" \\
  --lr_warmup_steps=0 \\
  --max_train_steps=400`,highlighted:`<span class="hljs-built_in">export</span> MODEL_NAME=<span class="hljs-string">&quot;CompVis/stable-diffusion-v1-4&quot;</span>
<span class="hljs-built_in">export</span> INSTANCE_DIR=<span class="hljs-string">&quot;path_to_training_images&quot;</span>
<span class="hljs-built_in">export</span> OUTPUT_DIR=<span class="hljs-string">&quot;path_to_saved_model&quot;</span>

accelerate launch train_dreambooth.py \\
  --pretrained_model_name_or_path=<span class="hljs-variable">$MODEL_NAME</span>  \\
  --instance_data_dir=<span class="hljs-variable">$INSTANCE_DIR</span> \\
  --output_dir=<span class="hljs-variable">$OUTPUT_DIR</span> \\
  --instance_prompt=<span class="hljs-string">&quot;a photo of sks dog&quot;</span> \\
  --resolution=512 \\
  --train_batch_size=1 \\
  --gradient_accumulation_steps=1 \\
  --learning_rate=5e-6 \\
  --lr_scheduler=<span class="hljs-string">&quot;constant&quot;</span> \\
  --lr_warmup_steps=0 \\
  --max_train_steps=400`}}),Me=new P({}),Le=new y({props:{code:`export MODEL_NAME="CompVis/stable-diffusion-v1-4"
export INSTANCE_DIR="path_to_training_images"
export CLASS_DIR="path_to_class_images"
export OUTPUT_DIR="path_to_saved_model"

accelerate launch train_dreambooth.py \\
  --pretrained_model_name_or_path=$MODEL_NAME  \\
  --instance_data_dir=$INSTANCE_DIR \\
  --class_data_dir=$CLASS_DIR \\
  --output_dir=$OUTPUT_DIR \\
  --with_prior_preservation --prior_loss_weight=1.0 \\
  --instance_prompt="a photo of sks dog" \\
  --class_prompt="a photo of dog" \\
  --resolution=512 \\
  --train_batch_size=1 \\
  --gradient_accumulation_steps=1 \\
  --learning_rate=5e-6 \\
  --lr_scheduler="constant" \\
  --lr_warmup_steps=0 \\
  --num_class_images=200 \\
  --max_train_steps=800`,highlighted:`<span class="hljs-built_in">export</span> MODEL_NAME=<span class="hljs-string">&quot;CompVis/stable-diffusion-v1-4&quot;</span>
<span class="hljs-built_in">export</span> INSTANCE_DIR=<span class="hljs-string">&quot;path_to_training_images&quot;</span>
<span class="hljs-built_in">export</span> CLASS_DIR=<span class="hljs-string">&quot;path_to_class_images&quot;</span>
<span class="hljs-built_in">export</span> OUTPUT_DIR=<span class="hljs-string">&quot;path_to_saved_model&quot;</span>

accelerate launch train_dreambooth.py \\
  --pretrained_model_name_or_path=<span class="hljs-variable">$MODEL_NAME</span>  \\
  --instance_data_dir=<span class="hljs-variable">$INSTANCE_DIR</span> \\
  --class_data_dir=<span class="hljs-variable">$CLASS_DIR</span> \\
  --output_dir=<span class="hljs-variable">$OUTPUT_DIR</span> \\
  --with_prior_preservation --prior_loss_weight=1.0 \\
  --instance_prompt=<span class="hljs-string">&quot;a photo of sks dog&quot;</span> \\
  --class_prompt=<span class="hljs-string">&quot;a photo of dog&quot;</span> \\
  --resolution=512 \\
  --train_batch_size=1 \\
  --gradient_accumulation_steps=1 \\
  --learning_rate=5e-6 \\
  --lr_scheduler=<span class="hljs-string">&quot;constant&quot;</span> \\
  --lr_warmup_steps=0 \\
  --num_class_images=200 \\
  --max_train_steps=800`}}),Ge=new P({}),Be=new y({props:{code:"  --checkpointing_steps=500",highlighted:"  --checkpointing_steps=500"}}),ze=new P({}),Ve=new y({props:{code:'  --resume_from_checkpoint="checkpoint-1500"',highlighted:'  --resume_from_checkpoint=<span class="hljs-string">&quot;checkpoint-1500&quot;</span>'}}),He=new P({}),Ye=new y({props:{code:`from diffusers import DiffusionPipeline, UNet2DConditionModel
from transformers import CLIPTextModel
import torch

# Load the pipeline with the same arguments (model, revision) that were used for training
model_id = "CompVis/stable-diffusion-v1-4"

unet = UNet2DConditionModel.from_pretrained("/sddata/dreambooth/daruma-v2-1/checkpoint-100/unet")

# if you have trained with \`--args.train_text_encoder\` make sure to also load the text encoder
text_encoder = CLIPTextModel.from_pretrained("/sddata/dreambooth/daruma-v2-1/checkpoint-100/text_encoder")

pipeline = DiffusionPipeline.from_pretrained(model_id, unet=unet, text_encoder=text_encoder, dtype=torch.float16)
pipeline.to("cuda")

# Perform inference, or save, or push to the hub
pipeline.save_pretrained("dreambooth-pipeline")`,highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline, UNet2DConditionModel
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> CLIPTextModel
<span class="hljs-keyword">import</span> torch

<span class="hljs-comment"># Load the pipeline with the same arguments (model, revision) that were used for training</span>
model_id = <span class="hljs-string">&quot;CompVis/stable-diffusion-v1-4&quot;</span>

unet = UNet2DConditionModel.from_pretrained(<span class="hljs-string">&quot;/sddata/dreambooth/daruma-v2-1/checkpoint-100/unet&quot;</span>)

<span class="hljs-comment"># if you have trained with \`--args.train_text_encoder\` make sure to also load the text encoder</span>
text_encoder = CLIPTextModel.from_pretrained(<span class="hljs-string">&quot;/sddata/dreambooth/daruma-v2-1/checkpoint-100/text_encoder&quot;</span>)

pipeline = DiffusionPipeline.from_pretrained(model_id, unet=unet, text_encoder=text_encoder, dtype=torch.float16)
pipeline.to(<span class="hljs-string">&quot;cuda&quot;</span>)

<span class="hljs-comment"># Perform inference, or save, or push to the hub</span>
pipeline.save_pretrained(<span class="hljs-string">&quot;dreambooth-pipeline&quot;</span>)`}}),Fe=new y({props:{code:`from accelerate import Accelerator
from diffusers import DiffusionPipeline

# Load the pipeline with the same arguments (model, revision) that were used for training
model_id = "CompVis/stable-diffusion-v1-4"
pipeline = DiffusionPipeline.from_pretrained(model_id)

accelerator = Accelerator()

# Use text_encoder if \`--train_text_encoder\` was used for the initial training
unet, text_encoder = accelerator.prepare(pipeline.unet, pipeline.text_encoder)

# Restore state from a checkpoint path. You have to use the absolute path here.
accelerator.load_state("/sddata/dreambooth/daruma-v2-1/checkpoint-100")

# Rebuild the pipeline with the unwrapped models (assignment to .unet and .text_encoder should work too)
pipeline = DiffusionPipeline.from_pretrained(
    model_id,
    unet=accelerator.unwrap_model(unet),
    text_encoder=accelerator.unwrap_model(text_encoder),
)

# Perform inference, or save, or push to the hub
pipeline.save_pretrained("dreambooth-pipeline")`,highlighted:`<span class="hljs-keyword">from</span> accelerate <span class="hljs-keyword">import</span> Accelerator
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline

<span class="hljs-comment"># Load the pipeline with the same arguments (model, revision) that were used for training</span>
model_id = <span class="hljs-string">&quot;CompVis/stable-diffusion-v1-4&quot;</span>
pipeline = DiffusionPipeline.from_pretrained(model_id)

accelerator = Accelerator()

<span class="hljs-comment"># Use text_encoder if \`--train_text_encoder\` was used for the initial training</span>
unet, text_encoder = accelerator.prepare(pipeline.unet, pipeline.text_encoder)

<span class="hljs-comment"># Restore state from a checkpoint path. You have to use the absolute path here.</span>
accelerator.load_state(<span class="hljs-string">&quot;/sddata/dreambooth/daruma-v2-1/checkpoint-100&quot;</span>)

<span class="hljs-comment"># Rebuild the pipeline with the unwrapped models (assignment to .unet and .text_encoder should work too)</span>
pipeline = DiffusionPipeline.from_pretrained(
    model_id,
    unet=accelerator.unwrap_model(unet),
    text_encoder=accelerator.unwrap_model(text_encoder),
)

<span class="hljs-comment"># Perform inference, or save, or push to the hub</span>
pipeline.save_pretrained(<span class="hljs-string">&quot;dreambooth-pipeline&quot;</span>)`}}),We=new P({}),Ze=new y({props:{code:"pip install bitsandbytes",highlighted:"pip install bitsandbytes"}}),Ke=new y({props:{code:`export MODEL_NAME="CompVis/stable-diffusion-v1-4"
export INSTANCE_DIR="path_to_training_images"
export CLASS_DIR="path_to_class_images"
export OUTPUT_DIR="path_to_saved_model"

accelerate launch train_dreambooth.py \\
  --pretrained_model_name_or_path=$MODEL_NAME  \\
  --instance_data_dir=$INSTANCE_DIR \\
  --class_data_dir=$CLASS_DIR \\
  --output_dir=$OUTPUT_DIR \\
  --with_prior_preservation --prior_loss_weight=1.0 \\
  --instance_prompt="a photo of sks dog" \\
  --class_prompt="a photo of dog" \\
  --resolution=512 \\
  --train_batch_size=1 \\
  --gradient_accumulation_steps=2 --gradient_checkpointing \\
  --use_8bit_adam \\
  --learning_rate=5e-6 \\
  --lr_scheduler="constant" \\
  --lr_warmup_steps=0 \\
  --num_class_images=200 \\
  --max_train_steps=800`,highlighted:`<span class="hljs-built_in">export</span> MODEL_NAME=<span class="hljs-string">&quot;CompVis/stable-diffusion-v1-4&quot;</span>
<span class="hljs-built_in">export</span> INSTANCE_DIR=<span class="hljs-string">&quot;path_to_training_images&quot;</span>
<span class="hljs-built_in">export</span> CLASS_DIR=<span class="hljs-string">&quot;path_to_class_images&quot;</span>
<span class="hljs-built_in">export</span> OUTPUT_DIR=<span class="hljs-string">&quot;path_to_saved_model&quot;</span>

accelerate launch train_dreambooth.py \\
  --pretrained_model_name_or_path=<span class="hljs-variable">$MODEL_NAME</span>  \\
  --instance_data_dir=<span class="hljs-variable">$INSTANCE_DIR</span> \\
  --class_data_dir=<span class="hljs-variable">$CLASS_DIR</span> \\
  --output_dir=<span class="hljs-variable">$OUTPUT_DIR</span> \\
  --with_prior_preservation --prior_loss_weight=1.0 \\
  --instance_prompt=<span class="hljs-string">&quot;a photo of sks dog&quot;</span> \\
  --class_prompt=<span class="hljs-string">&quot;a photo of dog&quot;</span> \\
  --resolution=512 \\
  --train_batch_size=1 \\
  --gradient_accumulation_steps=2 --gradient_checkpointing \\
  --use_8bit_adam \\
  --learning_rate=5e-6 \\
  --lr_scheduler=<span class="hljs-string">&quot;constant&quot;</span> \\
  --lr_warmup_steps=0 \\
  --num_class_images=200 \\
  --max_train_steps=800`}}),Qe=new P({}),_e=new jn({props:{$$slots:{default:[On]},$$scope:{ctx:lt}}}),et=new y({props:{code:`export MODEL_NAME="CompVis/stable-diffusion-v1-4"
export INSTANCE_DIR="path_to_training_images"
export CLASS_DIR="path_to_class_images"
export OUTPUT_DIR="path_to_saved_model"

accelerate launch train_dreambooth.py \\
  --pretrained_model_name_or_path=$MODEL_NAME  \\
  --train_text_encoder \\
  --instance_data_dir=$INSTANCE_DIR \\
  --class_data_dir=$CLASS_DIR \\
  --output_dir=$OUTPUT_DIR \\
  --with_prior_preservation --prior_loss_weight=1.0 \\
  --instance_prompt="a photo of sks dog" \\
  --class_prompt="a photo of dog" \\
  --resolution=512 \\
  --train_batch_size=1 \\
  --use_8bit_adam
  --gradient_checkpointing \\
  --learning_rate=2e-6 \\
  --lr_scheduler="constant" \\
  --lr_warmup_steps=0 \\
  --num_class_images=200 \\
  --max_train_steps=800`,highlighted:`<span class="hljs-built_in">export</span> MODEL_NAME=<span class="hljs-string">&quot;CompVis/stable-diffusion-v1-4&quot;</span>
<span class="hljs-built_in">export</span> INSTANCE_DIR=<span class="hljs-string">&quot;path_to_training_images&quot;</span>
<span class="hljs-built_in">export</span> CLASS_DIR=<span class="hljs-string">&quot;path_to_class_images&quot;</span>
<span class="hljs-built_in">export</span> OUTPUT_DIR=<span class="hljs-string">&quot;path_to_saved_model&quot;</span>

accelerate launch train_dreambooth.py \\
  --pretrained_model_name_or_path=<span class="hljs-variable">$MODEL_NAME</span>  \\
  --train_text_encoder \\
  --instance_data_dir=<span class="hljs-variable">$INSTANCE_DIR</span> \\
  --class_data_dir=<span class="hljs-variable">$CLASS_DIR</span> \\
  --output_dir=<span class="hljs-variable">$OUTPUT_DIR</span> \\
  --with_prior_preservation --prior_loss_weight=1.0 \\
  --instance_prompt=<span class="hljs-string">&quot;a photo of sks dog&quot;</span> \\
  --class_prompt=<span class="hljs-string">&quot;a photo of dog&quot;</span> \\
  --resolution=512 \\
  --train_batch_size=1 \\
  --use_8bit_adam
  --gradient_checkpointing \\
  --learning_rate=2e-6 \\
  --lr_scheduler=<span class="hljs-string">&quot;constant&quot;</span> \\
  --lr_warmup_steps=0 \\
  --num_class_images=200 \\
  --max_train_steps=800`}}),tt=new P({}),ot=new y({props:{code:`export MODEL_NAME="CompVis/stable-diffusion-v1-4"
export INSTANCE_DIR="path_to_training_images"
export CLASS_DIR="path_to_class_images"
export OUTPUT_DIR="path_to_saved_model"

accelerate launch train_dreambooth.py \\
  --pretrained_model_name_or_path=$MODEL_NAME \\
  --instance_data_dir=$INSTANCE_DIR \\
  --class_data_dir=$CLASS_DIR \\
  --output_dir=$OUTPUT_DIR \\
  --with_prior_preservation --prior_loss_weight=1.0 \\
  --instance_prompt="a photo of sks dog" \\
  --class_prompt="a photo of dog" \\
  --resolution=512 \\
  --train_batch_size=1 \\
  --sample_batch_size=1 \\
  --gradient_accumulation_steps=1 --gradient_checkpointing \\
  --learning_rate=5e-6 \\
  --lr_scheduler="constant" \\
  --lr_warmup_steps=0 \\
  --num_class_images=200 \\
  --max_train_steps=800 \\
  --mixed_precision=fp16`,highlighted:`<span class="hljs-built_in">export</span> MODEL_NAME=<span class="hljs-string">&quot;CompVis/stable-diffusion-v1-4&quot;</span>
<span class="hljs-built_in">export</span> INSTANCE_DIR=<span class="hljs-string">&quot;path_to_training_images&quot;</span>
<span class="hljs-built_in">export</span> CLASS_DIR=<span class="hljs-string">&quot;path_to_class_images&quot;</span>
<span class="hljs-built_in">export</span> OUTPUT_DIR=<span class="hljs-string">&quot;path_to_saved_model&quot;</span>

accelerate launch train_dreambooth.py \\
  --pretrained_model_name_or_path=<span class="hljs-variable">$MODEL_NAME</span> \\
  --instance_data_dir=<span class="hljs-variable">$INSTANCE_DIR</span> \\
  --class_data_dir=<span class="hljs-variable">$CLASS_DIR</span> \\
  --output_dir=<span class="hljs-variable">$OUTPUT_DIR</span> \\
  --with_prior_preservation --prior_loss_weight=1.0 \\
  --instance_prompt=<span class="hljs-string">&quot;a photo of sks dog&quot;</span> \\
  --class_prompt=<span class="hljs-string">&quot;a photo of dog&quot;</span> \\
  --resolution=512 \\
  --train_batch_size=1 \\
  --sample_batch_size=1 \\
  --gradient_accumulation_steps=1 --gradient_checkpointing \\
  --learning_rate=5e-6 \\
  --lr_scheduler=<span class="hljs-string">&quot;constant&quot;</span> \\
  --lr_warmup_steps=0 \\
  --num_class_images=200 \\
  --max_train_steps=800 \\
  --mixed_precision=fp16`}}),it=new P({}),rt=new y({props:{code:`from diffusers import StableDiffusionPipeline
import torch

model_id = "path_to_saved_model"
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to("cuda")

prompt = "A photo of sks dog in a bucket"
image = pipe(prompt, num_inference_steps=50, guidance_scale=7.5).images[0]

image.save("dog-bucket.png")`,highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> StableDiffusionPipeline
<span class="hljs-keyword">import</span> torch

model_id = <span class="hljs-string">&quot;path_to_saved_model&quot;</span>
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to(<span class="hljs-string">&quot;cuda&quot;</span>)

prompt = <span class="hljs-string">&quot;A photo of sks dog in a bucket&quot;</span>
image = pipe(prompt, num_inference_steps=<span class="hljs-number">50</span>, guidance_scale=<span class="hljs-number">7.5</span>).images[<span class="hljs-number">0</span>]

image.save(<span class="hljs-string">&quot;dog-bucket.png&quot;</span>)`}}),{c(){b=o("meta"),$=c(),w=o("h1"),k=o("a"),N=o("span"),u(E.$$.fragment),S=c(),W=o("span"),Vs=n("DreamBooth fine-tuning example"),_a=c(),De=o("p"),xe=o("a"),Hs=n("DreamBooth"),Ys=n(" is a method to personalize text-to-image models like stable diffusion given just a few (3~5) images of a subject."),ga=c(),J=o("p"),pt=o("img"),Fs=c(),Ae=o("em"),Ws=n("Dreambooth examples from the "),Pe=o("a"),Js=n("project\u2019s blog"),Zs=n("."),va=c(),Z=o("p"),Ks=n("The "),Se=o("a"),Qs=n("Dreambooth training script"),Xs=n(" shows how to implement this training procedure on a pre-trained Stable Diffusion model."),ba=c(),u(K.$$.fragment),wa=c(),C=o("h2"),Q=o("a"),$t=o("span"),u(je.$$.fragment),eo=c(),Et=o("span"),to=n("Training locally"),$a=c(),R=o("h3"),X=o("a"),yt=o("span"),u(Te.$$.fragment),ao=c(),kt=o("span"),so=n("Installing the dependencies"),Ea=c(),j=o("p"),oo=n("Before running the scripts, make sure to install the library\u2019s training dependencies. We also recommend to install "),Dt=o("code"),io=n("diffusers"),ro=n(" from the "),xt=o("code"),no=n("main"),lo=n(" github branch."),ya=c(),u(qe.$$.fragment),ka=c(),ee=o("p"),po=n("xFormers is not part of the training requirements, but "),ht=o("a"),ho=n("we recommend you install it if you can"),co=n(". It could make your training faster and less memory intensive."),Da=c(),te=o("p"),fo=n("After all dependencies have been set up you can configure a "),Ie=o("a"),uo=n("\u{1F917} Accelerate"),mo=n(" environment with:"),xa=c(),u(Ne.$$.fragment),Aa=c(),T=o("p"),_o=n("In this example we\u2019ll use model version "),At=o("code"),go=n("v1-4"),vo=n(", so please visit "),Ce=o("a"),bo=n("its card"),wo=n(" and carefully read the license before proceeding."),Pa=c(),ae=o("p"),$o=n("The command below will download and cache the model weights from the Hub because we use the model\u2019s Hub id "),Pt=o("code"),Eo=n("CompVis/stable-diffusion-v1-4"),yo=n(". You may also clone the repo locally and use the local path in your system where the checkout was saved."),Sa=c(),U=o("h3"),se=o("a"),St=o("span"),u(Re.$$.fragment),ko=c(),jt=o("span"),Do=n("Dog toy example"),ja=c(),oe=o("p"),xo=n("In this example we\u2019ll use "),Ue=o("a"),Ao=n("these images"),Po=n(" to add a new concept to Stable Diffusion using the Dreambooth process. They will be our training data. Please, download them and place them somewhere in your system."),Ta=c(),ct=o("p"),So=n("Then you can launch the training script using:"),qa=c(),u(Oe.$$.fragment),Ia=c(),O=o("h3"),ie=o("a"),Tt=o("span"),u(Me.$$.fragment),jo=c(),qt=o("span"),To=n("Training with a prior-preserving loss"),Na=c(),ft=o("p"),qo=n("Prior preservation is used to avoid overfitting and language-drift. Please, refer to the paper to learn more about it if you are interested. For prior preservation, we use other images of the same class as part of the training process. The nice thing is that we can generate those images using the Stable Diffusion model itself! The training script will save the generated images to a local path we specify."),Ca=c(),re=o("p"),Io=n("According to the paper, it\u2019s recommended to generate "),It=o("code"),No=n("num_epochs * num_samples"),Co=n(" images for prior preservation. 200-300 works well for most cases."),Ra=c(),u(Le.$$.fragment),Ua=c(),M=o("h3"),ne=o("a"),Nt=o("span"),u(Ge.$$.fragment),Ro=c(),Ct=o("span"),Uo=n("Saving checkpoints while training"),Oa=c(),ut=o("p"),Oo=n("It\u2019s easy to overfit while training with Dreambooth, so sometimes it\u2019s useful to save regular checkpoints during the process. One of the intermediate checkpoints might work better than the final model! To use this feature you need to pass the following argument to the training script:"),Ma=c(),u(Be.$$.fragment),La=c(),D=o("p"),Mo=n("This will save the full training state in subfolders of your "),Rt=o("code"),Lo=n("output_dir"),Go=n(". Subfolder names begin with the prefix "),Ut=o("code"),Bo=n("checkpoint-"),zo=n(", and then the number of steps performed so far; for example: "),Ot=o("code"),Vo=n("checkpoint-1500"),Ho=n(" would be a checkpoint saved after 1500 training steps."),Ga=c(),L=o("h4"),le=o("a"),Mt=o("span"),u(ze.$$.fragment),Yo=c(),Lt=o("span"),Fo=n("Resuming training from a saved checkpoint"),Ba=c(),q=o("p"),Wo=n("If you want to resume training from any of the saved checkpoints, you can pass the argument "),Gt=o("code"),Jo=n("--resume_from_checkpoint"),Zo=n(" and then indicate the name of the checkpoint you want to use. You can also use the special string "),Bt=o("code"),Ko=n('"latest"'),Qo=n(" to resume from the last checkpoint saved (i.e., the one with the largest number of steps). For example, the following would resume training from the checkpoint saved after 1500 steps:"),za=c(),u(Ve.$$.fragment),Va=c(),dt=o("p"),Xo=n("This would be a good opportunity to tweak some of your hyperparameters if you wish."),Ha=c(),G=o("h4"),pe=o("a"),zt=o("span"),u(He.$$.fragment),ei=c(),Vt=o("span"),ti=n("Performing inference using a saved checkpoint"),Ya=c(),mt=o("p"),ai=n("Saved checkpoints are stored in a format suitable for resuming training. They not only include the model weights, but also the state of the optimizer, data loaders and learning rate."),Fa=c(),B=o("p"),Ht=o("strong"),si=n("Note"),oi=n(": If you have installed "),Yt=o("code"),ii=n('"accelerate>=0.16.0"'),ri=n(` you can use the following code to run
inference from an intermediate checkpoint.`),Wa=c(),u(Ye.$$.fragment),Ja=c(),he=o("p"),ni=n("If you have installed "),Ft=o("code"),li=n('"accelerate<0.16.0"'),pi=n(" you need to first convert it to an inference pipeline. This is how you could do it:"),Za=c(),u(Fe.$$.fragment),Ka=c(),z=o("h3"),ce=o("a"),Wt=o("span"),u(We.$$.fragment),hi=c(),Jt=o("span"),ci=n("Training on a 16GB GPU"),Qa=c(),fe=o("p"),fi=n("With the help of gradient checkpointing and the 8-bit optimizer from "),Je=o("a"),ui=n("bitsandbytes"),di=n(", it\u2019s possible to train dreambooth on a 16GB GPU."),Xa=c(),u(Ze.$$.fragment),es=c(),ue=o("p"),mi=n("Then pass the "),Zt=o("code"),_i=n("--use_8bit_adam"),gi=n(" option to the training script."),ts=c(),u(Ke.$$.fragment),as=c(),V=o("h3"),de=o("a"),Kt=o("span"),u(Qe.$$.fragment),vi=c(),Qt=o("span"),bi=n("Fine-tune the text encoder in addition to the UNet"),ss=c(),x=o("p"),wi=n("The script also allows to fine-tune the "),Xt=o("code"),$i=n("text_encoder"),Ei=n(" along with the "),ea=o("code"),yi=n("unet"),ki=n(". It has been observed experimentally that this gives much better results, especially on faces. Please, refer to "),Xe=o("a"),Di=n("our blog"),xi=n(" for more details."),os=c(),me=o("p"),Ai=n("To enable this option, pass the "),ta=o("code"),Pi=n("--train_text_encoder"),Si=n(" argument to the training script."),is=c(),u(_e.$$.fragment),rs=c(),u(et.$$.fragment),ns=c(),H=o("h3"),ge=o("a"),aa=o("span"),u(tt.$$.fragment),ji=c(),sa=o("span"),Ti=n("Training on a 8 GB GPU:"),ls=c(),ve=o("p"),qi=n("Using "),at=o("a"),Ii=n("DeepSpeed"),Ni=n(` it\u2019s even possible to offload some
tensors from VRAM to either CPU or NVME, allowing training to proceed with less GPU memory.`),ps=c(),I=o("p"),Ci=n("DeepSpeed needs to be enabled with "),oa=o("code"),Ri=n("accelerate config"),Ui=n(`. During configuration,
answer yes to \u201CDo you want to use DeepSpeed?\u201C. Combining DeepSpeed stage 2, fp16
mixed precision, and offloading both the model parameters and the optimizer state to CPU, it\u2019s
possible to train on under 8 GB VRAM. The drawback is that this requires more system RAM (about 25 GB). See `),st=o("a"),Oi=n("the DeepSpeed documentation"),Mi=n(" for more configuration options."),hs=c(),be=o("p"),Li=n(`Changing the default Adam optimizer to DeepSpeed\u2019s special version of Adam
`),ia=o("code"),Gi=n("deepspeed.ops.adam.DeepSpeedCPUAdam"),Bi=n(` gives a substantial speedup, but enabling
it requires the system\u2019s CUDA toolchain version to be the same as the one installed with PyTorch. 8-bit optimizers don\u2019t seem to be compatible with DeepSpeed at the moment.`),cs=c(),u(ot.$$.fragment),fs=c(),Y=o("h2"),we=o("a"),ra=o("span"),u(it.$$.fragment),zi=c(),na=o("span"),Vi=n("Inference"),us=c(),A=o("p"),Hi=n("Once you have trained a model, inference can be done using the "),la=o("code"),Yi=n("StableDiffusionPipeline"),Fi=n(", by simply indicating the path where the model was saved. Make sure that your prompts include the special "),pa=o("code"),Wi=n("identifier"),Ji=n(" used during training ("),ha=o("code"),Zi=n("sks"),Ki=n(" in the previous examples)."),ds=c(),F=o("p"),ca=o("strong"),Qi=n("Note"),Xi=n(": If you have installed "),fa=o("code"),er=n('"accelerate>=0.16.0"'),tr=n(` you can use the following code to run
inference from an intermediate checkpoint.`),ms=c(),u(rt.$$.fragment),_s=c(),$e=o("p"),ar=n("You may also run inference from "),_t=o("a"),sr=n("any of the saved training checkpoints"),or=n("."),this.h()},l(e){const s=Nn('[data-svelte="svelte-1phssyn"]',document.head);b=i(s,"META",{name:!0,content:!0}),s.forEach(t),$=f(e),w=i(e,"H1",{class:!0});var nt=r(w);k=i(nt,"A",{id:!0,class:!0,href:!0});var ua=r(k);N=i(ua,"SPAN",{});var nr=r(N);d(E.$$.fragment,nr),nr.forEach(t),ua.forEach(t),S=f(nt),W=i(nt,"SPAN",{});var lr=r(W);Vs=l(lr,"DreamBooth fine-tuning example"),lr.forEach(t),nt.forEach(t),_a=f(e),De=i(e,"P",{});var ir=r(De);xe=i(ir,"A",{href:!0,rel:!0});var pr=r(xe);Hs=l(pr,"DreamBooth"),pr.forEach(t),Ys=l(ir," is a method to personalize text-to-image models like stable diffusion given just a few (3~5) images of a subject."),ir.forEach(t),ga=f(e),J=i(e,"P",{});var vs=r(J);pt=i(vs,"IMG",{src:!0,alt:!0}),Fs=f(vs),Ae=i(vs,"EM",{});var bs=r(Ae);Ws=l(bs,"Dreambooth examples from the "),Pe=i(bs,"A",{href:!0,rel:!0});var hr=r(Pe);Js=l(hr,"project\u2019s blog"),hr.forEach(t),Zs=l(bs,"."),bs.forEach(t),vs.forEach(t),va=f(e),Z=i(e,"P",{});var ws=r(Z);Ks=l(ws,"The "),Se=i(ws,"A",{href:!0,rel:!0});var cr=r(Se);Qs=l(cr,"Dreambooth training script"),cr.forEach(t),Xs=l(ws," shows how to implement this training procedure on a pre-trained Stable Diffusion model."),ws.forEach(t),ba=f(e),d(K.$$.fragment,e),wa=f(e),C=i(e,"H2",{class:!0});var $s=r(C);Q=i($s,"A",{id:!0,class:!0,href:!0});var fr=r(Q);$t=i(fr,"SPAN",{});var ur=r($t);d(je.$$.fragment,ur),ur.forEach(t),fr.forEach(t),eo=f($s),Et=i($s,"SPAN",{});var dr=r(Et);to=l(dr,"Training locally"),dr.forEach(t),$s.forEach(t),$a=f(e),R=i(e,"H3",{class:!0});var Es=r(R);X=i(Es,"A",{id:!0,class:!0,href:!0});var mr=r(X);yt=i(mr,"SPAN",{});var _r=r(yt);d(Te.$$.fragment,_r),_r.forEach(t),mr.forEach(t),ao=f(Es),kt=i(Es,"SPAN",{});var gr=r(kt);so=l(gr,"Installing the dependencies"),gr.forEach(t),Es.forEach(t),Ea=f(e),j=i(e,"P",{});var gt=r(j);oo=l(gt,"Before running the scripts, make sure to install the library\u2019s training dependencies. We also recommend to install "),Dt=i(gt,"CODE",{});var vr=r(Dt);io=l(vr,"diffusers"),vr.forEach(t),ro=l(gt," from the "),xt=i(gt,"CODE",{});var br=r(xt);no=l(br,"main"),br.forEach(t),lo=l(gt," github branch."),gt.forEach(t),ya=f(e),d(qe.$$.fragment,e),ka=f(e),ee=i(e,"P",{});var ys=r(ee);po=l(ys,"xFormers is not part of the training requirements, but "),ht=i(ys,"A",{href:!0});var wr=r(ht);ho=l(wr,"we recommend you install it if you can"),wr.forEach(t),co=l(ys,". It could make your training faster and less memory intensive."),ys.forEach(t),Da=f(e),te=i(e,"P",{});var ks=r(te);fo=l(ks,"After all dependencies have been set up you can configure a "),Ie=i(ks,"A",{href:!0,rel:!0});var $r=r(Ie);uo=l($r,"\u{1F917} Accelerate"),$r.forEach(t),mo=l(ks," environment with:"),ks.forEach(t),xa=f(e),d(Ne.$$.fragment,e),Aa=f(e),T=i(e,"P",{});var vt=r(T);_o=l(vt,"In this example we\u2019ll use model version "),At=i(vt,"CODE",{});var Er=r(At);go=l(Er,"v1-4"),Er.forEach(t),vo=l(vt,", so please visit "),Ce=i(vt,"A",{href:!0,rel:!0});var yr=r(Ce);bo=l(yr,"its card"),yr.forEach(t),wo=l(vt," and carefully read the license before proceeding."),vt.forEach(t),Pa=f(e),ae=i(e,"P",{});var Ds=r(ae);$o=l(Ds,"The command below will download and cache the model weights from the Hub because we use the model\u2019s Hub id "),Pt=i(Ds,"CODE",{});var kr=r(Pt);Eo=l(kr,"CompVis/stable-diffusion-v1-4"),kr.forEach(t),yo=l(Ds,". You may also clone the repo locally and use the local path in your system where the checkout was saved."),Ds.forEach(t),Sa=f(e),U=i(e,"H3",{class:!0});var xs=r(U);se=i(xs,"A",{id:!0,class:!0,href:!0});var Dr=r(se);St=i(Dr,"SPAN",{});var xr=r(St);d(Re.$$.fragment,xr),xr.forEach(t),Dr.forEach(t),ko=f(xs),jt=i(xs,"SPAN",{});var Ar=r(jt);Do=l(Ar,"Dog toy example"),Ar.forEach(t),xs.forEach(t),ja=f(e),oe=i(e,"P",{});var As=r(oe);xo=l(As,"In this example we\u2019ll use "),Ue=i(As,"A",{href:!0,rel:!0});var Pr=r(Ue);Ao=l(Pr,"these images"),Pr.forEach(t),Po=l(As," to add a new concept to Stable Diffusion using the Dreambooth process. They will be our training data. Please, download them and place them somewhere in your system."),As.forEach(t),Ta=f(e),ct=i(e,"P",{});var Sr=r(ct);So=l(Sr,"Then you can launch the training script using:"),Sr.forEach(t),qa=f(e),d(Oe.$$.fragment,e),Ia=f(e),O=i(e,"H3",{class:!0});var Ps=r(O);ie=i(Ps,"A",{id:!0,class:!0,href:!0});var jr=r(ie);Tt=i(jr,"SPAN",{});var Tr=r(Tt);d(Me.$$.fragment,Tr),Tr.forEach(t),jr.forEach(t),jo=f(Ps),qt=i(Ps,"SPAN",{});var qr=r(qt);To=l(qr,"Training with a prior-preserving loss"),qr.forEach(t),Ps.forEach(t),Na=f(e),ft=i(e,"P",{});var Ir=r(ft);qo=l(Ir,"Prior preservation is used to avoid overfitting and language-drift. Please, refer to the paper to learn more about it if you are interested. For prior preservation, we use other images of the same class as part of the training process. The nice thing is that we can generate those images using the Stable Diffusion model itself! The training script will save the generated images to a local path we specify."),Ir.forEach(t),Ca=f(e),re=i(e,"P",{});var Ss=r(re);Io=l(Ss,"According to the paper, it\u2019s recommended to generate "),It=i(Ss,"CODE",{});var Nr=r(It);No=l(Nr,"num_epochs * num_samples"),Nr.forEach(t),Co=l(Ss," images for prior preservation. 200-300 works well for most cases."),Ss.forEach(t),Ra=f(e),d(Le.$$.fragment,e),Ua=f(e),M=i(e,"H3",{class:!0});var js=r(M);ne=i(js,"A",{id:!0,class:!0,href:!0});var Cr=r(ne);Nt=i(Cr,"SPAN",{});var Rr=r(Nt);d(Ge.$$.fragment,Rr),Rr.forEach(t),Cr.forEach(t),Ro=f(js),Ct=i(js,"SPAN",{});var Ur=r(Ct);Uo=l(Ur,"Saving checkpoints while training"),Ur.forEach(t),js.forEach(t),Oa=f(e),ut=i(e,"P",{});var Or=r(ut);Oo=l(Or,"It\u2019s easy to overfit while training with Dreambooth, so sometimes it\u2019s useful to save regular checkpoints during the process. One of the intermediate checkpoints might work better than the final model! To use this feature you need to pass the following argument to the training script:"),Or.forEach(t),Ma=f(e),d(Be.$$.fragment,e),La=f(e),D=i(e,"P",{});var Ee=r(D);Mo=l(Ee,"This will save the full training state in subfolders of your "),Rt=i(Ee,"CODE",{});var Mr=r(Rt);Lo=l(Mr,"output_dir"),Mr.forEach(t),Go=l(Ee,". Subfolder names begin with the prefix "),Ut=i(Ee,"CODE",{});var Lr=r(Ut);Bo=l(Lr,"checkpoint-"),Lr.forEach(t),zo=l(Ee,", and then the number of steps performed so far; for example: "),Ot=i(Ee,"CODE",{});var Gr=r(Ot);Vo=l(Gr,"checkpoint-1500"),Gr.forEach(t),Ho=l(Ee," would be a checkpoint saved after 1500 training steps."),Ee.forEach(t),Ga=f(e),L=i(e,"H4",{class:!0});var Ts=r(L);le=i(Ts,"A",{id:!0,class:!0,href:!0});var Br=r(le);Mt=i(Br,"SPAN",{});var zr=r(Mt);d(ze.$$.fragment,zr),zr.forEach(t),Br.forEach(t),Yo=f(Ts),Lt=i(Ts,"SPAN",{});var Vr=r(Lt);Fo=l(Vr,"Resuming training from a saved checkpoint"),Vr.forEach(t),Ts.forEach(t),Ba=f(e),q=i(e,"P",{});var bt=r(q);Wo=l(bt,"If you want to resume training from any of the saved checkpoints, you can pass the argument "),Gt=i(bt,"CODE",{});var Hr=r(Gt);Jo=l(Hr,"--resume_from_checkpoint"),Hr.forEach(t),Zo=l(bt," and then indicate the name of the checkpoint you want to use. You can also use the special string "),Bt=i(bt,"CODE",{});var Yr=r(Bt);Ko=l(Yr,'"latest"'),Yr.forEach(t),Qo=l(bt," to resume from the last checkpoint saved (i.e., the one with the largest number of steps). For example, the following would resume training from the checkpoint saved after 1500 steps:"),bt.forEach(t),za=f(e),d(Ve.$$.fragment,e),Va=f(e),dt=i(e,"P",{});var Fr=r(dt);Xo=l(Fr,"This would be a good opportunity to tweak some of your hyperparameters if you wish."),Fr.forEach(t),Ha=f(e),G=i(e,"H4",{class:!0});var qs=r(G);pe=i(qs,"A",{id:!0,class:!0,href:!0});var Wr=r(pe);zt=i(Wr,"SPAN",{});var Jr=r(zt);d(He.$$.fragment,Jr),Jr.forEach(t),Wr.forEach(t),ei=f(qs),Vt=i(qs,"SPAN",{});var Zr=r(Vt);ti=l(Zr,"Performing inference using a saved checkpoint"),Zr.forEach(t),qs.forEach(t),Ya=f(e),mt=i(e,"P",{});var Kr=r(mt);ai=l(Kr,"Saved checkpoints are stored in a format suitable for resuming training. They not only include the model weights, but also the state of the optimizer, data loaders and learning rate."),Kr.forEach(t),Fa=f(e),B=i(e,"P",{});var da=r(B);Ht=i(da,"STRONG",{});var Qr=r(Ht);si=l(Qr,"Note"),Qr.forEach(t),oi=l(da,": If you have installed "),Yt=i(da,"CODE",{});var Xr=r(Yt);ii=l(Xr,'"accelerate>=0.16.0"'),Xr.forEach(t),ri=l(da,` you can use the following code to run
inference from an intermediate checkpoint.`),da.forEach(t),Wa=f(e),d(Ye.$$.fragment,e),Ja=f(e),he=i(e,"P",{});var Is=r(he);ni=l(Is,"If you have installed "),Ft=i(Is,"CODE",{});var en=r(Ft);li=l(en,'"accelerate<0.16.0"'),en.forEach(t),pi=l(Is," you need to first convert it to an inference pipeline. This is how you could do it:"),Is.forEach(t),Za=f(e),d(Fe.$$.fragment,e),Ka=f(e),z=i(e,"H3",{class:!0});var Ns=r(z);ce=i(Ns,"A",{id:!0,class:!0,href:!0});var tn=r(ce);Wt=i(tn,"SPAN",{});var an=r(Wt);d(We.$$.fragment,an),an.forEach(t),tn.forEach(t),hi=f(Ns),Jt=i(Ns,"SPAN",{});var sn=r(Jt);ci=l(sn,"Training on a 16GB GPU"),sn.forEach(t),Ns.forEach(t),Qa=f(e),fe=i(e,"P",{});var Cs=r(fe);fi=l(Cs,"With the help of gradient checkpointing and the 8-bit optimizer from "),Je=i(Cs,"A",{href:!0,rel:!0});var on=r(Je);ui=l(on,"bitsandbytes"),on.forEach(t),di=l(Cs,", it\u2019s possible to train dreambooth on a 16GB GPU."),Cs.forEach(t),Xa=f(e),d(Ze.$$.fragment,e),es=f(e),ue=i(e,"P",{});var Rs=r(ue);mi=l(Rs,"Then pass the "),Zt=i(Rs,"CODE",{});var rn=r(Zt);_i=l(rn,"--use_8bit_adam"),rn.forEach(t),gi=l(Rs," option to the training script."),Rs.forEach(t),ts=f(e),d(Ke.$$.fragment,e),as=f(e),V=i(e,"H3",{class:!0});var Us=r(V);de=i(Us,"A",{id:!0,class:!0,href:!0});var nn=r(de);Kt=i(nn,"SPAN",{});var ln=r(Kt);d(Qe.$$.fragment,ln),ln.forEach(t),nn.forEach(t),vi=f(Us),Qt=i(Us,"SPAN",{});var pn=r(Qt);bi=l(pn,"Fine-tune the text encoder in addition to the UNet"),pn.forEach(t),Us.forEach(t),ss=f(e),x=i(e,"P",{});var ye=r(x);wi=l(ye,"The script also allows to fine-tune the "),Xt=i(ye,"CODE",{});var hn=r(Xt);$i=l(hn,"text_encoder"),hn.forEach(t),Ei=l(ye," along with the "),ea=i(ye,"CODE",{});var cn=r(ea);yi=l(cn,"unet"),cn.forEach(t),ki=l(ye,". It has been observed experimentally that this gives much better results, especially on faces. Please, refer to "),Xe=i(ye,"A",{href:!0,rel:!0});var fn=r(Xe);Di=l(fn,"our blog"),fn.forEach(t),xi=l(ye," for more details."),ye.forEach(t),os=f(e),me=i(e,"P",{});var Os=r(me);Ai=l(Os,"To enable this option, pass the "),ta=i(Os,"CODE",{});var un=r(ta);Pi=l(un,"--train_text_encoder"),un.forEach(t),Si=l(Os," argument to the training script."),Os.forEach(t),is=f(e),d(_e.$$.fragment,e),rs=f(e),d(et.$$.fragment,e),ns=f(e),H=i(e,"H3",{class:!0});var Ms=r(H);ge=i(Ms,"A",{id:!0,class:!0,href:!0});var dn=r(ge);aa=i(dn,"SPAN",{});var mn=r(aa);d(tt.$$.fragment,mn),mn.forEach(t),dn.forEach(t),ji=f(Ms),sa=i(Ms,"SPAN",{});var _n=r(sa);Ti=l(_n,"Training on a 8 GB GPU:"),_n.forEach(t),Ms.forEach(t),ls=f(e),ve=i(e,"P",{});var Ls=r(ve);qi=l(Ls,"Using "),at=i(Ls,"A",{href:!0,rel:!0});var gn=r(at);Ii=l(gn,"DeepSpeed"),gn.forEach(t),Ni=l(Ls,` it\u2019s even possible to offload some
tensors from VRAM to either CPU or NVME, allowing training to proceed with less GPU memory.`),Ls.forEach(t),ps=f(e),I=i(e,"P",{});var wt=r(I);Ci=l(wt,"DeepSpeed needs to be enabled with "),oa=i(wt,"CODE",{});var vn=r(oa);Ri=l(vn,"accelerate config"),vn.forEach(t),Ui=l(wt,`. During configuration,
answer yes to \u201CDo you want to use DeepSpeed?\u201C. Combining DeepSpeed stage 2, fp16
mixed precision, and offloading both the model parameters and the optimizer state to CPU, it\u2019s
possible to train on under 8 GB VRAM. The drawback is that this requires more system RAM (about 25 GB). See `),st=i(wt,"A",{href:!0,rel:!0});var bn=r(st);Oi=l(bn,"the DeepSpeed documentation"),bn.forEach(t),Mi=l(wt," for more configuration options."),wt.forEach(t),hs=f(e),be=i(e,"P",{});var Gs=r(be);Li=l(Gs,`Changing the default Adam optimizer to DeepSpeed\u2019s special version of Adam
`),ia=i(Gs,"CODE",{});var wn=r(ia);Gi=l(wn,"deepspeed.ops.adam.DeepSpeedCPUAdam"),wn.forEach(t),Bi=l(Gs,` gives a substantial speedup, but enabling
it requires the system\u2019s CUDA toolchain version to be the same as the one installed with PyTorch. 8-bit optimizers don\u2019t seem to be compatible with DeepSpeed at the moment.`),Gs.forEach(t),cs=f(e),d(ot.$$.fragment,e),fs=f(e),Y=i(e,"H2",{class:!0});var Bs=r(Y);we=i(Bs,"A",{id:!0,class:!0,href:!0});var $n=r(we);ra=i($n,"SPAN",{});var En=r(ra);d(it.$$.fragment,En),En.forEach(t),$n.forEach(t),zi=f(Bs),na=i(Bs,"SPAN",{});var yn=r(na);Vi=l(yn,"Inference"),yn.forEach(t),Bs.forEach(t),us=f(e),A=i(e,"P",{});var ke=r(A);Hi=l(ke,"Once you have trained a model, inference can be done using the "),la=i(ke,"CODE",{});var kn=r(la);Yi=l(kn,"StableDiffusionPipeline"),kn.forEach(t),Fi=l(ke,", by simply indicating the path where the model was saved. Make sure that your prompts include the special "),pa=i(ke,"CODE",{});var Dn=r(pa);Wi=l(Dn,"identifier"),Dn.forEach(t),Ji=l(ke," used during training ("),ha=i(ke,"CODE",{});var xn=r(ha);Zi=l(xn,"sks"),xn.forEach(t),Ki=l(ke," in the previous examples)."),ke.forEach(t),ds=f(e),F=i(e,"P",{});var ma=r(F);ca=i(ma,"STRONG",{});var An=r(ca);Qi=l(An,"Note"),An.forEach(t),Xi=l(ma,": If you have installed "),fa=i(ma,"CODE",{});var Pn=r(fa);er=l(Pn,'"accelerate>=0.16.0"'),Pn.forEach(t),tr=l(ma,` you can use the following code to run
inference from an intermediate checkpoint.`),ma.forEach(t),ms=f(e),d(rt.$$.fragment,e),_s=f(e),$e=i(e,"P",{});var zs=r($e);ar=l(zs,"You may also run inference from "),_t=i(zs,"A",{href:!0});var Sn=r(_t);sr=l(Sn,"any of the saved training checkpoints"),Sn.forEach(t),or=l(zs,"."),zs.forEach(t),this.h()},h(){h(b,"name","hf:doc:metadata"),h(b,"content",JSON.stringify(Ln)),h(k,"id","dreambooth-finetuning-example"),h(k,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),h(k,"href","#dreambooth-finetuning-example"),h(w,"class","relative group"),h(xe,"href","https://arxiv.org/abs/2208.12242"),h(xe,"rel","nofollow"),Cn(pt.src,rr="https://dreambooth.github.io/DreamBooth_files/teaser_static.jpg")||h(pt,"src",rr),h(pt,"alt","Dreambooth examples from the project's blog"),h(Pe,"href","https://dreambooth.github.io"),h(Pe,"rel","nofollow"),h(Se,"href","https://github.com/huggingface/diffusers/tree/main/examples/dreambooth"),h(Se,"rel","nofollow"),h(Q,"id","training-locally"),h(Q,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),h(Q,"href","#training-locally"),h(C,"class","relative group"),h(X,"id","installing-the-dependencies"),h(X,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),h(X,"href","#installing-the-dependencies"),h(R,"class","relative group"),h(ht,"href","../optimization/xformers"),h(Ie,"href","https://github.com/huggingface/accelerate/"),h(Ie,"rel","nofollow"),h(Ce,"href","https://huggingface.co/CompVis/stable-diffusion-v1-4"),h(Ce,"rel","nofollow"),h(se,"id","dog-toy-example"),h(se,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),h(se,"href","#dog-toy-example"),h(U,"class","relative group"),h(Ue,"href","https://drive.google.com/drive/folders/1BO_dyz-p65qhBRRMRA4TbZ8qW4rB99JZ"),h(Ue,"rel","nofollow"),h(ie,"id","training-with-a-priorpreserving-loss"),h(ie,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),h(ie,"href","#training-with-a-priorpreserving-loss"),h(O,"class","relative group"),h(ne,"id","saving-checkpoints-while-training"),h(ne,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),h(ne,"href","#saving-checkpoints-while-training"),h(M,"class","relative group"),h(le,"id","resuming-training-from-a-saved-checkpoint"),h(le,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),h(le,"href","#resuming-training-from-a-saved-checkpoint"),h(L,"class","relative group"),h(pe,"id","performing-inference-using-a-saved-checkpoint"),h(pe,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),h(pe,"href","#performing-inference-using-a-saved-checkpoint"),h(G,"class","relative group"),h(ce,"id","training-on-a-16gb-gpu"),h(ce,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),h(ce,"href","#training-on-a-16gb-gpu"),h(z,"class","relative group"),h(Je,"href","https://github.com/TimDettmers/bitsandbytes"),h(Je,"rel","nofollow"),h(de,"id","finetune-the-text-encoder-in-addition-to-the-unet"),h(de,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),h(de,"href","#finetune-the-text-encoder-in-addition-to-the-unet"),h(V,"class","relative group"),h(Xe,"href","https://huggingface.co/blog/dreambooth"),h(Xe,"rel","nofollow"),h(ge,"id","training-on-a-8-gb-gpu"),h(ge,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),h(ge,"href","#training-on-a-8-gb-gpu"),h(H,"class","relative group"),h(at,"href","https://www.deepspeed.ai/"),h(at,"rel","nofollow"),h(st,"href","https://huggingface.co/docs/accelerate/usage_guides/deepspeed"),h(st,"rel","nofollow"),h(we,"id","inference"),h(we,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),h(we,"href","#inference"),h(Y,"class","relative group"),h(_t,"href","#performing-inference-using-a-saved-checkpoint")},m(e,s){a(document.head,b),p(e,$,s),p(e,w,s),a(w,k),a(k,N),m(E,N,null),a(w,S),a(w,W),a(W,Vs),p(e,_a,s),p(e,De,s),a(De,xe),a(xe,Hs),a(De,Ys),p(e,ga,s),p(e,J,s),a(J,pt),a(J,Fs),a(J,Ae),a(Ae,Ws),a(Ae,Pe),a(Pe,Js),a(Ae,Zs),p(e,va,s),p(e,Z,s),a(Z,Ks),a(Z,Se),a(Se,Qs),a(Z,Xs),p(e,ba,s),m(K,e,s),p(e,wa,s),p(e,C,s),a(C,Q),a(Q,$t),m(je,$t,null),a(C,eo),a(C,Et),a(Et,to),p(e,$a,s),p(e,R,s),a(R,X),a(X,yt),m(Te,yt,null),a(R,ao),a(R,kt),a(kt,so),p(e,Ea,s),p(e,j,s),a(j,oo),a(j,Dt),a(Dt,io),a(j,ro),a(j,xt),a(xt,no),a(j,lo),p(e,ya,s),m(qe,e,s),p(e,ka,s),p(e,ee,s),a(ee,po),a(ee,ht),a(ht,ho),a(ee,co),p(e,Da,s),p(e,te,s),a(te,fo),a(te,Ie),a(Ie,uo),a(te,mo),p(e,xa,s),m(Ne,e,s),p(e,Aa,s),p(e,T,s),a(T,_o),a(T,At),a(At,go),a(T,vo),a(T,Ce),a(Ce,bo),a(T,wo),p(e,Pa,s),p(e,ae,s),a(ae,$o),a(ae,Pt),a(Pt,Eo),a(ae,yo),p(e,Sa,s),p(e,U,s),a(U,se),a(se,St),m(Re,St,null),a(U,ko),a(U,jt),a(jt,Do),p(e,ja,s),p(e,oe,s),a(oe,xo),a(oe,Ue),a(Ue,Ao),a(oe,Po),p(e,Ta,s),p(e,ct,s),a(ct,So),p(e,qa,s),m(Oe,e,s),p(e,Ia,s),p(e,O,s),a(O,ie),a(ie,Tt),m(Me,Tt,null),a(O,jo),a(O,qt),a(qt,To),p(e,Na,s),p(e,ft,s),a(ft,qo),p(e,Ca,s),p(e,re,s),a(re,Io),a(re,It),a(It,No),a(re,Co),p(e,Ra,s),m(Le,e,s),p(e,Ua,s),p(e,M,s),a(M,ne),a(ne,Nt),m(Ge,Nt,null),a(M,Ro),a(M,Ct),a(Ct,Uo),p(e,Oa,s),p(e,ut,s),a(ut,Oo),p(e,Ma,s),m(Be,e,s),p(e,La,s),p(e,D,s),a(D,Mo),a(D,Rt),a(Rt,Lo),a(D,Go),a(D,Ut),a(Ut,Bo),a(D,zo),a(D,Ot),a(Ot,Vo),a(D,Ho),p(e,Ga,s),p(e,L,s),a(L,le),a(le,Mt),m(ze,Mt,null),a(L,Yo),a(L,Lt),a(Lt,Fo),p(e,Ba,s),p(e,q,s),a(q,Wo),a(q,Gt),a(Gt,Jo),a(q,Zo),a(q,Bt),a(Bt,Ko),a(q,Qo),p(e,za,s),m(Ve,e,s),p(e,Va,s),p(e,dt,s),a(dt,Xo),p(e,Ha,s),p(e,G,s),a(G,pe),a(pe,zt),m(He,zt,null),a(G,ei),a(G,Vt),a(Vt,ti),p(e,Ya,s),p(e,mt,s),a(mt,ai),p(e,Fa,s),p(e,B,s),a(B,Ht),a(Ht,si),a(B,oi),a(B,Yt),a(Yt,ii),a(B,ri),p(e,Wa,s),m(Ye,e,s),p(e,Ja,s),p(e,he,s),a(he,ni),a(he,Ft),a(Ft,li),a(he,pi),p(e,Za,s),m(Fe,e,s),p(e,Ka,s),p(e,z,s),a(z,ce),a(ce,Wt),m(We,Wt,null),a(z,hi),a(z,Jt),a(Jt,ci),p(e,Qa,s),p(e,fe,s),a(fe,fi),a(fe,Je),a(Je,ui),a(fe,di),p(e,Xa,s),m(Ze,e,s),p(e,es,s),p(e,ue,s),a(ue,mi),a(ue,Zt),a(Zt,_i),a(ue,gi),p(e,ts,s),m(Ke,e,s),p(e,as,s),p(e,V,s),a(V,de),a(de,Kt),m(Qe,Kt,null),a(V,vi),a(V,Qt),a(Qt,bi),p(e,ss,s),p(e,x,s),a(x,wi),a(x,Xt),a(Xt,$i),a(x,Ei),a(x,ea),a(ea,yi),a(x,ki),a(x,Xe),a(Xe,Di),a(x,xi),p(e,os,s),p(e,me,s),a(me,Ai),a(me,ta),a(ta,Pi),a(me,Si),p(e,is,s),m(_e,e,s),p(e,rs,s),m(et,e,s),p(e,ns,s),p(e,H,s),a(H,ge),a(ge,aa),m(tt,aa,null),a(H,ji),a(H,sa),a(sa,Ti),p(e,ls,s),p(e,ve,s),a(ve,qi),a(ve,at),a(at,Ii),a(ve,Ni),p(e,ps,s),p(e,I,s),a(I,Ci),a(I,oa),a(oa,Ri),a(I,Ui),a(I,st),a(st,Oi),a(I,Mi),p(e,hs,s),p(e,be,s),a(be,Li),a(be,ia),a(ia,Gi),a(be,Bi),p(e,cs,s),m(ot,e,s),p(e,fs,s),p(e,Y,s),a(Y,we),a(we,ra),m(it,ra,null),a(Y,zi),a(Y,na),a(na,Vi),p(e,us,s),p(e,A,s),a(A,Hi),a(A,la),a(la,Yi),a(A,Fi),a(A,pa),a(pa,Wi),a(A,Ji),a(A,ha),a(ha,Zi),a(A,Ki),p(e,ds,s),p(e,F,s),a(F,ca),a(ca,Qi),a(F,Xi),a(F,fa),a(fa,er),a(F,tr),p(e,ms,s),m(rt,e,s),p(e,_s,s),p(e,$e,s),a($e,ar),a($e,_t),a(_t,sr),a($e,or),gs=!0},p(e,[s]){const nt={};s&2&&(nt.$$scope={dirty:s,ctx:e}),K.$set(nt);const ua={};s&2&&(ua.$$scope={dirty:s,ctx:e}),_e.$set(ua)},i(e){gs||(_(E.$$.fragment,e),_(K.$$.fragment,e),_(je.$$.fragment,e),_(Te.$$.fragment,e),_(qe.$$.fragment,e),_(Ne.$$.fragment,e),_(Re.$$.fragment,e),_(Oe.$$.fragment,e),_(Me.$$.fragment,e),_(Le.$$.fragment,e),_(Ge.$$.fragment,e),_(Be.$$.fragment,e),_(ze.$$.fragment,e),_(Ve.$$.fragment,e),_(He.$$.fragment,e),_(Ye.$$.fragment,e),_(Fe.$$.fragment,e),_(We.$$.fragment,e),_(Ze.$$.fragment,e),_(Ke.$$.fragment,e),_(Qe.$$.fragment,e),_(_e.$$.fragment,e),_(et.$$.fragment,e),_(tt.$$.fragment,e),_(ot.$$.fragment,e),_(it.$$.fragment,e),_(rt.$$.fragment,e),gs=!0)},o(e){g(E.$$.fragment,e),g(K.$$.fragment,e),g(je.$$.fragment,e),g(Te.$$.fragment,e),g(qe.$$.fragment,e),g(Ne.$$.fragment,e),g(Re.$$.fragment,e),g(Oe.$$.fragment,e),g(Me.$$.fragment,e),g(Le.$$.fragment,e),g(Ge.$$.fragment,e),g(Be.$$.fragment,e),g(ze.$$.fragment,e),g(Ve.$$.fragment,e),g(He.$$.fragment,e),g(Ye.$$.fragment,e),g(Fe.$$.fragment,e),g(We.$$.fragment,e),g(Ze.$$.fragment,e),g(Ke.$$.fragment,e),g(Qe.$$.fragment,e),g(_e.$$.fragment,e),g(et.$$.fragment,e),g(tt.$$.fragment,e),g(ot.$$.fragment,e),g(it.$$.fragment,e),g(rt.$$.fragment,e),gs=!1},d(e){t(b),e&&t($),e&&t(w),v(E),e&&t(_a),e&&t(De),e&&t(ga),e&&t(J),e&&t(va),e&&t(Z),e&&t(ba),v(K,e),e&&t(wa),e&&t(C),v(je),e&&t($a),e&&t(R),v(Te),e&&t(Ea),e&&t(j),e&&t(ya),v(qe,e),e&&t(ka),e&&t(ee),e&&t(Da),e&&t(te),e&&t(xa),v(Ne,e),e&&t(Aa),e&&t(T),e&&t(Pa),e&&t(ae),e&&t(Sa),e&&t(U),v(Re),e&&t(ja),e&&t(oe),e&&t(Ta),e&&t(ct),e&&t(qa),v(Oe,e),e&&t(Ia),e&&t(O),v(Me),e&&t(Na),e&&t(ft),e&&t(Ca),e&&t(re),e&&t(Ra),v(Le,e),e&&t(Ua),e&&t(M),v(Ge),e&&t(Oa),e&&t(ut),e&&t(Ma),v(Be,e),e&&t(La),e&&t(D),e&&t(Ga),e&&t(L),v(ze),e&&t(Ba),e&&t(q),e&&t(za),v(Ve,e),e&&t(Va),e&&t(dt),e&&t(Ha),e&&t(G),v(He),e&&t(Ya),e&&t(mt),e&&t(Fa),e&&t(B),e&&t(Wa),v(Ye,e),e&&t(Ja),e&&t(he),e&&t(Za),v(Fe,e),e&&t(Ka),e&&t(z),v(We),e&&t(Qa),e&&t(fe),e&&t(Xa),v(Ze,e),e&&t(es),e&&t(ue),e&&t(ts),v(Ke,e),e&&t(as),e&&t(V),v(Qe),e&&t(ss),e&&t(x),e&&t(os),e&&t(me),e&&t(is),v(_e,e),e&&t(rs),v(et,e),e&&t(ns),e&&t(H),v(tt),e&&t(ls),e&&t(ve),e&&t(ps),e&&t(I),e&&t(hs),e&&t(be),e&&t(cs),v(ot,e),e&&t(fs),e&&t(Y),v(it),e&&t(us),e&&t(A),e&&t(ds),e&&t(F),e&&t(ms),v(rt,e),e&&t(_s),e&&t($e)}}}const Ln={local:"dreambooth-finetuning-example",sections:[{local:"training-locally",sections:[{local:"installing-the-dependencies",title:"Installing the dependencies"},{local:"dog-toy-example",title:"Dog toy example"},{local:"training-with-a-priorpreserving-loss",title:"Training with a prior-preserving loss"},{local:"saving-checkpoints-while-training",sections:[{local:"resuming-training-from-a-saved-checkpoint",title:"Resuming training from a saved checkpoint"},{local:"performing-inference-using-a-saved-checkpoint",title:"Performing inference using a saved checkpoint"}],title:"Saving checkpoints while training"},{local:"training-on-a-16gb-gpu",title:"Training on a 16GB GPU"},{local:"finetune-the-text-encoder-in-addition-to-the-unet",title:"Fine-tune the text encoder in addition to the UNet"},{local:"training-on-a-8-gb-gpu",title:"Training on a 8 GB GPU:"}],title:"Training locally "},{local:"inference",title:"Inference"}],title:"DreamBooth fine-tuning example"};function Gn(lt){return Rn(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Yn extends Tn{constructor(b){super();qn(this,b,Gn,Mn,In,{})}}export{Yn as default,Ln as metadata};
