import{s as Ie,f as Ze,n as ke,o as Ae}from"../chunks/scheduler.7bc62968.js";import{S as Be,i as De,g as n,s as a,r as j,A as Pe,h as l,f as i,c as s,j as je,u as R,x as r,k as m,y as Se,a as o,v as I,d as k,t as A,w as B}from"../chunks/index.2f8492b0.js";import{C as Re}from"../chunks/CodeBlock.4e96d6f7.js";import{H as ue,E as Fe}from"../chunks/EditOnGithub.2a9ce03a.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:",oe,h,Ce="",Le,ae,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 Re({props:{code:"cGlwJTIwaW5zdGFsbCUyMGRpZmZ1c2Vycw==",highlighted:"pip install diffusers",wrap:!1}}),C=new Re({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/johko/computer-vision-course/blob/main/chapters/en/unit5/generative-models/diffusion-models/simple-explanation.mdx"}}),{c(){u=n("meta"),S=a(),D=n("p"),F=a(),j(d.$$.fragment),N=a(),j(p.$$.fragment),z=a(),c=n("p"),c.textContent=pe,W=a(),w=n("p"),w.innerHTML=ce,q=a(),g=n("p"),g.innerHTML=we,X=a(),b=n("p"),b.textContent=ge,E=a(),y=n("p"),y.innerHTML=be,V=a(),v=n("p"),v.innerHTML=ye,Y=a(),T=n("p"),T.innerHTML=ve,Q=a(),M=n("p"),M.textContent=Te,K=a(),j(x.$$.fragment),O=a(),_=n("p"),_.innerHTML=Me,ee=a(),j(C.$$.fragment),te=a(),L=n("p"),L.innerHTML=xe,ie=a(),$=n("p"),$.textContent=_e,oe=a(),h=n("iframe"),h.innerHTML=Ce,ae=a(),j(J.$$.fragment),se=a(),G=n("p"),G.innerHTML=$e,ne=a(),H=n("p"),H.innerHTML=Je,le=a(),U=n("p"),U.innerHTML=Ge,re=a(),f=n("iframe"),f.innerHTML=He,he=a(),j(Z.$$.fragment),fe=a(),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",{}),je(D).forEach(i),F=s(e),R(d.$$.fragment,e),N=s(e),R(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),R(x.$$.fragment,e),O=s(e),_=l(e,"P",{"data-svelte-h":!0}),r(_)!=="svelte-1m8x1nt"&&(_.innerHTML=Me),ee=s(e),R(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),oe=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),ae=s(e),R(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),R(Z.$$.fragment,e),fe=s(e),P=l(e,"P",{}),je(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),o(e,S,t),o(e,D,t),o(e,F,t),I(d,e,t),o(e,N,t),I(p,e,t),o(e,z,t),o(e,c,t),o(e,W,t),o(e,w,t),o(e,q,t),o(e,g,t),o(e,X,t),o(e,b,t),o(e,E,t),o(e,y,t),o(e,V,t),o(e,v,t),o(e,Y,t),o(e,T,t),o(e,Q,t),o(e,M,t),o(e,K,t),I(x,e,t),o(e,O,t),o(e,_,t),o(e,ee,t),I(C,e,t),o(e,te,t),o(e,L,t),o(e,ie,t),o(e,$,t),o(e,oe,t),o(e,h,t),o(e,ae,t),I(J,e,t),o(e,se,t),o(e,G,t),o(e,ne,t),o(e,H,t),o(e,le,t),o(e,U,t),o(e,re,t),o(e,f,t),o(e,he,t),I(Z,e,t),o(e,fe,t),o(e,P,t),me=!0},p:ke,i(e){me||(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=!0)},o(e){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=!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(oe),i(h),i(ae),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 Ae(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Ye extends Be{constructor(u){super(),De(this,u,We,Ne,Ie,{})}}export{Ye as component};
