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