import{s as pe,n as oe,o as me}from"../chunks/scheduler.e4ff9b64.js";import{S as fe,i as ce,e as a,s,c as w,h as ue,a as p,d as l,b as i,f as se,g as _,j as U,k as ie,l as de,m as n,n as g,t as v,o as W,p as G}from"../chunks/index.09f1bca0.js";import{C as re,H as ae,E as Me}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.11400f6f.js";import{C as F}from"../chunks/CodeBlock.78f3be90.js";function be(D){let o,B,C,x,m,V,f,N,c,A="许多扩散系统共享相同的组件架构，这使得您能够将针对某一任务预训练的模型调整适配至完全不同的新任务。",R,u,K="本指南将展示如何通过初始化并修改预训练 <code>UNet2DConditionModel</code> 的架构，将文生图预训练模型改造为图像修复(inpainting)模型。",I,d,S,r,O='默认情况下，<code>UNet2DConditionModel</code> 的<a href="https://huggingface.co/docs/diffusers/v0.16.0/en/api/models#diffusers.UNet2DConditionModel.in_channels" rel="nofollow">输入样本</a>接受4个通道。例如加载 <a href="https://huggingface.co/stable-diffusion-v1-5/stable-diffusion-v1-5" rel="nofollow"><code>stable-diffusion-v1-5/stable-diffusion-v1-5</code></a> 这样的文生图预训练模型，查看其 <code>in_channels</code> 参数值：',X,M,L,b,ee='而图像修复任务需要输入样本具有9个通道。您可以在 <a href="https://huggingface.co/runwayml/stable-diffusion-inpainting" rel="nofollow"><code>runwayml/stable-diffusion-inpainting</code></a> 这样的预训练修复模型中验证此参数：',k,y,E,h,te="要将文生图模型改造为修复模型，您需要将 <code>in_channels</code> 参数从4调整为9。",Q,T,le="初始化一个加载了文生图预训练权重的 <code>UNet2DConditionModel</code>，并将 <code>in_channels</code> 设为9。由于输入通道数变化导致张量形状改变，需要设置 <code>ignore_mismatched_sizes=True</code> 和 <code>low_cpu_mem_usage=False</code> 来避免尺寸不匹配错误。",H,Z,z,$,ne="此时文生图模型的其他组件权重仍保持预训练状态，但UNet的输入卷积层权重(<code>conv_in.weight</code>)会随机初始化。由于这一关键变化，必须对模型进行修复任务的微调，否则模型将仅会输出噪声。",P,J,Y,j,q;return m=new re({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),f=new ae({props:{title:"将模型适配至新任务",local:"将模型适配至新任务",headingTag:"h1"}}),d=new ae({props:{title:"配置 UNet2DConditionModel 参数",local:"配置-unet2dconditionmodel-参数",headingTag:"h2"}}),M=new F({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMFN0YWJsZURpZmZ1c2lvblBpcGVsaW5lJTBBJTBBcGlwZWxpbmUlMjAlM0QlMjBTdGFibGVEaWZmdXNpb25QaXBlbGluZS5mcm9tX3ByZXRyYWluZWQoJTIyc3RhYmxlLWRpZmZ1c2lvbi12MS01JTJGc3RhYmxlLWRpZmZ1c2lvbi12MS01JTIyJTJDJTIwdXNlX3NhZmV0ZW5zb3JzJTNEVHJ1ZSklMEFwaXBlbGluZS51bmV0LmNvbmZpZyU1QiUyMmluX2NoYW5uZWxzJTIyJTVEJTBBNA==",highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> StableDiffusionPipeline

pipeline = StableDiffusionPipeline.from_pretrained(<span class="hljs-string">&quot;stable-diffusion-v1-5/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>`,wrap:!1}}),y=new F({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>`,wrap:!1}}),Z=new F({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMEF1dG9Nb2RlbCUwQSUwQW1vZGVsX2lkJTIwJTNEJTIwJTIyc3RhYmxlLWRpZmZ1c2lvbi12MS01JTJGc3RhYmxlLWRpZmZ1c2lvbi12MS01JTIyJTBBdW5ldCUyMCUzRCUyMEF1dG9Nb2RlbC5mcm9tX3ByZXRyYWluZWQoJTBBJTIwJTIwJTIwJTIwbW9kZWxfaWQlMkMlMEElMjAlMjAlMjAlMjBzdWJmb2xkZXIlM0QlMjJ1bmV0JTIyJTJDJTBBJTIwJTIwJTIwJTIwaW5fY2hhbm5lbHMlM0Q5JTJDJTBBJTIwJTIwJTIwJTIwbG93X2NwdV9tZW1fdXNhZ2UlM0RGYWxzZSUyQyUwQSUyMCUyMCUyMCUyMGlnbm9yZV9taXNtYXRjaGVkX3NpemVzJTNEVHJ1ZSUyQyUwQSUyMCUyMCUyMCUyMHVzZV9zYWZldGVuc29ycyUzRFRydWUlMkMlMEEp",highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> AutoModel

model_id = <span class="hljs-string">&quot;stable-diffusion-v1-5/stable-diffusion-v1-5&quot;</span>
unet = AutoModel.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>,
)`,wrap:!1}}),J=new Me({props:{source:"https://github.com/huggingface/diffusers/blob/main/docs/source/zh/training/adapt_a_model.md"}}),{c(){o=a("meta"),B=s(),C=a("p"),x=s(),w(m.$$.fragment),V=s(),w(f.$$.fragment),N=s(),c=a("p"),c.textContent=A,R=s(),u=a("p"),u.innerHTML=K,I=s(),w(d.$$.fragment),S=s(),r=a("p"),r.innerHTML=O,X=s(),w(M.$$.fragment),L=s(),b=a("p"),b.innerHTML=ee,k=s(),w(y.$$.fragment),E=s(),h=a("p"),h.innerHTML=te,Q=s(),T=a("p"),T.innerHTML=le,H=s(),w(Z.$$.fragment),z=s(),$=a("p"),$.innerHTML=ne,P=s(),w(J.$$.fragment),Y=s(),j=a("p"),this.h()},l(e){const t=ue("svelte-u9bgzb",document.head);o=p(t,"META",{name:!0,content:!0}),t.forEach(l),B=i(e),C=p(e,"P",{}),se(C).forEach(l),x=i(e),_(m.$$.fragment,e),V=i(e),_(f.$$.fragment,e),N=i(e),c=p(e,"P",{"data-svelte-h":!0}),U(c)!=="svelte-10h8vrc"&&(c.textContent=A),R=i(e),u=p(e,"P",{"data-svelte-h":!0}),U(u)!=="svelte-1g75zm2"&&(u.innerHTML=K),I=i(e),_(d.$$.fragment,e),S=i(e),r=p(e,"P",{"data-svelte-h":!0}),U(r)!=="svelte-nklbqh"&&(r.innerHTML=O),X=i(e),_(M.$$.fragment,e),L=i(e),b=p(e,"P",{"data-svelte-h":!0}),U(b)!=="svelte-v6bdix"&&(b.innerHTML=ee),k=i(e),_(y.$$.fragment,e),E=i(e),h=p(e,"P",{"data-svelte-h":!0}),U(h)!=="svelte-i2n3iv"&&(h.innerHTML=te),Q=i(e),T=p(e,"P",{"data-svelte-h":!0}),U(T)!=="svelte-6bo6ng"&&(T.innerHTML=le),H=i(e),_(Z.$$.fragment,e),z=i(e),$=p(e,"P",{"data-svelte-h":!0}),U($)!=="svelte-qygtll"&&($.innerHTML=ne),P=i(e),_(J.$$.fragment,e),Y=i(e),j=p(e,"P",{}),se(j).forEach(l),this.h()},h(){ie(o,"name","hf:doc:metadata"),ie(o,"content",ye)},m(e,t){de(document.head,o),n(e,B,t),n(e,C,t),n(e,x,t),g(m,e,t),n(e,V,t),g(f,e,t),n(e,N,t),n(e,c,t),n(e,R,t),n(e,u,t),n(e,I,t),g(d,e,t),n(e,S,t),n(e,r,t),n(e,X,t),g(M,e,t),n(e,L,t),n(e,b,t),n(e,k,t),g(y,e,t),n(e,E,t),n(e,h,t),n(e,Q,t),n(e,T,t),n(e,H,t),g(Z,e,t),n(e,z,t),n(e,$,t),n(e,P,t),g(J,e,t),n(e,Y,t),n(e,j,t),q=!0},p:oe,i(e){q||(v(m.$$.fragment,e),v(f.$$.fragment,e),v(d.$$.fragment,e),v(M.$$.fragment,e),v(y.$$.fragment,e),v(Z.$$.fragment,e),v(J.$$.fragment,e),q=!0)},o(e){W(m.$$.fragment,e),W(f.$$.fragment,e),W(d.$$.fragment,e),W(M.$$.fragment,e),W(y.$$.fragment,e),W(Z.$$.fragment,e),W(J.$$.fragment,e),q=!1},d(e){e&&(l(B),l(C),l(x),l(V),l(N),l(c),l(R),l(u),l(I),l(S),l(r),l(X),l(L),l(b),l(k),l(E),l(h),l(Q),l(T),l(H),l(z),l($),l(P),l(Y),l(j)),l(o),G(m,e),G(f,e),G(d,e),G(M,e),G(y,e),G(Z,e),G(J,e)}}}const ye='{"title":"将模型适配至新任务","local":"将模型适配至新任务","sections":[{"title":"配置 UNet2DConditionModel 参数","local":"配置-unet2dconditionmodel-参数","sections":[],"depth":2}],"depth":1}';function he(D){return me(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class we extends fe{constructor(o){super(),ce(this,o,he,be,pe,{})}}export{we as component};
