Skip to content

Commit b134f6a

Browse files
[Community] ControlNet Reference (#3508)
add controlnet reference and bugfix Co-authored-by: Patrick von Platen <[email protected]>
1 parent edc6505 commit b134f6a

File tree

3 files changed

+900
-24
lines changed

3 files changed

+900
-24
lines changed

examples/community/README.md

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,7 +1324,7 @@ image.save('tensorrt_img2img_new_zealand_hills.png')
13241324

13251325
### Stable Diffusion Reference
13261326

1327-
This pipeline uses the Reference only Control. Refer to the [sd-webui-controlnet discussion](https://github.com/Mikubill/sd-webui-controlnet/discussions/1236).
1327+
This pipeline uses the Reference Control. Refer to the [sd-webui-controlnet discussion: Reference-only Control](https://github.com/Mikubill/sd-webui-controlnet/discussions/1236)[sd-webui-controlnet discussion: Reference-adain Control](https://github.com/Mikubill/sd-webui-controlnet/discussions/1280).
13281328

13291329

13301330
```py
@@ -1365,6 +1365,54 @@ Output Image of `reference_attn=True` and `reference_adain=True`
13651365

13661366
![output_image](https://github.com/huggingface/diffusers/assets/24734142/3c5255d6-867d-4d35-b202-8dfd30cc6827)
13671367

1368+
### Stable Diffusion ControlNet Reference
1369+
1370+
This pipeline uses the Reference Control with ControlNet. Refer to the [sd-webui-controlnet discussion: Reference-only Control](https://github.com/Mikubill/sd-webui-controlnet/discussions/1236)[sd-webui-controlnet discussion: Reference-adain Control](https://github.com/Mikubill/sd-webui-controlnet/discussions/1280).
1371+
1372+
1373+
```py
1374+
import cv2
1375+
import torch
1376+
import numpy as np
1377+
from PIL import Image
1378+
from diffusers import UniPCMultistepScheduler
1379+
from diffusers.utils import load_image
1380+
1381+
input_image = load_image("https://hf.co/datasets/huggingface/documentation-images/resolve/main/diffusers/input_image_vermeer.png")
1382+
1383+
# get canny image
1384+
image = cv2.Canny(np.array(input_image), 100, 200)
1385+
image = image[:, :, None]
1386+
image = np.concatenate([image, image, image], axis=2)
1387+
canny_image = Image.fromarray(image)
1388+
1389+
controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny", torch_dtype=torch.float16)
1390+
pipe = StableDiffusionControlNetReferencePipeline.from_pretrained(
1391+
"runwayml/stable-diffusion-v1-5",
1392+
controlnet=controlnet,
1393+
safety_checker=None,
1394+
torch_dtype=torch.float16
1395+
).to('cuda:0')
1396+
1397+
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
1398+
1399+
result_img = pipe(ref_image=input_image,
1400+
prompt="1girl",
1401+
image=canny_image,
1402+
num_inference_steps=20,
1403+
reference_attn=True,
1404+
reference_adain=True).images[0]
1405+
```
1406+
1407+
Reference Image
1408+
1409+
![reference_image](https://hf.co/datasets/huggingface/documentation-images/resolve/main/diffusers/input_image_vermeer.png)
1410+
1411+
Output Image
1412+
1413+
![output_image](https://github.com/huggingface/diffusers/assets/24734142/7b9a5830-f173-4b92-b0cf-73d0e9c01d60)
1414+
1415+
13681416
### Stable Diffusion on IPEX
13691417

13701418
This diffusion pipeline aims to accelarate the inference of Stable-Diffusion on Intel Xeon CPUs with BF16/FP32 precision using [IPEX](https://github.com/intel/intel-extension-for-pytorch).
@@ -1462,4 +1510,3 @@ latency = elapsed_time(pipe4)
14621510
print("Latency of StableDiffusionPipeline--fp32",latency)
14631511

14641512
```
1465-

0 commit comments

Comments
 (0)