Buckets:
| # Gambar-ke-gambar | |
| Image-to-image mirip dengan [text-to-image](conditional_image_generation), tetapi selain prompt, Anda juga dapat memberikan gambar awal sebagai titik awal untuk proses difusi. Gambar awal tersebut dienkod ke ruang laten dan ditanami noise. Kemudian, model latent diffusion mengambil prompt dan gambar laten yang ber-noise, memprediksi noise yang ditambahkan, dan menghapus noise yang diprediksi dari gambar laten awal untuk mendapatkan gambar laten baru. Terakhir, sebuah decoder mengodekan kembali gambar laten baru tersebut menjadi gambar. | |
| Dengan ๐ค Diffusers, ini semudah 1-2-3: | |
| 1. Muat checkpoint ke dalam kelas [AutoPipelineForImage2Image](/docs/diffusers/main/id/api/pipelines/auto_pipeline#diffusers.AutoPipelineForImage2Image); pipeline ini secara otomatis menangani pemuatan kelas pipeline yang benar berdasarkan checkpoint: | |
| ```py | |
| import torch | |
| from diffusers import AutoPipelineForImage2Image | |
| from diffusers.utils import load_image, make_image_grid | |
| pipeline = AutoPipelineForImage2Image.from_pretrained( | |
| "kandinsky-community/kandinsky-2-2-decoder", dtype=torch.float16, use_safetensors=True | |
| ) | |
| pipeline.enable_model_cpu_offload() | |
| # remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed | |
| pipeline.enable_xformers_memory_efficient_attention() | |
| ``` | |
| > [!TIP] | |
| > Sepanjang panduan ini, Anda akan melihat kami menggunakan [enable_model_cpu_offload()](/docs/diffusers/main/id/api/pipelines/overview#diffusers.DiffusionPipeline.enable_model_cpu_offload) dan [enable_xformers_memory_efficient_attention()](/docs/diffusers/main/id/api/pipelines/overview#diffusers.DiffusionPipeline.enable_xformers_memory_efficient_attention) untuk menghemat memori dan meningkatkan kecepatan inferensi. Jika Anda menggunakan PyTorch 2.0, Anda tidak perlu memanggil [enable_xformers_memory_efficient_attention()](/docs/diffusers/main/id/api/pipelines/overview#diffusers.DiffusionPipeline.enable_xformers_memory_efficient_attention) pada pipeline Anda karena pipeline tersebut sudah menggunakan [scaled-dot product attention](../optimization/fp16#scaled-dot-product-attention) bawaan dari PyTorch 2.0. | |
| 2. Muat gambar untuk diteruskan ke pipeline: | |
| ```py | |
| init_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/cat.png") | |
| ``` | |
| 3. Lewatkan prompt dan gambar ke pipeline untuk menghasilkan gambar: | |
| ```py | |
| prompt = "cat wizard, gandalf, lord of the rings, detailed, fantasy, cute, adorable, Pixar, Disney, 8k" | |
| image = pipeline(prompt, image=init_image).images[0] | |
| make_image_grid([init_image, image], rows=1, cols=2) | |
| ``` | |
| gambar awal | |
| gambar yang dihasilkan | |
| ## Model populer | |
| Model image-to-image yang paling populer adalah [Stable Diffusion v1.5](https://huggingface.co/stable-diffusion-v1-5/stable-diffusion-v1-5), [Stable Diffusion XL (SDXL)](https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0), dan [Kandinsky 2.2](https://huggingface.co/kandinsky-community/kandinsky-2-2-decoder). Hasil dari model Stable Diffusion dan Kandinsky bervariasi karena perbedaan arsitektur dan proses pelatihan; secara umum, Anda dapat mengharapkan SDXL menghasilkan gambar dengan kualitas yang lebih tinggi daripada Stable Diffusion v1.5. Mari kita lihat secara singkat cara menggunakan masing-masing model ini dan membandingkan hasilnya. | |
| ### Stable Diffusion v1.5 | |
| Stable Diffusion v1.5 adalah model difusi laten yang diinisialisasi dari checkpoint sebelumnya, lalu ditala ulang selama 595K langkah pada gambar 512x512. Untuk menggunakan pipeline ini untuk image-to-image, Anda perlu menyiapkan gambar awal untuk diteruskan ke pipeline. Kemudian Anda dapat meneruskan prompt dan gambar ke pipeline untuk menghasilkan gambar baru: | |
| ```py | |
| import torch | |
| from diffusers import AutoPipelineForImage2Image | |
| from diffusers.utils import make_image_grid, load_image | |
| pipeline = AutoPipelineForImage2Image.from_pretrained( | |
| "stable-diffusion-v1-5/stable-diffusion-v1-5", dtype=torch.float16, variant="fp16", use_safetensors=True | |
| ) | |
| pipeline.enable_model_cpu_offload() | |
| # remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed | |
| pipeline.enable_xformers_memory_efficient_attention() | |
| # prepare image | |
| url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img-init.png" | |
| init_image = load_image(url) | |
| prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" | |
| # pass prompt and image to pipeline | |
| image = pipeline(prompt, image=init_image).images[0] | |
| make_image_grid([init_image, image], rows=1, cols=2) | |
| ``` | |
| gambar awal | |
| gambar yang dihasilkan | |
| ### Stable Diffusion XL (SDXL) | |
| SDXL adalah versi yang lebih kuat dari model Stable Diffusion. Model ini menggunakan model dasar yang lebih besar, serta model refiner tambahan untuk meningkatkan kualitas output dari model dasar. Baca panduan [SDXL](../api/pipelines/stable_diffusion/stable_diffusion_xl) untuk penjelasan lebih rinci tentang cara menggunakan model ini, serta teknik lain yang digunakan untuk menghasilkan gambar berkualitas tinggi. | |
| ```py | |
| import torch | |
| from diffusers import AutoPipelineForImage2Image | |
| from diffusers.utils import make_image_grid, load_image | |
| pipeline = AutoPipelineForImage2Image.from_pretrained( | |
| "stabilityai/stable-diffusion-xl-refiner-1.0", dtype=torch.float16, variant="fp16", use_safetensors=True | |
| ) | |
| pipeline.enable_model_cpu_offload() | |
| # remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed | |
| pipeline.enable_xformers_memory_efficient_attention() | |
| # prepare image | |
| url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img-sdxl-init.png" | |
| init_image = load_image(url) | |
| prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" | |
| # pass prompt and image to pipeline | |
| image = pipeline(prompt, image=init_image, strength=0.5).images[0] | |
| make_image_grid([init_image, image], rows=1, cols=2) | |
| ``` | |
| gambar awal | |
| gambar yang dihasilkan | |
| ### Kandinsky 2.2 | |
| Model Kandinsky berbeda dari model Stable Diffusion karena menggunakan model prior gambar untuk membuat embedding gambar. Embedding tersebut membantu menciptakan keselarasan yang lebih baik antara teks dan gambar, sehingga memungkinkan model difusi laten untuk menghasilkan gambar yang lebih baik. | |
| Cara paling sederhana untuk menggunakan Kandinsky 2.2 adalah: | |
| ```py | |
| import torch | |
| from diffusers import AutoPipelineForImage2Image | |
| from diffusers.utils import make_image_grid, load_image | |
| pipeline = AutoPipelineForImage2Image.from_pretrained( | |
| "kandinsky-community/kandinsky-2-2-decoder", dtype=torch.float16, use_safetensors=True | |
| ) | |
| pipeline.enable_model_cpu_offload() | |
| # remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed | |
| pipeline.enable_xformers_memory_efficient_attention() | |
| # prepare image | |
| url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img-init.png" | |
| init_image = load_image(url) | |
| prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" | |
| # pass prompt and image to pipeline | |
| image = pipeline(prompt, image=init_image).images[0] | |
| make_image_grid([init_image, image], rows=1, cols=2) | |
| ``` | |
| gambar awal | |
| gambar yang dihasilkan | |
| ## Konfigurasi parameter pipeline | |
| Ada beberapa parameter penting yang dapat Anda konfigurasikan dalam pipeline yang akan memengaruhi proses dan kualitas generasi gambar. Mari kita lihat lebih dekat apa yang dilakukan oleh parameter-parameter ini dan bagaimana mengubahnya memengaruhi output. | |
| ### Kekuatan | |
| `strength` adalah salah satu parameter terpenting yang perlu dipertimbangkan dan akan memberikan dampak besar pada gambar yang Anda hasilkan. Parameter ini menentukan seberapa mirip gambar yang dihasilkan dengan gambar awal. Dengan kata lain: | |
| - ๐ nilai `strength` yang lebih tinggi memberikan model lebih banyak "kreativitas" untuk menghasilkan gambar yang berbeda dari gambar awal; nilai `strength` 1,0 berarti gambar awal lebih atau kurang diabaikan | |
| - ๐ nilai `strength` yang lebih rendah berarti gambar yang dihasilkan lebih mirip dengan gambar awal | |
| Parameter `strength` dan `num_inference_steps` saling terkait karena `strength` menentukan jumlah langkah noise yang akan ditambahkan. Sebagai contoh, jika `num_inference_steps` adalah 50 dan `strength` adalah 0,8, maka ini berarti menambahkan 40 (50 * 0,8) langkah noise ke gambar awal dan kemudian melakukan denoising selama 40 langkah untuk mendapatkan gambar yang baru dihasilkan. | |
| ```py | |
| import torch | |
| from diffusers import AutoPipelineForImage2Image | |
| from diffusers.utils import make_image_grid, load_image | |
| pipeline = AutoPipelineForImage2Image.from_pretrained( | |
| "stable-diffusion-v1-5/stable-diffusion-v1-5", dtype=torch.float16, variant="fp16", use_safetensors=True | |
| ) | |
| pipeline.enable_model_cpu_offload() | |
| # remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed | |
| pipeline.enable_xformers_memory_efficient_attention() | |
| # prepare image | |
| url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img-init.png" | |
| init_image = load_image(url) | |
| prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" | |
| # pass prompt and image to pipeline | |
| image = pipeline(prompt, image=init_image, strength=0.8).images[0] | |
| make_image_grid([init_image, image], rows=1, cols=2) | |
| ``` | |
| strength = 0.4 | |
| strength = 0.6 | |
| strength = 1.0 | |
| ### Skala panduan | |
| Parameter `guidance_scale` digunakan untuk mengontrol seberapa dekat keselarasan antara gambar yang dihasilkan dan prompt teks. Nilai `guidance_scale` yang lebih tinggi berarti gambar yang Anda hasilkan lebih selaras dengan prompt, sedangkan nilai `guidance_scale` yang lebih rendah berarti gambar yang dihasilkan memiliki lebih banyak ruang untuk menyimpang dari prompt. | |
| Anda dapat menggabungkan `guidance_scale` dengan `strength` untuk kontrol yang lebih presisi atas seberapa ekspresif model tersebut. Misalnya, gabungkan `strength + guidance_scale` yang tinggi untuk kreativitas maksimal, atau gunakan kombinasi `strength` rendah dan `guidance_scale` rendah untuk menghasilkan gambar yang menyerupai gambar awal tetapi tidak terikat secara ketat pada prompt. | |
| ```py | |
| import torch | |
| from diffusers import AutoPipelineForImage2Image | |
| from diffusers.utils import make_image_grid, load_image | |
| pipeline = AutoPipelineForImage2Image.from_pretrained( | |
| "stable-diffusion-v1-5/stable-diffusion-v1-5", dtype=torch.float16, variant="fp16", use_safetensors=True | |
| ) | |
| pipeline.enable_model_cpu_offload() | |
| # remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed | |
| pipeline.enable_xformers_memory_efficient_attention() | |
| # prepare image | |
| url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img-init.png" | |
| init_image = load_image(url) | |
| prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" | |
| # pass prompt and image to pipeline | |
| image = pipeline(prompt, image=init_image, guidance_scale=8.0).images[0] | |
| make_image_grid([init_image, image], rows=1, cols=2) | |
| ``` | |
| guidance_scale = 0.1 | |
| guidance_scale = 5.0 | |
| guidance_scale = 10.0 | |
| ### Prompt negatif | |
| Prompt negatif mengondisikan model untuk *tidak* menyertakan elemen tertentu dalam gambar, dan dapat digunakan untuk meningkatkan kualitas gambar atau memodifikasi gambar. Misalnya, Anda dapat meningkatkan kualitas gambar dengan menyertakan prompt negatif seperti "poor details" atau "blurry" untuk mendorong model menghasilkan gambar dengan kualitas yang lebih tinggi. Atau, Anda dapat memodifikasi gambar dengan menentukan hal-hal yang harus dikecualikan dari gambar. | |
| ```py | |
| import torch | |
| from diffusers import AutoPipelineForImage2Image | |
| from diffusers.utils import make_image_grid, load_image | |
| pipeline = AutoPipelineForImage2Image.from_pretrained( | |
| "stabilityai/stable-diffusion-xl-refiner-1.0", dtype=torch.float16, variant="fp16", use_safetensors=True | |
| ) | |
| pipeline.enable_model_cpu_offload() | |
| # remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed | |
| pipeline.enable_xformers_memory_efficient_attention() | |
| # prepare image | |
| url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img-init.png" | |
| init_image = load_image(url) | |
| prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" | |
| negative_prompt = "ugly, deformed, disfigured, poor details, bad anatomy" | |
| # pass prompt and image to pipeline | |
| image = pipeline(prompt, negative_prompt=negative_prompt, image=init_image).images[0] | |
| make_image_grid([init_image, image], rows=1, cols=2) | |
| ``` | |
| negative_prompt = "ugly, deformed, disfigured, poor details, bad anatomy" | |
| negative_prompt = "jungle" | |
| ## Pipeline image-to-image berantai | |
| Ada beberapa cara menarik lainnya untuk menggunakan pipeline image-to-image selain sekadar menghasilkan gambar (meskipun itu juga cukup keren). Anda bisa melangkah lebih jauh dengan merangkainya bersama pipeline lain. | |
| ### Teks-ke-gambar-ke-gambar | |
| Menggabungkan pipeline text-to-image dan image-to-image memungkinkan Anda menghasilkan gambar dari teks dan menggunakan gambar yang dihasilkan sebagai gambar awal untuk pipeline image-to-image. Hal ini berguna jika Anda ingin menghasilkan gambar sepenuhnya dari nol. Sebagai contoh, mari kita gabungkan model Stable Diffusion dan model Kandinsky. | |
| Mulai dengan menghasilkan gambar menggunakan pipeline text-to-image: | |
| ```py | |
| from diffusers import AutoPipelineForText2Image, AutoPipelineForImage2Image | |
| import torch | |
| from diffusers.utils import make_image_grid | |
| pipeline = AutoPipelineForText2Image.from_pretrained( | |
| "stable-diffusion-v1-5/stable-diffusion-v1-5", dtype=torch.float16, variant="fp16", use_safetensors=True | |
| ) | |
| pipeline.enable_model_cpu_offload() | |
| # remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed | |
| pipeline.enable_xformers_memory_efficient_attention() | |
| text2image = pipeline("Astronaut in a jungle, cold color palette, muted colors, detailed, 8k").images[0] | |
| text2image | |
| ``` | |
| Sekarang Anda dapat meneruskan gambar yang dihasilkan ini ke pipeline image-to-image: | |
| ```py | |
| pipeline = AutoPipelineForImage2Image.from_pretrained( | |
| "kandinsky-community/kandinsky-2-2-decoder", dtype=torch.float16, use_safetensors=True | |
| ) | |
| pipeline.enable_model_cpu_offload() | |
| # remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed | |
| pipeline.enable_xformers_memory_efficient_attention() | |
| image2image = pipeline("Astronaut in a jungle, cold color palette, muted colors, detailed, 8k", image=text2image).images[0] | |
| make_image_grid([text2image, image2image], rows=1, cols=2) | |
| ``` | |
| ### Gambar-ke-gambar-ke-gambar | |
| Anda juga dapat merangkai beberapa pipeline image-to-image secara berurutan untuk membuat gambar yang lebih menarik. Hal ini dapat berguna untuk melakukan style transfer pada gambar secara iteratif, menghasilkan GIF pendek, memulihkan warna pada gambar, atau memulihkan area yang hilang pada gambar. | |
| Mulai dengan menghasilkan gambar: | |
| ```py | |
| import torch | |
| from diffusers import AutoPipelineForImage2Image | |
| from diffusers.utils import make_image_grid, load_image | |
| pipeline = AutoPipelineForImage2Image.from_pretrained( | |
| "stable-diffusion-v1-5/stable-diffusion-v1-5", dtype=torch.float16, variant="fp16", use_safetensors=True | |
| ) | |
| pipeline.enable_model_cpu_offload() | |
| # remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed | |
| pipeline.enable_xformers_memory_efficient_attention() | |
| # prepare image | |
| url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img-init.png" | |
| init_image = load_image(url) | |
| prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" | |
| # pass prompt and image to pipeline | |
| image = pipeline(prompt, image=init_image, output_type="latent").images[0] | |
| ``` | |
| > [!TIP] | |
| > Penting untuk menentukan `output_type="latent"` dalam pipeline agar semua output tetap berada di ruang laten, sehingga menghindari langkah decode-encode yang tidak perlu. Ini hanya berfungsi jika pipeline yang dirangkai menggunakan VAE yang sama. | |
| Lewatkan output laten dari pipeline ini ke pipeline berikutnya untuk menghasilkan gambar dalam [gaya seni buku komik](https://huggingface.co/ogkalu/Comic-Diffusion): | |
| ```py | |
| pipeline = AutoPipelineForImage2Image.from_pretrained( | |
| "ogkalu/Comic-Diffusion", dtype=torch.float16 | |
| ) | |
| pipeline.enable_model_cpu_offload() | |
| # remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed | |
| pipeline.enable_xformers_memory_efficient_attention() | |
| # need to include the token "charliebo artstyle" in the prompt to use this checkpoint | |
| image = pipeline("Astronaut in a jungle, charliebo artstyle", image=image, output_type="latent").images[0] | |
| ``` | |
| Ulangi sekali lagi untuk menghasilkan gambar akhir dalam [gaya pixel art](https://huggingface.co/kohbanye/pixel-art-style): | |
| ```py | |
| pipeline = AutoPipelineForImage2Image.from_pretrained( | |
| "kohbanye/pixel-art-style", dtype=torch.float16 | |
| ) | |
| pipeline.enable_model_cpu_offload() | |
| # remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed | |
| pipeline.enable_xformers_memory_efficient_attention() | |
| # need to include the token "pixelartstyle" in the prompt to use this checkpoint | |
| image = pipeline("Astronaut in a jungle, pixelartstyle", image=image).images[0] | |
| make_image_grid([init_image, image], rows=1, cols=2) | |
| ``` | |
| ### Gambar ke upscaler ke super-resolusi | |
| Cara lain untuk merangkai pipeline image-to-image Anda adalah dengan menggunakan pipeline upscaler dan super-resolution untuk benar-benar meningkatkan tingkat detail pada gambar. | |
| Mulai dengan pipeline gambar-ke-gambar: | |
| ```py | |
| import torch | |
| from diffusers import AutoPipelineForImage2Image | |
| from diffusers.utils import make_image_grid, load_image | |
| pipeline = AutoPipelineForImage2Image.from_pretrained( | |
| "stable-diffusion-v1-5/stable-diffusion-v1-5", dtype=torch.float16, variant="fp16", use_safetensors=True | |
| ) | |
| pipeline.enable_model_cpu_offload() | |
| # remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed | |
| pipeline.enable_xformers_memory_efficient_attention() | |
| # prepare image | |
| url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img-init.png" | |
| init_image = load_image(url) | |
| prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" | |
| # pass prompt and image to pipeline | |
| image_1 = pipeline(prompt, image=init_image, output_type="latent").images[0] | |
| ``` | |
| > [!TIP] | |
| > Penting untuk menentukan `output_type="latent"` dalam pipeline agar semua output tetap berada di ruang *latent* untuk menghindari langkah decode-encode yang tidak perlu. Ini hanya berfungsi jika pipeline yang dirangkai menggunakan VAE yang sama. | |
| Rangkai dengan pipeline upscaler untuk meningkatkan resolusi gambar: | |
| ```py | |
| from diffusers import StableDiffusionLatentUpscalePipeline | |
| upscaler = StableDiffusionLatentUpscalePipeline.from_pretrained( | |
| "stabilityai/sd-x2-latent-upscaler", dtype=torch.float16, use_safetensors=True | |
| ) | |
| upscaler.enable_model_cpu_offload() | |
| upscaler.enable_xformers_memory_efficient_attention() | |
| image_2 = upscaler(prompt, image=image_1).images[0] | |
| ``` | |
| Terakhir, hubungkan ke pipeline super-resolution untuk meningkatkan resolusi lebih lanjut: | |
| ```py | |
| from diffusers import StableDiffusionUpscalePipeline | |
| super_res = StableDiffusionUpscalePipeline.from_pretrained( | |
| "stabilityai/stable-diffusion-x4-upscaler", dtype=torch.float16, variant="fp16", use_safetensors=True | |
| ) | |
| super_res.enable_model_cpu_offload() | |
| super_res.enable_xformers_memory_efficient_attention() | |
| image_3 = super_res(prompt, image=image_2).images[0] | |
| make_image_grid([init_image, image_3.resize((512, 512))], rows=1, cols=2) | |
| ``` | |
| ## Kontrol generasi gambar | |
| Mencoba menghasilkan gambar yang persis seperti yang Anda inginkan bisa sulit, itulah mengapa teknik dan model generasi terkendali sangat berguna. Meskipun Anda dapat menggunakan `negative_prompt` untuk mengontrol generasi gambar secara parsial, ada metode yang lebih robust seperti prompt weighting dan ControlNets. | |
| ### Penimbangan prompt | |
| Prompt weighting memungkinkan Anda untuk menskalakan representasi setiap konsep dalam sebuah prompt. Sebagai contoh, dalam prompt seperti "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k", Anda dapat memilih untuk meningkatkan atau menurunkan embeddings dari "astronaut" dan "jungle". Library [Compel](https://github.com/damian0815/compel) menyediakan sintaks sederhana untuk menyesuaikan bobot prompt dan menghasilkan embeddings. Anda dapat mempelajari cara membuat embeddings dalam panduan [Prompt weighting](weighted_prompts). | |
| [AutoPipelineForImage2Image](/docs/diffusers/main/id/api/pipelines/auto_pipeline#diffusers.AutoPipelineForImage2Image) memiliki parameter `prompt_embeds` (dan `negative_prompt_embeds` jika Anda menggunakan negative prompt) yang memungkinkan Anda untuk meneruskan embeddings, yang menggantikan parameter `prompt`. | |
| ```py | |
| from diffusers import AutoPipelineForImage2Image | |
| import torch | |
| pipeline = AutoPipelineForImage2Image.from_pretrained( | |
| "stable-diffusion-v1-5/stable-diffusion-v1-5", dtype=torch.float16, variant="fp16", use_safetensors=True | |
| ) | |
| pipeline.enable_model_cpu_offload() | |
| # remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed | |
| pipeline.enable_xformers_memory_efficient_attention() | |
| image = pipeline(prompt_embeds=prompt_embeds, # generated from Compel | |
| negative_prompt_embeds=negative_prompt_embeds, # generated from Compel | |
| image=init_image, | |
| ).images[0] | |
| ``` | |
| ### ControlNet | |
| ControlNets menyediakan cara yang lebih fleksibel dan akurat untuk mengontrol pembuatan gambar karena Anda dapat menggunakan gambar kondisional tambahan. Gambar kondisional tersebut dapat berupa gambar canny, peta kedalaman, segmentasi gambar, bahkan coretan! Apa pun jenis gambar kondisional yang Anda pilih, ControlNet akan menghasilkan gambar yang mempertahankan informasi di dalamnya. | |
| Sebagai contoh, mari kita kondisikan sebuah gambar dengan peta kedalaman untuk mempertahankan informasi spasial dalam gambar tersebut. | |
| ```py | |
| from diffusers.utils import load_image, make_image_grid | |
| # prepare image | |
| url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img-init.png" | |
| init_image = load_image(url) | |
| init_image = init_image.resize((958, 960)) # resize to depth image dimensions | |
| depth_image = load_image("https://huggingface.co/lllyasviel/control_v11f1p_sd15_depth/resolve/main/images/control.png") | |
| make_image_grid([init_image, depth_image], rows=1, cols=2) | |
| ``` | |
| Muat model ControlNet yang dikondisikan pada peta kedalaman dan [AutoPipelineForImage2Image](/docs/diffusers/main/id/api/pipelines/auto_pipeline#diffusers.AutoPipelineForImage2Image): | |
| ```py | |
| from diffusers import ControlNetModel, AutoPipelineForImage2Image | |
| import torch | |
| controlnet = ControlNetModel.from_pretrained("lllyasviel/control_v11f1p_sd15_depth", dtype=torch.float16, variant="fp16", use_safetensors=True) | |
| pipeline = AutoPipelineForImage2Image.from_pretrained( | |
| "stable-diffusion-v1-5/stable-diffusion-v1-5", controlnet=controlnet, dtype=torch.float16, variant="fp16", use_safetensors=True | |
| ) | |
| pipeline.enable_model_cpu_offload() | |
| # remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed | |
| pipeline.enable_xformers_memory_efficient_attention() | |
| ``` | |
| Sekarang, buat gambar baru yang dikondisikan pada peta kedalaman, gambar awal, dan prompt: | |
| ```py | |
| prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" | |
| image_control_net = pipeline(prompt, image=init_image, control_image=depth_image).images[0] | |
| make_image_grid([init_image, depth_image, image_control_net], rows=1, cols=3) | |
| ``` | |
| gambar awal | |
| gambar kedalaman | |
| Gambar ControlNet | |
| Mari kita terapkan [gaya](https://huggingface.co/nitrosocke/elden-ring-diffusion) baru pada gambar yang dihasilkan dari ControlNet dengan merangkainya menggunakan pipeline image-to-image: | |
| ```py | |
| pipeline = AutoPipelineForImage2Image.from_pretrained( | |
| "nitrosocke/elden-ring-diffusion", dtype=torch.float16, | |
| ) | |
| pipeline.enable_model_cpu_offload() | |
| # remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed | |
| pipeline.enable_xformers_memory_efficient_attention() | |
| prompt = "elden ring style astronaut in a jungle" # include the token "elden ring style" in the prompt | |
| negative_prompt = "ugly, deformed, disfigured, poor details, bad anatomy" | |
| image_elden_ring = pipeline(prompt, negative_prompt=negative_prompt, image=image_control_net, strength=0.45, guidance_scale=10.5).images[0] | |
| make_image_grid([init_image, depth_image, image_control_net, image_elden_ring], rows=2, cols=2) | |
| ``` | |
| ## Optimalkan | |
| Menjalankan model difusi membutuhkan biaya komputasi yang tinggi dan intensif, tetapi dengan beberapa trik optimasi, sepenuhnya mungkin untuk menjalankannya pada GPU konsumen dan GPU tier gratis. Misalnya, Anda dapat menggunakan bentuk perhatian yang lebih hemat memori seperti [scaled-dot product attention](../optimization/fp16#scaled-dot-product-attention) dari PyTorch 2.0 atau [xFormers](../optimization/attention_backends) (Anda dapat menggunakan salah satu, tetapi tidak perlu menggunakan keduanya). Anda juga dapat memindahkan model ke GPU sementara komponen pipeline lainnya menunggu di CPU. | |
| ```diff | |
| + pipeline.enable_model_cpu_offload() | |
| + pipeline.enable_xformers_memory_efficient_attention() | |
| ``` | |
| Dengan [`torch.compile`](../optimization/fp16#torchcompile), Anda dapat meningkatkan kecepatan inferensi Anda lebih jauh dengan membungkus UNet Anda dengannya: | |
| ```py | |
| pipeline.unet = torch.compile(pipeline.unet, mode="reduce-overhead", fullgraph=True) | |
| ``` | |
| Untuk mempelajari lebih lanjut, lihat [Optimasi dan skala](../optimization/overview#optimization-techniques). | |
Xet Storage Details
- Size:
- 26.2 kB
- Xet hash:
- 264bbb73c36bbd6a80a0659bd61a9368f720ad03ef73cb41dfbde0b2b2120186
ยท
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.