Buckets:

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

Xet Storage Details

Size:
12.3 kB
·
Xet hash:
b1c43c080cde49798bf2882e4231e03bf32adf28375f1696afff89d3441eedc3

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.