Buckets:
| # IP-Adapter | |
| [IP-Adapter](https://huggingface.co/papers/2308.06721) điều hướng một mô hình khuếch tán được huấn luyện trước bằng một hình ảnh tham chiếu trong khi bạn vẫn giữ nguyên prompt văn bản. Nó đóng băng mô hình cơ sở và thêm một tập hợp nhỏ các lớp cross-attention hình ảnh, giúp việc khớp chủ thể hoặc phong cách từ một bức ảnh trở nên thực tế mà không cần fine-tuning. | |
| ```text | |
| text prompt reference image | |
| | | | |
| text encoder image encoder | |
| | | | |
| v v | |
| text cross-attn (frozen) IP-Adapter cross-attn | |
| \ / | |
| \ / | |
| +--------> denoiser <-----------+ | |
| (frozen base) | |
| ``` | |
| Checkpoint của IP-Adapter thường có kích thước ~100MB vì chúng lưu trữ trọng số của adapter, không phải toàn bộ mô hình. Hãy tải pipeline cơ sở trước, sau đó tải adapter bằng [load_ip_adapter()](/docs/diffusers/v0.43.0/vi/api/loaders/ip_adapter#diffusers.loaders.IPAdapterMixin.load_ip_adapter). | |
| > [!TIP] | |
| > IP-Adapters có sẵn cho nhiều mô hình như [Flux](../api/pipelines/flux#ip-adapter) và [Stable Diffusion 3](../api/pipelines/stable_diffusion/stable_diffusion_3), cùng nhiều mô hình khác. Các ví dụ trong hướng dẫn này sử dụng Stable Diffusion và Stable Diffusion XL. | |
| Sử dụng [set_ip_adapter_scale()](/docs/diffusers/v0.43.0/vi/api/loaders/ip_adapter#diffusers.loaders.IPAdapterMixin.set_ip_adapter_scale) để kiểm soát mức độ IP-Adapter điều hướng quá trình tạo. `1.0` áp dụng adapter ở cường độ đầy đủ; `0.5` thường cân bằng giữa prompt văn bản và prompt hình ảnh. | |
| Các ví dụ dưới đây cho thấy IP-Adapter trên các tác vụ phổ biến. | |
| ```py | |
| import torch | |
| from diffusers import AutoPipelineForText2Image | |
| from diffusers.utils import load_image | |
| pipeline = AutoPipelineForText2Image.from_pretrained( | |
| "stabilityai/stable-diffusion-xl-base-1.0", | |
| dtype=torch.float16 | |
| ).to("cuda") # or "mps", "xpu", "cpu" | |
| pipeline.load_ip_adapter( | |
| "h94/IP-Adapter", | |
| subfolder="sdxl_models", | |
| weight_name="ip-adapter_sdxl.bin" | |
| ) | |
| pipeline.set_ip_adapter_scale(0.8) | |
| ``` | |
| Truyền một hình ảnh vào `ip_adapter_image` cùng với một câu lệnh văn bản để tạo ra một hình ảnh. | |
| ```py | |
| image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/ip_adapter_diner.png") | |
| pipeline( | |
| prompt="a polar bear sitting in a chair drinking a milkshake", | |
| ip_adapter_image=image, | |
| negative_prompt="deformed, ugly, wrong proportion, low res, bad anatomy, worst quality, low quality", | |
| ).images[0] | |
| ``` | |
| Hình ảnh IP-Adapter | |
| hình ảnh được tạo | |
| ```py | |
| import torch | |
| from diffusers import AutoPipelineForImage2Image | |
| from diffusers.utils import load_image | |
| pipeline = AutoPipelineForImage2Image.from_pretrained( | |
| "stabilityai/stable-diffusion-xl-base-1.0", | |
| dtype=torch.float16 | |
| ).to("cuda") # or "mps", "xpu", "cpu" | |
| pipeline.load_ip_adapter( | |
| "h94/IP-Adapter", | |
| subfolder="sdxl_models", | |
| weight_name="ip-adapter_sdxl.bin" | |
| ) | |
| pipeline.set_ip_adapter_scale(0.8) | |
| image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/ip_adapter_bear_1.png") | |
| ip_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/ip_adapter_gummy.png") | |
| pipeline( | |
| prompt="best quality, high quality", | |
| image=image, | |
| ip_adapter_image=ip_image, | |
| strength=0.5, | |
| ).images[0] | |
| ``` | |
| ảnh đầu vào | |
| Hình ảnh IP-Adapter | |
| hình ảnh được tạo | |
| ```py | |
| import torch | |
| from diffusers import AutoPipelineForInpainting | |
| from diffusers.utils import load_image | |
| pipeline = AutoPipelineForInpainting.from_pretrained( | |
| "stabilityai/stable-diffusion-xl-base-1.0", | |
| dtype=torch.float16 | |
| ).to("cuda") # or "mps", "xpu", "cpu" | |
| pipeline.load_ip_adapter( | |
| "h94/IP-Adapter", | |
| subfolder="sdxl_models", | |
| weight_name="ip-adapter_sdxl.bin" | |
| ) | |
| pipeline.set_ip_adapter_scale(0.6) | |
| mask_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/ip_adapter_mask.png") | |
| image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/ip_adapter_bear_1.png") | |
| ip_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/ip_adapter_gummy.png") | |
| pipeline( | |
| prompt="a cute gummy bear waving", | |
| image=image, | |
| mask_image=mask_image, | |
| ip_adapter_image=ip_image, | |
| ).images[0] | |
| ``` | |
| ảnh đầu vào | |
| Hình ảnh IP-Adapter | |
| hình ảnh được tạo | |
| Phương thức [enable_model_cpu_offload()](/docs/diffusers/v0.43.0/vi/api/pipelines/overview#diffusers.DiffusionPipeline.enable_model_cpu_offload) hữu ích để giảm bộ nhớ và nên được bật sau khi IP-Adapter được nạp. Nếu không, bộ mã hóa hình ảnh của IP-Adapter cũng sẽ được chuyển xuống CPU và gây ra lỗi. | |
| ```py | |
| import torch | |
| from diffusers import AnimateDiffPipeline, DDIMScheduler, MotionAdapter | |
| from diffusers.utils import export_to_gif | |
| from diffusers.utils import load_image | |
| adapter = MotionAdapter.from_pretrained( | |
| "guoyww/animatediff-motion-adapter-v1-5-2", | |
| dtype=torch.float16 | |
| ) | |
| pipeline = AnimateDiffPipeline.from_pretrained( | |
| "emilianJR/epiCRealism", | |
| motion_adapter=adapter, | |
| dtype=torch.float16 | |
| ) | |
| scheduler = DDIMScheduler.from_pretrained( | |
| "emilianJR/epiCRealism", | |
| subfolder="scheduler", | |
| clip_sample=False, | |
| timestep_spacing="linspace", | |
| beta_schedule="linear", | |
| steps_offset=1, | |
| ) | |
| pipeline.scheduler = scheduler | |
| pipeline.vae.enable_slicing() | |
| pipeline.load_ip_adapter("h94/IP-Adapter", subfolder="models", weight_name="ip-adapter_sd15.bin") | |
| pipeline.enable_model_cpu_offload() | |
| ip_adapter_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/ip_adapter_inpaint.png") | |
| pipeline( | |
| prompt="A cute gummy bear waving", | |
| negative_prompt="bad quality, worse quality, low resolution", | |
| ip_adapter_image=ip_adapter_image, | |
| num_frames=16, | |
| guidance_scale=7.5, | |
| num_inference_steps=50, | |
| ).frames[0] | |
| ``` | |
| Hình ảnh IP-Adapter | |
| video được tạo | |
| ## Các biến thể checkpoint | |
| Nạp Plus khi chi tiết từ ảnh tham chiếu quan trọng nhất. Nạp FaceID khi bạn cần các vector nhận dạng danh tính của InsightFace thay vì các vector ảnh của CLIP. | |
| ```py | |
| import torch | |
| from transformers import CLIPVisionModelWithProjection | |
| from diffusers import AutoPipelineForText2Image | |
| image_encoder = CLIPVisionModelWithProjection.from_pretrained( | |
| "h94/IP-Adapter", | |
| subfolder="models/image_encoder", | |
| dtype=torch.float16 | |
| ) | |
| pipeline = AutoPipelineForText2Image.from_pretrained( | |
| "stabilityai/stable-diffusion-xl-base-1.0", | |
| image_encoder=image_encoder, | |
| dtype=torch.float16 | |
| ).to("cuda") # or "mps", "xpu", "cpu" | |
| pipeline.load_ip_adapter( | |
| "h94/IP-Adapter", | |
| subfolder="sdxl_models", | |
| weight_name="ip-adapter-plus_sdxl_vit-h.safetensors" | |
| ) | |
| ``` | |
| Các checkpoint FaceID sử dụng face embeddings từ [InsightFace](https://github.com/deepinsight/insightface) thay vì CLIP image embeddings. Hãy sử dụng [DDIMScheduler](/docs/diffusers/v0.43.0/vi/api/schedulers/ddim#diffusers.DDIMScheduler) hoặc [EulerDiscreteScheduler](/docs/diffusers/v0.43.0/vi/api/schedulers/euler#diffusers.EulerDiscreteScheduler) cho các mô hình FaceID. Trích xuất face embeddings và truyền chúng dưới dạng một danh sách các tensor vào `ip_adapter_image_embeds`. | |
| ```py | |
| # pip install insightface | |
| import cv2 | |
| import numpy as np | |
| import torch | |
| from diffusers import StableDiffusionPipeline, DDIMScheduler | |
| from diffusers.utils import load_image | |
| from insightface.app import FaceAnalysis | |
| pipeline = StableDiffusionPipeline.from_pretrained( | |
| "stable-diffusion-v1-5/stable-diffusion-v1-5", | |
| dtype=torch.float16, | |
| ).to("cuda") # or "mps", "xpu", "cpu" | |
| pipeline.scheduler = DDIMScheduler.from_config(pipeline.scheduler.config) | |
| pipeline.load_ip_adapter( | |
| "h94/IP-Adapter-FaceID", | |
| subfolder=None, | |
| weight_name="ip-adapter-faceid_sd15.bin", | |
| image_encoder_folder=None | |
| ) | |
| pipeline.set_ip_adapter_scale(0.6) | |
| image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/ip_mask_girl1.png") | |
| ref_images_embeds = [] | |
| app = FaceAnalysis(name="buffalo_l", providers=['CUDAExecutionProvider', 'CPUExecutionProvider']) | |
| app.prepare(ctx_id=0, det_size=(640, 640)) | |
| image = cv2.cvtColor(np.asarray(image), cv2.COLOR_RGB2BGR) | |
| faces = app.get(image) | |
| image = torch.from_numpy(faces[0].normed_embedding) | |
| ref_images_embeds.append(image.unsqueeze(0)) | |
| ref_images_embeds = torch.stack(ref_images_embeds, dim=0).unsqueeze(0) | |
| neg_ref_images_embeds = torch.zeros_like(ref_images_embeds) | |
| id_embeds = torch.cat([neg_ref_images_embeds, ref_images_embeds]).to(dtype=torch.float16, device="cuda") | |
| pipeline( | |
| prompt="A photo of a girl", | |
| ip_adapter_image_embeds=[id_embeds], | |
| negative_prompt="monochrome, lowres, bad anatomy, worst quality, low quality", | |
| ).images[0] | |
| ``` | |
| Đối với FaceID Plus, hãy nạp [CLIPVisionModelWithProjection](https://huggingface.co/docs/transformers/v5.22.2/en/model_doc/clip#transformers.CLIPVisionModelWithProjection) làm bộ mã hóa hình ảnh. | |
| ```py | |
| import torch | |
| from transformers import CLIPVisionModelWithProjection | |
| from diffusers import AutoPipelineForText2Image | |
| image_encoder = CLIPVisionModelWithProjection.from_pretrained( | |
| "laion/CLIP-ViT-H-14-laion2B-s32B-b79K", | |
| dtype=torch.float16, | |
| ) | |
| pipeline = AutoPipelineForText2Image.from_pretrained( | |
| "stable-diffusion-v1-5/stable-diffusion-v1-5", | |
| image_encoder=image_encoder, | |
| dtype=torch.float16 | |
| ).to("cuda") # or "mps", "xpu", "cpu" | |
| pipeline.load_ip_adapter( | |
| "h94/IP-Adapter-FaceID", | |
| subfolder=None, | |
| weight_name="ip-adapter-faceid-plus_sd15.bin" | |
| ) | |
| ``` | |
| FaceID Plus và Plus v2 cũng cần các embedding ảnh CLIP bên cạnh các embedding InsightFace. Sau khi bạn chuẩn bị xong các embedding khuôn mặt, hãy mã hóa ảnh tham chiếu và gán các embedding CLIP vào lớp chiếu ảnh. | |
| ```py | |
| import torch | |
| from diffusers.utils import load_image | |
| ip_adapter_images = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/ip_mask_girl1.png") | |
| num_images = 1 | |
| clip_embeds = pipeline.prepare_ip_adapter_image_embeds( | |
| ip_adapter_image=[ip_adapter_images], | |
| ip_adapter_image_embeds=None, | |
| device=torch.device("cuda"), | |
| num_images_per_prompt=num_images, | |
| do_classifier_free_guidance=True, | |
| )[0] | |
| pipeline.unet.encoder_hid_proj.image_projection_layers[0].clip_embeds = clip_embeds.to(dtype=torch.float16) | |
| # set to True if using IP-Adapter FaceID Plus v2 | |
| pipeline.unet.encoder_hid_proj.image_projection_layers[0].shortcut = False | |
| ``` | |
| ## Nhúng hình ảnh | |
| `prepare_ip_adapter_image_embeds()` mã hóa các hình ảnh IP-Adapter thành các embedding mà bạn có thể lưu lại và sử dụng lại. Hãy tính toán trước chúng một lần thay vì tải và mã hóa lại cùng những hình ảnh đó mỗi khi bạn chạy pipeline. | |
| ```py | |
| import torch | |
| from diffusers import AutoPipelineForText2Image | |
| from diffusers.utils import load_image | |
| pipeline = AutoPipelineForText2Image.from_pretrained( | |
| "stabilityai/stable-diffusion-xl-base-1.0", | |
| dtype=torch.float16 | |
| ).to("cuda") # or "mps", "xpu", "cpu" | |
| pipeline.load_ip_adapter( | |
| "h94/IP-Adapter", | |
| subfolder="sdxl_models", | |
| weight_name="ip-adapter_sdxl.bin" | |
| ) | |
| image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/ip_adapter_bear_1.png") | |
| image_embeds = pipeline.prepare_ip_adapter_image_embeds( | |
| ip_adapter_image=image, | |
| ip_adapter_image_embeds=None, | |
| device="cuda", | |
| num_images_per_prompt=1, | |
| do_classifier_free_guidance=True, | |
| ) | |
| torch.save(image_embeds, "image_embeds.ipadpt") | |
| ``` | |
| Tải lại các embedding ảnh bằng cách truyền chúng vào tham số `ip_adapter_image_embeds`. Đặt `image_encoder_folder` thành `None` vì bạn không còn cần image encoder để tạo ra các embedding ảnh nữa. | |
| > [!TIP] | |
| > Bạn cũng có thể nạp các vector nhúng hình ảnh từ các nguồn khác như ComfyUI. | |
| ```py | |
| pipeline = AutoPipelineForText2Image.from_pretrained( | |
| "stabilityai/stable-diffusion-xl-base-1.0", | |
| dtype=torch.float16 | |
| ).to("cuda") # or "mps", "xpu", "cpu" | |
| pipeline.load_ip_adapter( | |
| "h94/IP-Adapter", | |
| subfolder="sdxl_models", | |
| image_encoder_folder=None, | |
| weight_name="ip-adapter_sdxl.bin" | |
| ) | |
| pipeline.set_ip_adapter_scale(0.8) | |
| image_embeds = torch.load("image_embeds.ipadpt") | |
| pipeline( | |
| prompt="a polar bear sitting in a chair drinking a milkshake", | |
| ip_adapter_image_embeds=image_embeds, | |
| negative_prompt="deformed, ugly, wrong proportion, low res, bad anatomy, worst quality, low quality", | |
| num_inference_steps=100, | |
| ).images[0] | |
| ``` | |
| ## Che mặt nạ | |
| Binary masking cho phép gán một hình ảnh IP-Adapter vào một khu vực cụ thể của hình ảnh đầu ra, giúp ích cho việc kết hợp nhiều hình ảnh IP-Adapter. Mỗi hình ảnh IP-Adapter yêu cầu một binary mask. | |
| Nạp [IPAdapterMaskProcessor](/docs/diffusers/v0.43.0/vi/api/loaders/ip_adapter#diffusers.IPAdapterMaskProcessor) để tiền xử lý các mặt nạ hình ảnh. Để đạt kết quả tốt nhất, hãy cung cấp `height` và `width` đầu ra để đảm bảo các mặt nạ có tỷ lệ khung hình khác nhau được điều chỉnh kích thước phù hợp. Nếu các mặt nạ đầu vào đã khớp với tỷ lệ khung hình của hình ảnh được tạo, bạn không cần đặt `height` và `width`. | |
| ```py | |
| import torch | |
| from diffusers import AutoPipelineForText2Image | |
| from diffusers.image_processor import IPAdapterMaskProcessor | |
| from diffusers.utils import load_image | |
| pipeline = AutoPipelineForText2Image.from_pretrained( | |
| "stabilityai/stable-diffusion-xl-base-1.0", | |
| dtype=torch.float16 | |
| ).to("cuda") # or "mps", "xpu", "cpu" | |
| mask1 = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/ip_mask_mask1.png") | |
| mask2 = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/ip_mask_mask2.png") | |
| processor = IPAdapterMaskProcessor() | |
| masks = processor.preprocess([mask1, mask2], height=1024, width=1024) | |
| ``` | |
| mask 1 | |
| mask 2 | |
| Cung cấp cả các hình ảnh IP-Adapter và thang tỷ lệ của chúng dưới dạng danh sách. Truyền các mặt nạ đã tiền xử lý vào `cross_attention_kwargs` trong pipeline. | |
| ```py | |
| face_image1 = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/ip_mask_girl1.png") | |
| face_image2 = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/ip_mask_girl2.png") | |
| pipeline.load_ip_adapter( | |
| "h94/IP-Adapter", | |
| subfolder="sdxl_models", | |
| weight_name=["ip-adapter-plus-face_sdxl_vit-h.safetensors"] | |
| ) | |
| pipeline.set_ip_adapter_scale([[0.7, 0.7]]) | |
| ip_images = [[face_image1, face_image2]] | |
| masks = [masks.reshape(1, masks.shape[0], masks.shape[2], masks.shape[3])] | |
| pipeline( | |
| prompt="2 girls", | |
| ip_adapter_image=ip_images, | |
| negative_prompt="monochrome, lowres, bad anatomy, worst quality, low quality", | |
| cross_attention_kwargs={"ip_adapter_masks": masks} | |
| ).images[0] | |
| ``` | |
| Hình ảnh IP-Adapter 1 | |
| Hình ảnh IP-Adapter 2 | |
| được tạo với mask | |
| được tạo mà không có mặt nạ | |
| ## Công thức | |
| Kết hợp IP-Adapter với các adapter hoặc pipeline khác, chẳng hạn như nhiều adapter, ControlNet, InstantStyle, hoặc LCM ít bước, khi một hình ảnh tham chiếu là chưa đủ. | |
| ### Nhiều IP-Adapter | |
| Kết hợp nhiều IP-Adapter để tạo hình ảnh với phong cách đa dạng hơn. Ví dụ, bạn có thể sử dụng IP-Adapter Face để tạo khuôn mặt và nhân vật nhất quán, và IP-Adapter Plus để tạo những khuôn mặt đó theo các phong cách cụ thể. | |
| Tải bộ mã hóa hình ảnh với [CLIPVisionModelWithProjection](https://huggingface.co/docs/transformers/v5.22.2/en/model_doc/clip#transformers.CLIPVisionModelWithProjection). | |
| ```py | |
| import torch | |
| from diffusers import AutoPipelineForText2Image, DDIMScheduler | |
| from transformers import CLIPVisionModelWithProjection | |
| from diffusers.utils import load_image | |
| image_encoder = CLIPVisionModelWithProjection.from_pretrained( | |
| "h94/IP-Adapter", | |
| subfolder="models/image_encoder", | |
| dtype=torch.float16, | |
| ) | |
| ``` | |
| Tải một mô hình cơ sở, trình lập lịch và các IP-Adapter sau. | |
| - [ip-adapter-plus_sdxl_vit-h](https://huggingface.co/h94/IP-Adapter#ip-adapter-for-sdxl-10) sử dụng patch embeddings và bộ mã hóa ảnh ViT-H | |
| - [ip-adapter-plus-face_sdxl_vit-h](https://huggingface.co/h94/IP-Adapter#ip-adapter-for-sdxl-10) sử dụng patch embeddings và bộ mã hóa ảnh ViT-H nhưng được điều kiện hóa dựa trên các ảnh khuôn mặt đã được cắt | |
| ```py | |
| pipeline = AutoPipelineForText2Image.from_pretrained( | |
| "stabilityai/stable-diffusion-xl-base-1.0", | |
| dtype=torch.float16, | |
| image_encoder=image_encoder, | |
| ) | |
| pipeline.scheduler = DDIMScheduler.from_config(pipeline.scheduler.config) | |
| pipeline.load_ip_adapter( | |
| "h94/IP-Adapter", | |
| subfolder="sdxl_models", | |
| weight_name=["ip-adapter-plus_sdxl_vit-h.safetensors", "ip-adapter-plus-face_sdxl_vit-h.safetensors"] | |
| ) | |
| pipeline.set_ip_adapter_scale([0.7, 0.3]) | |
| # enable_model_cpu_offload to reduce memory usage | |
| pipeline.enable_model_cpu_offload() | |
| ``` | |
| Tải một hình ảnh và một thư mục chứa các hình ảnh của một phong cách nhất định để áp dụng. | |
| ```py | |
| face_image = load_image("https://huggingface.co/datasets/YiYiXu/testing-images/resolve/main/women_input.png") | |
| style_folder = "https://huggingface.co/datasets/YiYiXu/testing-images/resolve/main/style_ziggy" | |
| style_images = [load_image(f"{style_folder}/img{i}.png") for i in range(10)] | |
| ``` | |
| ảnh khuôn mặt | |
| ảnh phong cách | |
| Truyền các ảnh phong cách và khuôn mặt dưới dạng danh sách vào `ip_adapter_image`. | |
| ```py | |
| generator = torch.Generator(device="cpu").manual_seed(0) | |
| pipeline( | |
| prompt="wonderwoman", | |
| ip_adapter_image=[style_images, face_image], | |
| negative_prompt="monochrome, lowres, bad anatomy, worst quality, low quality", | |
| ).images[0] | |
| ``` | |
| hình ảnh được tạo | |
| ### Kiểm soát cấu trúc | |
| Để kiểm soát cấu trúc, hãy kết hợp IP-Adapter với [ControlNet](../api/pipelines/controlnet) được điều kiện bởi các bản đồ độ sâu, bản đồ cạnh, ước lượng tư thế và nhiều hơn nữa. | |
| Ví dụ dưới đây tải checkpoint [ControlNetModel](/docs/diffusers/v0.43.0/vi/api/models/controlnet#diffusers.ControlNetModel) được điều kiện bởi bản đồ độ sâu và kết hợp nó với IP-Adapter. | |
| ```py | |
| import torch | |
| from diffusers.utils import load_image | |
| from diffusers import StableDiffusionControlNetPipeline, ControlNetModel | |
| controlnet = ControlNetModel.from_pretrained( | |
| "lllyasviel/control_v11f1p_sd15_depth", | |
| dtype=torch.float16 | |
| ) | |
| pipeline = StableDiffusionControlNetPipeline.from_pretrained( | |
| "stable-diffusion-v1-5/stable-diffusion-v1-5", | |
| controlnet=controlnet, | |
| dtype=torch.float16 | |
| ).to("cuda") # or "mps", "xpu", "cpu" | |
| pipeline.load_ip_adapter( | |
| "h94/IP-Adapter", | |
| subfolder="models", | |
| weight_name="ip-adapter_sd15.bin" | |
| ) | |
| ``` | |
| Truyền bản đồ độ sâu và hình ảnh IP-Adapter vào pipeline. | |
| ```py | |
| depth_map = load_image("https://huggingface.co/datasets/YiYiXu/testing-images/resolve/main/depth.png") | |
| ip_adapter_image = load_image("https://huggingface.co/datasets/YiYiXu/testing-images/resolve/main/statue.png") | |
| pipeline( | |
| prompt="best quality, high quality", | |
| image=depth_map, | |
| ip_adapter_image=ip_adapter_image, | |
| negative_prompt="monochrome, lowres, bad anatomy, worst quality, low quality", | |
| ).images[0] | |
| ``` | |
| Hình ảnh IP-Adapter | |
| bản đồ độ sâu | |
| hình ảnh được tạo | |
| ### Kiểm soát kiểu dáng và bố cục | |
| Để kiểm soát phong cách và bố cục, hãy kết hợp IP-Adapter với [InstantStyle](https://huggingface.co/papers/2404.02733). InstantStyle tách biệt *phong cách* (màu sắc, kết cấu, cảm giác tổng thể) khỏi *nội dung* và chỉ áp dụng phong cách trong các khối dành riêng cho phong cách, nhờ đó các vùng nội dung được giữ nguyên vẹn. Điều này mang lại tính nhất quán phong cách mạnh mẽ hơn và khả năng kiểm soát bố cục rõ ràng hơn. | |
| Chỉ kích hoạt IP-Adapter ở các lớp được chọn bằng [set_ip_adapter_scale()](/docs/diffusers/v0.43.0/vi/api/loaders/ip_adapter#diffusers.loaders.IPAdapterMixin.set_ip_adapter_scale). Ví dụ bên dưới bật nó ở khối xuống `block_2` (bố cục) và khối lên `block_0` (phong cách) của mô hình. | |
| ```py | |
| import torch | |
| from diffusers import AutoPipelineForText2Image | |
| from diffusers.utils import load_image | |
| pipeline = AutoPipelineForText2Image.from_pretrained( | |
| "stabilityai/stable-diffusion-xl-base-1.0", | |
| dtype=torch.float16 | |
| ).to("cuda") # or "mps", "xpu", "cpu" | |
| pipeline.load_ip_adapter( | |
| "h94/IP-Adapter", | |
| subfolder="sdxl_models", | |
| weight_name="ip-adapter_sdxl.bin" | |
| ) | |
| scale = { | |
| "down": {"block_2": [0.0, 1.0]}, | |
| "up": {"block_0": [0.0, 1.0, 0.0]}, | |
| } | |
| pipeline.set_ip_adapter_scale(scale) | |
| ``` | |
| Tải ảnh phong cách và tạo ảnh. | |
| ```py | |
| style_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/0052a70beed5bf71b92610a43a52df6d286cd5f3/diffusers/rabbit.jpg") | |
| pipeline( | |
| prompt="a cat, masterpiece, best quality, high quality", | |
| ip_adapter_image=style_image, | |
| negative_prompt="text, watermark, lowres, low quality, worst quality, deformed, glitch, low contrast, noisy, saturation, blurry", | |
| guidance_scale=5, | |
| ).images[0] | |
| ``` | |
| style image | |
| hình ảnh được tạo | |
| Các hình dưới đây so sánh kích hoạt chỉ phong cách (lên `block_0`) của InstantStyle với việc bật IP-Adapter ở tất cả các lớp. Chế độ tất cả các lớp thường bám sát prompt hình ảnh hơn và có thể làm giảm tính đa dạng. Hãy ưu tiên thang đo chỉ phong cách khi bạn muốn giữ nguyên bố cục của InstantStyle. | |
| > [!TIP] | |
| > Bạn không cần chỉ định tất cả các lớp trong từ điển `scale`. Các lớp không được bao gồm sẽ được đặt thành 0, điều này có nghĩa là IP-Adapter bị vô hiệu hóa. | |
| ```py | |
| scale = { | |
| "up": {"block_0": [0.0, 1.0, 0.0]}, | |
| } | |
| pipeline.set_ip_adapter_scale(scale) | |
| pipeline( | |
| prompt="a cat, masterpiece, best quality, high quality", | |
| ip_adapter_image=style_image, | |
| negative_prompt="text, watermark, lowres, low quality, worst quality, deformed, glitch, low contrast, noisy, saturation, blurry", | |
| guidance_scale=5, | |
| ).images[0] | |
| ``` | |
| chỉ style (up block_0) | |
| tất cả các lớp | |
| ### Tạo tức thì | |
| Kết hợp IP-Adapter với một LoRA [LCM](../api/pipelines/latent_consistency_models) để tạo ảnh trong vài bước. | |
| ```py | |
| import torch | |
| from diffusers import DiffusionPipeline, LCMScheduler | |
| from diffusers.utils import load_image | |
| pipeline = DiffusionPipeline.from_pretrained( | |
| "sd-dreambooth-library/herge-style", | |
| dtype=torch.float16 | |
| ) | |
| pipeline.load_ip_adapter( | |
| "h94/IP-Adapter", | |
| subfolder="models", | |
| weight_name="ip-adapter_sd15.bin" | |
| ) | |
| pipeline.load_lora_weights("latent-consistency/lcm-lora-sdv1-5") | |
| pipeline.scheduler = LCMScheduler.from_config(pipeline.scheduler.config) | |
| pipeline.enable_model_cpu_offload() | |
| pipeline.set_ip_adapter_scale(0.4) | |
| ip_adapter_image = load_image("https://user-images.githubusercontent.com/24734142/266492875-2d50d223-8475-44f0-a7c6-08b51cb53572.png") | |
| pipeline( | |
| prompt="herge_style woman in armor, best quality, high quality", | |
| ip_adapter_image=ip_adapter_image, | |
| num_inference_steps=4, | |
| guidance_scale=1, | |
| ).images[0] | |
| ``` | |
| hình ảnh được tạo | |
Xet Storage Details
- Size:
- 24.9 kB
- Xet hash:
- 14da644471101ed8ce55fd390ac153a2d33a53950e5c1374875d4590331800f6
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.