import{S as Gt,i as Bt,s as Vt,e as l,k as m,w as K,t as n,M as Dt,c as i,d as a,m as u,a as s,x as ee,h as o,b as p,G as t,g as d,y as te,L as It,q as ae,o as ne,B as oe,v as Xt}from"../../chunks/vendor-hf-doc-builder.js";import{I as Wt}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as rt}from"../../chunks/CodeBlock-hf-doc-builder.js";function xt(dt){let M,le,_,v,X,Z,Ee,x,Ue,ie,G,ke,se,g,Ze,B,Te,Ce,re,b,E,A,T,Je,Q,je,de,c,$e,V,Ne,We,C,Ge,Be,J,S,Ve,De,R,Ie,Xe,pe,j,fe,U,xe,$,z,Ae,Qe,ce,N,me,k,Se,F,Re,ze,ue,f,Fe,D,Pe,Ye,P,qe,Oe,Y,Le,He,q,Ke,et,O,tt,at,he,W,ye,y,nt,L,ot,lt,H,it,st,we;return Z=new Wt({}),T=new Wt({}),j=new rt({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMFN0YWJsZURpZmZ1c2lvblBpcGVsaW5lJTBBJTBBcGlwZWxpbmUlMjAlM0QlMjBTdGFibGVEaWZmdXNpb25QaXBlbGluZS5mcm9tX3ByZXRyYWluZWQoJTIycnVud2F5bWwlMkZzdGFibGUtZGlmZnVzaW9uLXYxLTUlMjIlMkMlMjB1c2Vfc2FmZXRlbnNvcnMlM0RUcnVlKSUwQXBpcGVsaW5lLnVuZXQuY29uZmlnJTVCJTIyaW5fY2hhbm5lbHMlMjIlNUQlMEE0",highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> StableDiffusionPipeline

pipeline = StableDiffusionPipeline.from_pretrained(<span class="hljs-string">&quot;runwayml/stable-diffusion-v1-5&quot;</span>, use_safetensors=<span class="hljs-literal">True</span>)
pipeline.unet.config[<span class="hljs-string">&quot;in_channels&quot;</span>]
<span class="hljs-number">4</span>`}}),N=new rt({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMFN0YWJsZURpZmZ1c2lvblBpcGVsaW5lJTBBJTBBcGlwZWxpbmUlMjAlM0QlMjBTdGFibGVEaWZmdXNpb25QaXBlbGluZS5mcm9tX3ByZXRyYWluZWQoJTIycnVud2F5bWwlMkZzdGFibGUtZGlmZnVzaW9uLWlucGFpbnRpbmclMjIlMkMlMjB1c2Vfc2FmZXRlbnNvcnMlM0RUcnVlKSUwQXBpcGVsaW5lLnVuZXQuY29uZmlnJTVCJTIyaW5fY2hhbm5lbHMlMjIlNUQlMEE5",highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> StableDiffusionPipeline

pipeline = StableDiffusionPipeline.from_pretrained(<span class="hljs-string">&quot;runwayml/stable-diffusion-inpainting&quot;</span>, use_safetensors=<span class="hljs-literal">True</span>)
pipeline.unet.config[<span class="hljs-string">&quot;in_channels&quot;</span>]
<span class="hljs-number">9</span>`}}),W=new rt({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMFVOZXQyRENvbmRpdGlvbk1vZGVsJTBBJTBBbW9kZWxfaWQlMjAlM0QlMjAlMjJydW53YXltbCUyRnN0YWJsZS1kaWZmdXNpb24tdjEtNSUyMiUwQXVuZXQlMjAlM0QlMjBVTmV0MkRDb25kaXRpb25Nb2RlbC5mcm9tX3ByZXRyYWluZWQoJTBBJTIwJTIwJTIwJTIwbW9kZWxfaWQlMkMlMEElMjAlMjAlMjAlMjBzdWJmb2xkZXIlM0QlMjJ1bmV0JTIyJTJDJTBBJTIwJTIwJTIwJTIwaW5fY2hhbm5lbHMlM0Q5JTJDJTBBJTIwJTIwJTIwJTIwbG93X2NwdV9tZW1fdXNhZ2UlM0RGYWxzZSUyQyUwQSUyMCUyMCUyMCUyMGlnbm9yZV9taXNtYXRjaGVkX3NpemVzJTNEVHJ1ZSUyQyUwQSUyMCUyMCUyMCUyMHVzZV9zYWZldGVuc29ycyUzRFRydWUlMkMlMEEp",highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> UNet2DConditionModel

model_id = <span class="hljs-string">&quot;runwayml/stable-diffusion-v1-5&quot;</span>
unet = UNet2DConditionModel.from_pretrained(
    model_id,
    subfolder=<span class="hljs-string">&quot;unet&quot;</span>,
    in_channels=<span class="hljs-number">9</span>,
    low_cpu_mem_usage=<span class="hljs-literal">False</span>,
    ignore_mismatched_sizes=<span class="hljs-literal">True</span>,
    use_safetensors=<span class="hljs-literal">True</span>,
)`}}),{c(){M=l("meta"),le=m(),_=l("h1"),v=l("a"),X=l("span"),K(Z.$$.fragment),Ee=m(),x=l("span"),Ue=n("Adapt a model to a new task"),ie=m(),G=l("p"),ke=n("Many diffusion systems share the same components, allowing you to adapt a pretrained model for one task to an entirely different task."),se=m(),g=l("p"),Ze=n("This guide will show you how to adapt a pretrained text-to-image model for inpainting by initializing and modifying the architecture of a pretrained "),B=l("a"),Te=n("UNet2DConditionModel"),Ce=n("."),re=m(),b=l("h2"),E=l("a"),A=l("span"),K(T.$$.fragment),Je=m(),Q=l("span"),je=n("Configure UNet2DConditionModel parameters"),de=m(),c=l("p"),$e=n("A "),V=l("a"),Ne=n("UNet2DConditionModel"),We=n(" by default accepts 4 channels in the "),C=l("a"),Ge=n("input sample"),Be=n(". For example, load a pretrained text-to-image model like "),J=l("a"),S=l("code"),Ve=n("runwayml/stable-diffusion-v1-5"),De=n(" and take a look at the number of "),R=l("code"),Ie=n("in_channels"),Xe=n(":"),pe=m(),K(j.$$.fragment),fe=m(),U=l("p"),xe=n("Inpainting requires 9 channels in the input sample. You can check this value in a pretrained inpainting model like "),$=l("a"),z=l("code"),Ae=n("runwayml/stable-diffusion-inpainting"),Qe=n(":"),ce=m(),K(N.$$.fragment),me=m(),k=l("p"),Se=n("To adapt your text-to-image model for inpainting, you\u2019ll need to change the number of "),F=l("code"),Re=n("in_channels"),ze=n(" from 4 to 9."),ue=m(),f=l("p"),Fe=n("Initialize a "),D=l("a"),Pe=n("UNet2DConditionModel"),Ye=n(" with the pretrained text-to-image model weights, and change "),P=l("code"),qe=n("in_channels"),Oe=n(" to 9. Changing the number of "),Y=l("code"),Le=n("in_channels"),He=n(" means you need to set "),q=l("code"),Ke=n("ignore_mismatched_sizes=True"),et=n(" and "),O=l("code"),tt=n("low_cpu_mem_usage=False"),at=n(" to avoid a size mismatch error because the shape is different now."),he=m(),K(W.$$.fragment),ye=m(),y=l("p"),nt=n("The pretrained weights of the other components from the text-to-image model are initialized from their checkpoints, but the input channel weights ("),L=l("code"),ot=n("conv_in.weight"),lt=n(") of the "),H=l("code"),it=n("unet"),st=n(" are randomly initialized. It is important to finetune the model for inpainting because otherwise the model returns noise."),this.h()},l(e){const r=Dt('[data-svelte="svelte-1phssyn"]',document.head);M=i(r,"META",{name:!0,content:!0}),r.forEach(a),le=u(e),_=i(e,"H1",{class:!0});var Me=s(_);v=i(Me,"A",{id:!0,class:!0,href:!0});var pt=s(v);X=i(pt,"SPAN",{});var ft=s(X);ee(Z.$$.fragment,ft),ft.forEach(a),pt.forEach(a),Ee=u(Me),x=i(Me,"SPAN",{});var ct=s(x);Ue=o(ct,"Adapt a model to a new task"),ct.forEach(a),Me.forEach(a),ie=u(e),G=i(e,"P",{});var mt=s(G);ke=o(mt,"Many diffusion systems share the same components, allowing you to adapt a pretrained model for one task to an entirely different task."),mt.forEach(a),se=u(e),g=i(e,"P",{});var _e=s(g);Ze=o(_e,"This guide will show you how to adapt a pretrained text-to-image model for inpainting by initializing and modifying the architecture of a pretrained "),B=i(_e,"A",{href:!0});var ut=s(B);Te=o(ut,"UNet2DConditionModel"),ut.forEach(a),Ce=o(_e,"."),_e.forEach(a),re=u(e),b=i(e,"H2",{class:!0});var be=s(b);E=i(be,"A",{id:!0,class:!0,href:!0});var ht=s(E);A=i(ht,"SPAN",{});var yt=s(A);ee(T.$$.fragment,yt),yt.forEach(a),ht.forEach(a),Je=u(be),Q=i(be,"SPAN",{});var wt=s(Q);je=o(wt,"Configure UNet2DConditionModel parameters"),wt.forEach(a),be.forEach(a),de=u(e),c=i(e,"P",{});var w=s(c);$e=o(w,"A "),V=i(w,"A",{href:!0});var Mt=s(V);Ne=o(Mt,"UNet2DConditionModel"),Mt.forEach(a),We=o(w," by default accepts 4 channels in the "),C=i(w,"A",{href:!0,rel:!0});var _t=s(C);Ge=o(_t,"input sample"),_t.forEach(a),Be=o(w,". For example, load a pretrained text-to-image model like "),J=i(w,"A",{href:!0,rel:!0});var bt=s(J);S=i(bt,"CODE",{});var vt=s(S);Ve=o(vt,"runwayml/stable-diffusion-v1-5"),vt.forEach(a),bt.forEach(a),De=o(w," and take a look at the number of "),R=i(w,"CODE",{});var gt=s(R);Ie=o(gt,"in_channels"),gt.forEach(a),Xe=o(w,":"),w.forEach(a),pe=u(e),ee(j.$$.fragment,e),fe=u(e),U=i(e,"P",{});var ve=s(U);xe=o(ve,"Inpainting requires 9 channels in the input sample. You can check this value in a pretrained inpainting model like "),$=i(ve,"A",{href:!0,rel:!0});var Et=s($);z=i(Et,"CODE",{});var Ut=s(z);Ae=o(Ut,"runwayml/stable-diffusion-inpainting"),Ut.forEach(a),Et.forEach(a),Qe=o(ve,":"),ve.forEach(a),ce=u(e),ee(N.$$.fragment,e),me=u(e),k=i(e,"P",{});var ge=s(k);Se=o(ge,"To adapt your text-to-image model for inpainting, you\u2019ll need to change the number of "),F=i(ge,"CODE",{});var kt=s(F);Re=o(kt,"in_channels"),kt.forEach(a),ze=o(ge," from 4 to 9."),ge.forEach(a),ue=u(e),f=i(e,"P",{});var h=s(f);Fe=o(h,"Initialize a "),D=i(h,"A",{href:!0});var Zt=s(D);Pe=o(Zt,"UNet2DConditionModel"),Zt.forEach(a),Ye=o(h," with the pretrained text-to-image model weights, and change "),P=i(h,"CODE",{});var Tt=s(P);qe=o(Tt,"in_channels"),Tt.forEach(a),Oe=o(h," to 9. Changing the number of "),Y=i(h,"CODE",{});var Ct=s(Y);Le=o(Ct,"in_channels"),Ct.forEach(a),He=o(h," means you need to set "),q=i(h,"CODE",{});var Jt=s(q);Ke=o(Jt,"ignore_mismatched_sizes=True"),Jt.forEach(a),et=o(h," and "),O=i(h,"CODE",{});var jt=s(O);tt=o(jt,"low_cpu_mem_usage=False"),jt.forEach(a),at=o(h," to avoid a size mismatch error because the shape is different now."),h.forEach(a),he=u(e),ee(W.$$.fragment,e),ye=u(e),y=i(e,"P",{});var I=s(y);nt=o(I,"The pretrained weights of the other components from the text-to-image model are initialized from their checkpoints, but the input channel weights ("),L=i(I,"CODE",{});var $t=s(L);ot=o($t,"conv_in.weight"),$t.forEach(a),lt=o(I,") of the "),H=i(I,"CODE",{});var Nt=s(H);it=o(Nt,"unet"),Nt.forEach(a),st=o(I," are randomly initialized. It is important to finetune the model for inpainting because otherwise the model returns noise."),I.forEach(a),this.h()},h(){p(M,"name","hf:doc:metadata"),p(M,"content",JSON.stringify(At)),p(v,"id","adapt-a-model-to-a-new-task"),p(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"),p(v,"href","#adapt-a-model-to-a-new-task"),p(_,"class","relative group"),p(B,"href","/docs/diffusers/main/en/api/models/unet2d-cond#diffusers.UNet2DConditionModel"),p(E,"id","configure-unet2dconditionmodel-parameters"),p(E,"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"),p(E,"href","#configure-unet2dconditionmodel-parameters"),p(b,"class","relative group"),p(V,"href","/docs/diffusers/main/en/api/models/unet2d-cond#diffusers.UNet2DConditionModel"),p(C,"href","https://huggingface.co/docs/diffusers/v0.16.0/en/api/models#diffusers.UNet2DConditionModel.in_channels"),p(C,"rel","nofollow"),p(J,"href","https://huggingface.co/runwayml/stable-diffusion-v1-5"),p(J,"rel","nofollow"),p($,"href","https://huggingface.co/runwayml/stable-diffusion-inpainting"),p($,"rel","nofollow"),p(D,"href","/docs/diffusers/main/en/api/models/unet2d-cond#diffusers.UNet2DConditionModel")},m(e,r){t(document.head,M),d(e,le,r),d(e,_,r),t(_,v),t(v,X),te(Z,X,null),t(_,Ee),t(_,x),t(x,Ue),d(e,ie,r),d(e,G,r),t(G,ke),d(e,se,r),d(e,g,r),t(g,Ze),t(g,B),t(B,Te),t(g,Ce),d(e,re,r),d(e,b,r),t(b,E),t(E,A),te(T,A,null),t(b,Je),t(b,Q),t(Q,je),d(e,de,r),d(e,c,r),t(c,$e),t(c,V),t(V,Ne),t(c,We),t(c,C),t(C,Ge),t(c,Be),t(c,J),t(J,S),t(S,Ve),t(c,De),t(c,R),t(R,Ie),t(c,Xe),d(e,pe,r),te(j,e,r),d(e,fe,r),d(e,U,r),t(U,xe),t(U,$),t($,z),t(z,Ae),t(U,Qe),d(e,ce,r),te(N,e,r),d(e,me,r),d(e,k,r),t(k,Se),t(k,F),t(F,Re),t(k,ze),d(e,ue,r),d(e,f,r),t(f,Fe),t(f,D),t(D,Pe),t(f,Ye),t(f,P),t(P,qe),t(f,Oe),t(f,Y),t(Y,Le),t(f,He),t(f,q),t(q,Ke),t(f,et),t(f,O),t(O,tt),t(f,at),d(e,he,r),te(W,e,r),d(e,ye,r),d(e,y,r),t(y,nt),t(y,L),t(L,ot),t(y,lt),t(y,H),t(H,it),t(y,st),we=!0},p:It,i(e){we||(ae(Z.$$.fragment,e),ae(T.$$.fragment,e),ae(j.$$.fragment,e),ae(N.$$.fragment,e),ae(W.$$.fragment,e),we=!0)},o(e){ne(Z.$$.fragment,e),ne(T.$$.fragment,e),ne(j.$$.fragment,e),ne(N.$$.fragment,e),ne(W.$$.fragment,e),we=!1},d(e){a(M),e&&a(le),e&&a(_),oe(Z),e&&a(ie),e&&a(G),e&&a(se),e&&a(g),e&&a(re),e&&a(b),oe(T),e&&a(de),e&&a(c),e&&a(pe),oe(j,e),e&&a(fe),e&&a(U),e&&a(ce),oe(N,e),e&&a(me),e&&a(k),e&&a(ue),e&&a(f),e&&a(he),oe(W,e),e&&a(ye),e&&a(y)}}}const At={local:"adapt-a-model-to-a-new-task",sections:[{local:"configure-unet2dconditionmodel-parameters",title:"Configure UNet2DConditionModel parameters"}],title:"Adapt a model to a new task"};function Qt(dt){return Xt(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Ft extends Gt{constructor(M){super();Bt(this,M,Qt,xt,Vt,{})}}export{Ft as default,At as metadata};
