import{S as wi,i as xi,s as ki,e as s,k as m,w as E,t as a,M as Ei,c as l,d as t,m as d,a as p,x as A,h as r,b as h,N as bi,G as o,g as f,y as T,q as j,o as D,B as P,v as Ai,L as vr}from"../../chunks/vendor-hf-doc-builder.js";import{T as ta}from"../../chunks/Tip-hf-doc-builder.js";import{I as ut}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as R}from"../../chunks/CodeBlock-hf-doc-builder.js";import{D as Ti}from"../../chunks/DocNotebookDropdown-hf-doc-builder.js";import{F as yi,M as oa}from"../../chunks/Markdown-hf-doc-builder.js";function ji(F){let i,$,n,v,_;return{c(){i=s("p"),$=a("There is a community-created collection of trained Textual Inversion models in the "),n=s("a"),v=a("Stable Diffusion Textual Inversion Concepts Library"),_=a(" which are readily available for inference. Over time, this\u2019ll hopefully grow into a useful resource as more concepts are added!"),this.h()},l(b){i=l(b,"P",{});var y=p(i);$=r(y,"There is a community-created collection of trained Textual Inversion models in the "),n=l(y,"A",{href:!0,rel:!0});var I=p(n);v=r(I,"Stable Diffusion Textual Inversion Concepts Library"),I.forEach(t),_=r(y," which are readily available for inference. Over time, this\u2019ll hopefully grow into a useful resource as more concepts are added!"),y.forEach(t),this.h()},h(){h(n,"href","https://huggingface.co/sd-concepts-library"),h(n,"rel","nofollow")},m(b,y){f(b,i,y),o(i,$),o(i,n),o(n,v),o(i,_)},d(b){b&&t(i)}}}function Di(F){let i,$,n,v,_;return{c(){i=s("p"),$=a("\u{1F4A1} A full training run takes ~1 hour on one V100 GPU. While you\u2019re waiting for the training to complete, feel free to check out "),n=s("a"),v=a("how Textual Inversion works"),_=a(" in the section below if you\u2019re curious!"),this.h()},l(b){i=l(b,"P",{});var y=p(i);$=r(y,"\u{1F4A1} A full training run takes ~1 hour on one V100 GPU. While you\u2019re waiting for the training to complete, feel free to check out "),n=l(y,"A",{href:!0});var I=p(n);v=r(I,"how Textual Inversion works"),I.forEach(t),_=r(y," in the section below if you\u2019re curious!"),y.forEach(t),this.h()},h(){h(n,"href","#how-it-works")},m(b,y){f(b,i,y),o(i,$),o(i,n),o(n,v),o(i,_)},d(b){b&&t(i)}}}function Pi(F){let i,$,n,v,_,b,y,I,C,k,O;return k=new R({props:{code:"--num_vectors=5",highlighted:"--num_vectors=5"}}),{c(){i=s("p"),$=a("\u{1F4A1} If you want to increase the trainable capacity, you can associate your placeholder token, "),n=s("em"),v=a("e.g."),_=m(),b=s("code"),y=a("<cat-toy>"),I=a(` to
multiple embedding vectors. This can help the model to better capture the style of more (complex) images.
To enable training multiple embedding vectors, simply pass:`),C=m(),E(k.$$.fragment)},l(g){i=l(g,"P",{});var x=p(i);$=r(x,"\u{1F4A1} If you want to increase the trainable capacity, you can associate your placeholder token, "),n=l(x,"EM",{});var N=p(n);v=r(N,"e.g."),N.forEach(t),_=d(x),b=l(x,"CODE",{});var G=p(b);y=r(G,"<cat-toy>"),G.forEach(t),I=r(x,` to
multiple embedding vectors. This can help the model to better capture the style of more (complex) images.
To enable training multiple embedding vectors, simply pass:`),x.forEach(t),C=d(g),A(k.$$.fragment,g)},m(g,x){f(g,i,x),o(i,$),o(i,n),o(n,v),o(i,_),o(i,b),o(b,y),o(i,I),f(g,C,x),T(k,g,x),O=!0},p:vr,i(g){O||(j(k.$$.fragment,g),O=!0)},o(g){D(k.$$.fragment,g),O=!1},d(g){g&&t(i),g&&t(C),P(k,g)}}}function Ii(F){let i,$,n,v;return i=new R({props:{code:`export MODEL_NAME="runwayml/stable-diffusion-v1-5"
export DATA_DIR="./cat"

accelerate launch textual_inversion.py \\
  --pretrained_model_name_or_path=$MODEL_NAME \\
  --train_data_dir=$DATA_DIR \\
  --learnable_property="object" \\
  --placeholder_token="<cat-toy>" --initializer_token="toy" \\
  --resolution=512 \\
  --train_batch_size=1 \\
  --gradient_accumulation_steps=4 \\
  --max_train_steps=3000 \\
  --learning_rate=5.0e-04 --scale_lr \\
  --lr_scheduler="constant" \\
  --lr_warmup_steps=0 \\
  --output_dir="textual_inversion_cat"`,highlighted:`<span class="hljs-built_in">export</span> MODEL_NAME=<span class="hljs-string">&quot;runwayml/stable-diffusion-v1-5&quot;</span>
<span class="hljs-built_in">export</span> DATA_DIR=<span class="hljs-string">&quot;./cat&quot;</span>

accelerate launch textual_inversion.py \\
  --pretrained_model_name_or_path=<span class="hljs-variable">$MODEL_NAME</span> \\
  --train_data_dir=<span class="hljs-variable">$DATA_DIR</span> \\
  --learnable_property=<span class="hljs-string">&quot;object&quot;</span> \\
  --placeholder_token=<span class="hljs-string">&quot;&lt;cat-toy&gt;&quot;</span> --initializer_token=<span class="hljs-string">&quot;toy&quot;</span> \\
  --resolution=512 \\
  --train_batch_size=1 \\
  --gradient_accumulation_steps=4 \\
  --max_train_steps=3000 \\
  --learning_rate=5.0e-04 --scale_lr \\
  --lr_scheduler=<span class="hljs-string">&quot;constant&quot;</span> \\
  --lr_warmup_steps=0 \\
  --output_dir=<span class="hljs-string">&quot;textual_inversion_cat&quot;</span>`}}),n=new ta({props:{$$slots:{default:[Pi]},$$scope:{ctx:F}}}),{c(){E(i.$$.fragment),$=m(),E(n.$$.fragment)},l(_){A(i.$$.fragment,_),$=d(_),A(n.$$.fragment,_)},m(_,b){T(i,_,b),f(_,$,b),T(n,_,b),v=!0},p(_,b){const y={};b&2&&(y.$$scope={dirty:b,ctx:_}),n.$set(y)},i(_){v||(j(i.$$.fragment,_),j(n.$$.fragment,_),v=!0)},o(_){D(i.$$.fragment,_),D(n.$$.fragment,_),v=!1},d(_){P(i,_),_&&t($),P(n,_)}}}function qi(F){let i,$;return i=new oa({props:{$$slots:{default:[Ii]},$$scope:{ctx:F}}}),{c(){E(i.$$.fragment)},l(n){A(i.$$.fragment,n)},m(n,v){T(i,n,v),$=!0},p(n,v){const _={};v&2&&(_.$$scope={dirty:v,ctx:n}),i.$set(_)},i(n){$||(j(i.$$.fragment,n),$=!0)},o(n){D(i.$$.fragment,n),$=!1},d(n){P(i,n)}}}function Si(F){let i,$,n,v,_,b,y,I,C,k,O,g,x,N,G,ne,U,K,ae,L,V,te,J,Z,Q,X,B,M;return k=new R({props:{code:"pip install -U -r requirements_flax.txt",highlighted:"pip install -U -r requirements_flax.txt"}}),B=new R({props:{code:`export MODEL_NAME="duongna/stable-diffusion-v1-4-flax"
export DATA_DIR="./cat"

python textual_inversion_flax.py \\
  --pretrained_model_name_or_path=$MODEL_NAME \\
  --train_data_dir=$DATA_DIR \\
  --learnable_property="object" \\
  --placeholder_token="<cat-toy>" --initializer_token="toy" \\
  --resolution=512 \\
  --train_batch_size=1 \\
  --max_train_steps=3000 \\
  --learning_rate=5.0e-04 --scale_lr \\
  --output_dir="textual_inversion_cat"`,highlighted:`<span class="hljs-built_in">export</span> MODEL_NAME=<span class="hljs-string">&quot;duongna/stable-diffusion-v1-4-flax&quot;</span>
<span class="hljs-built_in">export</span> DATA_DIR=<span class="hljs-string">&quot;./cat&quot;</span>

python textual_inversion_flax.py \\
  --pretrained_model_name_or_path=<span class="hljs-variable">$MODEL_NAME</span> \\
  --train_data_dir=<span class="hljs-variable">$DATA_DIR</span> \\
  --learnable_property=<span class="hljs-string">&quot;object&quot;</span> \\
  --placeholder_token=<span class="hljs-string">&quot;&lt;cat-toy&gt;&quot;</span> --initializer_token=<span class="hljs-string">&quot;toy&quot;</span> \\
  --resolution=512 \\
  --train_batch_size=1 \\
  --max_train_steps=3000 \\
  --learning_rate=5.0e-04 --scale_lr \\
  --output_dir=<span class="hljs-string">&quot;textual_inversion_cat&quot;</span>`}}),{c(){i=s("p"),$=a("If you have access to TPUs, try out the "),n=s("a"),v=a("Flax training script"),_=a(" to train even faster (this\u2019ll also work for GPUs). With the same configuration settings, the Flax training script should be at least 70% faster than the PyTorch training script! \u26A1\uFE0F"),b=m(),y=s("p"),I=a("Before you begin, make sure you install the Flax specific dependencies:"),C=m(),E(k.$$.fragment),O=m(),g=s("p"),x=a("Specify the "),N=s("code"),G=a("MODEL_NAME"),ne=a(" environment variable (either a Hub model repository id or a path to the directory containing the model weights) and pass it to the "),U=s("code"),K=a("~diffusers.DiffusionPipeline.from_pretrained.pretrained_model_name_or_path"),ae=a(" argument."),L=m(),V=s("p"),te=a("Then you can launch the "),J=s("a"),Z=a("training script"),Q=a(":"),X=m(),E(B.$$.fragment),this.h()},l(w){i=l(w,"P",{});var S=p(i);$=r(S,"If you have access to TPUs, try out the "),n=l(S,"A",{href:!0,rel:!0});var pe=p(n);v=r(pe,"Flax training script"),pe.forEach(t),_=r(S," to train even faster (this\u2019ll also work for GPUs). With the same configuration settings, the Flax training script should be at least 70% faster than the PyTorch training script! \u26A1\uFE0F"),S.forEach(t),b=d(w),y=l(w,"P",{});var fe=p(y);I=r(fe,"Before you begin, make sure you install the Flax specific dependencies:"),fe.forEach(t),C=d(w),A(k.$$.fragment,w),O=d(w),g=l(w,"P",{});var z=p(g);x=r(z,"Specify the "),N=l(z,"CODE",{});var W=p(N);G=r(W,"MODEL_NAME"),W.forEach(t),ne=r(z," environment variable (either a Hub model repository id or a path to the directory containing the model weights) and pass it to the "),U=l(z,"CODE",{});var ue=p(U);K=r(ue,"~diffusers.DiffusionPipeline.from_pretrained.pretrained_model_name_or_path"),ue.forEach(t),ae=r(z," argument."),z.forEach(t),L=d(w),V=l(w,"P",{});var H=p(V);te=r(H,"Then you can launch the "),J=l(H,"A",{href:!0,rel:!0});var re=p(J);Z=r(re,"training script"),re.forEach(t),Q=r(H,":"),H.forEach(t),X=d(w),A(B.$$.fragment,w),this.h()},h(){h(n,"href","https://github.com/huggingface/diffusers/blob/main/examples/textual_inversion/textual_inversion_flax.py"),h(n,"rel","nofollow"),h(J,"href","https://github.com/huggingface/diffusers/blob/main/examples/textual_inversion/textual_inversion_flax.py"),h(J,"rel","nofollow")},m(w,S){f(w,i,S),o(i,$),o(i,n),o(n,v),o(i,_),f(w,b,S),f(w,y,S),o(y,I),f(w,C,S),T(k,w,S),f(w,O,S),f(w,g,S),o(g,x),o(g,N),o(N,G),o(g,ne),o(g,U),o(U,K),o(g,ae),f(w,L,S),f(w,V,S),o(V,te),o(V,J),o(J,Z),o(V,Q),f(w,X,S),T(B,w,S),M=!0},p:vr,i(w){M||(j(k.$$.fragment,w),j(B.$$.fragment,w),M=!0)},o(w){D(k.$$.fragment,w),D(B.$$.fragment,w),M=!1},d(w){w&&t(i),w&&t(b),w&&t(y),w&&t(C),P(k,w),w&&t(O),w&&t(g),w&&t(L),w&&t(V),w&&t(X),P(B,w)}}}function Oi(F){let i,$;return i=new oa({props:{$$slots:{default:[Si]},$$scope:{ctx:F}}}),{c(){E(i.$$.fragment)},l(n){A(i.$$.fragment,n)},m(n,v){T(i,n,v),$=!0},p(n,v){const _={};v&2&&(_.$$scope={dirty:v,ctx:n}),i.$set(_)},i(n){$||(j(i.$$.fragment,n),$=!0)},o(n){D(i.$$.fragment,n),$=!1},d(n){P(i,n)}}}function Mi(F){let i,$,n,v,_;return{c(){i=s("p"),$=a("\u{1F4A1} The community has created a large library of different textual inversion embedding vectors, called "),n=s("a"),v=a("sd-concepts-library"),_=a(`.
Instead of training textual inversion embeddings from scratch you can also see whether a fitting textual inversion embedding has already been added to the libary.`),this.h()},l(b){i=l(b,"P",{});var y=p(i);$=r(y,"\u{1F4A1} The community has created a large library of different textual inversion embedding vectors, called "),n=l(y,"A",{href:!0,rel:!0});var I=p(n);v=r(I,"sd-concepts-library"),I.forEach(t),_=r(y,`.
Instead of training textual inversion embeddings from scratch you can also see whether a fitting textual inversion embedding has already been added to the libary.`),y.forEach(t),this.h()},h(){h(n,"href","https://huggingface.co/sd-concepts-library"),h(n,"rel","nofollow")},m(b,y){f(b,i,y),o(i,$),o(i,n),o(n,v),o(i,_)},d(b){b&&t(i)}}}function Ni(F){let i,$,n,v,_,b,y,I,C,k,O,g,x,N,G,ne,U,K,ae,L,V,te,J,Z,Q,X,B,M,w,S,pe,fe,z,W,ue,H,re,Re,Ee,Y,Ae;return i=new ta({props:{$$slots:{default:[Mi]},$$scope:{ctx:F}}}),k=new R({props:{code:`from diffusers import StableDiffusionPipeline
import torch

model_id = "runwayml/stable-diffusion-v1-5"
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to("cuda")`,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;runwayml/stable-diffusion-v1-5&quot;</span>
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to(<span class="hljs-string">&quot;cuda&quot;</span>)`}}),K=new R({props:{code:'pipe.load_textual_inversion("sd-concepts-library/cat-toy")',highlighted:'pipe.load_textual_inversion(<span class="hljs-string">&quot;sd-concepts-library/cat-toy&quot;</span>)'}}),X=new R({props:{code:`prompt = "A <cat-toy> backpack"

image = pipe(prompt, num_inference_steps=50).images[0]
image.save("cat-backpack.png")`,highlighted:`prompt = <span class="hljs-string">&quot;A &lt;cat-toy&gt; backpack&quot;</span>

image = pipe(prompt, num_inference_steps=<span class="hljs-number">50</span>).images[<span class="hljs-number">0</span>]
image.save(<span class="hljs-string">&quot;cat-backpack.png&quot;</span>)`}}),Y=new R({props:{code:'pipe.load_textual_inversion("./charturnerv2.pt")',highlighted:'pipe.load_textual_inversion(<span class="hljs-string">&quot;./charturnerv2.pt&quot;</span>)'}}),{c(){E(i.$$.fragment),$=m(),n=s("p"),v=a(`To load the textual inversion embeddings you first need to load the base model that was used when training
your textual inversion embedding vectors. Here we assume that `),_=s("a"),b=s("code"),y=a("runwayml/stable-diffusion-v1-5"),I=a(`
was used as a base model so we load it first:`),C=m(),E(k.$$.fragment),O=m(),g=s("p"),x=a("Next, we need to load the textual inversion embedding vector which can be done via the "),N=s("code"),G=a("TextualInversionLoaderMixin.load_textual_inversion"),ne=a(`
function. Here we\u2019ll load the embeddings of the \u201D<cat-toy>\u201D example from before.`),U=m(),E(K.$$.fragment),ae=m(),L=s("p"),V=a("Now we can run the pipeline making sure that the placeholder token "),te=s("code"),J=a("<cat-toy>"),Z=a(" is used in our prompt."),Q=m(),E(X.$$.fragment),B=m(),M=s("p"),w=a("The function "),S=s("code"),pe=a("TextualInversionLoaderMixin.load_textual_inversion"),fe=a(` can not only
load textual embedding vectors saved in Diffusers\u2019 format, but also embedding vectors
saved in `),z=s("a"),W=a("Automatic1111"),ue=a(` format.
To do so, you can first download an embedding vector from `),H=s("a"),re=a("civitAI"),Re=a(`
and then load it locally:`),Ee=m(),E(Y.$$.fragment),this.h()},l(c){A(i.$$.fragment,c),$=d(c),n=l(c,"P",{});var q=p(n);v=r(q,`To load the textual inversion embeddings you first need to load the base model that was used when training
your textual inversion embedding vectors. Here we assume that `),_=l(q,"A",{href:!0});var ee=p(_);b=l(ee,"CODE",{});var Ge=p(b);y=r(Ge,"runwayml/stable-diffusion-v1-5"),Ge.forEach(t),ee.forEach(t),I=r(q,`
was used as a base model so we load it first:`),q.forEach(t),C=d(c),A(k.$$.fragment,c),O=d(c),g=l(c,"P",{});var se=p(g);x=r(se,"Next, we need to load the textual inversion embedding vector which can be done via the "),N=l(se,"CODE",{});var ct=p(N);G=r(ct,"TextualInversionLoaderMixin.load_textual_inversion"),ct.forEach(t),ne=r(se,`
function. Here we\u2019ll load the embeddings of the \u201D<cat-toy>\u201D example from before.`),se.forEach(t),U=d(c),A(K.$$.fragment,c),ae=d(c),L=l(c,"P",{});var ge=p(L);V=r(ge,"Now we can run the pipeline making sure that the placeholder token "),te=l(ge,"CODE",{});var ce=p(te);J=r(ce,"<cat-toy>"),ce.forEach(t),Z=r(ge," is used in our prompt."),ge.forEach(t),Q=d(c),A(X.$$.fragment,c),B=d(c),M=l(c,"P",{});var oe=p(M);w=r(oe,"The function "),S=l(oe,"CODE",{});var le=p(S);pe=r(le,"TextualInversionLoaderMixin.load_textual_inversion"),le.forEach(t),fe=r(oe,` can not only
load textual embedding vectors saved in Diffusers\u2019 format, but also embedding vectors
saved in `),z=l(oe,"A",{href:!0,rel:!0});var mt=p(z);W=r(mt,"Automatic1111"),mt.forEach(t),ue=r(oe,` format.
To do so, you can first download an embedding vector from `),H=l(oe,"A",{href:!0,rel:!0});var me=p(H);re=r(me,"civitAI"),me.forEach(t),Re=r(oe,`
and then load it locally:`),oe.forEach(t),Ee=d(c),A(Y.$$.fragment,c),this.h()},h(){h(_,"href","runwayml/stable-diffusion-v1-5"),h(z,"href","https://github.com/AUTOMATIC1111/stable-diffusion-webui"),h(z,"rel","nofollow"),h(H,"href","https://civitai.com/models/3036?modelVersionId=8387"),h(H,"rel","nofollow")},m(c,q){T(i,c,q),f(c,$,q),f(c,n,q),o(n,v),o(n,_),o(_,b),o(b,y),o(n,I),f(c,C,q),T(k,c,q),f(c,O,q),f(c,g,q),o(g,x),o(g,N),o(N,G),o(g,ne),f(c,U,q),T(K,c,q),f(c,ae,q),f(c,L,q),o(L,V),o(L,te),o(te,J),o(L,Z),f(c,Q,q),T(X,c,q),f(c,B,q),f(c,M,q),o(M,w),o(M,S),o(S,pe),o(M,fe),o(M,z),o(z,W),o(M,ue),o(M,H),o(H,re),o(M,Re),f(c,Ee,q),T(Y,c,q),Ae=!0},p(c,q){const ee={};q&2&&(ee.$$scope={dirty:q,ctx:c}),i.$set(ee)},i(c){Ae||(j(i.$$.fragment,c),j(k.$$.fragment,c),j(K.$$.fragment,c),j(X.$$.fragment,c),j(Y.$$.fragment,c),Ae=!0)},o(c){D(i.$$.fragment,c),D(k.$$.fragment,c),D(K.$$.fragment,c),D(X.$$.fragment,c),D(Y.$$.fragment,c),Ae=!1},d(c){P(i,c),c&&t($),c&&t(n),c&&t(C),P(k,c),c&&t(O),c&&t(g),c&&t(U),P(K,c),c&&t(ae),c&&t(L),c&&t(Q),P(X,c),c&&t(B),c&&t(M),c&&t(Ee),P(Y,c)}}}function Fi(F){let i,$;return i=new oa({props:{$$slots:{default:[Ni]},$$scope:{ctx:F}}}),{c(){E(i.$$.fragment)},l(n){A(i.$$.fragment,n)},m(n,v){T(i,n,v),$=!0},p(n,v){const _={};v&2&&(_.$$scope={dirty:v,ctx:n}),i.$set(_)},i(n){$||(j(i.$$.fragment,n),$=!0)},o(n){D(i.$$.fragment,n),$=!1},d(n){P(i,n)}}}function Ci(F){let i,$,n,v,_,b,y,I,C,k,O;return k=new R({props:{code:`import jax
import numpy as np
from flax.jax_utils import replicate
from flax.training.common_utils import shard
from diffusers import FlaxStableDiffusionPipeline

model_path = "path-to-your-trained-model"
pipe, params = FlaxStableDiffusionPipeline.from_pretrained(model_path, dtype=jax.numpy.bfloat16)

prompt = "A <cat-toy> backpack"
prng_seed = jax.random.PRNGKey(0)
num_inference_steps = 50

num_samples = jax.device_count()
prompt = num_samples * [prompt]
prompt_ids = pipeline.prepare_inputs(prompt)

# shard inputs and rng
params = replicate(params)
prng_seed = jax.random.split(prng_seed, jax.device_count())
prompt_ids = shard(prompt_ids)

images = pipeline(prompt_ids, params, prng_seed, num_inference_steps, jit=True).images
images = pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:])))
image.save("cat-backpack.png")`,highlighted:`<span class="hljs-keyword">import</span> jax
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-keyword">from</span> flax.jax_utils <span class="hljs-keyword">import</span> replicate
<span class="hljs-keyword">from</span> flax.training.common_utils <span class="hljs-keyword">import</span> shard
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> FlaxStableDiffusionPipeline

model_path = <span class="hljs-string">&quot;path-to-your-trained-model&quot;</span>
pipe, params = FlaxStableDiffusionPipeline.from_pretrained(model_path, dtype=jax.numpy.bfloat16)

prompt = <span class="hljs-string">&quot;A &lt;cat-toy&gt; backpack&quot;</span>
prng_seed = jax.random.PRNGKey(<span class="hljs-number">0</span>)
num_inference_steps = <span class="hljs-number">50</span>

num_samples = jax.device_count()
prompt = num_samples * [prompt]
prompt_ids = pipeline.prepare_inputs(prompt)

<span class="hljs-comment"># shard inputs and rng</span>
params = replicate(params)
prng_seed = jax.random.split(prng_seed, jax.device_count())
prompt_ids = shard(prompt_ids)

images = pipeline(prompt_ids, params, prng_seed, num_inference_steps, jit=<span class="hljs-literal">True</span>).images
images = pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-<span class="hljs-number">3</span>:])))
image.save(<span class="hljs-string">&quot;cat-backpack.png&quot;</span>)`}}),{c(){i=s("p"),$=a("Currently there is no "),n=s("code"),v=a("load_textual_inversion"),_=a(` function for Flax so one has to make sure the textual inversion
embedding vector is saved as part of the model after training.`),b=m(),y=s("p"),I=a("The model can then be run just like any other Flax model:"),C=m(),E(k.$$.fragment)},l(g){i=l(g,"P",{});var x=p(i);$=r(x,"Currently there is no "),n=l(x,"CODE",{});var N=p(n);v=r(N,"load_textual_inversion"),N.forEach(t),_=r(x,` function for Flax so one has to make sure the textual inversion
embedding vector is saved as part of the model after training.`),x.forEach(t),b=d(g),y=l(g,"P",{});var G=p(y);I=r(G,"The model can then be run just like any other Flax model:"),G.forEach(t),C=d(g),A(k.$$.fragment,g)},m(g,x){f(g,i,x),o(i,$),o(i,n),o(n,v),o(i,_),f(g,b,x),f(g,y,x),o(y,I),f(g,C,x),T(k,g,x),O=!0},p:vr,i(g){O||(j(k.$$.fragment,g),O=!0)},o(g){D(k.$$.fragment,g),O=!1},d(g){g&&t(i),g&&t(b),g&&t(y),g&&t(C),P(k,g)}}}function Li(F){let i,$;return i=new oa({props:{$$slots:{default:[Ci]},$$scope:{ctx:F}}}),{c(){E(i.$$.fragment)},l(n){A(i.$$.fragment,n)},m(n,v){T(i,n,v),$=!0},p(n,v){const _={};v&2&&(_.$$scope={dirty:v,ctx:n}),i.$set(_)},i(n){$||(j(i.$$.fragment,n),$=!0)},o(n){D(i.$$.fragment,n),$=!1},d(n){P(i,n)}}}function Hi(F){let i,$,n,v,_,b,y,I,C,k,O,g,x,N,G,ne,U,K,ae,L,V,te,J,Z,Q,X,B,M,w,S,pe,fe,z,W,ue,H,re,Re,Ee,Y,Ae,c,q,ee,Ge,se,ct,ge,ce,oe,le,mt,me,aa,ra,no,Be,so,dt,ia,lo,We,po,ht,na,fo,Ve,uo,de,sa,Je,la,pa,Pt,fa,ua,co,ve,Te,It,Ke,ca,qt,ma,mo,_t,da,ho,Qe,_o,$e,je,St,Xe,ha,Ot,_a,go,De,ga,Mt,va,$a,vo,Ye,$o,gt,ba,bo,Ze,yo,be,Pe,Nt,et,ya,Ft,wa,wo,Ie,xa,tt,ka,Ea,xo,ot,ko,ie,Aa,Ct,Ta,ja,Lt,Da,Pa,Ht,Ia,qa,Eo,qe,Sa,at,Oa,Ma,Ao,Se,To,Oe,jo,ye,Me,zt,rt,Na,Ut,Fa,Do,vt,Ca,Po,he,Ne,Rt,La,Ha,Gt,za,Ua,Ra,$t,Bt,Ga,Ba,Wa,we,Wt,Va,Ja,Vt,Ka,Qa,Jt,Xa,Io,it,qo,xe,Fe,Kt,nt,Ya,Qt,Za,So,Ce,er,bt,tr,or,Oo,yt,ar,Mo,Le,No,ke,He,Xt,st,rr,Yt,ir,Fo,wt,xt,$r,Co,lt,nr,kt,sr,Lo,_e,lr,Zt,pr,fr,eo,ur,cr,Ho,ze,mr,to,dr,hr,zo;return b=new ut({}),O=new Ti({props:{classNames:"absolute z-10 right-0 top-0",options:[{label:"Mixed",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers_doc/en/text_inversion.ipynb"},{label:"PyTorch",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers_doc/en/pytorch/text_inversion.ipynb"},{label:"TensorFlow",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers_doc/en/tensorflow/text_inversion.ipynb"},{label:"Mixed",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/en/text_inversion.ipynb"},{label:"PyTorch",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/en/pytorch/text_inversion.ipynb"},{label:"TensorFlow",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/en/tensorflow/text_inversion.ipynb"}]}}),ee=new ta({props:{$$slots:{default:[ji]},$$scope:{ctx:F}}}),ce=new R({props:{code:"pip install diffusers accelerate transformers",highlighted:"pip install diffusers accelerate transformers"}}),Be=new R({props:{code:"accelerate config",highlighted:"accelerate config"}}),We=new R({props:{code:"accelerate config default",highlighted:"accelerate config default"}}),Ve=new R({props:{code:`from accelerate.utils import write_basic_config

write_basic_config()`,highlighted:`from accelerate.utils import write_basic_config

write_basic_config()`}}),Ke=new ut({}),Qe=new R({props:{code:"--push_to_hub",highlighted:"--push_to_hub"}}),Xe=new ut({}),Ye=new R({props:{code:"--checkpointing_steps=500",highlighted:"--checkpointing_steps=500"}}),Ze=new R({props:{code:'--resume_from_checkpoint="checkpoint-1500"',highlighted:'--resume_from_checkpoint=<span class="hljs-string">&quot;checkpoint-1500&quot;</span>'}}),et=new ut({}),ot=new R({props:{code:`from huggingface_hub import snapshot_download

local_dir = "./cat"
snapshot_download(
    "diffusers/cat_toy_example", local_dir=local_dir, repo_type="dataset", ignore_patterns=".gitattributes"
)`,highlighted:`<span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> snapshot_download

local_dir = <span class="hljs-string">&quot;./cat&quot;</span>
snapshot_download(
    <span class="hljs-string">&quot;diffusers/cat_toy_example&quot;</span>, local_dir=local_dir, repo_type=<span class="hljs-string">&quot;dataset&quot;</span>, ignore_patterns=<span class="hljs-string">&quot;.gitattributes&quot;</span>
)`}}),Se=new ta({props:{$$slots:{default:[Di]},$$scope:{ctx:F}}}),Oe=new yi({props:{pytorch:!0,tensorflow:!1,jax:!0,$$slots:{jax:[Oi],pytorch:[qi]},$$scope:{ctx:F}}}),rt=new ut({}),it=new R({props:{code:`--validation_prompt="A <cat-toy> backpack"
--num_validation_images=4
--validation_steps=100`,highlighted:`--validation_prompt=<span class="hljs-string">&quot;A &lt;cat-toy&gt; backpack&quot;</span>
--num_validation_images=4
--validation_steps=100`}}),nt=new ut({}),Le=new yi({props:{pytorch:!0,tensorflow:!1,jax:!0,$$slots:{jax:[Li],pytorch:[Fi]},$$scope:{ctx:F}}}),st=new ut({}),{c(){i=s("meta"),$=m(),n=s("h1"),v=s("a"),_=s("span"),E(b.$$.fragment),y=m(),I=s("span"),C=a("Textual Inversion"),k=m(),E(O.$$.fragment),g=m(),x=s("p"),N=s("a"),G=a("Textual Inversion"),ne=a(" is a technique for capturing novel concepts from a small number of example images. While the technique was originally demonstrated with a "),U=s("a"),K=a("latent diffusion model"),ae=a(", it has since been applied to other model variants like "),L=s("a"),V=a("Stable Diffusion"),te=a(". The learned concepts can be used to better control the images generated from text-to-image pipelines. It learns new \u201Cwords\u201D in the text encoder\u2019s embedding space, which are used within text prompts for personalized image generation."),J=m(),Z=s("p"),Q=s("img"),B=m(),M=s("small"),w=a("By using just 3-5 images you can teach new concepts to a model such as Stable Diffusion for personalized image generation "),S=s("a"),pe=a("(image source)"),fe=a("."),z=m(),W=s("p"),ue=a("This guide will show you how to train a "),H=s("a"),re=s("code"),Re=a("runwayml/stable-diffusion-v1-5"),Ee=a(" model with Textual Inversion. All the training scripts for Textual Inversion used in this guide can be found "),Y=s("a"),Ae=a("here"),c=a(" if you\u2019re interested in taking a closer look at how things work under the hood."),q=m(),E(ee.$$.fragment),Ge=m(),se=s("p"),ct=a("Before you begin, make sure you install the library\u2019s training dependencies:"),ge=m(),E(ce.$$.fragment),oe=m(),le=s("p"),mt=a("After all the dependencies have been set up, initialize a "),me=s("a"),aa=a("\u{1F917}Accelerate"),ra=a(" environment with:"),no=m(),E(Be.$$.fragment),so=m(),dt=s("p"),ia=a("To setup a default \u{1F917} Accelerate environment without choosing any configurations:"),lo=m(),E(We.$$.fragment),po=m(),ht=s("p"),na=a("Or if your environment doesn\u2019t support an interactive shell like a notebook, you can use:"),fo=m(),E(Ve.$$.fragment),uo=m(),de=s("p"),sa=a("Finally, you try and "),Je=s("a"),la=a("install xFormers"),pa=a(" to reduce your memory footprint with xFormers memory-efficient attention. Once you have xFormers installed, add the "),Pt=s("code"),fa=a("--enable_xformers_memory_efficient_attention"),ua=a(" argument to the training script. xFormers is not supported for Flax."),co=m(),ve=s("h2"),Te=s("a"),It=s("span"),E(Ke.$$.fragment),ca=m(),qt=s("span"),ma=a("Upload model to Hub"),mo=m(),_t=s("p"),da=a("If you want to store your model on the Hub, add the following argument to the training script:"),ho=m(),E(Qe.$$.fragment),_o=m(),$e=s("h2"),je=s("a"),St=s("span"),E(Xe.$$.fragment),ha=m(),Ot=s("span"),_a=a("Save and load checkpoints"),go=m(),De=s("p"),ga=a("It is often a good idea to regularly save checkpoints of your model during training. This way, you can resume training from a saved checkpoint if your training is interrupted for any reason. To save a checkpoint, pass the following argument to the training script to save the full training state in a subfolder in "),Mt=s("code"),va=a("output_dir"),$a=a(" every 500 steps:"),vo=m(),E(Ye.$$.fragment),$o=m(),gt=s("p"),ba=a("To resume training from a saved checkpoint, pass the following argument to the training script and the specific checkpoint you\u2019d like to resume from:"),bo=m(),E(Ze.$$.fragment),yo=m(),be=s("h2"),Pe=s("a"),Nt=s("span"),E(et.$$.fragment),ya=m(),Ft=s("span"),wa=a("Finetuning"),wo=m(),Ie=s("p"),xa=a("For your training dataset, download these "),tt=s("a"),ka=a("images of a cat toy"),Ea=a(" and store them in a directory:"),xo=m(),E(ot.$$.fragment),ko=m(),ie=s("p"),Aa=a("Specify the "),Ct=s("code"),Ta=a("MODEL_NAME"),ja=a(" environment variable (either a Hub model repository id or a path to the directory containing the model weights) and pass it to the "),Lt=s("code"),Da=a("~diffusers.DiffusionPipeline.from_pretrained.pretrained_model_name_or_path"),Pa=a(" argument, and the "),Ht=s("code"),Ia=a("DATA_DIR"),qa=a(" environment variable to the path of the directory containing the images."),Eo=m(),qe=s("p"),Sa=a("Now you can launch the "),at=s("a"),Oa=a("training script"),Ma=a(":"),Ao=m(),E(Se.$$.fragment),To=m(),E(Oe.$$.fragment),jo=m(),ye=s("h3"),Me=s("a"),zt=s("span"),E(rt.$$.fragment),Na=m(),Ut=s("span"),Fa=a("Intermediate logging"),Do=m(),vt=s("p"),Ca=a("If you\u2019re interested in following along with your model training progress, you can save the generated images from the training process. Add the following arguments to the training script to enable intermediate logging:"),Po=m(),he=s("ul"),Ne=s("li"),Rt=s("code"),La=a("validation_prompt"),Ha=a(", the prompt used to generate samples (this is set to "),Gt=s("code"),za=a("None"),Ua=a(" by default and intermediate logging is disabled)"),Ra=m(),$t=s("li"),Bt=s("code"),Ga=a("num_validation_images"),Ba=a(", the number of sample images to generate"),Wa=m(),we=s("li"),Wt=s("code"),Va=a("validation_steps"),Ja=a(", the number of steps before generating "),Vt=s("code"),Ka=a("num_validation_images"),Qa=a(" from the "),Jt=s("code"),Xa=a("validation_prompt"),Io=m(),E(it.$$.fragment),qo=m(),xe=s("h2"),Fe=s("a"),Kt=s("span"),E(nt.$$.fragment),Ya=m(),Qt=s("span"),Za=a("Inference"),So=m(),Ce=s("p"),er=a("Once you have trained a model, you can use it for inference with the "),bt=s("a"),tr=a("StableDiffusionPipeline"),or=a("."),Oo=m(),yt=s("p"),ar=a(`The textual inversion script will by default only save the textual inversion embedding vector(s) that have
been added to the text encoder embedding matrix and consequently been trained.`),Mo=m(),E(Le.$$.fragment),No=m(),ke=s("h2"),He=s("a"),Xt=s("span"),E(st.$$.fragment),rr=m(),Yt=s("span"),ir=a("How it works"),Fo=m(),wt=s("p"),xt=s("img"),Co=m(),lt=s("small"),nr=a("Architecture overview from the Textual Inversion "),kt=s("a"),sr=a("blog post."),Lo=m(),_e=s("p"),lr=a("Usually, text prompts are tokenized into an embedding before being passed to a model, which is often a transformer. Textual Inversion does something similar, but it learns a new token embedding, "),Zt=s("code"),pr=a("v*"),fr=a(", from a special token "),eo=s("code"),ur=a("S*"),cr=a(" in the diagram above. The model output is used to condition the diffusion model, which helps the diffusion model understand the prompt and new concepts from just a few example images."),Ho=m(),ze=s("p"),mr=a("To do this, Textual Inversion uses a generator model and noisy versions of the training images. The generator tries to predict less noisy versions of the images, and the token embedding "),to=s("code"),dr=a("v*"),hr=a(" is optimized based on how well the generator does. If the token embedding successfully captures the new concept, it gives more useful information to the diffusion model and helps create clearer images with less noise. This optimization process typically occurs after several thousand steps of exposure to a variety of prompt and image variants."),this.h()},l(e){const u=Ei('[data-svelte="svelte-1phssyn"]',document.head);i=l(u,"META",{name:!0,content:!0}),u.forEach(t),$=d(e),n=l(e,"H1",{class:!0});var pt=p(n);v=l(pt,"A",{id:!0,class:!0,href:!0});var oo=p(v);_=l(oo,"SPAN",{});var ao=p(_);A(b.$$.fragment,ao),ao.forEach(t),oo.forEach(t),y=d(pt),I=l(pt,"SPAN",{});var ro=p(I);C=r(ro,"Textual Inversion"),ro.forEach(t),pt.forEach(t),k=d(e),A(O.$$.fragment,e),g=d(e),x=l(e,"P",{});var ft=p(x);N=l(ft,"A",{href:!0,rel:!0});var br=p(N);G=r(br,"Textual Inversion"),br.forEach(t),ne=r(ft," is a technique for capturing novel concepts from a small number of example images. While the technique was originally demonstrated with a "),U=l(ft,"A",{href:!0,rel:!0});var yr=p(U);K=r(yr,"latent diffusion model"),yr.forEach(t),ae=r(ft,", it has since been applied to other model variants like "),L=l(ft,"A",{href:!0,rel:!0});var wr=p(L);V=r(wr,"Stable Diffusion"),wr.forEach(t),te=r(ft,". The learned concepts can be used to better control the images generated from text-to-image pipelines. It learns new \u201Cwords\u201D in the text encoder\u2019s embedding space, which are used within text prompts for personalized image generation."),ft.forEach(t),J=d(e),Z=l(e,"P",{});var xr=p(Z);Q=l(xr,"IMG",{src:!0,alt:!0}),xr.forEach(t),B=d(e),M=l(e,"SMALL",{});var Uo=p(M);w=r(Uo,"By using just 3-5 images you can teach new concepts to a model such as Stable Diffusion for personalized image generation "),S=l(Uo,"A",{href:!0});var kr=p(S);pe=r(kr,"(image source)"),kr.forEach(t),fe=r(Uo,"."),Uo.forEach(t),z=d(e),W=l(e,"P",{});var Et=p(W);ue=r(Et,"This guide will show you how to train a "),H=l(Et,"A",{href:!0,rel:!0});var Er=p(H);re=l(Er,"CODE",{});var Ar=p(re);Re=r(Ar,"runwayml/stable-diffusion-v1-5"),Ar.forEach(t),Er.forEach(t),Ee=r(Et," model with Textual Inversion. All the training scripts for Textual Inversion used in this guide can be found "),Y=l(Et,"A",{href:!0,rel:!0});var Tr=p(Y);Ae=r(Tr,"here"),Tr.forEach(t),c=r(Et," if you\u2019re interested in taking a closer look at how things work under the hood."),Et.forEach(t),q=d(e),A(ee.$$.fragment,e),Ge=d(e),se=l(e,"P",{});var jr=p(se);ct=r(jr,"Before you begin, make sure you install the library\u2019s training dependencies:"),jr.forEach(t),ge=d(e),A(ce.$$.fragment,e),oe=d(e),le=l(e,"P",{});var Ro=p(le);mt=r(Ro,"After all the dependencies have been set up, initialize a "),me=l(Ro,"A",{href:!0,rel:!0});var Dr=p(me);aa=r(Dr,"\u{1F917}Accelerate"),Dr.forEach(t),ra=r(Ro," environment with:"),Ro.forEach(t),no=d(e),A(Be.$$.fragment,e),so=d(e),dt=l(e,"P",{});var Pr=p(dt);ia=r(Pr,"To setup a default \u{1F917} Accelerate environment without choosing any configurations:"),Pr.forEach(t),lo=d(e),A(We.$$.fragment,e),po=d(e),ht=l(e,"P",{});var Ir=p(ht);na=r(Ir,"Or if your environment doesn\u2019t support an interactive shell like a notebook, you can use:"),Ir.forEach(t),fo=d(e),A(Ve.$$.fragment,e),uo=d(e),de=l(e,"P",{});var At=p(de);sa=r(At,"Finally, you try and "),Je=l(At,"A",{href:!0,rel:!0});var qr=p(Je);la=r(qr,"install xFormers"),qr.forEach(t),pa=r(At," to reduce your memory footprint with xFormers memory-efficient attention. Once you have xFormers installed, add the "),Pt=l(At,"CODE",{});var Sr=p(Pt);fa=r(Sr,"--enable_xformers_memory_efficient_attention"),Sr.forEach(t),ua=r(At," argument to the training script. xFormers is not supported for Flax."),At.forEach(t),co=d(e),ve=l(e,"H2",{class:!0});var Go=p(ve);Te=l(Go,"A",{id:!0,class:!0,href:!0});var Or=p(Te);It=l(Or,"SPAN",{});var Mr=p(It);A(Ke.$$.fragment,Mr),Mr.forEach(t),Or.forEach(t),ca=d(Go),qt=l(Go,"SPAN",{});var Nr=p(qt);ma=r(Nr,"Upload model to Hub"),Nr.forEach(t),Go.forEach(t),mo=d(e),_t=l(e,"P",{});var Fr=p(_t);da=r(Fr,"If you want to store your model on the Hub, add the following argument to the training script:"),Fr.forEach(t),ho=d(e),A(Qe.$$.fragment,e),_o=d(e),$e=l(e,"H2",{class:!0});var Bo=p($e);je=l(Bo,"A",{id:!0,class:!0,href:!0});var Cr=p(je);St=l(Cr,"SPAN",{});var Lr=p(St);A(Xe.$$.fragment,Lr),Lr.forEach(t),Cr.forEach(t),ha=d(Bo),Ot=l(Bo,"SPAN",{});var Hr=p(Ot);_a=r(Hr,"Save and load checkpoints"),Hr.forEach(t),Bo.forEach(t),go=d(e),De=l(e,"P",{});var Wo=p(De);ga=r(Wo,"It is often a good idea to regularly save checkpoints of your model during training. This way, you can resume training from a saved checkpoint if your training is interrupted for any reason. To save a checkpoint, pass the following argument to the training script to save the full training state in a subfolder in "),Mt=l(Wo,"CODE",{});var zr=p(Mt);va=r(zr,"output_dir"),zr.forEach(t),$a=r(Wo," every 500 steps:"),Wo.forEach(t),vo=d(e),A(Ye.$$.fragment,e),$o=d(e),gt=l(e,"P",{});var Ur=p(gt);ba=r(Ur,"To resume training from a saved checkpoint, pass the following argument to the training script and the specific checkpoint you\u2019d like to resume from:"),Ur.forEach(t),bo=d(e),A(Ze.$$.fragment,e),yo=d(e),be=l(e,"H2",{class:!0});var Vo=p(be);Pe=l(Vo,"A",{id:!0,class:!0,href:!0});var Rr=p(Pe);Nt=l(Rr,"SPAN",{});var Gr=p(Nt);A(et.$$.fragment,Gr),Gr.forEach(t),Rr.forEach(t),ya=d(Vo),Ft=l(Vo,"SPAN",{});var Br=p(Ft);wa=r(Br,"Finetuning"),Br.forEach(t),Vo.forEach(t),wo=d(e),Ie=l(e,"P",{});var Jo=p(Ie);xa=r(Jo,"For your training dataset, download these "),tt=l(Jo,"A",{href:!0,rel:!0});var Wr=p(tt);ka=r(Wr,"images of a cat toy"),Wr.forEach(t),Ea=r(Jo," and store them in a directory:"),Jo.forEach(t),xo=d(e),A(ot.$$.fragment,e),ko=d(e),ie=l(e,"P",{});var Ue=p(ie);Aa=r(Ue,"Specify the "),Ct=l(Ue,"CODE",{});var Vr=p(Ct);Ta=r(Vr,"MODEL_NAME"),Vr.forEach(t),ja=r(Ue," environment variable (either a Hub model repository id or a path to the directory containing the model weights) and pass it to the "),Lt=l(Ue,"CODE",{});var Jr=p(Lt);Da=r(Jr,"~diffusers.DiffusionPipeline.from_pretrained.pretrained_model_name_or_path"),Jr.forEach(t),Pa=r(Ue," argument, and the "),Ht=l(Ue,"CODE",{});var Kr=p(Ht);Ia=r(Kr,"DATA_DIR"),Kr.forEach(t),qa=r(Ue," environment variable to the path of the directory containing the images."),Ue.forEach(t),Eo=d(e),qe=l(e,"P",{});var Ko=p(qe);Sa=r(Ko,"Now you can launch the "),at=l(Ko,"A",{href:!0,rel:!0});var Qr=p(at);Oa=r(Qr,"training script"),Qr.forEach(t),Ma=r(Ko,":"),Ko.forEach(t),Ao=d(e),A(Se.$$.fragment,e),To=d(e),A(Oe.$$.fragment,e),jo=d(e),ye=l(e,"H3",{class:!0});var Qo=p(ye);Me=l(Qo,"A",{id:!0,class:!0,href:!0});var Xr=p(Me);zt=l(Xr,"SPAN",{});var Yr=p(zt);A(rt.$$.fragment,Yr),Yr.forEach(t),Xr.forEach(t),Na=d(Qo),Ut=l(Qo,"SPAN",{});var Zr=p(Ut);Fa=r(Zr,"Intermediate logging"),Zr.forEach(t),Qo.forEach(t),Do=d(e),vt=l(e,"P",{});var ei=p(vt);Ca=r(ei,"If you\u2019re interested in following along with your model training progress, you can save the generated images from the training process. Add the following arguments to the training script to enable intermediate logging:"),ei.forEach(t),Po=d(e),he=l(e,"UL",{});var Tt=p(he);Ne=l(Tt,"LI",{});var io=p(Ne);Rt=l(io,"CODE",{});var ti=p(Rt);La=r(ti,"validation_prompt"),ti.forEach(t),Ha=r(io,", the prompt used to generate samples (this is set to "),Gt=l(io,"CODE",{});var oi=p(Gt);za=r(oi,"None"),oi.forEach(t),Ua=r(io," by default and intermediate logging is disabled)"),io.forEach(t),Ra=d(Tt),$t=l(Tt,"LI",{});var _r=p($t);Bt=l(_r,"CODE",{});var ai=p(Bt);Ga=r(ai,"num_validation_images"),ai.forEach(t),Ba=r(_r,", the number of sample images to generate"),_r.forEach(t),Wa=d(Tt),we=l(Tt,"LI",{});var jt=p(we);Wt=l(jt,"CODE",{});var ri=p(Wt);Va=r(ri,"validation_steps"),ri.forEach(t),Ja=r(jt,", the number of steps before generating "),Vt=l(jt,"CODE",{});var ii=p(Vt);Ka=r(ii,"num_validation_images"),ii.forEach(t),Qa=r(jt," from the "),Jt=l(jt,"CODE",{});var ni=p(Jt);Xa=r(ni,"validation_prompt"),ni.forEach(t),jt.forEach(t),Tt.forEach(t),Io=d(e),A(it.$$.fragment,e),qo=d(e),xe=l(e,"H2",{class:!0});var Xo=p(xe);Fe=l(Xo,"A",{id:!0,class:!0,href:!0});var si=p(Fe);Kt=l(si,"SPAN",{});var li=p(Kt);A(nt.$$.fragment,li),li.forEach(t),si.forEach(t),Ya=d(Xo),Qt=l(Xo,"SPAN",{});var pi=p(Qt);Za=r(pi,"Inference"),pi.forEach(t),Xo.forEach(t),So=d(e),Ce=l(e,"P",{});var Yo=p(Ce);er=r(Yo,"Once you have trained a model, you can use it for inference with the "),bt=l(Yo,"A",{href:!0});var fi=p(bt);tr=r(fi,"StableDiffusionPipeline"),fi.forEach(t),or=r(Yo,"."),Yo.forEach(t),Oo=d(e),yt=l(e,"P",{});var ui=p(yt);ar=r(ui,`The textual inversion script will by default only save the textual inversion embedding vector(s) that have
been added to the text encoder embedding matrix and consequently been trained.`),ui.forEach(t),Mo=d(e),A(Le.$$.fragment,e),No=d(e),ke=l(e,"H2",{class:!0});var Zo=p(ke);He=l(Zo,"A",{id:!0,class:!0,href:!0});var ci=p(He);Xt=l(ci,"SPAN",{});var mi=p(Xt);A(st.$$.fragment,mi),mi.forEach(t),ci.forEach(t),rr=d(Zo),Yt=l(Zo,"SPAN",{});var di=p(Yt);ir=r(di,"How it works"),di.forEach(t),Zo.forEach(t),Fo=d(e),wt=l(e,"P",{});var hi=p(wt);xt=l(hi,"IMG",{src:!0,alt:!0}),hi.forEach(t),Co=d(e),lt=l(e,"SMALL",{});var gr=p(lt);nr=r(gr,"Architecture overview from the Textual Inversion "),kt=l(gr,"A",{href:!0});var _i=p(kt);sr=r(_i,"blog post."),_i.forEach(t),gr.forEach(t),Lo=d(e),_e=l(e,"P",{});var Dt=p(_e);lr=r(Dt,"Usually, text prompts are tokenized into an embedding before being passed to a model, which is often a transformer. Textual Inversion does something similar, but it learns a new token embedding, "),Zt=l(Dt,"CODE",{});var gi=p(Zt);pr=r(gi,"v*"),gi.forEach(t),fr=r(Dt,", from a special token "),eo=l(Dt,"CODE",{});var vi=p(eo);ur=r(vi,"S*"),vi.forEach(t),cr=r(Dt," in the diagram above. The model output is used to condition the diffusion model, which helps the diffusion model understand the prompt and new concepts from just a few example images."),Dt.forEach(t),Ho=d(e),ze=l(e,"P",{});var ea=p(ze);mr=r(ea,"To do this, Textual Inversion uses a generator model and noisy versions of the training images. The generator tries to predict less noisy versions of the images, and the token embedding "),to=l(ea,"CODE",{});var $i=p(to);dr=r($i,"v*"),$i.forEach(t),hr=r(ea," is optimized based on how well the generator does. If the token embedding successfully captures the new concept, it gives more useful information to the diffusion model and helps create clearer images with less noise. This optimization process typically occurs after several thousand steps of exposure to a variety of prompt and image variants."),ea.forEach(t),this.h()},h(){h(i,"name","hf:doc:metadata"),h(i,"content",JSON.stringify(zi)),h(v,"id","textual-inversion"),h(v,"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(v,"href","#textual-inversion"),h(n,"class","relative group"),h(N,"href","https://arxiv.org/abs/2208.01618"),h(N,"rel","nofollow"),h(U,"href","https://github.com/CompVis/latent-diffusion"),h(U,"rel","nofollow"),h(L,"href","https://huggingface.co/docs/diffusers/main/en/conceptual/stable_diffusion"),h(L,"rel","nofollow"),bi(Q.src,X="https://textual-inversion.github.io/static/images/editing/colorful_teapot.JPG")||h(Q,"src",X),h(Q,"alt","Textual Inversion example"),h(S,"href","https://github.com/rinongal/textual_inversion"),h(H,"href","https://huggingface.co/runwayml/stable-diffusion-v1-5"),h(H,"rel","nofollow"),h(Y,"href","https://github.com/huggingface/diffusers/tree/main/examples/textual_inversion"),h(Y,"rel","nofollow"),h(me,"href","https://github.com/huggingface/accelerate/"),h(me,"rel","nofollow"),h(Je,"href","https://huggingface.co/docs/diffusers/main/en/training/optimization/xformers"),h(Je,"rel","nofollow"),h(Te,"id","upload-model-to-hub"),h(Te,"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(Te,"href","#upload-model-to-hub"),h(ve,"class","relative group"),h(je,"id","save-and-load-checkpoints"),h(je,"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(je,"href","#save-and-load-checkpoints"),h($e,"class","relative group"),h(Pe,"id","finetuning"),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","#finetuning"),h(be,"class","relative group"),h(tt,"href","https://huggingface.co/datasets/diffusers/cat_toy_example"),h(tt,"rel","nofollow"),h(at,"href","https://github.com/huggingface/diffusers/blob/main/examples/textual_inversion/textual_inversion.py"),h(at,"rel","nofollow"),h(Me,"id","intermediate-logging"),h(Me,"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(Me,"href","#intermediate-logging"),h(ye,"class","relative group"),h(Fe,"id","inference"),h(Fe,"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(Fe,"href","#inference"),h(xe,"class","relative group"),h(bt,"href","/docs/diffusers/v0.16.0/en/api/pipelines/stable_diffusion/text2img#diffusers.StableDiffusionPipeline"),h(He,"id","how-it-works"),h(He,"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(He,"href","#how-it-works"),h(ke,"class","relative group"),bi(xt.src,$r="https://textual-inversion.github.io/static/images/training/training.JPG")||h(xt,"src",$r),h(xt,"alt","Diagram from the paper showing overview"),h(kt,"href","https://textual-inversion.github.io/")},m(e,u){o(document.head,i),f(e,$,u),f(e,n,u),o(n,v),o(v,_),T(b,_,null),o(n,y),o(n,I),o(I,C),f(e,k,u),T(O,e,u),f(e,g,u),f(e,x,u),o(x,N),o(N,G),o(x,ne),o(x,U),o(U,K),o(x,ae),o(x,L),o(L,V),o(x,te),f(e,J,u),f(e,Z,u),o(Z,Q),f(e,B,u),f(e,M,u),o(M,w),o(M,S),o(S,pe),o(M,fe),f(e,z,u),f(e,W,u),o(W,ue),o(W,H),o(H,re),o(re,Re),o(W,Ee),o(W,Y),o(Y,Ae),o(W,c),f(e,q,u),T(ee,e,u),f(e,Ge,u),f(e,se,u),o(se,ct),f(e,ge,u),T(ce,e,u),f(e,oe,u),f(e,le,u),o(le,mt),o(le,me),o(me,aa),o(le,ra),f(e,no,u),T(Be,e,u),f(e,so,u),f(e,dt,u),o(dt,ia),f(e,lo,u),T(We,e,u),f(e,po,u),f(e,ht,u),o(ht,na),f(e,fo,u),T(Ve,e,u),f(e,uo,u),f(e,de,u),o(de,sa),o(de,Je),o(Je,la),o(de,pa),o(de,Pt),o(Pt,fa),o(de,ua),f(e,co,u),f(e,ve,u),o(ve,Te),o(Te,It),T(Ke,It,null),o(ve,ca),o(ve,qt),o(qt,ma),f(e,mo,u),f(e,_t,u),o(_t,da),f(e,ho,u),T(Qe,e,u),f(e,_o,u),f(e,$e,u),o($e,je),o(je,St),T(Xe,St,null),o($e,ha),o($e,Ot),o(Ot,_a),f(e,go,u),f(e,De,u),o(De,ga),o(De,Mt),o(Mt,va),o(De,$a),f(e,vo,u),T(Ye,e,u),f(e,$o,u),f(e,gt,u),o(gt,ba),f(e,bo,u),T(Ze,e,u),f(e,yo,u),f(e,be,u),o(be,Pe),o(Pe,Nt),T(et,Nt,null),o(be,ya),o(be,Ft),o(Ft,wa),f(e,wo,u),f(e,Ie,u),o(Ie,xa),o(Ie,tt),o(tt,ka),o(Ie,Ea),f(e,xo,u),T(ot,e,u),f(e,ko,u),f(e,ie,u),o(ie,Aa),o(ie,Ct),o(Ct,Ta),o(ie,ja),o(ie,Lt),o(Lt,Da),o(ie,Pa),o(ie,Ht),o(Ht,Ia),o(ie,qa),f(e,Eo,u),f(e,qe,u),o(qe,Sa),o(qe,at),o(at,Oa),o(qe,Ma),f(e,Ao,u),T(Se,e,u),f(e,To,u),T(Oe,e,u),f(e,jo,u),f(e,ye,u),o(ye,Me),o(Me,zt),T(rt,zt,null),o(ye,Na),o(ye,Ut),o(Ut,Fa),f(e,Do,u),f(e,vt,u),o(vt,Ca),f(e,Po,u),f(e,he,u),o(he,Ne),o(Ne,Rt),o(Rt,La),o(Ne,Ha),o(Ne,Gt),o(Gt,za),o(Ne,Ua),o(he,Ra),o(he,$t),o($t,Bt),o(Bt,Ga),o($t,Ba),o(he,Wa),o(he,we),o(we,Wt),o(Wt,Va),o(we,Ja),o(we,Vt),o(Vt,Ka),o(we,Qa),o(we,Jt),o(Jt,Xa),f(e,Io,u),T(it,e,u),f(e,qo,u),f(e,xe,u),o(xe,Fe),o(Fe,Kt),T(nt,Kt,null),o(xe,Ya),o(xe,Qt),o(Qt,Za),f(e,So,u),f(e,Ce,u),o(Ce,er),o(Ce,bt),o(bt,tr),o(Ce,or),f(e,Oo,u),f(e,yt,u),o(yt,ar),f(e,Mo,u),T(Le,e,u),f(e,No,u),f(e,ke,u),o(ke,He),o(He,Xt),T(st,Xt,null),o(ke,rr),o(ke,Yt),o(Yt,ir),f(e,Fo,u),f(e,wt,u),o(wt,xt),f(e,Co,u),f(e,lt,u),o(lt,nr),o(lt,kt),o(kt,sr),f(e,Lo,u),f(e,_e,u),o(_e,lr),o(_e,Zt),o(Zt,pr),o(_e,fr),o(_e,eo),o(eo,ur),o(_e,cr),f(e,Ho,u),f(e,ze,u),o(ze,mr),o(ze,to),o(to,dr),o(ze,hr),zo=!0},p(e,[u]){const pt={};u&2&&(pt.$$scope={dirty:u,ctx:e}),ee.$set(pt);const oo={};u&2&&(oo.$$scope={dirty:u,ctx:e}),Se.$set(oo);const ao={};u&2&&(ao.$$scope={dirty:u,ctx:e}),Oe.$set(ao);const ro={};u&2&&(ro.$$scope={dirty:u,ctx:e}),Le.$set(ro)},i(e){zo||(j(b.$$.fragment,e),j(O.$$.fragment,e),j(ee.$$.fragment,e),j(ce.$$.fragment,e),j(Be.$$.fragment,e),j(We.$$.fragment,e),j(Ve.$$.fragment,e),j(Ke.$$.fragment,e),j(Qe.$$.fragment,e),j(Xe.$$.fragment,e),j(Ye.$$.fragment,e),j(Ze.$$.fragment,e),j(et.$$.fragment,e),j(ot.$$.fragment,e),j(Se.$$.fragment,e),j(Oe.$$.fragment,e),j(rt.$$.fragment,e),j(it.$$.fragment,e),j(nt.$$.fragment,e),j(Le.$$.fragment,e),j(st.$$.fragment,e),zo=!0)},o(e){D(b.$$.fragment,e),D(O.$$.fragment,e),D(ee.$$.fragment,e),D(ce.$$.fragment,e),D(Be.$$.fragment,e),D(We.$$.fragment,e),D(Ve.$$.fragment,e),D(Ke.$$.fragment,e),D(Qe.$$.fragment,e),D(Xe.$$.fragment,e),D(Ye.$$.fragment,e),D(Ze.$$.fragment,e),D(et.$$.fragment,e),D(ot.$$.fragment,e),D(Se.$$.fragment,e),D(Oe.$$.fragment,e),D(rt.$$.fragment,e),D(it.$$.fragment,e),D(nt.$$.fragment,e),D(Le.$$.fragment,e),D(st.$$.fragment,e),zo=!1},d(e){t(i),e&&t($),e&&t(n),P(b),e&&t(k),P(O,e),e&&t(g),e&&t(x),e&&t(J),e&&t(Z),e&&t(B),e&&t(M),e&&t(z),e&&t(W),e&&t(q),P(ee,e),e&&t(Ge),e&&t(se),e&&t(ge),P(ce,e),e&&t(oe),e&&t(le),e&&t(no),P(Be,e),e&&t(so),e&&t(dt),e&&t(lo),P(We,e),e&&t(po),e&&t(ht),e&&t(fo),P(Ve,e),e&&t(uo),e&&t(de),e&&t(co),e&&t(ve),P(Ke),e&&t(mo),e&&t(_t),e&&t(ho),P(Qe,e),e&&t(_o),e&&t($e),P(Xe),e&&t(go),e&&t(De),e&&t(vo),P(Ye,e),e&&t($o),e&&t(gt),e&&t(bo),P(Ze,e),e&&t(yo),e&&t(be),P(et),e&&t(wo),e&&t(Ie),e&&t(xo),P(ot,e),e&&t(ko),e&&t(ie),e&&t(Eo),e&&t(qe),e&&t(Ao),P(Se,e),e&&t(To),P(Oe,e),e&&t(jo),e&&t(ye),P(rt),e&&t(Do),e&&t(vt),e&&t(Po),e&&t(he),e&&t(Io),P(it,e),e&&t(qo),e&&t(xe),P(nt),e&&t(So),e&&t(Ce),e&&t(Oo),e&&t(yt),e&&t(Mo),P(Le,e),e&&t(No),e&&t(ke),P(st),e&&t(Fo),e&&t(wt),e&&t(Co),e&&t(lt),e&&t(Lo),e&&t(_e),e&&t(Ho),e&&t(ze)}}}const zi={local:"textual-inversion",sections:[{local:"upload-model-to-hub",title:"Upload model to Hub"},{local:"save-and-load-checkpoints",title:"Save and load checkpoints"},{local:"finetuning",sections:[{local:"intermediate-logging",title:"Intermediate logging"}],title:"Finetuning"},{local:"inference",title:"Inference"},{local:"how-it-works",title:"How it works"}],title:"Textual Inversion"};function Ui(F){return Ai(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Ki extends wi{constructor(i){super();xi(this,i,Ui,Hi,ki,{})}}export{Ki as default,zi as metadata};
