diffusers icon indicating copy to clipboard operation
diffusers copied to clipboard

StableDiffusionXLControlNetImg2ImgPipeline Pipeline: torch.Size() takes an iterable of 'int' (item 4 is 'float')

Open shubhdotai opened this issue 3 years ago • 4 comments

Describe the bug

Error while using StableDiffusionXLControlNetImg2ImgPipeline with Refiner model and Depth ControlNet

  812 def _get_add_time_ids(
  813     self,
  814     original_size,
 (...)
  822     dtype,
  823 ):
  824     if self.config.requires_aesthetics_score:
--> 825         add_time_ids = list(original_size + crops_coords_top_left + (aesthetic_score,))
  826         add_neg_time_ids = list(
  827             negative_original_size + negative_crops_coords_top_left + (negative_aesthetic_score,)
  828         )
  829     else:

TypeError: torch.Size() takes an iterable of 'int' (item 4 is 'float')

Reproduction

import torch
import numpy as np
from PIL import Image

from transformers import DPTFeatureExtractor, DPTForDepthEstimation
from diffusers import ControlNetModel, EulerAncestralDiscreteScheduler, StableDiffusionXLControlNetPipeline, StableDiffusionXLControlNetImg2ImgPipeline, AutoencoderKL
from diffusers.utils import load_image

depth_estimator = DPTForDepthEstimation.from_pretrained("Intel/dpt-hybrid-midas").to("cuda")
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-hybrid-midas")


def get_depth_map(image):
    image = feature_extractor(images=image, return_tensors="pt").pixel_values.to("cuda")
    with torch.no_grad(), torch.autocast("cuda"):
        depth_map = depth_estimator(image).predicted_depth

    depth_map = torch.nn.functional.interpolate(
        depth_map.unsqueeze(1),
        size=(1024, 1024),
        mode="bicubic",
        align_corners=False,
    )
    depth_min = torch.amin(depth_map, dim=[1, 2, 3], keepdim=True)
    depth_max = torch.amax(depth_map, dim=[1, 2, 3], keepdim=True)
    depth_map = (depth_map - depth_min) / (depth_max - depth_min)
    image = torch.cat([depth_map] * 3, dim=1)

    image = image.permute(0, 2, 3, 1).cpu().numpy()[0]
    image = Image.fromarray((image * 255.0).clip(0, 255).astype(np.uint8))
    return image


image = load_image("https://huggingface.co/lllyasviel/sd-controlnet-depth/resolve/main/images/stormtrooper.png")
depth_image = get_depth_map(image)

# Models
SDXL_VAE = "madebyollin/sdxl-vae-fp16-fix"
SDXL_DEPTH_CONTROL = "diffusers/controlnet-depth-sdxl-1.0"
SDXL_BASE = "stabilityai/stable-diffusion-xl-base-1.0"
SDXL_REFINER = "stabilityai/stable-diffusion-xl-refiner-1.0"


sdxl_vae = AutoencoderKL.from_pretrained(SDXL_VAE, torch_dtype=torch.float16).to("cuda")
sdxl_depth_controlnet = ControlNetModel.from_pretrained(
    SDXL_DEPTH_CONTROL,
    variant="fp16",
    use_safetensors=True,
    torch_dtype=torch.float16,
).to("cuda")


# Text2Img Pipe
sdxl_pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
    SDXL_BASE,
    controlnet=sdxl_depth_controlnet,
    vae=sdxl_vae,
    variant="fp16",
    use_safetensors=True,
    torch_dtype=torch.float16,
).to("cuda")
sdxl_pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(sdxl_pipe.scheduler.config)

# Firstpass Image
firstpass = sdxl_pipe(
    prompt="In a garden, best quality",
    negative_prompt="worst quality",
    generator=torch.manual_seed(1234),
    width=1024,
    height=1024,
    num_inference_steps=30,
    image=depth_image,
    controlnet_conditioning_scale=0.5,
    guidance_scale=6,
    num_images_per_prompt=1,
).images[0]

sdxl_pipe.enable_model_cpu_offload()

# Img2Img Pipe (Refiner)
sdxl_refined = StableDiffusionXLControlNetImg2ImgPipeline.from_pretrained(
    SDXL_REFINER,
    controlnet=sdxl_depth_controlnet,
    vae=sdxl_vae,
    text_encoder_2=sdxl_pipe.text_encoder_2,
    variant="fp16",
    use_safetensors=True,
    torch_dtype=torch.float16,
).to("cuda")
sdxl_refined.scheduler = EulerAncestralDiscreteScheduler.from_config(sdxl_refined.scheduler.config)

# Refined Image
refined = sdxl_refined(
    prompt="In a garden, best quality",
    negative_prompt="worst quality",
    generator=torch.manual_seed(1234),
    width=1024,
    height=1024,
    num_inference_steps=30,
    image= firstpass.convert("RGB"),
    control_image= depth_image,
    controlnet_conditioning_scale=0.5,
    guidance_scale=6,
    num_images_per_prompt=1,
).images[0]

Logs

No response

System Info

  • diffusers version: 0.22.0.dev0
  • Platform: Linux-5.15.0-1041-aws-x86_64-with-glibc2.31
  • Python version: 3.10.12
  • PyTorch version (GPU?): 2.0.1 (True)
  • Huggingface_hub version: 0.16.4
  • Transformers version: 4.30.2
  • Accelerate version: 0.21.0
  • xFormers version: 0.0.20
  • Using GPU in script?:
  • Using distributed or parallel set-up in script?:

Who can help?

@sayakpaul @patrickvonplaten @will

shubhdotai avatar Sep 20 '23 05:09 shubhdotai

I don't the think refiner is expected to be used here, though. Pinging @yiyixuxu for further clarification.

You could probably just pass the initial image (firstpass) to a regular SDXL img2img refiner pipeline here no?

sayakpaul avatar Sep 20 '23 08:09 sayakpaul

I don't the think refiner is expected to be used here, though. Pinging @yiyixuxu for further clarification.

You could probably just pass the initial image (firstpass) to a regular SDXL img2img refiner pipeline here no?

Got it. So it works if I use Base model in Img2Img ControlNet Pipeline but doesn't work with Refiner Model. Is there any way I can use Refiner Model along with ControlNet Conditioning?

shubhdotai avatar Sep 20 '23 10:09 shubhdotai

Yeah I think this won't work because the controlnet has only been trained on stable-diffusion-base-xl and not on the refiner. The refiner has a very different architecture

patrickvonplaten avatar Sep 20 '23 14:09 patrickvonplaten

Thanks for the clarification. Closing the issue!

shubhdotai avatar Sep 20 '23 16:09 shubhdotai