Skip to content

Commit 69fdb87

Browse files
authored
[Pipeline] Adding BoxDiff to community examples (#7947)
add boxdiff to community examples
1 parent b2140a8 commit 69fdb87

File tree

2 files changed

+1763
-0
lines changed

2 files changed

+1763
-0
lines changed

examples/community/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Please also check out our [Community Scripts](https://github.com/huggingface/dif
6868
| InstantID Pipeline | Stable Diffusion XL Pipeline that supports InstantID | [InstantID Pipeline](#instantid-pipeline) | [![Hugging Face Space](https://img.shields.io/badge/🤗%20Hugging%20Face-Space-yellow)](https://huggingface.co/spaces/InstantX/InstantID) | [Haofan Wang](https://github.com/haofanwang) |
6969
| UFOGen Scheduler | Scheduler for UFOGen Model (compatible with Stable Diffusion pipelines) | [UFOGen Scheduler](#ufogen-scheduler) | - | [dg845](https://github.com/dg845) |
7070
| Stable Diffusion XL IPEX Pipeline | Accelerate Stable Diffusion XL inference pipeline with BF16/FP32 precision on Intel Xeon CPUs with [IPEX](https://github.com/intel/intel-extension-for-pytorch) | [Stable Diffusion XL on IPEX](#stable-diffusion-xl-on-ipex) | - | [Dan Li](https://github.com/ustcuna/) |
71+
| Stable Diffusion BoxDiff Pipeline | Training-free controlled generation with bounding boxes using [BoxDiff](https://github.com/showlab/BoxDiff) | [Stable Diffusion BoxDiff Pipeline](#stable-diffusion-boxdiff) | - | [Jingyang Zhang](https://github.com/zjysteven/) |
7172

7273
To load a custom pipeline you just need to pass the `custom_pipeline` argument to `DiffusionPipeline`, as one of the files in `diffusers/examples/community`. Feel free to send a PR with your own pipelines, we will merge them quickly.
7374

@@ -1676,6 +1677,68 @@ image = pipe(prompt, image=input_image, strength=0.75,).images[0]
16761677
image.save('tensorrt_img2img_new_zealand_hills.png')
16771678
```
16781679

1680+
### Stable Diffusion BoxDiff
1681+
BoxDiff is a training-free method for controlled generation with bounding box coordinates. It shoud work with any Stable Diffusion model. Below shows an example with `stable-diffusion-2-1-base`.
1682+
```py
1683+
import torch
1684+
from PIL import Image, ImageDraw
1685+
from copy import deepcopy
1686+
1687+
from examples.community.pipeline_stable_diffusion_boxdiff import StableDiffusionBoxDiffPipeline
1688+
1689+
def draw_box_with_text(img, boxes, names):
1690+
colors = ["red", "olive", "blue", "green", "orange", "brown", "cyan", "purple"]
1691+
img_new = deepcopy(img)
1692+
draw = ImageDraw.Draw(img_new)
1693+
1694+
W, H = img.size
1695+
for bid, box in enumerate(boxes):
1696+
draw.rectangle([box[0] * W, box[1] * H, box[2] * W, box[3] * H], outline=colors[bid % len(colors)], width=4)
1697+
draw.text((box[0] * W, box[1] * H), names[bid], fill=colors[bid % len(colors)])
1698+
return img_new
1699+
1700+
pipe = StableDiffusionBoxDiffPipeline.from_pretrained(
1701+
"stabilityai/stable-diffusion-2-1-base",
1702+
torch_dtype=torch.float16,
1703+
)
1704+
pipe.to("cuda")
1705+
1706+
# example 1
1707+
prompt = "as the aurora lights up the sky, a herd of reindeer leisurely wanders on the grassy meadow, admiring the breathtaking view, a serene lake quietly reflects the magnificent display, and in the distance, a snow-capped mountain stands majestically, fantasy, 8k, highly detailed"
1708+
phrases = [
1709+
"aurora",
1710+
"reindeer",
1711+
"meadow",
1712+
"lake",
1713+
"mountain"
1714+
]
1715+
boxes = [[1,3,512,202], [75,344,421,495], [1,327,508,507], [2,217,507,341], [1,135,509,242]]
1716+
1717+
# example 2
1718+
# prompt = "A rabbit wearing sunglasses looks very proud"
1719+
# phrases = ["rabbit", "sunglasses"]
1720+
# boxes = [[67,87,366,512], [66,130,364,262]]
1721+
1722+
boxes = [[x / 512 for x in box] for box in boxes]
1723+
1724+
images = pipe(
1725+
prompt,
1726+
boxdiff_phrases=phrases,
1727+
boxdiff_boxes=boxes,
1728+
boxdiff_kwargs={
1729+
"attention_res": 16,
1730+
"normalize_eot": True
1731+
},
1732+
num_inference_steps=50,
1733+
guidance_scale=7.5,
1734+
generator=torch.manual_seed(42),
1735+
safety_checker=None
1736+
).images
1737+
1738+
draw_box_with_text(images[0], boxes, phrases).save("output.png")
1739+
```
1740+
1741+
16791742
### Stable Diffusion Reference
16801743

16811744
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).

0 commit comments

Comments
 (0)