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