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