import{S as Pt,i as St,s as xt,e as r,k as f,w as E,t as o,M as Yt,c as i,d as s,m,a as n,x as J,h as l,b as d,N as Dt,G as t,g as p,y as k,L as Ht,q as U,o as T,B as G,v as Qt}from"../../chunks/vendor-hf-doc-builder.js";import{I as Vt}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as F}from"../../chunks/CodeBlock-hf-doc-builder.js";import{D as Ft}from"../../chunks/DocNotebookDropdown-hf-doc-builder.js";function At(ut){let y,pe,b,v,A,I,Ie,X,Ze,fe,Z,me,u,Ce,O,We,qe,C,z,Be,Ne,L,Re,De,ce,w,Pe,P,K,Se,xe,de,W,ue,M,Ye,S,He,Qe,he,q,ge,c,Ve,ee,Fe,Ae,te,Xe,Oe,se,ze,Le,ae,Ke,et,oe,tt,st,ye,B,be,x,at,ve,N,we,Y,H,ht,Me,h,ot,le,lt,rt,re,it,nt,ie,pt,ft,_e,R,$e,_,mt,ne,ct,dt,je,D,Ee,Q,V,gt,Je;return I=new Vt({}),Z=new Ft({props:{classNames:"absolute z-10 right-0 top-0",options:[{label:"Mixed",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers_doc/en/reusing_seeds.ipynb"},{label:"PyTorch",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers_doc/en/pytorch/reusing_seeds.ipynb"},{label:"TensorFlow",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers_doc/en/tensorflow/reusing_seeds.ipynb"},{label:"Mixed",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/en/reusing_seeds.ipynb"},{label:"PyTorch",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/en/pytorch/reusing_seeds.ipynb"},{label:"TensorFlow",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/en/tensorflow/reusing_seeds.ipynb"}]}}),W=new F({props:{code:"cHJvbXB0JTIwJTNEJTIwJTIyTGFicmFkb3IlMjBpbiUyMHRoZSUyMHN0eWxlJTIwb2YlMjBWZXJtZWVyJTIy",highlighted:'prompt = <span class="hljs-string">&quot;Labrador in the style of Vermeer&quot;</span>'}}),q=new F({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMERpZmZ1c2lvblBpcGVsaW5lJTBBJTBBcGlwZSUyMCUzRCUyMERpZmZ1c2lvblBpcGVsaW5lLmZyb21fcHJldHJhaW5lZCglMEElMjAlMjAlMjAlMjAlMjJydW53YXltbCUyRnN0YWJsZS1kaWZmdXNpb24tdjEtNSUyMiUyQyUyMHRvcmNoX2R0eXBlJTNEdG9yY2guZmxvYXQxNiUyQyUyMHVzZV9zYWZldGVuc29ycyUzRFRydWUlMEEpJTBBcGlwZSUyMCUzRCUyMHBpcGUudG8oJTIyY3VkYSUyMik=",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-meta">... </span>    <span class="hljs-string">&quot;runwayml/stable-diffusion-v1-5&quot;</span>, torch_dtype=torch.float16, use_safetensors=<span class="hljs-literal">True</span>
<span class="hljs-meta">... </span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>pipe = pipe.to(<span class="hljs-string">&quot;cuda&quot;</span>)`}}),B=new F({props:{code:"aW1wb3J0JTIwdG9yY2glMEElMEFnZW5lcmF0b3IlMjAlM0QlMjAlNUJ0b3JjaC5HZW5lcmF0b3IoZGV2aWNlJTNEJTIyY3VkYSUyMikubWFudWFsX3NlZWQoaSklMjBmb3IlMjBpJTIwaW4lMjByYW5nZSg0KSU1RA==",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>)]`}}),N=new F({props:{code:"aW1hZ2VzJTIwJTNEJTIwcGlwZShwcm9tcHQlMkMlMjBnZW5lcmF0b3IlM0RnZW5lcmF0b3IlMkMlMjBudW1faW1hZ2VzX3Blcl9wcm9tcHQlM0Q0KS5pbWFnZXMlMEFpbWFnZXM=",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`}}),R=new F({props:{code:"cHJvbXB0JTIwJTNEJTIwJTVCcHJvbXB0JTIwJTJCJTIwdCUyMGZvciUyMHQlMjBpbiUyMCU1QiUyMiUyQyUyMGhpZ2hseSUyMHJlYWxpc3RpYyUyMiUyQyUyMCUyMiUyQyUyMGFydHN5JTIyJTJDJTIwJTIyJTJDJTIwdHJlbmRpbmclMjIlMkMlMjAlMjIlMkMlMjBjb2xvcmZ1bCUyMiU1RCU1RCUwQWdlbmVyYXRvciUyMCUzRCUyMCU1QnRvcmNoLkdlbmVyYXRvcihkZXZpY2UlM0QlMjJjdWRhJTIyKS5tYW51YWxfc2VlZCgwKSUyMGZvciUyMGklMjBpbiUyMHJhbmdlKDQpJTVE",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>)]`}}),D=new F({props:{code:"aW1hZ2VzJTIwJTNEJTIwcGlwZShwcm9tcHQlMkMlMjBnZW5lcmF0b3IlM0RnZW5lcmF0b3IpLmltYWdlcyUwQWltYWdlcw==",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(){y=r("meta"),pe=f(),b=r("h1"),v=r("a"),A=r("span"),E(I.$$.fragment),Ie=f(),X=r("span"),Ze=o("Improve image quality with deterministic generation"),fe=f(),E(Z.$$.fragment),me=f(),u=r("p"),Ce=o("A common way to improve the quality of generated images is with "),O=r("em"),We=o("deterministic batch generation"),qe=o(", 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 "),C=r("a"),z=r("code"),Be=o("torch.Generator"),Ne=o("\u2019s to the pipeline for batched image generation, and tie each "),L=r("code"),Re=o("Generator"),De=o(" to a seed so you can reuse it for an image."),ce=f(),w=r("p"),Pe=o("Let\u2019s use "),P=r("a"),K=r("code"),Se=o("runwayml/stable-diffusion-v1-5"),xe=o(" for example, and generate several versions of the following prompt:"),de=f(),E(W.$$.fragment),ue=f(),M=r("p"),Ye=o("Instantiate a pipeline with "),S=r("a"),He=o("DiffusionPipeline.from_pretrained()"),Qe=o(" and place it on a GPU (if available):"),he=f(),E(q.$$.fragment),ge=f(),c=r("p"),Ve=o("Now, define four different "),ee=r("code"),Fe=o("Generator"),Ae=o("\u2019s and assign each "),te=r("code"),Xe=o("Generator"),Oe=o(" a seed ("),se=r("code"),ze=o("0"),Le=o(" to "),ae=r("code"),Ke=o("3"),et=o(") so you can reuse a "),oe=r("code"),tt=o("Generator"),st=o(" later for a specific image:"),ye=f(),E(B.$$.fragment),be=f(),x=r("p"),at=o("Generate the images and have a look:"),ve=f(),E(N.$$.fragment),we=f(),Y=r("p"),H=r("img"),Me=f(),h=r("p"),ot=o("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 "),le=r("code"),lt=o("Generator"),rt=o(" with seed "),re=r("code"),it=o("0"),nt=o(", so you\u2019ll reuse that "),ie=r("code"),pt=o("Generator"),ft=o(" for the second round of inference. To improve the quality of the image, add some additional text to the prompt:"),_e=f(),E(R.$$.fragment),$e=f(),_=r("p"),mt=o("Create four generators with seed "),ne=r("code"),ct=o("0"),dt=o(", and generate another batch of images, all of which should look like the first image from the previous round!"),je=f(),E(D.$$.fragment),Ee=f(),Q=r("p"),V=r("img"),this.h()},l(e){const a=Yt('[data-svelte="svelte-1phssyn"]',document.head);y=i(a,"META",{name:!0,content:!0}),a.forEach(s),pe=m(e),b=i(e,"H1",{class:!0});var ke=n(b);v=i(ke,"A",{id:!0,class:!0,href:!0});var yt=n(v);A=i(yt,"SPAN",{});var bt=n(A);J(I.$$.fragment,bt),bt.forEach(s),yt.forEach(s),Ie=m(ke),X=i(ke,"SPAN",{});var vt=n(X);Ze=l(vt,"Improve image quality with deterministic generation"),vt.forEach(s),ke.forEach(s),fe=m(e),J(Z.$$.fragment,e),me=m(e),u=i(e,"P",{});var $=n(u);Ce=l($,"A common way to improve the quality of generated images is with "),O=i($,"EM",{});var wt=n(O);We=l(wt,"deterministic batch generation"),wt.forEach(s),qe=l($,", 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 "),C=i($,"A",{href:!0,rel:!0});var Mt=n(C);z=i(Mt,"CODE",{});var _t=n(z);Be=l(_t,"torch.Generator"),_t.forEach(s),Mt.forEach(s),Ne=l($,"\u2019s to the pipeline for batched image generation, and tie each "),L=i($,"CODE",{});var $t=n(L);Re=l($t,"Generator"),$t.forEach(s),De=l($," to a seed so you can reuse it for an image."),$.forEach(s),ce=m(e),w=i(e,"P",{});var Ue=n(w);Pe=l(Ue,"Let\u2019s use "),P=i(Ue,"A",{href:!0});var jt=n(P);K=i(jt,"CODE",{});var Et=n(K);Se=l(Et,"runwayml/stable-diffusion-v1-5"),Et.forEach(s),jt.forEach(s),xe=l(Ue," for example, and generate several versions of the following prompt:"),Ue.forEach(s),de=m(e),J(W.$$.fragment,e),ue=m(e),M=i(e,"P",{});var Te=n(M);Ye=l(Te,"Instantiate a pipeline with "),S=i(Te,"A",{href:!0});var Jt=n(S);He=l(Jt,"DiffusionPipeline.from_pretrained()"),Jt.forEach(s),Qe=l(Te," and place it on a GPU (if available):"),Te.forEach(s),he=m(e),J(q.$$.fragment,e),ge=m(e),c=i(e,"P",{});var g=n(c);Ve=l(g,"Now, define four different "),ee=i(g,"CODE",{});var kt=n(ee);Fe=l(kt,"Generator"),kt.forEach(s),Ae=l(g,"\u2019s and assign each "),te=i(g,"CODE",{});var Ut=n(te);Xe=l(Ut,"Generator"),Ut.forEach(s),Oe=l(g," a seed ("),se=i(g,"CODE",{});var Tt=n(se);ze=l(Tt,"0"),Tt.forEach(s),Le=l(g," to "),ae=i(g,"CODE",{});var Gt=n(ae);Ke=l(Gt,"3"),Gt.forEach(s),et=l(g,") so you can reuse a "),oe=i(g,"CODE",{});var It=n(oe);tt=l(It,"Generator"),It.forEach(s),st=l(g," later for a specific image:"),g.forEach(s),ye=m(e),J(B.$$.fragment,e),be=m(e),x=i(e,"P",{});var Zt=n(x);at=l(Zt,"Generate the images and have a look:"),Zt.forEach(s),ve=m(e),J(N.$$.fragment,e),we=m(e),Y=i(e,"P",{});var Ct=n(Y);H=i(Ct,"IMG",{src:!0,alt:!0}),Ct.forEach(s),Me=m(e),h=i(e,"P",{});var j=n(h);ot=l(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 "),le=i(j,"CODE",{});var Wt=n(le);lt=l(Wt,"Generator"),Wt.forEach(s),rt=l(j," with seed "),re=i(j,"CODE",{});var qt=n(re);it=l(qt,"0"),qt.forEach(s),nt=l(j,", so you\u2019ll reuse that "),ie=i(j,"CODE",{});var Bt=n(ie);pt=l(Bt,"Generator"),Bt.forEach(s),ft=l(j," for the second round of inference. To improve the quality of the image, add some additional text to the prompt:"),j.forEach(s),_e=m(e),J(R.$$.fragment,e),$e=m(e),_=i(e,"P",{});var Ge=n(_);mt=l(Ge,"Create four generators with seed "),ne=i(Ge,"CODE",{});var Nt=n(ne);ct=l(Nt,"0"),Nt.forEach(s),dt=l(Ge,", and generate another batch of images, all of which should look like the first image from the previous round!"),Ge.forEach(s),je=m(e),J(D.$$.fragment,e),Ee=m(e),Q=i(e,"P",{});var Rt=n(Q);V=i(Rt,"IMG",{src:!0,alt:!0}),Rt.forEach(s),this.h()},h(){d(y,"name","hf:doc:metadata"),d(y,"content",JSON.stringify(Xt)),d(v,"id","improve-image-quality-with-deterministic-generation"),d(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"),d(v,"href","#improve-image-quality-with-deterministic-generation"),d(b,"class","relative group"),d(C,"href","https://pytorch.org/docs/stable/generated/torch.Generator.html#generator"),d(C,"rel","nofollow"),d(P,"href","runwayml/stable-diffusion-v1-5"),d(S,"href","/docs/diffusers/v0.20.0/en/api/pipelines/overview#diffusers.DiffusionPipeline.from_pretrained"),Dt(H.src,ht="https://huggingface.co/datasets/diffusers/diffusers-images-docs/resolve/main/reusabe_seeds.jpg")||d(H,"src",ht),d(H,"alt","img"),Dt(V.src,gt="https://huggingface.co/datasets/diffusers/diffusers-images-docs/resolve/main/reusabe_seeds_2.jpg")||d(V,"src",gt),d(V,"alt","img")},m(e,a){t(document.head,y),p(e,pe,a),p(e,b,a),t(b,v),t(v,A),k(I,A,null),t(b,Ie),t(b,X),t(X,Ze),p(e,fe,a),k(Z,e,a),p(e,me,a),p(e,u,a),t(u,Ce),t(u,O),t(O,We),t(u,qe),t(u,C),t(C,z),t(z,Be),t(u,Ne),t(u,L),t(L,Re),t(u,De),p(e,ce,a),p(e,w,a),t(w,Pe),t(w,P),t(P,K),t(K,Se),t(w,xe),p(e,de,a),k(W,e,a),p(e,ue,a),p(e,M,a),t(M,Ye),t(M,S),t(S,He),t(M,Qe),p(e,he,a),k(q,e,a),p(e,ge,a),p(e,c,a),t(c,Ve),t(c,ee),t(ee,Fe),t(c,Ae),t(c,te),t(te,Xe),t(c,Oe),t(c,se),t(se,ze),t(c,Le),t(c,ae),t(ae,Ke),t(c,et),t(c,oe),t(oe,tt),t(c,st),p(e,ye,a),k(B,e,a),p(e,be,a),p(e,x,a),t(x,at),p(e,ve,a),k(N,e,a),p(e,we,a),p(e,Y,a),t(Y,H),p(e,Me,a),p(e,h,a),t(h,ot),t(h,le),t(le,lt),t(h,rt),t(h,re),t(re,it),t(h,nt),t(h,ie),t(ie,pt),t(h,ft),p(e,_e,a),k(R,e,a),p(e,$e,a),p(e,_,a),t(_,mt),t(_,ne),t(ne,ct),t(_,dt),p(e,je,a),k(D,e,a),p(e,Ee,a),p(e,Q,a),t(Q,V),Je=!0},p:Ht,i(e){Je||(U(I.$$.fragment,e),U(Z.$$.fragment,e),U(W.$$.fragment,e),U(q.$$.fragment,e),U(B.$$.fragment,e),U(N.$$.fragment,e),U(R.$$.fragment,e),U(D.$$.fragment,e),Je=!0)},o(e){T(I.$$.fragment,e),T(Z.$$.fragment,e),T(W.$$.fragment,e),T(q.$$.fragment,e),T(B.$$.fragment,e),T(N.$$.fragment,e),T(R.$$.fragment,e),T(D.$$.fragment,e),Je=!1},d(e){s(y),e&&s(pe),e&&s(b),G(I),e&&s(fe),G(Z,e),e&&s(me),e&&s(u),e&&s(ce),e&&s(w),e&&s(de),G(W,e),e&&s(ue),e&&s(M),e&&s(he),G(q,e),e&&s(ge),e&&s(c),e&&s(ye),G(B,e),e&&s(be),e&&s(x),e&&s(ve),G(N,e),e&&s(we),e&&s(Y),e&&s(Me),e&&s(h),e&&s(_e),G(R,e),e&&s($e),e&&s(_),e&&s(je),G(D,e),e&&s(Ee),e&&s(Q)}}}const Xt={local:"improve-image-quality-with-deterministic-generation",title:"Improve image quality with deterministic generation"};function Ot(ut){return Qt(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class ts extends Pt{constructor(y){super();St(this,y,Ot,At,xt,{})}}export{ts as default,Xt as metadata};
