Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IP-Adapter for StableDiffusion3Img2ImgPipeline #10589

Merged
merged 2 commits into from
Jan 16, 2025

Conversation

guiyrt
Copy link
Contributor

@guiyrt guiyrt commented Jan 15, 2025

What does this PR do?

Extends SD3 IP-Adapter implementation to StableDiffusion3Img2ImgPipeline, part of #9966. Some outputs for reference:

Inference code
import torch

from diffusers import StableDiffusion3Img2ImgPipeline
from diffusers.utils import load_image
from transformers import SiglipVisionModel, SiglipImageProcessor

model_id = "stabilityai/stable-diffusion-3.5-large"
image_encoder_id = "google/siglip-so400m-patch14-384"
ip_adapter_id = "guiyrt/InstantX-SD3.5-Large-IP-Adapter-diffusers"

feature_extractor = SiglipImageProcessor.from_pretrained(
    image_encoder_id, torch_dtype=torch.float16
)

image_encoder = SiglipVisionModel.from_pretrained(
    image_encoder_id, torch_dtype=torch.float16
)

pipe = StableDiffusion3Img2ImgPipeline.from_pretrained(
    model_id,
    torch_dtype=torch.float16,
    feature_extractor=feature_extractor,
    image_encoder=image_encoder,
)

# Load IP Adapter
pipe.load_ip_adapter(ip_adapter_id)
pipe.set_ip_adapter_scale(0.75)
pipe._exclude_from_cpu_offload.append("image_encoder")
pipe.enable_sequential_cpu_offload()

# Input
source_image = load_image("source.jpg")
ip_adapter_img = load_image("ip_adapter_img.png")

# please note that SD3.5 Large is sensitive to highres generation like 1536x1536
images = pipe(
    image=source_image,
    width=1024,
    height=1024,
    prompt="an astronaut",
    negative_prompt="lowres, low quality, worst quality",
    num_images_per_prompt=4,
    generator=torch.manual_seed(42),
    ip_adapter_image=ip_adapter_img,
    guidance_scale=6,
    num_inference_steps=60,
    strength=0.65,
).images

for i, image in enumerate(images):
    image.save(f"result_{i}.jpg")

astro_grid

Before submitting

Who can review?

@hlky @yiyixuxu

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Collaborator

@hlky hlky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @guiyrt

@hlky hlky merged commit e8114bd into huggingface:main Jan 16, 2025
12 checks passed
@guiyrt guiyrt deleted the sd3-img2img-ipadapter branch January 16, 2025 11:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants