Buckets:

rtrm's picture
download
raw
12.3 kB
import{s as $e,o as ve,n as Je}from"../chunks/scheduler.182ea377.js";import{S as ke,i as _e,g as p,s as l,p as y,A as je,h as m,f as s,c as i,j as me,q as b,m as d,k as F,v as X,a,r as w,d as M,t as T,u as $}from"../chunks/index.008d68e4.js";import{T as Ue}from"../chunks/Tip.4f096367.js";import{I as Ze}from"../chunks/IconCopyLink.96bbb92b.js";import{C as E}from"../chunks/CodeBlock.5ed6eb7b.js";import{D as Ie}from"../chunks/DocNotebookDropdown.bb388256.js";function Ce(B){let o,k="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.",c,r,v="For example, if you want to use the same seed to create 4 identical images:",u,h,f;return h=new E({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>)]`}}),{c(){o=p("p"),o.innerHTML=k,c=l(),r=p("p"),r.textContent=v,u=l(),y(h.$$.fragment)},l(n){o=m(n,"P",{"data-svelte-h":!0}),d(o)!=="svelte-1xdwpkl"&&(o.innerHTML=k),c=i(n),r=m(n,"P",{"data-svelte-h":!0}),d(r)!=="svelte-gkh5x9"&&(r.textContent=v),u=i(n),b(h.$$.fragment,n)},m(n,g){a(n,o,g),a(n,c,g),a(n,r,g),a(n,u,g),w(h,n,g),f=!0},p:Je,i(n){f||(M(h.$$.fragment,n),f=!0)},o(n){T(h.$$.fragment,n),f=!1},d(n){n&&(s(o),s(c),s(r),s(u)),$(h,n)}}}function Ge(B){let o,k,c,r,v,u,h,f,n="Improve image quality with deterministic generation",g,_,P,j,ce='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.',S,U,ue='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:',Y,Z,D,I,de='Instantiate a pipeline with <a href="/docs/diffusers/v0.25.0/pt/api/pipelines/overview#diffusers.DiffusionPipeline.from_pretrained">DiffusionPipeline.from_pretrained()</a> and place it on a GPU (if available):',A,C,K,G,he="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:",O,W,ee,J,te,x,fe="Generate the images and have a look:",se,H,ae,V,ge='<img src="https://huggingface.co/datasets/diffusers/diffusers-images-docs/resolve/main/reusabe_seeds.jpg" alt="img"/>',ne,N,ye="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:",le,q,ie,Q,be="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!",oe,L,re,R,we='<img src="https://huggingface.co/datasets/diffusers/diffusers-images-docs/resolve/main/reusabe_seeds_2.jpg" alt="img"/>',pe;return u=new Ze({}),_=new Ie({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/pt/reusing_seeds.ipynb"},{label:"PyTorch",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers_doc/pt/pytorch/reusing_seeds.ipynb"},{label:"TensorFlow",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers_doc/pt/tensorflow/reusing_seeds.ipynb"},{label:"Mixed",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/pt/reusing_seeds.ipynb"},{label:"PyTorch",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/pt/pytorch/reusing_seeds.ipynb"},{label:"TensorFlow",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/pt/tensorflow/reusing_seeds.ipynb"}]}}),Z=new E({props:{code:"cHJvbXB0JTIwJTNEJTIwJTIyTGFicmFkb3IlMjBpbiUyMHRoZSUyMHN0eWxlJTIwb2YlMjBWZXJtZWVyJTIy",highlighted:'prompt = <span class="hljs-string">&quot;Labrador in the style of Vermeer&quot;</span>'}}),C=new E({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>)`}}),W=new E({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>)]'}}),J=new Ue({props:{warning:!0,$$slots:{default:[Ce]},$$scope:{ctx:B}}}),H=new E({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>)`}}),q=new E({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>)]`}}),L=new E({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>)`}}),{c(){o=p("meta"),k=l(),c=p("h1"),r=p("a"),v=p("span"),y(u.$$.fragment),h=l(),f=p("span"),f.textContent=n,g=l(),y(_.$$.fragment),P=l(),j=p("p"),j.innerHTML=ce,S=l(),U=p("p"),U.innerHTML=ue,Y=l(),y(Z.$$.fragment),D=l(),I=p("p"),I.innerHTML=de,A=l(),y(C.$$.fragment),K=l(),G=p("p"),G.innerHTML=he,O=l(),y(W.$$.fragment),ee=l(),y(J.$$.fragment),te=l(),x=p("p"),x.textContent=fe,se=l(),y(H.$$.fragment),ae=l(),V=p("p"),V.innerHTML=ge,ne=l(),N=p("p"),N.innerHTML=ye,le=l(),y(q.$$.fragment),ie=l(),Q=p("p"),Q.innerHTML=be,oe=l(),y(L.$$.fragment),re=l(),R=p("p"),R.innerHTML=we,this.h()},l(e){const t=je("svelte-1phssyn",document.head);o=m(t,"META",{name:!0,content:!0}),t.forEach(s),k=i(e),c=m(e,"H1",{class:!0});var z=me(c);r=m(z,"A",{id:!0,class:!0,href:!0});var Me=me(r);v=m(Me,"SPAN",{});var Te=me(v);b(u.$$.fragment,Te),Te.forEach(s),Me.forEach(s),h=i(z),f=m(z,"SPAN",{"data-svelte-h":!0}),d(f)!=="svelte-16pg2e3"&&(f.textContent=n),z.forEach(s),g=i(e),b(_.$$.fragment,e),P=i(e),j=m(e,"P",{"data-svelte-h":!0}),d(j)!=="svelte-zzbc1w"&&(j.innerHTML=ce),S=i(e),U=m(e,"P",{"data-svelte-h":!0}),d(U)!=="svelte-w9a6jp"&&(U.innerHTML=ue),Y=i(e),b(Z.$$.fragment,e),D=i(e),I=m(e,"P",{"data-svelte-h":!0}),d(I)!=="svelte-898o2n"&&(I.innerHTML=de),A=i(e),b(C.$$.fragment,e),K=i(e),G=m(e,"P",{"data-svelte-h":!0}),d(G)!=="svelte-12jnkti"&&(G.innerHTML=he),O=i(e),b(W.$$.fragment,e),ee=i(e),b(J.$$.fragment,e),te=i(e),x=m(e,"P",{"data-svelte-h":!0}),d(x)!=="svelte-6lev99"&&(x.textContent=fe),se=i(e),b(H.$$.fragment,e),ae=i(e),V=m(e,"P",{"data-svelte-h":!0}),d(V)!=="svelte-ohfhuy"&&(V.innerHTML=ge),ne=i(e),N=m(e,"P",{"data-svelte-h":!0}),d(N)!=="svelte-48m8h7"&&(N.innerHTML=ye),le=i(e),b(q.$$.fragment,e),ie=i(e),Q=m(e,"P",{"data-svelte-h":!0}),d(Q)!=="svelte-1nhypn9"&&(Q.innerHTML=be),oe=i(e),b(L.$$.fragment,e),re=i(e),R=m(e,"P",{"data-svelte-h":!0}),d(R)!=="svelte-ufx7w5"&&(R.innerHTML=we),this.h()},h(){F(o,"name","hf:doc:metadata"),F(o,"content",JSON.stringify(We)),F(r,"id","improve-image-quality-with-deterministic-generation"),F(r,"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"),F(r,"href","#improve-image-quality-with-deterministic-generation"),F(c,"class","relative group")},m(e,t){X(document.head,o),a(e,k,t),a(e,c,t),X(c,r),X(r,v),w(u,v,null),X(c,h),X(c,f),a(e,g,t),w(_,e,t),a(e,P,t),a(e,j,t),a(e,S,t),a(e,U,t),a(e,Y,t),w(Z,e,t),a(e,D,t),a(e,I,t),a(e,A,t),w(C,e,t),a(e,K,t),a(e,G,t),a(e,O,t),w(W,e,t),a(e,ee,t),w(J,e,t),a(e,te,t),a(e,x,t),a(e,se,t),w(H,e,t),a(e,ae,t),a(e,V,t),a(e,ne,t),a(e,N,t),a(e,le,t),w(q,e,t),a(e,ie,t),a(e,Q,t),a(e,oe,t),w(L,e,t),a(e,re,t),a(e,R,t),pe=!0},p(e,[t]){const z={};t&2&&(z.$$scope={dirty:t,ctx:e}),J.$set(z)},i(e){pe||(M(u.$$.fragment,e),M(_.$$.fragment,e),M(Z.$$.fragment,e),M(C.$$.fragment,e),M(W.$$.fragment,e),M(J.$$.fragment,e),M(H.$$.fragment,e),M(q.$$.fragment,e),M(L.$$.fragment,e),pe=!0)},o(e){T(u.$$.fragment,e),T(_.$$.fragment,e),T(Z.$$.fragment,e),T(C.$$.fragment,e),T(W.$$.fragment,e),T(J.$$.fragment,e),T(H.$$.fragment,e),T(q.$$.fragment,e),T(L.$$.fragment,e),pe=!1},d(e){e&&(s(k),s(c),s(g),s(P),s(j),s(S),s(U),s(Y),s(D),s(I),s(A),s(K),s(G),s(O),s(ee),s(te),s(x),s(se),s(ae),s(V),s(ne),s(N),s(le),s(ie),s(Q),s(oe),s(re),s(R)),s(o),$(u),$(_,e),$(Z,e),$(C,e),$(W,e),$(J,e),$(H,e),$(q,e),$(L,e)}}}const We={local:"improve-image-quality-with-deterministic-generation",title:"Improve image quality with deterministic generation"};function xe(B){return ve(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Re extends ke{constructor(o){super(),_e(this,o,xe,Ge,$e,{})}}export{Re as component};

Xet Storage Details

Size:
12.3 kB
·
Xet hash:
fe1c9dd1f39117898844bcb959bfbce00aa55c91a32e84ca189c586978304573

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