From 95b558f4491d624fe4fce0a56160ca96afe87b6c Mon Sep 17 00:00:00 2001 From: Parag Ekbote Date: Sat, 1 Mar 2025 06:47:39 +0000 Subject: [PATCH 1/2] Add example of Ip-Adapter-Callback. --- docs/source/en/using-diffusers/callback.md | 69 ++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/docs/source/en/using-diffusers/callback.md b/docs/source/en/using-diffusers/callback.md index 68c621ffc50d..7abfaf9c9bda 100644 --- a/docs/source/en/using-diffusers/callback.md +++ b/docs/source/en/using-diffusers/callback.md @@ -157,6 +157,75 @@ pipeline( ) ``` +## IP Adapter Cutoff + +IP Adapter is an image prompt adapter that can be used for diffusion models without any changes to the underlying model. We can use the IP Adapter Cutoff Callback to disable the IP Adapter after a certain number of steps. To set up the callback, you need to specify the number of denoising steps after which the callback comes into effect. You can do so by using either one of these two arguments: + +- `cutoff_step_ratio`: Float number with the ratio of the steps. +- `cutoff_step_index`: Integer number with the exact number of the step. + +We need to download the diffusion model and load the ip_adapter for it as follows: + +```py +from diffusers import AutoPipelineForText2Image +from diffusers.utils import load_image +import torch + +pipeline = AutoPipelineForText2Image.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16).to("cuda") +pipeline.load_ip_adapter("h94/IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter_sdxl.bin") +pipeline.set_ip_adapter_scale(0.6) +``` +The setup for the callback should look something like this: + +```py + +from diffusers import AutoPipelineForText2Image +from diffusers.callbacks import IPAdapterScaleCutoffCallback +from diffusers.utils import load_image +import torch + + +pipeline = AutoPipelineForText2Image.from_pretrained( + "stabilityai/stable-diffusion-xl-base-1.0", + torch_dtype=torch.float16 +).to("cuda") + + +pipeline.load_ip_adapter( + "h94/IP-Adapter", + subfolder="sdxl_models", + weight_name="ip-adapter_sdxl.bin" +) + +pipeline.set_ip_adapter_scale(0.6) + + +callback = IPAdapterScaleCutoffCallback( + cutoff_step_ratio=None, + cutoff_step_index=5 +) + +image = load_image( + "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/ip_adapter_diner.png" +) + +generator = torch.Generator(device="cuda").manual_seed(2628670641) + +images = pipeline( + prompt="a tiger sitting in a chair drinking orange juice", + ip_adapter_image=image, + negative_prompt="deformed, ugly, wrong proportion, low res, bad anatomy, worst quality, low quality", + generator=generator, + num_inference_steps=50, + callback_on_step_end=callback, +).images + +images[0].save("custom_callback_img.png") +``` + +without IPAdapterScaleCutoffCallback + +with IPAdapterScaleCutoffCallback ## Display image after each generation step > [!TIP] From 1e3389aeb7d0969a325adcd95e8c19111afe8b9d Mon Sep 17 00:00:00 2001 From: Parag Ekbote Date: Sun, 2 Mar 2025 14:34:34 +0000 Subject: [PATCH 2/2] Add image links from HF Hub. --- docs/source/en/using-diffusers/callback.md | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/docs/source/en/using-diffusers/callback.md b/docs/source/en/using-diffusers/callback.md index 7abfaf9c9bda..2462fed1a3cf 100644 --- a/docs/source/en/using-diffusers/callback.md +++ b/docs/source/en/using-diffusers/callback.md @@ -223,9 +223,18 @@ images = pipeline( images[0].save("custom_callback_img.png") ``` -without IPAdapterScaleCutoffCallback +
+
+ generated image of a tiger sitting in a chair drinking orange juice +
without IPAdapterScaleCutoffCallback
+
+
+ generated image of a tiger sitting in a chair drinking orange juice with ip adapter callback +
with IPAdapterScaleCutoffCallback
+
+
+ -with IPAdapterScaleCutoffCallback ## Display image after each generation step > [!TIP]