import{s as ye,o as be,n as we}from"../chunks/scheduler.182ea377.js";import{S as Me,i as Te,g as p,s as l,r as h,A as $e,h as m,f as s,c as i,j as he,u as g,x as u,k as ge,y as Je,a,v as y,d as b,t as w,w as M}from"../chunks/index.abf12888.js";import{T as ve}from"../chunks/Tip.230e2334.js";import{C as q}from"../chunks/CodeBlock.57fe6e13.js";import{D as je}from"../chunks/DocNotebookDropdown.5fa27ace.js";import{H as Ue}from"../chunks/Heading.16916d63.js";function ke(z){let o,j="To create a batched seed, you should use a list comprehension that iterates over the length specified in <code>range()</code>. This creates a unique <code>Generator</code> object for each image in the batch. If you only multiply the <code>Generator</code> by the batch size, this only creates one <code>Generator</code> object that is used sequentially for each image in the batch.",f,c,d="For example, if you want to use the same seed to create 4 identical images:",T,r,$;return r=new q({props:{code:"JUUyJTlEJThDJTIwJTVCdG9yY2guR2VuZXJhdG9yKCkubWFudWFsX3NlZWQoc2VlZCklNUQlMjAqJTIwNCUwQSUwQSVFMiU5QyU4NSUyMCU1QnRvcmNoLkdlbmVyYXRvcigpLm1hbnVhbF9zZWVkKHNlZWQpJTIwZm9yJTIwXyUyMGluJTIwcmFuZ2UoNCklNUQ=",highlighted:`❌ [torch.Generator().manual_seed(seed)] * <span class="hljs-number">4</span>

✅ [torch.Generator().manual_seed(seed) <span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">4</span>)]`,wrap:!1}}),{c(){o=p("p"),o.innerHTML=j,f=l(),c=p("p"),c.textContent=d,T=l(),h(r.$$.fragment)},l(n){o=m(n,"P",{"data-svelte-h":!0}),u(o)!=="svelte-1xdwpkl"&&(o.innerHTML=j),f=i(n),c=m(n,"P",{"data-svelte-h":!0}),u(c)!=="svelte-gkh5x9"&&(c.textContent=d),T=i(n),g(r.$$.fragment,n)},m(n,J){a(n,o,J),a(n,f,J),a(n,c,J),a(n,T,J),y(r,n,J),$=!0},p:we,i(n){$||(b(r.$$.fragment,n),$=!0)},o(n){w(r.$$.fragment,n),$=!1},d(n){n&&(s(o),s(f),s(c),s(T)),M(r,n)}}}function _e(z){let o,j,f,c,d,T,r,$,n,J='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.',F,U,ie='Let’s use <a href="https://huggingface.co/runwayml/stable-diffusion-v1-5" rel="nofollow"><code>runwayml/stable-diffusion-v1-5</code></a> for example, and generate several versions of the following prompt:',X,k,B,_,oe='Instantiate a pipeline with <a href="/docs/diffusers/v0.26.3/en/api/pipelines/overview#diffusers.DiffusionPipeline.from_pretrained">DiffusionPipeline.from_pretrained()</a> and place it on a GPU (if available):',E,Z,P,I,re="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:",Y,G,S,v,D,C,pe="Generate the images and have a look:",A,W,K,H,me='<img src="https://huggingface.co/datasets/diffusers/diffusers-images-docs/resolve/main/reusabe_seeds.jpg" alt="img"/>',O,x,ce="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:",ee,V,te,N,ue="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!",se,Q,ae,R,fe='<img src="https://huggingface.co/datasets/diffusers/diffusers-images-docs/resolve/main/reusabe_seeds_2.jpg" alt="img"/>',ne,L,le;return d=new Ue({props:{title:"Improve image quality with deterministic generation",local:"improve-image-quality-with-deterministic-generation",headingTag:"h1"}}),r=new je({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"}]}}),k=new q({props:{code:"cHJvbXB0JTIwJTNEJTIwJTIyTGFicmFkb3IlMjBpbiUyMHRoZSUyMHN0eWxlJTIwb2YlMjBWZXJtZWVyJTIy",highlighted:'prompt = <span class="hljs-string">&quot;Labrador in the style of Vermeer&quot;</span>',wrap:!1}}),Z=new q({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwRGlmZnVzaW9uUGlwZWxpbmUlMEFmcm9tJTIwZGlmZnVzZXJzLnV0aWxzJTIwaW1wb3J0JTIwbWFrZV9pbWFnZV9ncmlkJTBBJTBBcGlwZSUyMCUzRCUyMERpZmZ1c2lvblBpcGVsaW5lLmZyb21fcHJldHJhaW5lZCglMEElMjAlMjAlMjAlMjAlMjJydW53YXltbCUyRnN0YWJsZS1kaWZmdXNpb24tdjEtNSUyMiUyQyUyMHRvcmNoX2R0eXBlJTNEdG9yY2guZmxvYXQxNiUyQyUyMHVzZV9zYWZldGVuc29ycyUzRFRydWUlMEEpJTBBcGlwZSUyMCUzRCUyMHBpcGUudG8oJTIyY3VkYSUyMik=",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline
<span class="hljs-keyword">from</span> diffusers.utils <span class="hljs-keyword">import</span> make_image_grid

pipe = DiffusionPipeline.from_pretrained(
    <span class="hljs-string">&quot;runwayml/stable-diffusion-v1-5&quot;</span>, torch_dtype=torch.float16, use_safetensors=<span class="hljs-literal">True</span>
)
pipe = pipe.to(<span class="hljs-string">&quot;cuda&quot;</span>)`,wrap:!1}}),G=new q({props:{code:"Z2VuZXJhdG9yJTIwJTNEJTIwJTVCdG9yY2guR2VuZXJhdG9yKGRldmljZSUzRCUyMmN1ZGElMjIpLm1hbnVhbF9zZWVkKGkpJTIwZm9yJTIwaSUyMGluJTIwcmFuZ2UoNCklNUQ=",highlighted:'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}}),v=new ve({props:{warning:!0,$$slots:{default:[ke]},$$scope:{ctx:z}}}),W=new q({props:{code:"aW1hZ2VzJTIwJTNEJTIwcGlwZShwcm9tcHQlMkMlMjBnZW5lcmF0b3IlM0RnZW5lcmF0b3IlMkMlMjBudW1faW1hZ2VzX3Blcl9wcm9tcHQlM0Q0KS5pbWFnZXMlMEFtYWtlX2ltYWdlX2dyaWQoaW1hZ2VzJTJDJTIwcm93cyUzRDIlMkMlMjBjb2xzJTNEMik=",highlighted:`images = pipe(prompt, generator=generator, num_images_per_prompt=<span class="hljs-number">4</span>).images
make_image_grid(images, rows=<span class="hljs-number">2</span>, cols=<span class="hljs-number">2</span>)`,wrap:!1}}),V=new q({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}}),Q=new q({props:{code:"aW1hZ2VzJTIwJTNEJTIwcGlwZShwcm9tcHQlMkMlMjBnZW5lcmF0b3IlM0RnZW5lcmF0b3IpLmltYWdlcyUwQW1ha2VfaW1hZ2VfZ3JpZChpbWFnZXMlMkMlMjByb3dzJTNEMiUyQyUyMGNvbHMlM0QyKQ==",highlighted:`images = pipe(prompt, generator=generator).images
make_image_grid(images, rows=<span class="hljs-number">2</span>, cols=<span class="hljs-number">2</span>)`,wrap:!1}}),{c(){o=p("meta"),j=l(),f=p("p"),c=l(),h(d.$$.fragment),T=l(),h(r.$$.fragment),$=l(),n=p("p"),n.innerHTML=J,F=l(),U=p("p"),U.innerHTML=ie,X=l(),h(k.$$.fragment),B=l(),_=p("p"),_.innerHTML=oe,E=l(),h(Z.$$.fragment),P=l(),I=p("p"),I.innerHTML=re,Y=l(),h(G.$$.fragment),S=l(),h(v.$$.fragment),D=l(),C=p("p"),C.textContent=pe,A=l(),h(W.$$.fragment),K=l(),H=p("p"),H.innerHTML=me,O=l(),x=p("p"),x.innerHTML=ce,ee=l(),h(V.$$.fragment),te=l(),N=p("p"),N.innerHTML=ue,se=l(),h(Q.$$.fragment),ae=l(),R=p("p"),R.innerHTML=fe,ne=l(),L=p("p"),this.h()},l(e){const t=$e("svelte-u9bgzb",document.head);o=m(t,"META",{name:!0,content:!0}),t.forEach(s),j=i(e),f=m(e,"P",{}),he(f).forEach(s),c=i(e),g(d.$$.fragment,e),T=i(e),g(r.$$.fragment,e),$=i(e),n=m(e,"P",{"data-svelte-h":!0}),u(n)!=="svelte-zzbc1w"&&(n.innerHTML=J),F=i(e),U=m(e,"P",{"data-svelte-h":!0}),u(U)!=="svelte-w9a6jp"&&(U.innerHTML=ie),X=i(e),g(k.$$.fragment,e),B=i(e),_=m(e,"P",{"data-svelte-h":!0}),u(_)!=="svelte-evofh8"&&(_.innerHTML=oe),E=i(e),g(Z.$$.fragment,e),P=i(e),I=m(e,"P",{"data-svelte-h":!0}),u(I)!=="svelte-12jnkti"&&(I.innerHTML=re),Y=i(e),g(G.$$.fragment,e),S=i(e),g(v.$$.fragment,e),D=i(e),C=m(e,"P",{"data-svelte-h":!0}),u(C)!=="svelte-6lev99"&&(C.textContent=pe),A=i(e),g(W.$$.fragment,e),K=i(e),H=m(e,"P",{"data-svelte-h":!0}),u(H)!=="svelte-ohfhuy"&&(H.innerHTML=me),O=i(e),x=m(e,"P",{"data-svelte-h":!0}),u(x)!=="svelte-48m8h7"&&(x.innerHTML=ce),ee=i(e),g(V.$$.fragment,e),te=i(e),N=m(e,"P",{"data-svelte-h":!0}),u(N)!=="svelte-1nhypn9"&&(N.innerHTML=ue),se=i(e),g(Q.$$.fragment,e),ae=i(e),R=m(e,"P",{"data-svelte-h":!0}),u(R)!=="svelte-ufx7w5"&&(R.innerHTML=fe),ne=i(e),L=m(e,"P",{}),he(L).forEach(s),this.h()},h(){ge(o,"name","hf:doc:metadata"),ge(o,"content",Ze)},m(e,t){Je(document.head,o),a(e,j,t),a(e,f,t),a(e,c,t),y(d,e,t),a(e,T,t),y(r,e,t),a(e,$,t),a(e,n,t),a(e,F,t),a(e,U,t),a(e,X,t),y(k,e,t),a(e,B,t),a(e,_,t),a(e,E,t),y(Z,e,t),a(e,P,t),a(e,I,t),a(e,Y,t),y(G,e,t),a(e,S,t),y(v,e,t),a(e,D,t),a(e,C,t),a(e,A,t),y(W,e,t),a(e,K,t),a(e,H,t),a(e,O,t),a(e,x,t),a(e,ee,t),y(V,e,t),a(e,te,t),a(e,N,t),a(e,se,t),y(Q,e,t),a(e,ae,t),a(e,R,t),a(e,ne,t),a(e,L,t),le=!0},p(e,[t]){const de={};t&2&&(de.$$scope={dirty:t,ctx:e}),v.$set(de)},i(e){le||(b(d.$$.fragment,e),b(r.$$.fragment,e),b(k.$$.fragment,e),b(Z.$$.fragment,e),b(G.$$.fragment,e),b(v.$$.fragment,e),b(W.$$.fragment,e),b(V.$$.fragment,e),b(Q.$$.fragment,e),le=!0)},o(e){w(d.$$.fragment,e),w(r.$$.fragment,e),w(k.$$.fragment,e),w(Z.$$.fragment,e),w(G.$$.fragment,e),w(v.$$.fragment,e),w(W.$$.fragment,e),w(V.$$.fragment,e),w(Q.$$.fragment,e),le=!1},d(e){e&&(s(j),s(f),s(c),s(T),s($),s(n),s(F),s(U),s(X),s(B),s(_),s(E),s(P),s(I),s(Y),s(S),s(D),s(C),s(A),s(K),s(H),s(O),s(x),s(ee),s(te),s(N),s(se),s(ae),s(R),s(ne),s(L)),s(o),M(d,e),M(r,e),M(k,e),M(Z,e),M(G,e),M(v,e),M(W,e),M(V,e),M(Q,e)}}}const Ze='{"title":"Improve image quality with deterministic generation","local":"improve-image-quality-with-deterministic-generation","sections":[],"depth":1}';function Ie(z){return be(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Ne extends Me{constructor(o){super(),Te(this,o,Ie,_e,ye,{})}}export{Ne as component};
