Buckets:

hf-doc-build/doc / diffusers /v0.16.0 /en /_app /pages /using-diffusers /img2img.mdx-hf-doc-builder.js
rtrm's picture
download
raw
13.8 kB
import{S as yt,i as kt,s as It,e as o,k as f,w as j,t as n,M as Dt,c as r,d as t,m,a as l,x as P,h as p,b as u,N as Ge,G as i,g as a,y as x,q as G,o as q,B as M,v as Et}from"../../chunks/vendor-hf-doc-builder.js";import{T as St}from"../../chunks/Tip-hf-doc-builder.js";import{I as jt}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as re}from"../../chunks/CodeBlock-hf-doc-builder.js";import{D as Pt}from"../../chunks/DocNotebookDropdown-hf-doc-builder.js";function xt(le){let c,y,g,d,$;return{c(){c=o("p"),y=n("\u{1F4A1} "),g=o("code"),d=n("strength"),$=n(" is a value between 0.0 and 1.0 that controls the amount of noise added to the input image. Values that approach 1.0 allow for lots of variations but will also produce images that are not semantically consistent with the input.")},l(h){c=r(h,"P",{});var b=l(c);y=p(b,"\u{1F4A1} "),g=r(b,"CODE",{});var k=l(g);d=p(k,"strength"),k.forEach(t),$=p(b," is a value between 0.0 and 1.0 that controls the amount of noise added to the input image. Values that approach 1.0 allow for lots of variations but will also produce images that are not semantically consistent with the input."),b.forEach(t)},m(h,b){a(h,c,b),i(c,y),i(c,g),i(g,d),i(c,$)},d(h){h&&t(c)}}}function Gt(le){let c,y,g,d,$,h,b,k,qe,ne,C,pe,I,Me,z,Ce,Te,fe,H,Le,me,T,ue,_,Be,J,Oe,Ae,L,Z,Ve,Ne,ce,B,ge,U,Ye,he,O,de,A,ee,tt,be,D,_e,E,Fe,te,Re,ze,ve,V,we,N,se,st,$e,X,He,ye,Y,ke,F,ie,it,Ie,v,Je,ae,Ue,Xe,oe,Ke,Qe,De,S,We,K,Ze,et,Ee,w,at,Se;return h=new jt({}),C=new Pt({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/img2img.ipynb"},{label:"PyTorch",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers_doc/en/pytorch/img2img.ipynb"},{label:"TensorFlow",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers_doc/en/tensorflow/img2img.ipynb"},{label:"Mixed",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/en/img2img.ipynb"},{label:"PyTorch",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/en/pytorch/img2img.ipynb"},{label:"TensorFlow",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/en/tensorflow/img2img.ipynb"}]}}),T=new re({props:{code:"!pip install diffusers transformers ftfy accelerate",highlighted:"!pip install diffusers transformers ftfy accelerate"}}),B=new re({props:{code:`import torch
import requests
from PIL import Image
from io import BytesIO
from diffusers import StableDiffusionImg2ImgPipeline
device = "cuda"
pipe = StableDiffusionImg2ImgPipeline.from_pretrained("nitrosocke/Ghibli-Diffusion", torch_dtype=torch.float16).to(
device
)`,highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">import</span> requests
<span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image
<span class="hljs-keyword">from</span> io <span class="hljs-keyword">import</span> BytesIO
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> StableDiffusionImg2ImgPipeline
device = <span class="hljs-string">&quot;cuda&quot;</span>
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(<span class="hljs-string">&quot;nitrosocke/Ghibli-Diffusion&quot;</span>, torch_dtype=torch.float16).to(
device
)`}}),O=new re({props:{code:`url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"
response = requests.get(url)
init_image = Image.open(BytesIO(response.content)).convert("RGB")
init_image.thumbnail((768, 768))
init_image`,highlighted:`url = <span class="hljs-string">&quot;https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg&quot;</span>
response = requests.get(url)
init_image = Image.<span class="hljs-built_in">open</span>(BytesIO(response.content)).convert(<span class="hljs-string">&quot;RGB&quot;</span>)
init_image.thumbnail((<span class="hljs-number">768</span>, <span class="hljs-number">768</span>))
init_image`}}),D=new St({props:{$$slots:{default:[xt]},$$scope:{ctx:le}}}),V=new re({props:{code:`prompt = "ghibli style, a fantasy landscape with castles"
generator = torch.Generator(device=device).manual_seed(1024)
image = pipe(prompt=prompt, image=init_image, strength=0.75, guidance_scale=7.5, generator=generator).images[0]
image`,highlighted:`prompt = <span class="hljs-string">&quot;ghibli style, a fantasy landscape with castles&quot;</span>
generator = torch.Generator(device=device).manual_seed(<span class="hljs-number">1024</span>)
image = pipe(prompt=prompt, image=init_image, strength=<span class="hljs-number">0.75</span>, guidance_scale=<span class="hljs-number">7.5</span>, generator=generator).images[<span class="hljs-number">0</span>]
image`}}),Y=new re({props:{code:`from diffusers import LMSDiscreteScheduler
lms = LMSDiscreteScheduler.from_config(pipe.scheduler.config)
pipe.scheduler = lms
generator = torch.Generator(device=device).manual_seed(1024)
image = pipe(prompt=prompt, image=init_image, strength=0.75, guidance_scale=7.5, generator=generator).images[0]
image`,highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> LMSDiscreteScheduler
lms = LMSDiscreteScheduler.from_config(pipe.scheduler.config)
pipe.scheduler = lms
generator = torch.Generator(device=device).manual_seed(<span class="hljs-number">1024</span>)
image = pipe(prompt=prompt, image=init_image, strength=<span class="hljs-number">0.75</span>, guidance_scale=<span class="hljs-number">7.5</span>, generator=generator).images[<span class="hljs-number">0</span>]
image`}}),{c(){c=o("meta"),y=f(),g=o("h1"),d=o("a"),$=o("span"),j(h.$$.fragment),b=f(),k=o("span"),qe=n("Text-guided image-to-image generation"),ne=f(),j(C.$$.fragment),pe=f(),I=o("p"),Me=n("The "),z=o("a"),Ce=n("StableDiffusionImg2ImgPipeline"),Te=n(" lets you pass a text prompt and an initial image to condition the generation of new images."),fe=f(),H=o("p"),Le=n("Before you begin, make sure you have all the necessary libraries installed:"),me=f(),j(T.$$.fragment),ue=f(),_=o("p"),Be=n("Get started by creating a "),J=o("a"),Oe=n("StableDiffusionImg2ImgPipeline"),Ae=n(" with a pretrained Stable Diffusion model like "),L=o("a"),Z=o("code"),Ve=n("nitrosocke/Ghibli-Diffusion"),Ne=n("."),ce=f(),j(B.$$.fragment),ge=f(),U=o("p"),Ye=n("Download and preprocess an initial image so you can pass it to the pipeline:"),he=f(),j(O.$$.fragment),de=f(),A=o("div"),ee=o("img"),be=f(),j(D.$$.fragment),_e=f(),E=o("p"),Fe=n("Define the prompt (for this checkpoint finetuned on Ghibli-style art, you need to prefix the prompt with the "),te=o("code"),Re=n("ghibli style"),ze=n(" tokens) and run the pipeline:"),ve=f(),j(V.$$.fragment),we=f(),N=o("div"),se=o("img"),$e=f(),X=o("p"),He=n("You can also try experimenting with a different scheduler to see how that affects the output:"),ye=f(),j(Y.$$.fragment),ke=f(),F=o("div"),ie=o("img"),Ie=f(),v=o("p"),Je=n("Check out the Spaces below, and try generating images with different values for "),ae=o("code"),Ue=n("strength"),Xe=n(". You\u2019ll notice that using lower values for "),oe=o("code"),Ke=n("strength"),Qe=n(" produces images that are more similar to the original image."),De=f(),S=o("p"),We=n("Feel free to also switch the scheduler to the "),K=o("a"),Ze=n("LMSDiscreteScheduler"),et=n(" and see how that affects the output."),Ee=f(),w=o("iframe"),this.h()},l(e){const s=Dt('[data-svelte="svelte-1phssyn"]',document.head);c=r(s,"META",{name:!0,content:!0}),s.forEach(t),y=m(e),g=r(e,"H1",{class:!0});var R=l(g);d=r(R,"A",{id:!0,class:!0,href:!0});var ot=l(d);$=r(ot,"SPAN",{});var rt=l($);P(h.$$.fragment,rt),rt.forEach(t),ot.forEach(t),b=m(R),k=r(R,"SPAN",{});var lt=l(k);qe=p(lt,"Text-guided image-to-image generation"),lt.forEach(t),R.forEach(t),ne=m(e),P(C.$$.fragment,e),pe=m(e),I=r(e,"P",{});var je=l(I);Me=p(je,"The "),z=r(je,"A",{href:!0});var nt=l(z);Ce=p(nt,"StableDiffusionImg2ImgPipeline"),nt.forEach(t),Te=p(je," lets you pass a text prompt and an initial image to condition the generation of new images."),je.forEach(t),fe=m(e),H=r(e,"P",{});var pt=l(H);Le=p(pt,"Before you begin, make sure you have all the necessary libraries installed:"),pt.forEach(t),me=m(e),P(T.$$.fragment,e),ue=m(e),_=r(e,"P",{});var Q=l(_);Be=p(Q,"Get started by creating a "),J=r(Q,"A",{href:!0});var ft=l(J);Oe=p(ft,"StableDiffusionImg2ImgPipeline"),ft.forEach(t),Ae=p(Q," with a pretrained Stable Diffusion model like "),L=r(Q,"A",{href:!0,rel:!0});var mt=l(L);Z=r(mt,"CODE",{});var ut=l(Z);Ve=p(ut,"nitrosocke/Ghibli-Diffusion"),ut.forEach(t),mt.forEach(t),Ne=p(Q,"."),Q.forEach(t),ce=m(e),P(B.$$.fragment,e),ge=m(e),U=r(e,"P",{});var ct=l(U);Ye=p(ct,"Download and preprocess an initial image so you can pass it to the pipeline:"),ct.forEach(t),he=m(e),P(O.$$.fragment,e),de=m(e),A=r(e,"DIV",{class:!0});var gt=l(A);ee=r(gt,"IMG",{src:!0}),gt.forEach(t),be=m(e),P(D.$$.fragment,e),_e=m(e),E=r(e,"P",{});var Pe=l(E);Fe=p(Pe,"Define the prompt (for this checkpoint finetuned on Ghibli-style art, you need to prefix the prompt with the "),te=r(Pe,"CODE",{});var ht=l(te);Re=p(ht,"ghibli style"),ht.forEach(t),ze=p(Pe," tokens) and run the pipeline:"),Pe.forEach(t),ve=m(e),P(V.$$.fragment,e),we=m(e),N=r(e,"DIV",{class:!0});var dt=l(N);se=r(dt,"IMG",{src:!0}),dt.forEach(t),$e=m(e),X=r(e,"P",{});var bt=l(X);He=p(bt,"You can also try experimenting with a different scheduler to see how that affects the output:"),bt.forEach(t),ye=m(e),P(Y.$$.fragment,e),ke=m(e),F=r(e,"DIV",{class:!0});var _t=l(F);ie=r(_t,"IMG",{src:!0}),_t.forEach(t),Ie=m(e),v=r(e,"P",{});var W=l(v);Je=p(W,"Check out the Spaces below, and try generating images with different values for "),ae=r(W,"CODE",{});var vt=l(ae);Ue=p(vt,"strength"),vt.forEach(t),Xe=p(W,". You\u2019ll notice that using lower values for "),oe=r(W,"CODE",{});var wt=l(oe);Ke=p(wt,"strength"),wt.forEach(t),Qe=p(W," produces images that are more similar to the original image."),W.forEach(t),De=m(e),S=r(e,"P",{});var xe=l(S);We=p(xe,"Feel free to also switch the scheduler to the "),K=r(xe,"A",{href:!0});var $t=l(K);Ze=p($t,"LMSDiscreteScheduler"),$t.forEach(t),et=p(xe," and see how that affects the output."),xe.forEach(t),Ee=m(e),w=r(e,"IFRAME",{src:!0,frameborder:!0,width:!0,height:!0}),l(w).forEach(t),this.h()},h(){u(c,"name","hf:doc:metadata"),u(c,"content",JSON.stringify(qt)),u(d,"id","textguided-imagetoimage-generation"),u(d,"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"),u(d,"href","#textguided-imagetoimage-generation"),u(g,"class","relative group"),u(z,"href","/docs/diffusers/v0.16.0/en/api/pipelines/stable_diffusion/img2img#diffusers.StableDiffusionImg2ImgPipeline"),u(J,"href","/docs/diffusers/v0.16.0/en/api/pipelines/stable_diffusion/img2img#diffusers.StableDiffusionImg2ImgPipeline"),u(L,"href","https://huggingface.co/nitrosocke/Ghibli-Diffusion"),u(L,"rel","nofollow"),Ge(ee.src,tt="https://huggingface.co/datasets/YiYiXu/test-doc-assets/resolve/main/image_2_image_using_diffusers_cell_8_output_0.jpeg")||u(ee,"src",tt),u(A,"class","flex justify-center"),Ge(se.src,st="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/ghibli-castles.png")||u(se,"src",st),u(N,"class","flex justify-center"),Ge(ie.src,it="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/lms-ghibli.png")||u(ie,"src",it),u(F,"class","flex justify-center"),u(K,"href","/docs/diffusers/v0.16.0/en/api/schedulers/lms_discrete#diffusers.LMSDiscreteScheduler"),Ge(w.src,at="https://stevhliu-ghibli-img2img.hf.space")||u(w,"src",at),u(w,"frameborder","0"),u(w,"width","850"),u(w,"height","500")},m(e,s){i(document.head,c),a(e,y,s),a(e,g,s),i(g,d),i(d,$),x(h,$,null),i(g,b),i(g,k),i(k,qe),a(e,ne,s),x(C,e,s),a(e,pe,s),a(e,I,s),i(I,Me),i(I,z),i(z,Ce),i(I,Te),a(e,fe,s),a(e,H,s),i(H,Le),a(e,me,s),x(T,e,s),a(e,ue,s),a(e,_,s),i(_,Be),i(_,J),i(J,Oe),i(_,Ae),i(_,L),i(L,Z),i(Z,Ve),i(_,Ne),a(e,ce,s),x(B,e,s),a(e,ge,s),a(e,U,s),i(U,Ye),a(e,he,s),x(O,e,s),a(e,de,s),a(e,A,s),i(A,ee),a(e,be,s),x(D,e,s),a(e,_e,s),a(e,E,s),i(E,Fe),i(E,te),i(te,Re),i(E,ze),a(e,ve,s),x(V,e,s),a(e,we,s),a(e,N,s),i(N,se),a(e,$e,s),a(e,X,s),i(X,He),a(e,ye,s),x(Y,e,s),a(e,ke,s),a(e,F,s),i(F,ie),a(e,Ie,s),a(e,v,s),i(v,Je),i(v,ae),i(ae,Ue),i(v,Xe),i(v,oe),i(oe,Ke),i(v,Qe),a(e,De,s),a(e,S,s),i(S,We),i(S,K),i(K,Ze),i(S,et),a(e,Ee,s),a(e,w,s),Se=!0},p(e,[s]){const R={};s&2&&(R.$$scope={dirty:s,ctx:e}),D.$set(R)},i(e){Se||(G(h.$$.fragment,e),G(C.$$.fragment,e),G(T.$$.fragment,e),G(B.$$.fragment,e),G(O.$$.fragment,e),G(D.$$.fragment,e),G(V.$$.fragment,e),G(Y.$$.fragment,e),Se=!0)},o(e){q(h.$$.fragment,e),q(C.$$.fragment,e),q(T.$$.fragment,e),q(B.$$.fragment,e),q(O.$$.fragment,e),q(D.$$.fragment,e),q(V.$$.fragment,e),q(Y.$$.fragment,e),Se=!1},d(e){t(c),e&&t(y),e&&t(g),M(h),e&&t(ne),M(C,e),e&&t(pe),e&&t(I),e&&t(fe),e&&t(H),e&&t(me),M(T,e),e&&t(ue),e&&t(_),e&&t(ce),M(B,e),e&&t(ge),e&&t(U),e&&t(he),M(O,e),e&&t(de),e&&t(A),e&&t(be),M(D,e),e&&t(_e),e&&t(E),e&&t(ve),M(V,e),e&&t(we),e&&t(N),e&&t($e),e&&t(X),e&&t(ye),M(Y,e),e&&t(ke),e&&t(F),e&&t(Ie),e&&t(v),e&&t(De),e&&t(S),e&&t(Ee),e&&t(w)}}}const qt={local:"textguided-imagetoimage-generation",title:"Text-guided image-to-image generation"};function Mt(le){return Et(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class At extends yt{constructor(c){super();kt(this,c,Mt,Gt,It,{})}}export{At as default,qt as metadata};

Xet Storage Details

Size:
13.8 kB
·
Xet hash:
3fe858e50a4b224340c8baae056d09d6a233931f8ec4c2f0ff57cb845d1d1f86

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