import{s as ge,n as ue,o as he}from"../chunks/scheduler.182ea377.js";import{S as ye,i as be,g as i,s as n,r as m,A as Me,h as o,f as s,c as l,j as fe,u as c,x as p,k as de,y as we,a,v as f,d,t as g,w as u}from"../chunks/index.abf12888.js";import{C}from"../chunks/CodeBlock.57fe6e13.js";import{D as ve}from"../chunks/DocNotebookDropdown.d9060979.js";import{H as $e}from"../chunks/Heading.16916d63.js";function je(se){let r,B,G,q,h,R,y,E,b,ae='A common way to improve the quality of generated images is with <em>deterministic batch generation</em>, 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 <a href="https://pytorch.org/docs/stable/generated/torch.Generator.html#generator" rel="nofollow"><code>torch.Generator</code></a>’s to the pipeline for batched image generation, and tie each <code>Generator</code> to a seed so you can reuse it for an image.',L,M,ne='Let’s use <a href="runwayml/stable-diffusion-v1-5"><code>runwayml/stable-diffusion-v1-5</code></a> for example, and generate several versions of the following prompt:',P,w,Y,v,le='Instantiate a pipeline with <a href="/docs/diffusers/main/en/api/pipelines/overview#diffusers.DiffusionPipeline.from_pretrained">DiffusionPipeline.from_pretrained()</a> and place it on a GPU (if available):',N,$,S,j,ie="Now, define four different <code>Generator</code>’s and assign each <code>Generator</code> a seed (<code>0</code> to <code>3</code>) so you can reuse a <code>Generator</code> later for a specific image:",Q,T,V,J,oe="Generate the images and have a look:",F,U,X,_,re='<img src="https://huggingface.co/datasets/diffusers/diffusers-images-docs/resolve/main/reusabe_seeds.jpg" alt="img"/>',z,k,pe="In this example, you’ll 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 <code>Generator</code> with seed <code>0</code>, so you’ll reuse that <code>Generator</code> for the second round of inference. To improve the quality of the image, add some additional text to the prompt:",A,Z,D,W,me="Create four generators with seed <code>0</code>, and generate another batch of images, all of which should look like the first image from the previous round!",K,H,O,I,ce='<img src="https://huggingface.co/datasets/diffusers/diffusers-images-docs/resolve/main/reusabe_seeds_2.jpg" alt="img"/>',ee,x,te;return h=new $e({props:{title:"Improve image quality with deterministic generation",local:"improve-image-quality-with-deterministic-generation",headingTag:"h1"}}),y=new ve({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 C({props:{code:"cHJvbXB0JTIwJTNEJTIwJTIyTGFicmFkb3IlMjBpbiUyMHRoZSUyMHN0eWxlJTIwb2YlMjBWZXJtZWVyJTIy",highlighted:'prompt = <span class="hljs-string">&quot;Labrador in the style of Vermeer&quot;</span>',wrap:!1}}),$=new C({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>)`,wrap:!1}}),T=new C({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>)]`,wrap:!1}}),U=new C({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`,wrap:!1}}),Z=new C({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>)]`,wrap:!1}}),H=new C({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`,wrap:!1}}),{c(){r=i("meta"),B=n(),G=i("p"),q=n(),m(h.$$.fragment),R=n(),m(y.$$.fragment),E=n(),b=i("p"),b.innerHTML=ae,L=n(),M=i("p"),M.innerHTML=ne,P=n(),m(w.$$.fragment),Y=n(),v=i("p"),v.innerHTML=le,N=n(),m($.$$.fragment),S=n(),j=i("p"),j.innerHTML=ie,Q=n(),m(T.$$.fragment),V=n(),J=i("p"),J.textContent=oe,F=n(),m(U.$$.fragment),X=n(),_=i("p"),_.innerHTML=re,z=n(),k=i("p"),k.innerHTML=pe,A=n(),m(Z.$$.fragment),D=n(),W=i("p"),W.innerHTML=me,K=n(),m(H.$$.fragment),O=n(),I=i("p"),I.innerHTML=ce,ee=n(),x=i("p"),this.h()},l(e){const t=Me("svelte-u9bgzb",document.head);r=o(t,"META",{name:!0,content:!0}),t.forEach(s),B=l(e),G=o(e,"P",{}),fe(G).forEach(s),q=l(e),c(h.$$.fragment,e),R=l(e),c(y.$$.fragment,e),E=l(e),b=o(e,"P",{"data-svelte-h":!0}),p(b)!=="svelte-zzbc1w"&&(b.innerHTML=ae),L=l(e),M=o(e,"P",{"data-svelte-h":!0}),p(M)!=="svelte-1d03f5x"&&(M.innerHTML=ne),P=l(e),c(w.$$.fragment,e),Y=l(e),v=o(e,"P",{"data-svelte-h":!0}),p(v)!=="svelte-z2y7lo"&&(v.innerHTML=le),N=l(e),c($.$$.fragment,e),S=l(e),j=o(e,"P",{"data-svelte-h":!0}),p(j)!=="svelte-1fiadvn"&&(j.innerHTML=ie),Q=l(e),c(T.$$.fragment,e),V=l(e),J=o(e,"P",{"data-svelte-h":!0}),p(J)!=="svelte-6lev99"&&(J.textContent=oe),F=l(e),c(U.$$.fragment,e),X=l(e),_=o(e,"P",{"data-svelte-h":!0}),p(_)!=="svelte-ohfhuy"&&(_.innerHTML=re),z=l(e),k=o(e,"P",{"data-svelte-h":!0}),p(k)!=="svelte-48m8h7"&&(k.innerHTML=pe),A=l(e),c(Z.$$.fragment,e),D=l(e),W=o(e,"P",{"data-svelte-h":!0}),p(W)!=="svelte-1nhypn9"&&(W.innerHTML=me),K=l(e),c(H.$$.fragment,e),O=l(e),I=o(e,"P",{"data-svelte-h":!0}),p(I)!=="svelte-ufx7w5"&&(I.innerHTML=ce),ee=l(e),x=o(e,"P",{}),fe(x).forEach(s),this.h()},h(){de(r,"name","hf:doc:metadata"),de(r,"content",Te)},m(e,t){we(document.head,r),a(e,B,t),a(e,G,t),a(e,q,t),f(h,e,t),a(e,R,t),f(y,e,t),a(e,E,t),a(e,b,t),a(e,L,t),a(e,M,t),a(e,P,t),f(w,e,t),a(e,Y,t),a(e,v,t),a(e,N,t),f($,e,t),a(e,S,t),a(e,j,t),a(e,Q,t),f(T,e,t),a(e,V,t),a(e,J,t),a(e,F,t),f(U,e,t),a(e,X,t),a(e,_,t),a(e,z,t),a(e,k,t),a(e,A,t),f(Z,e,t),a(e,D,t),a(e,W,t),a(e,K,t),f(H,e,t),a(e,O,t),a(e,I,t),a(e,ee,t),a(e,x,t),te=!0},p:ue,i(e){te||(d(h.$$.fragment,e),d(y.$$.fragment,e),d(w.$$.fragment,e),d($.$$.fragment,e),d(T.$$.fragment,e),d(U.$$.fragment,e),d(Z.$$.fragment,e),d(H.$$.fragment,e),te=!0)},o(e){g(h.$$.fragment,e),g(y.$$.fragment,e),g(w.$$.fragment,e),g($.$$.fragment,e),g(T.$$.fragment,e),g(U.$$.fragment,e),g(Z.$$.fragment,e),g(H.$$.fragment,e),te=!1},d(e){e&&(s(B),s(G),s(q),s(R),s(E),s(b),s(L),s(M),s(P),s(Y),s(v),s(N),s(S),s(j),s(Q),s(V),s(J),s(F),s(X),s(_),s(z),s(k),s(A),s(D),s(W),s(K),s(O),s(I),s(ee),s(x)),s(r),u(h,e),u(y,e),u(w,e),u($,e),u(T,e),u(U,e),u(Z,e),u(H,e)}}}const Te='{"title":"Improve image quality with deterministic generation","local":"improve-image-quality-with-deterministic-generation","sections":[],"depth":1}';function Je(se){return he(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class He extends ye{constructor(r){super(),be(this,r,Je,je,ge,{})}}export{He as component};
