import{s as ue,n as he,o as ge}from"../chunks/scheduler.182ea377.js";import{S as ye,i as be,g as i,s as n,r as m,A as we,h as o,f as s,c as l,j as fe,u as c,x as p,k as de,y as Me,a,v as f,d,t as u,w as h}from"../chunks/index.abf12888.js";import{C}from"../chunks/CodeBlock.57fe6e13.js";import{D as Te}from"../chunks/DocNotebookDropdown.d9060979.js";import{H as Je}from"../chunks/Heading.16916d63.js";function ve(se){let r,V,H,z,g,R,y,q,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.',B,w,ne='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:',L,M,E,T,le='Instantiate a pipeline with <a href="/docs/diffusers/v0.22.0/en/api/pipelines/overview#diffusers.DiffusionPipeline.from_pretrained">DiffusionPipeline.from_pretrained()</a> and place it on a GPU (if available):',N,J,P,v,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,$,X,j,oe="Generate the images and have a look:",Y,_,F,k,re='<img src="https://huggingface.co/datasets/diffusers/diffusers-images-docs/resolve/main/reusabe_seeds.jpg" alt="img"/>',S,U,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:",D,Z,A,I,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,W,O,G,ce='<img src="https://huggingface.co/datasets/diffusers/diffusers-images-docs/resolve/main/reusabe_seeds_2.jpg" alt="img"/>',ee,x,te;return g=new Je({props:{title:"Improve image quality with deterministic generation",local:"improve-image-quality-with-deterministic-generation",headingTag:"h1"}}),y=new Te({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"}]}}),M=new C({props:{code:"cHJvbXB0JTIwJTNEJTIwJTIyTGFicmFkb3IlMjBpbiUyMHRoZSUyMHN0eWxlJTIwb2YlMjBWZXJtZWVyJTIy",highlighted:'prompt = <span class="hljs-string">&quot;Labrador in the style of Vermeer&quot;</span>',wrap:!1}}),J=new C({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}}),$=new C({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}}),_=new C({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}}),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}}),W=new C({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(){r=i("meta"),V=n(),H=i("p"),z=n(),m(g.$$.fragment),R=n(),m(y.$$.fragment),q=n(),b=i("p"),b.innerHTML=ae,B=n(),w=i("p"),w.innerHTML=ne,L=n(),m(M.$$.fragment),E=n(),T=i("p"),T.innerHTML=le,N=n(),m(J.$$.fragment),P=n(),v=i("p"),v.innerHTML=ie,Q=n(),m($.$$.fragment),X=n(),j=i("p"),j.textContent=oe,Y=n(),m(_.$$.fragment),F=n(),k=i("p"),k.innerHTML=re,S=n(),U=i("p"),U.innerHTML=pe,D=n(),m(Z.$$.fragment),A=n(),I=i("p"),I.innerHTML=me,K=n(),m(W.$$.fragment),O=n(),G=i("p"),G.innerHTML=ce,ee=n(),x=i("p"),this.h()},l(e){const t=we("svelte-u9bgzb",document.head);r=o(t,"META",{name:!0,content:!0}),t.forEach(s),V=l(e),H=o(e,"P",{}),fe(H).forEach(s),z=l(e),c(g.$$.fragment,e),R=l(e),c(y.$$.fragment,e),q=l(e),b=o(e,"P",{"data-svelte-h":!0}),p(b)!=="svelte-zzbc1w"&&(b.innerHTML=ae),B=l(e),w=o(e,"P",{"data-svelte-h":!0}),p(w)!=="svelte-w9a6jp"&&(w.innerHTML=ne),L=l(e),c(M.$$.fragment,e),E=l(e),T=o(e,"P",{"data-svelte-h":!0}),p(T)!=="svelte-15v4daz"&&(T.innerHTML=le),N=l(e),c(J.$$.fragment,e),P=l(e),v=o(e,"P",{"data-svelte-h":!0}),p(v)!=="svelte-12jnkti"&&(v.innerHTML=ie),Q=l(e),c($.$$.fragment,e),X=l(e),j=o(e,"P",{"data-svelte-h":!0}),p(j)!=="svelte-6lev99"&&(j.textContent=oe),Y=l(e),c(_.$$.fragment,e),F=l(e),k=o(e,"P",{"data-svelte-h":!0}),p(k)!=="svelte-ohfhuy"&&(k.innerHTML=re),S=l(e),U=o(e,"P",{"data-svelte-h":!0}),p(U)!=="svelte-48m8h7"&&(U.innerHTML=pe),D=l(e),c(Z.$$.fragment,e),A=l(e),I=o(e,"P",{"data-svelte-h":!0}),p(I)!=="svelte-1nhypn9"&&(I.innerHTML=me),K=l(e),c(W.$$.fragment,e),O=l(e),G=o(e,"P",{"data-svelte-h":!0}),p(G)!=="svelte-ufx7w5"&&(G.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",$e)},m(e,t){Me(document.head,r),a(e,V,t),a(e,H,t),a(e,z,t),f(g,e,t),a(e,R,t),f(y,e,t),a(e,q,t),a(e,b,t),a(e,B,t),a(e,w,t),a(e,L,t),f(M,e,t),a(e,E,t),a(e,T,t),a(e,N,t),f(J,e,t),a(e,P,t),a(e,v,t),a(e,Q,t),f($,e,t),a(e,X,t),a(e,j,t),a(e,Y,t),f(_,e,t),a(e,F,t),a(e,k,t),a(e,S,t),a(e,U,t),a(e,D,t),f(Z,e,t),a(e,A,t),a(e,I,t),a(e,K,t),f(W,e,t),a(e,O,t),a(e,G,t),a(e,ee,t),a(e,x,t),te=!0},p:he,i(e){te||(d(g.$$.fragment,e),d(y.$$.fragment,e),d(M.$$.fragment,e),d(J.$$.fragment,e),d($.$$.fragment,e),d(_.$$.fragment,e),d(Z.$$.fragment,e),d(W.$$.fragment,e),te=!0)},o(e){u(g.$$.fragment,e),u(y.$$.fragment,e),u(M.$$.fragment,e),u(J.$$.fragment,e),u($.$$.fragment,e),u(_.$$.fragment,e),u(Z.$$.fragment,e),u(W.$$.fragment,e),te=!1},d(e){e&&(s(V),s(H),s(z),s(R),s(q),s(b),s(B),s(w),s(L),s(E),s(T),s(N),s(P),s(v),s(Q),s(X),s(j),s(Y),s(F),s(k),s(S),s(U),s(D),s(A),s(I),s(K),s(O),s(G),s(ee),s(x)),s(r),h(g,e),h(y,e),h(M,e),h(J,e),h($,e),h(_,e),h(Z,e),h(W,e)}}}const $e='{"title":"Improve image quality with deterministic generation","local":"improve-image-quality-with-deterministic-generation","sections":[],"depth":1}';function je(se){return ge(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class We extends ye{constructor(r){super(),be(this,r,je,ve,ue,{})}}export{We as component};
