liming-ai / ControlNet_Plus_Plus

Official PyTorch implementation of ECCV 2024 Paper: ControlNet++: Improving Conditional Controls with Efficient Consistency Feedback.
https://liming-ai.github.io/ControlNet_Plus_Plus
Apache License 2.0
367 stars 16 forks source link

Standalone script to run inference #3

Closed ogencoglu closed 4 months ago

ogencoglu commented 4 months ago

Thanks for the great work!

Do you have a standalone script/notebook to run without the demo or UI?

liming-ai commented 4 months ago

Thanks for the great work!

Do you have a standalone script/notebook to run without the demo or UI?

Thanks for your interest, you can refer to the code implementation of the original ControlNet and replace the checkpoint path. For example:

import torch
import os
from huggingface_hub import HfApi
from pathlib import Path
from diffusers.utils import load_image
from PIL import Image
import numpy as np
from controlnet_aux import LineartDetector

from diffusers import (
    ControlNetModel,
    StableDiffusionControlNetPipeline,
    UniPCMultistepScheduler,
)

# replace this checkpoint with our ControlNet++
checkpoint = "ControlNet-1-1-preview/control_v11p_sd15_lineart"

image = load_image(
    "https://huggingface.co/ControlNet-1-1-preview/control_v11p_sd15_lineart/resolve/main/images/input.png"
)
image = image.resize((512, 512))

prompt = "michael jackson concert"
processor = LineartDetector.from_pretrained("lllyasviel/Annotators")

control_image = processor(image)
control_image.save("./images/control.png")

controlnet = ControlNetModel.from_pretrained(checkpoint, torch_dtype=torch.float16)
pipe = StableDiffusionControlNetPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float16
)

pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
pipe.enable_model_cpu_offload()

generator = torch.manual_seed(0)
image = pipe(prompt, num_inference_steps=30, generator=generator, image=control_image).images[0]

image.save('images/image_out.png')
ogencoglu commented 4 months ago

Thanks for the swift reply!