Views
No views yet
diffusers format.
It can be used in combination with Stable Diffusion, such as runwayml/stable-diffusion-v1-5.
diffusers and related packages:$ pip install diffusers transformers accelerate1# !pip install transformers accelerate
2from diffusers import StableDiffusionControlNetInpaintPipeline, ControlNetModel
3from diffusers.utils import load_image
4import numpy as np
5import torch
6
7init_image = load_image(
8 "https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_inpaint/boy.png"
9)
10init_image = init_image.resize((512, 512))
11
12generator = torch.Generator(device="cpu").manual_seed(1)
13
14mask_image = load_image(
15 "https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_inpaint/boy_mask.png"
16)
17mask_image = mask_image.resize((512, 512))
18
19
20def make_inpaint_condition(image, image_mask):
21 image = np.array(image.convert("RGB")).astype(np.float32) / 255.0
22 image_mask = np.array(image_mask.convert("L")).astype(np.float32) / 255.0
23
24 assert image.shape[0:1] == image_mask.shape[0:1], "image and image_mask must have the same image size"
25 image[image_mask > 0.5] = -1.0 # set as masked pixel
26 image = np.expand_dims(image, 0).transpose(0, 3, 1, 2)
27 image = torch.from_numpy(image)
28 return image
29
30
31control_image = make_inpaint_condition(init_image, mask_image)
32
33controlnet = ControlNetModel.from_pretrained(
34 "lllyasviel/control_v11p_sd15_inpaint", torch_dtype=torch.float16
35)
36pipe = StableDiffusionControlNetInpaintPipeline.from_pretrained(
37 "runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float16
38)
39
40pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
41pipe.enable_model_cpu_offload()
42
43# generate image
44image = pipe(
45 "a handsome man with ray-ban sunglasses",
46 num_inference_steps=20,
47 generator=generator,
48 eta=1.0,
49 image=init_image,
50 mask_image=mask_image,
51 control_image=control_image,
52).images[0]


| Model Name | Control Image Overview | Condition Image | Control Image Example | Generated Image Example |
|---|---|---|---|---|
| lllyasviel/control_v11p_sd15_canny | Trained with canny edge detection | A monochrome image with white edges on a black background. | ![]() | ![]() |
| lllyasviel/control_v11e_sd15_ip2p | Trained with pixel to pixel instruction | No condition . | ![]() | ![]() |
| lllyasviel/control_v11p_sd15_inpaint | Trained with image inpainting | No condition. | ![]() | ![]() |
| lllyasviel/control_v11p_sd15_mlsd | Trained with multi-level line segment detection | An image with annotated line segments. | ![]() | ![]() |
| lllyasviel/control_v11f1p_sd15_depth | Trained with depth estimation | An image with depth information, usually represented as a grayscale image. | ![]() | ![]() |
| lllyasviel/control_v11p_sd15_normalbae | Trained with surface normal estimation | An image with surface normal information, usually represented as a color-coded image. | ![]() | ![]() |
| lllyasviel/control_v11p_sd15_seg | Trained with image segmentation | An image with segmented regions, usually represented as a color-coded image. | ![]() | ![]() |
| lllyasviel/control_v11p_sd15_lineart | Trained with line art generation | An image with line art, usually black lines on a white background. | ![]() | ![]() |
| lllyasviel/control_v11p_sd15s2_lineart_anime | Trained with anime line art generation | An image with anime-style line art. | ![]() | ![]() |
| lllyasviel/control_v11p_sd15_openpose | Trained with human pose estimation | An image with human poses, usually represented as a set of keypoints or skeletons. | ![]() | ![]() |
| lllyasviel/control_v11p_sd15_scribble | Trained with scribble-based image generation | An image with scribbles, usually random or user-drawn strokes. | ![]() | ![]() |
| lllyasviel/control_v11p_sd15_softedge | Trained with soft edge image generation | An image with soft edges, usually to create a more painterly or artistic effect. | ![]() | ![]() |
| lllyasviel/control_v11e_sd15_shuffle | Trained with image shuffling | An image with shuffled patches or regions. | ![]() | ![]() |
| lllyasviel/control_v11f1e_sd15_tile | Trained with image tiling | A blurry image or part of an image . | ![]() | ![]() |