File size: 9,686 Bytes
437acd2 1881086 437acd2 f6f262b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
---
license: openrail
base_model: runwayml/stable-diffusion-v1-5
tags:
- art
- controlnet
- stable-diffusion
---
# Controlnet
Controlnet is an auxiliary model which augments pre-trained diffusion models with an additional conditioning.
Controlnet comes with multiple auxiliary models, each which allows a different type of conditioning
Controlnet's auxiliary models are trained with stable diffusion 1.5. Experimentally, the auxiliary models can be used with other diffusion models such as dreamboothed stable diffusion.
The auxiliary conditioning is passed directly to the diffusers pipeline. If you want to process an image to create the auxiliary conditioning, external dependencies are required.
Some of the additional conditionings can be extracted from images via additional models. We extracted these
additional models from the original controlnet repo into a separate package that can be found on [github](https://github.com/patrickvonplaten/human_pose.git).
## Canny edge detection
Install opencv
```sh
$ pip install opencv-contrib-python
```
```python
import cv2
from PIL import Image
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
import torch
import numpy as np
image = Image.open('images/bird.png')
image = np.array(image)
low_threshold = 100
high_threshold = 200
image = cv2.Canny(image, low_threshold, high_threshold)
image = image[:, :, None]
image = np.concatenate([image, image, image], axis=2)
image = Image.fromarray(image)
controlnet = ControlNetModel.from_pretrained(
"fusing/stable-diffusion-v1-5-controlnet-canny",
)
pipe = StableDiffusionControlNetPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5", controlnet=controlnet, safety_checker=None
)
pipe.to('cuda')
image = pipe("bird", image).images[0]
image.save('images/bird_canny_out.png')
```
![bird](./images/bird.png)
![bird_canny](./images/bird_canny.png)
![bird_canny_out](./images/bird_canny_out.png)
## M-LSD Straight line detection
Install the additional controlnet models package.
```sh
$ pip install git+https://github.com/patrickvonplaten/human_pose.git
```
```py
from PIL import Image
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
import torch
from human_pose import MLSDdetector
mlsd = MLSDdetector.from_pretrained('lllyasviel/ControlNet')
image = Image.open('images/room.png')
image = mlsd(image)
controlnet = ControlNetModel.from_pretrained(
"fusing/stable-diffusion-v1-5-controlnet-mlsd",
)
pipe = StableDiffusionControlNetPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5", controlnet=controlnet, safety_checker=None
)
pipe.to('cuda')
image = pipe("room", image).images[0]
image.save('images/room_mlsd_out.png')
```
![room](./images/room.png)
![room_mlsd](./images/room_mlsd.png)
![room_mlsd_out](./images/room_mlsd_out.png)
## Pose estimation
Install the additional controlnet models package.
```sh
$ pip install git+https://github.com/patrickvonplaten/human_pose.git
```
```py
from PIL import Image
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
import torch
from human_pose import OpenposeDetector
openpose = OpenposeDetector.from_pretrained('lllyasviel/ControlNet')
image = Image.open('images/pose.png')
image = openpose(image)
controlnet = ControlNetModel.from_pretrained(
"fusing/stable-diffusion-v1-5-controlnet-openpose",
)
pipe = StableDiffusionControlNetPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5", controlnet=controlnet, safety_checker=None
)
pipe.to('cuda')
image = pipe("chef in the kitchen", image).images[0]
image.save('images/chef_pose_out.png')
```
![pose](./images/pose.png)
![openpose](./images/openpose.png)
![chef_pose_out](./images/chef_pose_out.png)
## Semantic Segmentation
Semantic segmentation relies on transformers. Transformers is a
dependency of diffusers for running controlnet, so you should
have it installed already.
```py
from transformers import AutoImageProcessor, UperNetForSemanticSegmentation
from PIL import Image
import numpy as np
from controlnet_utils import ade_palette
import torch
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
image_processor = AutoImageProcessor.from_pretrained("openmmlab/upernet-convnext-small")
image_segmentor = UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-convnext-small")
image = Image.open("./images/house.png").convert('RGB')
pixel_values = image_processor(image, return_tensors="pt").pixel_values
with torch.no_grad():
outputs = image_segmentor(pixel_values)
seg = image_processor.post_process_semantic_segmentation(outputs, target_sizes=[image.size[::-1]])[0]
color_seg = np.zeros((seg.shape[0], seg.shape[1], 3), dtype=np.uint8) # height, width, 3
palette = np.array(ade_palette())
for label, color in enumerate(palette):
color_seg[seg == label, :] = color
color_seg = color_seg.astype(np.uint8)
image = Image.fromarray(color_seg)
controlnet = ControlNetModel.from_pretrained(
"fusing/stable-diffusion-v1-5-controlnet-seg",
)
pipe = StableDiffusionControlNetPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5", controlnet=controlnet, safety_checker=None
)
pipe.to('cuda')
image = pipe("house", image).images[0]
image.save('./images/house_seg_out.png')
```
![house](images/house.png)
![house_seg](images/house_seg.png)
![house_seg_out](images/house_seg_out.png)
## Depth control
Depth control relies on transformers. Transformers is a dependency of diffusers for running controlnet, so
you should have it installed already.
```py
from transformers import pipeline
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
from PIL import Image
import numpy as np
depth_estimator = pipeline('depth-estimation')
image = Image.open('./images/stormtrooper.png')
image = depth_estimator(image)['depth']
image = np.array(image)
image = image[:, :, None]
image = np.concatenate([image, image, image], axis=2)
image = Image.fromarray(image)
controlnet = ControlNetModel.from_pretrained(
"fusing/stable-diffusion-v1-5-controlnet-depth",
)
pipe = StableDiffusionControlNetPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5", controlnet=controlnet, safety_checker=None
)
pipe.to('cuda')
image = pipe("Stormtrooper's lecture", image).images[0]
image.save('./images/stormtrooper_depth_out.png')
```
![stormtrooper](./images/stormtrooper.png)
![stormtrooler_depth](./images/stormtrooper_depth.png)
![stormtrooler_depth_out](./images/stormtrooper_depth_out.png)
## Normal map
```py
from PIL import Image
from transformers import pipeline
import numpy as np
import cv2
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
image = Image.open("images/toy.png").convert("RGB")
depth_estimator = pipeline("depth-estimation", model ="Intel/dpt-hybrid-midas" )
image = depth_estimator(image)['predicted_depth'][0]
image = image.numpy()
image_depth = image.copy()
image_depth -= np.min(image_depth)
image_depth /= np.max(image_depth)
bg_threhold = 0.4
x = cv2.Sobel(image, cv2.CV_32F, 1, 0, ksize=3)
x[image_depth < bg_threhold] = 0
y = cv2.Sobel(image, cv2.CV_32F, 0, 1, ksize=3)
y[image_depth < bg_threhold] = 0
z = np.ones_like(x) * np.pi * 2.0
image = np.stack([x, y, z], axis=2)
image /= np.sum(image ** 2.0, axis=2, keepdims=True) ** 0.5
image = (image * 127.5 + 127.5).clip(0, 255).astype(np.uint8)
image = Image.fromarray(image)
controlnet = ControlNetModel.from_pretrained(
"fusing/stable-diffusion-v1-5-controlnet-normal",
)
pipe = StableDiffusionControlNetPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5", controlnet=controlnet, safety_checker=None
)
pipe.to('cuda')
image = pipe("cute toy", image).images[0]
image.save('images/toy_normal_out.png')
```
![toy](./images/toy.png)
![toy_normal](./images/toy_normal.png)
![toy_normal_out](./images/toy_normal_out.png)
## Scribble
Install the additional controlnet models package.
```sh
$ pip install git+https://github.com/patrickvonplaten/human_pose.git
```
```py
from PIL import Image
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
import torch
from human_pose import HEDdetector
hed = HEDdetector.from_pretrained('lllyasviel/ControlNet')
image = Image.open('images/bag.png')
image = hed(image, scribble=True)
controlnet = ControlNetModel.from_pretrained(
"fusing/stable-diffusion-v1-5-controlnet-scribble",
)
pipe = StableDiffusionControlNetPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5", controlnet=controlnet, safety_checker=None
)
pipe.to('cuda')
image = pipe("bag", image).images[0]
image.save('images/bag_scribble_out.png')
```
![bag](./images/bag.png)
![bag_scribble](./images/bag_scribble.png)
![bag_scribble_out](./images/bag_scribble_out.png)
## HED Boundary
Install the additional controlnet models package.
```sh
$ pip install git+https://github.com/patrickvonplaten/human_pose.git
```
```py
from PIL import Image
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
import torch
from human_pose import HEDdetector
hed = HEDdetector.from_pretrained('lllyasviel/ControlNet')
image = Image.open('images/man.png')
image = hed(image)
controlnet = ControlNetModel.from_pretrained(
"fusing/stable-diffusion-v1-5-controlnet-hed",
)
pipe = StableDiffusionControlNetPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5", controlnet=controlnet, safety_checker=None
)
pipe.to('cuda')
image = pipe("oil painting of handsome old man, masterpiece", image).images[0]
image.save('images/man_hed_out.png')
```
![man](./images/man.png)
![man_hed](./images/man_hed.png)
![man_hed_out](./images/man_hed_out.png)
|