import{s as Ie,f as Ze,n as Ae,o as ke}from"../chunks/scheduler.7bc62968.js";import{S as Be,i as De,g as n,s as o,r as R,A as Pe,h as l,f as i,c as s,j as Re,u as j,x as r,k as m,y as Se,a,v as I,d as A,t as k,w as B}from"../chunks/index.2f8492b0.js";import{C as je}from"../chunks/CodeBlock.bb61a5a9.js";import{H as ue,E as Fe}from"../chunks/getInferenceSnippets.cd80cb94.js";function Ne(de){let u,S,D,F,d,N,p,z,c,pe="Although diffusion models and GANs can generate many unique images, they can’t always generate what you need exactly. Hence, you have to fine-tune a model, which usually requires a lot of data and computation. However, some techniques can be used to personalize a model with just a few examples.",W,w,ce='One example is Dreambooth by Google Research, a training technique that updates the entire diffusion model by training on just a few images of a subject or style. It works by associating a special word in the prompt with the example images. The details on Dreambooth can be found in the <a href="https://dreambooth.github.io/" rel="nofollow">paper</a> and the <a href="https://huggingface.co/docs/diffusers/training/dreambooth" rel="nofollow">Hugging Face Dreambooth training documentation</a>.',q,g,we=`Below, you can see the results of Dreambooth being used to train on 4 images of a dog and some inference examples.
<img src="https://huggingface.co/datasets/hf-vision/course-assets/resolve/main/teaser_static.jpg" alt="Dreambooth Dog Example"/>
You can recreate the results following the Hugging Face documentation given above.`,X,b,ge="From this example, it’s clear that the model has learned about that specific dog and can generate new images of that dog in diverse poses and backgrounds. Although computing, data, and time are improvements, others have found more efficient ways to customize a model.",E,y,be='This is where the current most popular method for this comes in, which is Low Rank Adaptation (LoRA). This method was initially developed to efficiently fine-tune Large Language Models by Microsoft in this <a href="https://arxiv.org/abs/2106.09685" rel="nofollow">paper</a>. The main idea is to factorise the weight update matrix into 2 low-rank matrices, which are optimized during training whilst the rest of the model is frozen. <a href="https://huggingface.co/docs/peft/conceptual_guides/lora" rel="nofollow">Hugging face documentation</a> has a good conceptual guide on how LoRA works.',V,v,ye='Now, if we put those ideas together we can use LoRA to efficiently fine-tune a diffusion model on a few examples using Dreambooth. A tutorial Google Colab notebook on how to do this can be found <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers/SDXL_DreamBooth_LoRA_.ipynb" rel="nofollow">here</a>.',Y,T,ve=`Due to the quality and efficiency of this method, many people have created their own LoRA parameters which many can be found on a website called <a href="https://civitai.com/models" rel="nofollow">Civitai</a> and <a href="https://huggingface.co/collections/multimodalart/awesome-sdxl-loras-64f9af6d5cce4f4e8f351466" rel="nofollow">Hugging Face</a>.
For Civitai you can download the LoRA weights which usually are in the range of 50-500MB and in the case of Hugging Face version you can just load the model directly from the model hub.
Below is an example of how to load the LoRA weights in both cases and then fuse them with the model.`,Q,M,Te="We can start with installing diffusers library.",K,x,O,_,Me="We will initialize the <code>StableDiffusionXLPipeline</code> and load LoRA adapter weights.",ee,C,te,L,xe="This makes it quick to load a customised diffusion model and use it for inference, especially since there are a lot of models to choose from. Then, if we want to remove the LoRA weights, we can call <code>pipe.unfuse_lora()</code> which will return the model to its original state. As for the <code>lora_scale</code> parameter, this is a hyperparameter that controls how much the LoRA weights are used during inference. A value of 1.0 means the LoRA weights are fully used and a value of 0.0 means the LoRA weights are not used at all. The best value is often between 0.7 and 1.0 but it’s worth experimenting with different values to see what works best for your use case.",ie,$,_e="You can try some of the Hugging Face LoRA models in this Gradio demo:",ae,h,Ce="",Le,oe,J,se,G,$e='Diffusion models have many ways in which they can be guided to create a desired output, such as prompts, negative prompts, guidance scale, inpainting and many others. Here, we will focus on a method that has many variants and can be combined with all the other methods, called ControlNet. It was introduced in this <a href="https://arxiv.org/abs/2302.05543" rel="nofollow">paper</a> by Stanford University. This method allows us to guide the diffusion model with an image that usually holds very specific information such as depth, pose, edges, and many others. This allows for more consistency in the generated images, which is often a problem with diffusion models.',ne,H,Je=`ControlNet can be used in both text-to-image and image-to-image. Below is a text 2 image example using a ControlNet which was trained on edge detection conditioning, with the top left image being used as input.
Here we can see how all of the generated images have a very similar shape but with different colours. This is because the ControlNet is guiding the diffusion model to create images with the same shape as the input image.
<img src="https://github.com/lllyasviel/ControlNet/raw/main/github_page/p1.png" alt="bird"/>`,le,U,Ge='For code to run ControlNet with Stable Diffusion XL refer to the official documentation <a href="https://huggingface.co/docs/diffusers/api/pipelines/controlnet_sdxl#diffusers.StableDiffusionXLControlNetPipeline" rel="nofollow">here</a> but if you just want to test out some examples take a look at this Gradio demo that lets you try different types of ControlNet:',re,f,He="",Ue,he,Z,fe,P,me;return d=new ue({props:{title:"Control over Diffusion Models",local:"control-over-diffusion-models",headingTag:"h1"}}),p=new ue({props:{title:"Dreambooth",local:"dreambooth",headingTag:"h2"}}),x=new je({props:{code:"cGlwJTIwaW5zdGFsbCUyMGRpZmZ1c2Vycw==",highlighted:"pip install diffusers",wrap:!1}}),C=new je({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMFN0YWJsZURpZmZ1c2lvblhMUGlwZWxpbmUlMEFpbXBvcnQlMjB0b3JjaCUwQSUwQW1vZGVsJTIwJTNEJTIwJTIyc3RhYmlsaXR5YWklMkZzdGFibGUtZGlmZnVzaW9uLXhsLWJhc2UtMS4wJTIyJTBBcGlwZSUyMCUzRCUyMFN0YWJsZURpZmZ1c2lvblhMUGlwZWxpbmUuZnJvbV9wcmV0cmFpbmVkKG1vZGVsJTJDJTIwdG9yY2hfZHR5cGUlM0R0b3JjaC5mbG9hdDE2KSUwQXBpcGUubG9hZF9sb3JhX3dlaWdodHMoJTBBJTIwJTIwJTIwJTIwJTIybG9yYV93ZWlnaHRzLnNhZmV0ZW5zb3JzJTIyJTBBKSUyMCUyMCUyMyUyMGlmJTIweW91JTIwd2FudCUyMHRvJTIwaW5zdGFsbCUyMGZyb20lMjBhJTIwd2VpZ2h0JTIwZmlsZSUwQXBpcGUubG9hZF9sb3JhX3dlaWdodHMoJTBBJTIwJTIwJTIwJTIwJTIyb3N0cmlzJTJGY3JheW9uX3N0eWxlX2xvcmFfc2R4bCUyMiUwQSklMjAlMjAlMjMlMjBpZiUyMHlvdSUyMHdpc2glMjB0byUyMGluc3RhbGwlMjBhJTIwbG9yYSUyMGZyb20lMjBhJTIwcmVwb3NpdG9yeSUyMGRpcmVjdGx5JTBBcGlwZS5mdXNlX2xvcmEobG9yYV9zY2FsZSUzRDAuOCk=",highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> StableDiffusionXLPipeline
<span class="hljs-keyword">import</span> torch

model = <span class="hljs-string">&quot;stabilityai/stable-diffusion-xl-base-1.0&quot;</span>
pipe = StableDiffusionXLPipeline.from_pretrained(model, torch_dtype=torch.float16)
pipe.load_lora_weights(
    <span class="hljs-string">&quot;lora_weights.safetensors&quot;</span>
)  <span class="hljs-comment"># if you want to install from a weight file</span>
pipe.load_lora_weights(
    <span class="hljs-string">&quot;ostris/crayon_style_lora_sdxl&quot;</span>
)  <span class="hljs-comment"># if you wish to install a lora from a repository directly</span>
pipe.fuse_lora(lora_scale=<span class="hljs-number">0.8</span>)`,wrap:!1}}),J=new ue({props:{title:"Guided Diffusion via ControlNet",local:"guided-diffusion-via-controlnet",headingTag:"h2"}}),Z=new Fe({props:{source:"https://github.com/huggingface/computer-vision-course/blob/main/chapters/en/unit5/generative-models/diffusion-models/simple-explanation.mdx"}}),{c(){u=n("meta"),S=o(),D=n("p"),F=o(),R(d.$$.fragment),N=o(),R(p.$$.fragment),z=o(),c=n("p"),c.textContent=pe,W=o(),w=n("p"),w.innerHTML=ce,q=o(),g=n("p"),g.innerHTML=we,X=o(),b=n("p"),b.textContent=ge,E=o(),y=n("p"),y.innerHTML=be,V=o(),v=n("p"),v.innerHTML=ye,Y=o(),T=n("p"),T.innerHTML=ve,Q=o(),M=n("p"),M.textContent=Te,K=o(),R(x.$$.fragment),O=o(),_=n("p"),_.innerHTML=Me,ee=o(),R(C.$$.fragment),te=o(),L=n("p"),L.innerHTML=xe,ie=o(),$=n("p"),$.textContent=_e,ae=o(),h=n("iframe"),h.innerHTML=Ce,oe=o(),R(J.$$.fragment),se=o(),G=n("p"),G.innerHTML=$e,ne=o(),H=n("p"),H.innerHTML=Je,le=o(),U=n("p"),U.innerHTML=Ge,re=o(),f=n("iframe"),f.innerHTML=He,he=o(),R(Z.$$.fragment),fe=o(),P=n("p"),this.h()},l(e){const t=Pe("svelte-u9bgzb",document.head);u=l(t,"META",{name:!0,content:!0}),t.forEach(i),S=s(e),D=l(e,"P",{}),Re(D).forEach(i),F=s(e),j(d.$$.fragment,e),N=s(e),j(p.$$.fragment,e),z=s(e),c=l(e,"P",{"data-svelte-h":!0}),r(c)!=="svelte-dm5ekh"&&(c.textContent=pe),W=s(e),w=l(e,"P",{"data-svelte-h":!0}),r(w)!=="svelte-7epn70"&&(w.innerHTML=ce),q=s(e),g=l(e,"P",{"data-svelte-h":!0}),r(g)!=="svelte-3y9hjg"&&(g.innerHTML=we),X=s(e),b=l(e,"P",{"data-svelte-h":!0}),r(b)!=="svelte-1mlf4v3"&&(b.textContent=ge),E=s(e),y=l(e,"P",{"data-svelte-h":!0}),r(y)!=="svelte-1vfqo5c"&&(y.innerHTML=be),V=s(e),v=l(e,"P",{"data-svelte-h":!0}),r(v)!=="svelte-18olxdv"&&(v.innerHTML=ye),Y=s(e),T=l(e,"P",{"data-svelte-h":!0}),r(T)!=="svelte-s85m5l"&&(T.innerHTML=ve),Q=s(e),M=l(e,"P",{"data-svelte-h":!0}),r(M)!=="svelte-12lbort"&&(M.textContent=Te),K=s(e),j(x.$$.fragment,e),O=s(e),_=l(e,"P",{"data-svelte-h":!0}),r(_)!=="svelte-1m8x1nt"&&(_.innerHTML=Me),ee=s(e),j(C.$$.fragment,e),te=s(e),L=l(e,"P",{"data-svelte-h":!0}),r(L)!=="svelte-1yk0z3z"&&(L.innerHTML=xe),ie=s(e),$=l(e,"P",{"data-svelte-h":!0}),r($)!=="svelte-ajhdq4"&&($.textContent=_e),ae=s(e),h=l(e,"IFRAME",{src:!0,frameborder:!0,width:!0,height:!0,"data-svelte-h":!0}),r(h)!=="svelte-11rdemp"&&(h.innerHTML=Ce),oe=s(e),j(J.$$.fragment,e),se=s(e),G=l(e,"P",{"data-svelte-h":!0}),r(G)!=="svelte-1iuomxt"&&(G.innerHTML=$e),ne=s(e),H=l(e,"P",{"data-svelte-h":!0}),r(H)!=="svelte-yqr741"&&(H.innerHTML=Je),le=s(e),U=l(e,"P",{"data-svelte-h":!0}),r(U)!=="svelte-1j6zjnh"&&(U.innerHTML=Ge),re=s(e),f=l(e,"IFRAME",{src:!0,frameborder:!0,width:!0,height:!0,"data-svelte-h":!0}),r(f)!=="svelte-ppftsn"&&(f.innerHTML=He),he=s(e),j(Z.$$.fragment,e),fe=s(e),P=l(e,"P",{}),Re(P).forEach(i),this.h()},h(){m(u,"name","hf:doc:metadata"),m(u,"content",ze),Ze(h.src,Le="https://multimodalart-LoraTheExplorer.hf.space")||m(h,"src",Le),m(h,"frameborder","0"),m(h,"width","850"),m(h,"height","450"),Ze(f.src,Ue="https://hysts-ControlNet-v1-1.hf.space")||m(f,"src",Ue),m(f,"frameborder","0"),m(f,"width","850"),m(f,"height","450")},m(e,t){Se(document.head,u),a(e,S,t),a(e,D,t),a(e,F,t),I(d,e,t),a(e,N,t),I(p,e,t),a(e,z,t),a(e,c,t),a(e,W,t),a(e,w,t),a(e,q,t),a(e,g,t),a(e,X,t),a(e,b,t),a(e,E,t),a(e,y,t),a(e,V,t),a(e,v,t),a(e,Y,t),a(e,T,t),a(e,Q,t),a(e,M,t),a(e,K,t),I(x,e,t),a(e,O,t),a(e,_,t),a(e,ee,t),I(C,e,t),a(e,te,t),a(e,L,t),a(e,ie,t),a(e,$,t),a(e,ae,t),a(e,h,t),a(e,oe,t),I(J,e,t),a(e,se,t),a(e,G,t),a(e,ne,t),a(e,H,t),a(e,le,t),a(e,U,t),a(e,re,t),a(e,f,t),a(e,he,t),I(Z,e,t),a(e,fe,t),a(e,P,t),me=!0},p:Ae,i(e){me||(A(d.$$.fragment,e),A(p.$$.fragment,e),A(x.$$.fragment,e),A(C.$$.fragment,e),A(J.$$.fragment,e),A(Z.$$.fragment,e),me=!0)},o(e){k(d.$$.fragment,e),k(p.$$.fragment,e),k(x.$$.fragment,e),k(C.$$.fragment,e),k(J.$$.fragment,e),k(Z.$$.fragment,e),me=!1},d(e){e&&(i(S),i(D),i(F),i(N),i(z),i(c),i(W),i(w),i(q),i(g),i(X),i(b),i(E),i(y),i(V),i(v),i(Y),i(T),i(Q),i(M),i(K),i(O),i(_),i(ee),i(te),i(L),i(ie),i($),i(ae),i(h),i(oe),i(se),i(G),i(ne),i(H),i(le),i(U),i(re),i(f),i(he),i(fe),i(P)),i(u),B(d,e),B(p,e),B(x,e),B(C,e),B(J,e),B(Z,e)}}}const ze='{"title":"Control over Diffusion Models","local":"control-over-diffusion-models","sections":[{"title":"Dreambooth","local":"dreambooth","sections":[],"depth":2},{"title":"Guided Diffusion via ControlNet","local":"guided-diffusion-via-controlnet","sections":[],"depth":2}],"depth":1}';function We(de){return ke(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Ye extends Be{constructor(u){super(),De(this,u,We,Ne,Ie,{})}}export{Ye as component};
