import{S as Nt,i as St,s as Ut,e as i,k as f,w as I,t as s,M as Bt,c as n,d as a,m,a as l,x as A,h as r,b as d,N as Mt,G as t,g as p,y as L,L as Rt,q as T,o as M,B as N,v as Vt}from"../../chunks/vendor-hf-doc-builder.js";import{I as Ht}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as z}from"../../chunks/CodeBlock-hf-doc-builder.js";function Jt(ct){let v,le,_,y,F,q,De,K,Ce,pe,u,xe,Q,Oe,Ie,k,W,Ae,Le,X,Te,Me,fe,w,Ne,S,Y,Se,Ue,me,G,ce,$,Be,U,Re,Ve,de,P,ue,c,He,Z,Je,ze,ee,Fe,Ke,te,Qe,We,ae,Xe,Ye,se,Ze,et,he,D,ge,B,tt,ve,C,_e,R,V,dt,ye,h,at,re,st,rt,oe,ot,it,ie,nt,lt,we,x,$e,E,pt,ne,ft,mt,Ee,O,be,H,J,ut,je;return q=new Ht({}),G=new z({props:{code:'prompt = "Labrador in the style of Vermeer"',highlighted:'prompt = <span class="hljs-string">&quot;Labrador in the style of Vermeer&quot;</span>'}}),P=new z({props:{code:`from diffusers import DiffusionPipeline

pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
pipe = pipe.to("cuda")`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline

<span class="hljs-meta">&gt;&gt;&gt; </span>pipe = DiffusionPipeline.from_pretrained(<span class="hljs-string">&quot;runwayml/stable-diffusion-v1-5&quot;</span>, torch_dtype=torch.float16)
<span class="hljs-meta">&gt;&gt;&gt; </span>pipe = pipe.to(<span class="hljs-string">&quot;cuda&quot;</span>)`}}),D=new z({props:{code:`import torch

generator = [torch.Generator(device="cuda").manual_seed(i) for i in range(4)]`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> torch

<span class="hljs-meta">&gt;&gt;&gt; </span>generator = [torch.Generator(device=<span class="hljs-string">&quot;cuda&quot;</span>).manual_seed(i) <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">4</span>)]`}}),C=new z({props:{code:`images = pipe(prompt, generator=generator, num_images_per_prompt=4).images
images`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>images = pipe(prompt, generator=generator, num_images_per_prompt=<span class="hljs-number">4</span>).images
<span class="hljs-meta">&gt;&gt;&gt; </span>images`}}),x=new z({props:{code:`prompt = [prompt + t for t in [", highly realistic", ", artsy", ", trending", ", colorful"]]
generator = [torch.Generator(device="cuda").manual_seed(0) for i in range(4)]`,highlighted:`prompt = [prompt + t <span class="hljs-keyword">for</span> t <span class="hljs-keyword">in</span> [<span class="hljs-string">&quot;, highly realistic&quot;</span>, <span class="hljs-string">&quot;, artsy&quot;</span>, <span class="hljs-string">&quot;, trending&quot;</span>, <span class="hljs-string">&quot;, colorful&quot;</span>]]
generator = [torch.Generator(device=<span class="hljs-string">&quot;cuda&quot;</span>).manual_seed(<span class="hljs-number">0</span>) <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">4</span>)]`}}),O=new z({props:{code:`images = pipe(prompt, generator=generator).images
images`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>images = pipe(prompt, generator=generator).images
<span class="hljs-meta">&gt;&gt;&gt; </span>images`}}),{c(){v=i("meta"),le=f(),_=i("h1"),y=i("a"),F=i("span"),I(q.$$.fragment),De=f(),K=i("span"),Ce=s("Improve image quality with deterministic generation"),pe=f(),u=i("p"),xe=s("A common way to improve the quality of generated images is with "),Q=i("em"),Oe=s("deterministic batch generation"),Ie=s(", generate a batch of images and select one image to improve with a more detailed prompt in a second round of inference. The key is to pass a list of "),k=i("a"),W=i("code"),Ae=s("torch.Generator"),Le=s("\u2019s to the pipeline for batched image generation, and tie each "),X=i("code"),Te=s("Generator"),Me=s(" to a seed so you can reuse it for an image."),fe=f(),w=i("p"),Ne=s("Let\u2019s use "),S=i("a"),Y=i("code"),Se=s("runwayml/stable-diffusion-v1-5"),Ue=s(" for example, and generate several versions of the following prompt:"),me=f(),I(G.$$.fragment),ce=f(),$=i("p"),Be=s("Instantiate a pipeline with "),U=i("a"),Re=s("DiffusionPipeline.from_pretrained()"),Ve=s(" and place it on a GPU (if available):"),de=f(),I(P.$$.fragment),ue=f(),c=i("p"),He=s("Now, define four different "),Z=i("code"),Je=s("Generator"),ze=s("\u2019s and assign each "),ee=i("code"),Fe=s("Generator"),Ke=s(" a seed ("),te=i("code"),Qe=s("0"),We=s(" to "),ae=i("code"),Xe=s("3"),Ye=s(") so you can reuse a "),se=i("code"),Ze=s("Generator"),et=s(" later for a specific image:"),he=f(),I(D.$$.fragment),ge=f(),B=i("p"),tt=s("Generate the images and have a look:"),ve=f(),I(C.$$.fragment),_e=f(),R=i("p"),V=i("img"),ye=f(),h=i("p"),at=s("In this example, you\u2019ll improve upon the first image - but in reality, you can use any image you want (even the image with double sets of eyes!). The first image used the "),re=i("code"),st=s("Generator"),rt=s(" with seed "),oe=i("code"),ot=s("0"),it=s(", so you\u2019ll reuse that "),ie=i("code"),nt=s("Generator"),lt=s(" for the second round of inference. To improve the quality of the image, add some additional text to the prompt:"),we=f(),I(x.$$.fragment),$e=f(),E=i("p"),pt=s("Create four generators with seed "),ne=i("code"),ft=s("0"),mt=s(", and generate another batch of images, all of which should look like the first image from the previous round!"),Ee=f(),I(O.$$.fragment),be=f(),H=i("p"),J=i("img"),this.h()},l(e){const o=Bt('[data-svelte="svelte-1phssyn"]',document.head);v=n(o,"META",{name:!0,content:!0}),o.forEach(a),le=m(e),_=n(e,"H1",{class:!0});var qe=l(_);y=n(qe,"A",{id:!0,class:!0,href:!0});var ht=l(y);F=n(ht,"SPAN",{});var gt=l(F);A(q.$$.fragment,gt),gt.forEach(a),ht.forEach(a),De=m(qe),K=n(qe,"SPAN",{});var vt=l(K);Ce=r(vt,"Improve image quality with deterministic generation"),vt.forEach(a),qe.forEach(a),pe=m(e),u=n(e,"P",{});var b=l(u);xe=r(b,"A common way to improve the quality of generated images is with "),Q=n(b,"EM",{});var _t=l(Q);Oe=r(_t,"deterministic batch generation"),_t.forEach(a),Ie=r(b,", generate a batch of images and select one image to improve with a more detailed prompt in a second round of inference. The key is to pass a list of "),k=n(b,"A",{href:!0,rel:!0});var yt=l(k);W=n(yt,"CODE",{});var wt=l(W);Ae=r(wt,"torch.Generator"),wt.forEach(a),yt.forEach(a),Le=r(b,"\u2019s to the pipeline for batched image generation, and tie each "),X=n(b,"CODE",{});var $t=l(X);Te=r($t,"Generator"),$t.forEach(a),Me=r(b," to a seed so you can reuse it for an image."),b.forEach(a),fe=m(e),w=n(e,"P",{});var ke=l(w);Ne=r(ke,"Let\u2019s use "),S=n(ke,"A",{href:!0});var Et=l(S);Y=n(Et,"CODE",{});var bt=l(Y);Se=r(bt,"runwayml/stable-diffusion-v1-5"),bt.forEach(a),Et.forEach(a),Ue=r(ke," for example, and generate several versions of the following prompt:"),ke.forEach(a),me=m(e),A(G.$$.fragment,e),ce=m(e),$=n(e,"P",{});var Ge=l($);Be=r(Ge,"Instantiate a pipeline with "),U=n(Ge,"A",{href:!0});var jt=l(U);Re=r(jt,"DiffusionPipeline.from_pretrained()"),jt.forEach(a),Ve=r(Ge," and place it on a GPU (if available):"),Ge.forEach(a),de=m(e),A(P.$$.fragment,e),ue=m(e),c=n(e,"P",{});var g=l(c);He=r(g,"Now, define four different "),Z=n(g,"CODE",{});var qt=l(Z);Je=r(qt,"Generator"),qt.forEach(a),ze=r(g,"\u2019s and assign each "),ee=n(g,"CODE",{});var kt=l(ee);Fe=r(kt,"Generator"),kt.forEach(a),Ke=r(g," a seed ("),te=n(g,"CODE",{});var Gt=l(te);Qe=r(Gt,"0"),Gt.forEach(a),We=r(g," to "),ae=n(g,"CODE",{});var Pt=l(ae);Xe=r(Pt,"3"),Pt.forEach(a),Ye=r(g,") so you can reuse a "),se=n(g,"CODE",{});var Dt=l(se);Ze=r(Dt,"Generator"),Dt.forEach(a),et=r(g," later for a specific image:"),g.forEach(a),he=m(e),A(D.$$.fragment,e),ge=m(e),B=n(e,"P",{});var Ct=l(B);tt=r(Ct,"Generate the images and have a look:"),Ct.forEach(a),ve=m(e),A(C.$$.fragment,e),_e=m(e),R=n(e,"P",{});var xt=l(R);V=n(xt,"IMG",{src:!0,alt:!0}),xt.forEach(a),ye=m(e),h=n(e,"P",{});var j=l(h);at=r(j,"In this example, you\u2019ll improve upon the first image - but in reality, you can use any image you want (even the image with double sets of eyes!). The first image used the "),re=n(j,"CODE",{});var Ot=l(re);st=r(Ot,"Generator"),Ot.forEach(a),rt=r(j," with seed "),oe=n(j,"CODE",{});var It=l(oe);ot=r(It,"0"),It.forEach(a),it=r(j,", so you\u2019ll reuse that "),ie=n(j,"CODE",{});var At=l(ie);nt=r(At,"Generator"),At.forEach(a),lt=r(j," for the second round of inference. To improve the quality of the image, add some additional text to the prompt:"),j.forEach(a),we=m(e),A(x.$$.fragment,e),$e=m(e),E=n(e,"P",{});var Pe=l(E);pt=r(Pe,"Create four generators with seed "),ne=n(Pe,"CODE",{});var Lt=l(ne);ft=r(Lt,"0"),Lt.forEach(a),mt=r(Pe,", and generate another batch of images, all of which should look like the first image from the previous round!"),Pe.forEach(a),Ee=m(e),A(O.$$.fragment,e),be=m(e),H=n(e,"P",{});var Tt=l(H);J=n(Tt,"IMG",{src:!0,alt:!0}),Tt.forEach(a),this.h()},h(){d(v,"name","hf:doc:metadata"),d(v,"content",JSON.stringify(zt)),d(y,"id","improve-image-quality-with-deterministic-generation"),d(y,"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"),d(y,"href","#improve-image-quality-with-deterministic-generation"),d(_,"class","relative group"),d(k,"href","https://pytorch.org/docs/stable/generated/torch.Generator.html#generator"),d(k,"rel","nofollow"),d(S,"href","runwayml/stable-diffusion-v1-5"),d(U,"href","/docs/diffusers/v0.16.0/en/api/diffusion_pipeline#diffusers.DiffusionPipeline.from_pretrained"),Mt(V.src,dt="https://huggingface.co/datasets/diffusers/diffusers-images-docs/resolve/main/reusabe_seeds.jpg")||d(V,"src",dt),d(V,"alt","img"),Mt(J.src,ut="https://huggingface.co/datasets/diffusers/diffusers-images-docs/resolve/main/reusabe_seeds_2.jpg")||d(J,"src",ut),d(J,"alt","img")},m(e,o){t(document.head,v),p(e,le,o),p(e,_,o),t(_,y),t(y,F),L(q,F,null),t(_,De),t(_,K),t(K,Ce),p(e,pe,o),p(e,u,o),t(u,xe),t(u,Q),t(Q,Oe),t(u,Ie),t(u,k),t(k,W),t(W,Ae),t(u,Le),t(u,X),t(X,Te),t(u,Me),p(e,fe,o),p(e,w,o),t(w,Ne),t(w,S),t(S,Y),t(Y,Se),t(w,Ue),p(e,me,o),L(G,e,o),p(e,ce,o),p(e,$,o),t($,Be),t($,U),t(U,Re),t($,Ve),p(e,de,o),L(P,e,o),p(e,ue,o),p(e,c,o),t(c,He),t(c,Z),t(Z,Je),t(c,ze),t(c,ee),t(ee,Fe),t(c,Ke),t(c,te),t(te,Qe),t(c,We),t(c,ae),t(ae,Xe),t(c,Ye),t(c,se),t(se,Ze),t(c,et),p(e,he,o),L(D,e,o),p(e,ge,o),p(e,B,o),t(B,tt),p(e,ve,o),L(C,e,o),p(e,_e,o),p(e,R,o),t(R,V),p(e,ye,o),p(e,h,o),t(h,at),t(h,re),t(re,st),t(h,rt),t(h,oe),t(oe,ot),t(h,it),t(h,ie),t(ie,nt),t(h,lt),p(e,we,o),L(x,e,o),p(e,$e,o),p(e,E,o),t(E,pt),t(E,ne),t(ne,ft),t(E,mt),p(e,Ee,o),L(O,e,o),p(e,be,o),p(e,H,o),t(H,J),je=!0},p:Rt,i(e){je||(T(q.$$.fragment,e),T(G.$$.fragment,e),T(P.$$.fragment,e),T(D.$$.fragment,e),T(C.$$.fragment,e),T(x.$$.fragment,e),T(O.$$.fragment,e),je=!0)},o(e){M(q.$$.fragment,e),M(G.$$.fragment,e),M(P.$$.fragment,e),M(D.$$.fragment,e),M(C.$$.fragment,e),M(x.$$.fragment,e),M(O.$$.fragment,e),je=!1},d(e){a(v),e&&a(le),e&&a(_),N(q),e&&a(pe),e&&a(u),e&&a(fe),e&&a(w),e&&a(me),N(G,e),e&&a(ce),e&&a($),e&&a(de),N(P,e),e&&a(ue),e&&a(c),e&&a(he),N(D,e),e&&a(ge),e&&a(B),e&&a(ve),N(C,e),e&&a(_e),e&&a(R),e&&a(ye),e&&a(h),e&&a(we),N(x,e),e&&a($e),e&&a(E),e&&a(Ee),N(O,e),e&&a(be),e&&a(H)}}}const zt={local:"improve-image-quality-with-deterministic-generation",title:"Improve image quality with deterministic generation"};function Ft(ct){return Vt(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Xt extends Nt{constructor(v){super();St(this,v,Ft,Jt,Ut,{})}}export{Xt as default,zt as metadata};
