Diffusers documentation

Ảnh sang ảnh

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v0.42.1).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Ảnh sang ảnh

Image-to-image tương tự như text-to-image, nhưng ngoài prompt, bạn còn có thể truyền vào một hình ảnh ban đầu làm điểm khởi đầu cho quá trình khuếch tán. Hình ảnh ban đầu được mã hóa sang không gian tiềm ẩn và nhiễu được thêm vào. Sau đó, mô hình khuếch tán tiềm ẩn nhận prompt và hình ảnh tiềm ẩn có nhiễu, dự đoán nhiễu đã thêm, và loại bỏ nhiễu dự đoán khỏi hình ảnh tiềm ẩn ban đầu để có được hình ảnh tiềm ẩn mới. Cuối cùng, một bộ giải mã sẽ giải mã hình ảnh tiềm ẩn mới trở lại thành hình ảnh.

Với 🤗 Diffusers, điều này đơn giản như 1-2-3:

  1. Tải checkpoint vào lớp AutoPipelineForImage2Image; pipeline này tự động xử lý việc tải đúng lớp pipeline dựa trên checkpoint:
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()

Bạn sẽ nhận thấy trong suốt hướng dẫn, chúng tôi sử dụng enable_model_cpu_offload() và enable_xformers_memory_efficient_attention() để tiết kiệm bộ nhớ và tăng tốc độ suy luận. Nếu bạn đang sử dụng PyTorch 2.0, bạn không cần gọi enable_xformers_memory_efficient_attention() trên pipeline của mình vì nó đã sử dụng scaled-dot product attention bản địa của PyTorch 2.0.

  1. Tải một hình ảnh để truyền vào pipeline:
init_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/cat.png")
  1. Truyền một prompt và một hình ảnh vào pipeline để tạo ra một hình ảnh:
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)
ảnh ban đầu
hình ảnh được tạo

Các mô hình phổ biến

Các mô hình image-to-image phổ biến nhất là Stable Diffusion v1.5, Stable Diffusion XL (SDXL) và Kandinsky 2.2. Kết quả từ các mô hình Stable Diffusion và Kandinsky có sự khác biệt do khác nhau về kiến trúc và quy trình huấn luyện; nhìn chung, bạn có thể kỳ vọng SDXL tạo ra hình ảnh có chất lượng cao hơn so với Stable Diffusion v1.5. Hãy cùng xem nhanh cách sử dụng từng mô hình này và so sánh kết quả của chúng.

Stable Diffusion v1.5

Stable Diffusion v1.5 là một mô hình latent diffusion được khởi tạo từ một checkpoint trước đó, sau đó được finetune thêm trong 595K bước trên các ảnh 512x512. Để sử dụng pipeline này cho image-to-image, bạn cần chuẩn bị một ảnh ban đầu để truyền vào pipeline. Sau đó, bạn có thể truyền prompt và ảnh vào pipeline để tạo ra một ảnh mới:

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)
ảnh ban đầu
hình ảnh được tạo

Stable Diffusion XL (SDXL)

SDXL là phiên bản mạnh hơn của mô hình Stable Diffusion. Nó sử dụng một mô hình cơ sở lớn hơn và một mô hình tinh chỉnh bổ sung để tăng chất lượng đầu ra của mô hình cơ sở. Đọc hướng dẫn SDXL để có cái nhìn chi tiết hơn về cách sử dụng mô hình này, cũng như các kỹ thuật khác mà nó sử dụng để tạo ra hình ảnh chất lượng cao.

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)
ảnh ban đầu
hình ảnh được tạo

Kandinsky 2.2

Mô hình Kandinsky khác với các mô hình Stable Diffusion vì nó sử dụng một mô hình tiên nghiệm hình ảnh để tạo ra các vector nhúng hình ảnh. Các vector nhúng này giúp tạo ra sự tương thích tốt hơn giữa văn bản và hình ảnh, cho phép mô hình khuếch tán tiềm ẩn tạo ra các hình ảnh chất lượng hơn.

Cách đơn giản nhất để sử dụng Kandinsky 2.2 là:

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)
ảnh ban đầu
hình ảnh được tạo

Cấu hình tham số pipeline

Có một số tham số quan trọng mà bạn có thể cấu hình trong pipeline, ảnh hưởng đến quá trình tạo ảnh và chất lượng ảnh. Hãy cùng xem xét kỹ hơn các tham số này hoạt động như thế nào và việc thay đổi chúng tác động ra sao đến kết quả đầu ra.

Độ mạnh

strength là một trong những tham số quan trọng nhất cần xem xét và sẽ có tác động lớn đến hình ảnh bạn tạo ra. Tham số này quyết định mức độ hình ảnh tạo ra giống với hình ảnh ban đầu. Nói cách khác:

  • 📈 giá trị strength cao hơn cho phép mô hình có thêm “sáng tạo” để tạo ra hình ảnh khác với hình ảnh ban đầu; giá trị strength là 1.0 có nghĩa là hình ảnh ban đầu gần như bị bỏ qua
  • 📉 giá trị strength thấp hơn có nghĩa là hình ảnh được tạo ra giống với hình ảnh ban đầu hơn

Các tham số strength và num_inference_steps có liên quan vì strength xác định số bước nhiễu cần thêm. Ví dụ, nếu num_inference_steps là 50 và strength là 0.8, điều này có nghĩa là thêm 40 (50 * 0.8) bước nhiễu vào hình ảnh ban đầu và sau đó khử nhiễu trong 40 bước để có được hình ảnh mới được tạo ra.

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

Thang độ dẫn

Tham số guidance_scale được dùng để kiểm soát mức độ khớp giữa hình ảnh được tạo ra và prompt văn bản. Giá trị guidance_scale cao hơn có nghĩa là hình ảnh bạn tạo ra sẽ khớp chặt chẽ hơn với prompt, trong khi giá trị guidance_scale thấp hơn cho phép hình ảnh được tạo ra có nhiều không gian để lệch khỏi prompt hơn.

Bạn có thể kết hợp guidance_scale với strength để kiểm soát chính xác hơn mức độ biểu cảm của mô hình. Ví dụ, kết hợp strength + guidance_scale cao để đạt mức sáng tạo tối đa, hoặc sử dụng tổ hợp strength thấp và guidance_scale thấp để tạo ra hình ảnh giống với hình ảnh ban đầu nhưng không bị ràng buộc chặt chẽ vào prompt.

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

Negative prompt

Prompt âm tính điều kiện hóa mô hình để không bao gồm các yếu tố trong hình ảnh, và có thể được sử dụng để cải thiện chất lượng hình ảnh hoặc chỉnh sửa hình ảnh. Ví dụ, bạn có thể cải thiện chất lượng hình ảnh bằng cách bao gồm các prompt âm tính như “poor details” hoặc “blurry” để khuyến khích mô hình tạo ra hình ảnh có chất lượng cao hơn. Hoặc bạn có thể chỉnh sửa hình ảnh bằng cách chỉ định các yếu tố cần loại trừ khỏi hình ảnh.

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 = "xấu, biến dạng, méo mó, chi tiết kém, giải phẫu sai"
negative_prompt = "jungle"

Các pipeline ảnh-đến-ảnh được nối tiếp

Có một số cách thú vị khác để bạn sử dụng pipeline image-to-image ngoài việc chỉ tạo ra một hình ảnh (mặc dù điều đó cũng khá tuyệt). Bạn có thể đi xa hơn một bước và kết nối nó với các pipeline khác.

Văn bản thành ảnh thành ảnh

Việc kết nối (chaining) một pipeline text-to-image và một pipeline image-to-image cho phép bạn tạo ra một hình ảnh từ văn bản và sử dụng hình ảnh đã tạo đó làm hình ảnh đầu vào cho pipeline image-to-image. Điều này hữu ích nếu bạn muốn tạo một hình ảnh hoàn toàn từ đầu. Ví dụ, hãy kết nối một mô hình Stable Diffusion và một mô hình Kandinsky.

Bắt đầu bằng cách tạo một hình ảnh bằng pipeline text-to-image:

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

Bây giờ bạn có thể truyền hình ảnh đã tạo này vào pipeline image-to-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()

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)

Hình sang hình sang hình

Bạn cũng có thể kết nối nhiều pipeline image-to-image với nhau để tạo ra những hình ảnh thú vị hơn. Điều này có thể hữu ích để thực hiện chuyển đổi phong cách (style transfer) lặp đi lặp lại trên một hình ảnh, tạo các GIF ngắn, khôi phục màu sắc cho một hình ảnh, hoặc khôi phục các vùng bị thiếu của một hình ảnh.

Bắt đầu bằng cách tạo một hình ảnh:

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]

Việc chỉ định output_type="latent" trong pipeline là quan trọng để giữ tất cả các đầu ra trong không gian ẩn, tránh bước giải mã - mã hóa không cần thiết. Điều này chỉ hoạt động nếu các pipeline được nối tiếp sử dụng cùng một VAE.

Truyền đầu ra latent từ pipeline này sang pipeline tiếp theo để tạo ra một hình ảnh theo phong cách truyện tranh:

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]

Lặp lại một lần nữa để tạo ra hình ảnh cuối cùng theo phong cách pixel art:

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)

Image-to-upscaler-to-super-resolution

Một cách khác để bạn kết nối pipeline image-to-image của mình là sử dụng pipeline upscaler và super-resolution để thực sự tăng mức độ chi tiết trong một hình ảnh.

Bắt đầu với một pipeline từ ảnh sang ảnh:

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]

Việc chỉ định output_type="latent" trong pipeline là quan trọng để giữ tất cả các đầu ra trong không gian latent, tránh bước decode-encode không cần thiết. Điều này chỉ hoạt động nếu các pipeline được nối tiếp sử dụng cùng một VAE.

Kết nối nó với một pipeline upscaler để tăng độ phân giải của hình ảnh:

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]

Cuối cùng, kết nối nó với một pipeline siêu phân giải để tăng cường độ phân giải thêm nữa:

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)

Kiểm soát tạo ảnh

Việc tạo ra một hình ảnh trông chính xác như mong muốn có thể rất khó khăn, đó là lý do tại sao các kỹ thuật và mô hình tạo ảnh có kiểm soát lại hữu ích đến vậy. Trong khi bạn có thể sử dụng negative_prompt để kiểm soát một phần quá trình tạo ảnh, vẫn có những phương pháp mạnh mẽ hơn như prompt weighting và ControlNets.

Trọng số prompt

Prompt weighting cho phép bạn điều chỉnh mức độ biểu diễn của từng khái niệm trong một prompt. Ví dụ, trong một prompt như “Astronaut in a jungle, cold color palette, muted colors, detailed, 8k”, bạn có thể tăng hoặc giảm các embeddings của “astronaut” và “jungle”. Thư viện Compel cung cấp một cú pháp đơn giản để điều chỉnh trọng số prompt và tạo ra các embeddings. Bạn có thể tìm hiểu cách tạo các embeddings trong hướng dẫn Prompt weighting.

AutoPipelineForImage2Image có tham số prompt_embeds (và negative_prompt_embeds nếu bạn đang dùng negative prompt) cho phép bạn truyền các embeddings, thay thế cho tham số prompt.

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 cung cấp một cách linh hoạt và chính xác hơn để kiểm soát việc tạo hình ảnh vì bạn có thể sử dụng một hình ảnh điều kiện bổ sung. Hình ảnh điều kiện có thể là hình ảnh canny, bản đồ độ sâu, phân đoạn hình ảnh, và thậm chí là các nét vẽ phác thảo! Bất kể loại hình ảnh điều kiện nào bạn chọn, ControlNet sẽ tạo ra một hình ảnh giữ lại thông tin trong đó.

Ví dụ, hãy điều kiện hóa một hình ảnh bằng bản đồ độ sâu để giữ lại thông tin không gian trong hình ảnh.

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)

Tải một mô hình ControlNet được điều kiện bởi các bản đồ độ sâu và AutoPipelineForImage2Image:

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()

Bây giờ, tạo một hình ảnh mới dựa trên bản đồ độ sâu, hình ảnh ban đầu và câu lệnh:

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)
ảnh ban đầu
ảnh độ sâu
Ảnh ControlNet

Hãy áp dụng một phong cách mới cho hình ảnh được tạo từ ControlNet bằng cách kết nối nó với pipeline image-to-image:

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)

Tối ưu

Việc chạy các mô hình khuếch tán (diffusion) đòi hỏi chi phí tính toán cao và tiêu tốn tài nguyên, nhưng với một vài mẹo tối ưu hóa, hoàn toàn có thể chạy chúng trên các GPU dành cho người dùng phổ thông và GPU miễn phí. Ví dụ, bạn có thể sử dụng một dạng chú ý (attention) tiết kiệm bộ nhớ hơn như scaled-dot product attention của PyTorch 2.0 hoặc xFormers (bạn có thể dùng một trong hai, nhưng không cần dùng cả hai). Bạn cũng có thể chuyển (offload) mô hình sang GPU trong khi các thành phần khác của pipeline chờ trên CPU.

+ pipeline.enable_model_cpu_offload()
+ pipeline.enable_xformers_memory_efficient_attention()

Với torch.compile, bạn có thể tăng tốc độ suy luận của mình hơn nữa bằng cách bọc UNet của bạn bằng nó:

pipeline.unet = torch.compile(pipeline.unet, mode="reduce-overhead", fullgraph=True)

Để tìm hiểu thêm, hãy xem các hướng dẫn Giảm mức sử dụng bộ nhớ và Tăng tốc suy luận.

Update on GitHub