import{s as oe,n as pe,o as me}from"../chunks/scheduler.e4ff9b64.js";import{S as fe,i as de,e as a,s as n,c as w,h as ue,a as o,d as l,b as i,f as ne,g as _,j as g,k as ie,l as ce,m as s,n as v,t as U,o as W,p as B}from"../chunks/index.09f1bca0.js";import{C as re,H as ae,E as be}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.7f9679ec.js";import{C as D}from"../chunks/CodeBlock.c975fdb9.js";function Me(F){let p,j,C,x,m,N,f,R,d,A="许多扩散系统共享相同的组件架构，这使得您能够将针对某一任务预训练的模型调整适配至完全不同的新任务。",S,u,O="本指南将展示如何通过初始化并修改预训练 <code>UNet2DConditionModel</code> 的架构，将文生图预训练模型改造为图像修复(inpainting)模型。",I,c,V,r,K='默认情况下，<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,b,L,M,ee='而图像修复任务需要输入样本具有9个通道。您可以在 <a href="https://huggingface.co/stable-diffusion-v1-5/stable-diffusion-inpainting" rel="nofollow"><code>stable-diffusion-v1-5/stable-diffusion-inpainting</code></a> 这样的预训练修复模型中验证此参数：',E,T,k,y,te="要将文生图模型改造为修复模型，您需要将 <code>in_channels</code> 参数从4调整为9。",z,h,le="初始化一个加载了文生图预训练权重的 <code>UNet2DConditionModel</code>，并将 <code>in_channels</code> 设为9。由于输入通道数变化导致张量形状改变，需要设置 <code>ignore_mismatched_sizes=True</code> 和 <code>low_cpu_mem_usage=False</code> 来避免尺寸不匹配错误。",H,J,Q,Z,se="此时文生图模型的其他组件权重仍保持预训练状态，但UNet的输入卷积层权重(<code>conv_in.weight</code>)会随机初始化。由于这一关键变化，必须对模型进行修复任务的微调，否则模型将仅会输出噪声。",P,$,Y,G,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"}}),c=new ae({props:{title:"配置 UNet2DConditionModel 参数",local:"配置-unet2dconditionmodel-参数",headingTag:"h2"}}),b=new D({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}}),T=new D({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMFN0YWJsZURpZmZ1c2lvblBpcGVsaW5lJTBBJTBBcGlwZWxpbmUlMjAlM0QlMjBTdGFibGVEaWZmdXNpb25QaXBlbGluZS5mcm9tX3ByZXRyYWluZWQoJTIyc3RhYmxlLWRpZmZ1c2lvbi12MS01JTJGc3RhYmxlLWRpZmZ1c2lvbi1pbnBhaW50aW5nJTIyJTJDJTIwdXNlX3NhZmV0ZW5zb3JzJTNEVHJ1ZSklMEFwaXBlbGluZS51bmV0LmNvbmZpZyU1QiUyMmluX2NoYW5uZWxzJTIyJTVEJTBBOQ==",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-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}}),J=new D({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}}),$=new be({props:{source:"https://github.com/huggingface/diffusers/blob/main/docs/source/zh/training/adapt_a_model.md"}}),{c(){p=a("meta"),j=n(),C=a("p"),x=n(),w(m.$$.fragment),N=n(),w(f.$$.fragment),R=n(),d=a("p"),d.textContent=A,S=n(),u=a("p"),u.innerHTML=O,I=n(),w(c.$$.fragment),V=n(),r=a("p"),r.innerHTML=K,X=n(),w(b.$$.fragment),L=n(),M=a("p"),M.innerHTML=ee,E=n(),w(T.$$.fragment),k=n(),y=a("p"),y.innerHTML=te,z=n(),h=a("p"),h.innerHTML=le,H=n(),w(J.$$.fragment),Q=n(),Z=a("p"),Z.innerHTML=se,P=n(),w($.$$.fragment),Y=n(),G=a("p"),this.h()},l(e){const t=ue("svelte-u9bgzb",document.head);p=o(t,"META",{name:!0,content:!0}),t.forEach(l),j=i(e),C=o(e,"P",{}),ne(C).forEach(l),x=i(e),_(m.$$.fragment,e),N=i(e),_(f.$$.fragment,e),R=i(e),d=o(e,"P",{"data-svelte-h":!0}),g(d)!=="svelte-10h8vrc"&&(d.textContent=A),S=i(e),u=o(e,"P",{"data-svelte-h":!0}),g(u)!=="svelte-1g75zm2"&&(u.innerHTML=O),I=i(e),_(c.$$.fragment,e),V=i(e),r=o(e,"P",{"data-svelte-h":!0}),g(r)!=="svelte-nklbqh"&&(r.innerHTML=K),X=i(e),_(b.$$.fragment,e),L=i(e),M=o(e,"P",{"data-svelte-h":!0}),g(M)!=="svelte-cgy9ux"&&(M.innerHTML=ee),E=i(e),_(T.$$.fragment,e),k=i(e),y=o(e,"P",{"data-svelte-h":!0}),g(y)!=="svelte-i2n3iv"&&(y.innerHTML=te),z=i(e),h=o(e,"P",{"data-svelte-h":!0}),g(h)!=="svelte-6bo6ng"&&(h.innerHTML=le),H=i(e),_(J.$$.fragment,e),Q=i(e),Z=o(e,"P",{"data-svelte-h":!0}),g(Z)!=="svelte-qygtll"&&(Z.innerHTML=se),P=i(e),_($.$$.fragment,e),Y=i(e),G=o(e,"P",{}),ne(G).forEach(l),this.h()},h(){ie(p,"name","hf:doc:metadata"),ie(p,"content",Te)},m(e,t){ce(document.head,p),s(e,j,t),s(e,C,t),s(e,x,t),v(m,e,t),s(e,N,t),v(f,e,t),s(e,R,t),s(e,d,t),s(e,S,t),s(e,u,t),s(e,I,t),v(c,e,t),s(e,V,t),s(e,r,t),s(e,X,t),v(b,e,t),s(e,L,t),s(e,M,t),s(e,E,t),v(T,e,t),s(e,k,t),s(e,y,t),s(e,z,t),s(e,h,t),s(e,H,t),v(J,e,t),s(e,Q,t),s(e,Z,t),s(e,P,t),v($,e,t),s(e,Y,t),s(e,G,t),q=!0},p:pe,i(e){q||(U(m.$$.fragment,e),U(f.$$.fragment,e),U(c.$$.fragment,e),U(b.$$.fragment,e),U(T.$$.fragment,e),U(J.$$.fragment,e),U($.$$.fragment,e),q=!0)},o(e){W(m.$$.fragment,e),W(f.$$.fragment,e),W(c.$$.fragment,e),W(b.$$.fragment,e),W(T.$$.fragment,e),W(J.$$.fragment,e),W($.$$.fragment,e),q=!1},d(e){e&&(l(j),l(C),l(x),l(N),l(R),l(d),l(S),l(u),l(I),l(V),l(r),l(X),l(L),l(M),l(E),l(k),l(y),l(z),l(h),l(H),l(Q),l(Z),l(P),l(Y),l(G)),l(p),B(m,e),B(f,e),B(c,e),B(b,e),B(T,e),B(J,e),B($,e)}}}const Te='{"title":"将模型适配至新任务","local":"将模型适配至新任务","sections":[{"title":"配置 UNet2DConditionModel 参数","local":"配置-unet2dconditionmodel-参数","sections":[],"depth":2}],"depth":1}';function ye(F){return me(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class we extends fe{constructor(p){super(),de(this,p,ye,Me,oe,{})}}export{we as component};
