import{s as Ae,a as je,n as Be,o as De}from"../chunks/scheduler.35aab934.js";import{S as Pe,i as Se,e as s,s as o,c as j,h as Fe,a as l,d as i,b as n,f as Ie,g as I,j as r,k as m,l as ze,m as a,n as k,t as A,o as B,p as D}from"../chunks/index.de17b971.js";import{C as Ne}from"../chunks/CopyLLMTxtMenu.48bc5cc2.js";import{C as ke}from"../chunks/CodeBlock.c1f62de5.js";import{H as pe,E as We}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.8fdece89.js";function qe(ce){let u,F,P,z,d,N,p,W,c,q,w,we="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.",X,g,ge='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>.',E,b,be=`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.`,V,y,ye="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.",Y,v,ve='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.',Q,T,Te='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>.',K,M,Me=`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.`,O,x,xe="We can start with installing diffusers library.",ee,_,te,$,_e="We will initialize the <code>StableDiffusionXLPipeline</code> and load LoRA adapter weights.",ie,C,ae,L,$e="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.",oe,J,Ce="You can try some of the Hugging Face LoRA models in this Gradio demo:",ne,h,Le="",Je,se,G,le,H,Ge='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.',re,U,He=`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"/>`,he,Z,Ue='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:',fe,f,Ze="",Re,me,R,ue,S,de;return d=new Ne({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),p=new pe({props:{title:"Control over Diffusion Models",local:"control-over-diffusion-models",headingTag:"h1"}}),c=new pe({props:{title:"Dreambooth",local:"dreambooth",headingTag:"h2"}}),_=new ke({props:{code:"cGlwJTIwaW5zdGFsbCUyMGRpZmZ1c2Vycw==",highlighted:"pip install diffusers",wrap:!1}}),C=new ke({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}}),G=new pe({props:{title:"Guided Diffusion via ControlNet",local:"guided-diffusion-via-controlnet",headingTag:"h2"}}),R=new We({props:{source:"https://github.com/huggingface/computer-vision-course/blob/main/chapters/en/unit5/generative-models/diffusion-models/simple-explanation.mdx"}}),{c(){u=s("meta"),F=o(),P=s("p"),z=o(),j(d.$$.fragment),N=o(),j(p.$$.fragment),W=o(),j(c.$$.fragment),q=o(),w=s("p"),w.textContent=we,X=o(),g=s("p"),g.innerHTML=ge,E=o(),b=s("p"),b.innerHTML=be,V=o(),y=s("p"),y.textContent=ye,Y=o(),v=s("p"),v.innerHTML=ve,Q=o(),T=s("p"),T.innerHTML=Te,K=o(),M=s("p"),M.innerHTML=Me,O=o(),x=s("p"),x.textContent=xe,ee=o(),j(_.$$.fragment),te=o(),$=s("p"),$.innerHTML=_e,ie=o(),j(C.$$.fragment),ae=o(),L=s("p"),L.innerHTML=$e,oe=o(),J=s("p"),J.textContent=Ce,ne=o(),h=s("iframe"),h.innerHTML=Le,se=o(),j(G.$$.fragment),le=o(),H=s("p"),H.innerHTML=Ge,re=o(),U=s("p"),U.innerHTML=He,he=o(),Z=s("p"),Z.innerHTML=Ue,fe=o(),f=s("iframe"),f.innerHTML=Ze,me=o(),j(R.$$.fragment),ue=o(),S=s("p"),this.h()},l(e){const t=Fe("svelte-u9bgzb",document.head);u=l(t,"META",{name:!0,content:!0}),t.forEach(i),F=n(e),P=l(e,"P",{}),Ie(P).forEach(i),z=n(e),I(d.$$.fragment,e),N=n(e),I(p.$$.fragment,e),W=n(e),I(c.$$.fragment,e),q=n(e),w=l(e,"P",{"data-svelte-h":!0}),r(w)!=="svelte-dm5ekh"&&(w.textContent=we),X=n(e),g=l(e,"P",{"data-svelte-h":!0}),r(g)!=="svelte-7epn70"&&(g.innerHTML=ge),E=n(e),b=l(e,"P",{"data-svelte-h":!0}),r(b)!=="svelte-3y9hjg"&&(b.innerHTML=be),V=n(e),y=l(e,"P",{"data-svelte-h":!0}),r(y)!=="svelte-1mlf4v3"&&(y.textContent=ye),Y=n(e),v=l(e,"P",{"data-svelte-h":!0}),r(v)!=="svelte-1vfqo5c"&&(v.innerHTML=ve),Q=n(e),T=l(e,"P",{"data-svelte-h":!0}),r(T)!=="svelte-18olxdv"&&(T.innerHTML=Te),K=n(e),M=l(e,"P",{"data-svelte-h":!0}),r(M)!=="svelte-s85m5l"&&(M.innerHTML=Me),O=n(e),x=l(e,"P",{"data-svelte-h":!0}),r(x)!=="svelte-12lbort"&&(x.textContent=xe),ee=n(e),I(_.$$.fragment,e),te=n(e),$=l(e,"P",{"data-svelte-h":!0}),r($)!=="svelte-1m8x1nt"&&($.innerHTML=_e),ie=n(e),I(C.$$.fragment,e),ae=n(e),L=l(e,"P",{"data-svelte-h":!0}),r(L)!=="svelte-1yk0z3z"&&(L.innerHTML=$e),oe=n(e),J=l(e,"P",{"data-svelte-h":!0}),r(J)!=="svelte-ajhdq4"&&(J.textContent=Ce),ne=n(e),h=l(e,"IFRAME",{src:!0,frameborder:!0,width:!0,height:!0,"data-svelte-h":!0}),r(h)!=="svelte-11rdemp"&&(h.innerHTML=Le),se=n(e),I(G.$$.fragment,e),le=n(e),H=l(e,"P",{"data-svelte-h":!0}),r(H)!=="svelte-1iuomxt"&&(H.innerHTML=Ge),re=n(e),U=l(e,"P",{"data-svelte-h":!0}),r(U)!=="svelte-yqr741"&&(U.innerHTML=He),he=n(e),Z=l(e,"P",{"data-svelte-h":!0}),r(Z)!=="svelte-1j6zjnh"&&(Z.innerHTML=Ue),fe=n(e),f=l(e,"IFRAME",{src:!0,frameborder:!0,width:!0,height:!0,"data-svelte-h":!0}),r(f)!=="svelte-ppftsn"&&(f.innerHTML=Ze),me=n(e),I(R.$$.fragment,e),ue=n(e),S=l(e,"P",{}),Ie(S).forEach(i),this.h()},h(){m(u,"name","hf:doc:metadata"),m(u,"content",Xe),je(h.src,Je="https://multimodalart-LoraTheExplorer.hf.space")||m(h,"src",Je),m(h,"frameborder","0"),m(h,"width","850"),m(h,"height","450"),je(f.src,Re="https://hysts-ControlNet-v1-1.hf.space")||m(f,"src",Re),m(f,"frameborder","0"),m(f,"width","850"),m(f,"height","450")},m(e,t){ze(document.head,u),a(e,F,t),a(e,P,t),a(e,z,t),k(d,e,t),a(e,N,t),k(p,e,t),a(e,W,t),k(c,e,t),a(e,q,t),a(e,w,t),a(e,X,t),a(e,g,t),a(e,E,t),a(e,b,t),a(e,V,t),a(e,y,t),a(e,Y,t),a(e,v,t),a(e,Q,t),a(e,T,t),a(e,K,t),a(e,M,t),a(e,O,t),a(e,x,t),a(e,ee,t),k(_,e,t),a(e,te,t),a(e,$,t),a(e,ie,t),k(C,e,t),a(e,ae,t),a(e,L,t),a(e,oe,t),a(e,J,t),a(e,ne,t),a(e,h,t),a(e,se,t),k(G,e,t),a(e,le,t),a(e,H,t),a(e,re,t),a(e,U,t),a(e,he,t),a(e,Z,t),a(e,fe,t),a(e,f,t),a(e,me,t),k(R,e,t),a(e,ue,t),a(e,S,t),de=!0},p:Be,i(e){de||(A(d.$$.fragment,e),A(p.$$.fragment,e),A(c.$$.fragment,e),A(_.$$.fragment,e),A(C.$$.fragment,e),A(G.$$.fragment,e),A(R.$$.fragment,e),de=!0)},o(e){B(d.$$.fragment,e),B(p.$$.fragment,e),B(c.$$.fragment,e),B(_.$$.fragment,e),B(C.$$.fragment,e),B(G.$$.fragment,e),B(R.$$.fragment,e),de=!1},d(e){e&&(i(F),i(P),i(z),i(N),i(W),i(q),i(w),i(X),i(g),i(E),i(b),i(V),i(y),i(Y),i(v),i(Q),i(T),i(K),i(M),i(O),i(x),i(ee),i(te),i($),i(ie),i(ae),i(L),i(oe),i(J),i(ne),i(h),i(se),i(le),i(H),i(re),i(U),i(he),i(Z),i(fe),i(f),i(me),i(ue),i(S)),i(u),D(d,e),D(p,e),D(c,e),D(_,e),D(C,e),D(G,e),D(R,e)}}}const Xe='{"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 Ee(ce){return De(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class et extends Pe{constructor(u){super(),Se(this,u,Ee,qe,Ae,{})}}export{et as component};
