mirror of
https://github.com/huggingface/diffusers.git
synced 2026-01-27 17:22:53 +03:00
[Pipeline] Adding BoxDiff to community examples (#7947)
add boxdiff to community examples
This commit is contained in:
@@ -68,6 +68,7 @@ Please also check out our [Community Scripts](https://github.com/huggingface/dif
|
||||
| InstantID Pipeline | Stable Diffusion XL Pipeline that supports InstantID | [InstantID Pipeline](#instantid-pipeline) | [](https://huggingface.co/spaces/InstantX/InstantID) | [Haofan Wang](https://github.com/haofanwang) |
|
||||
| UFOGen Scheduler | Scheduler for UFOGen Model (compatible with Stable Diffusion pipelines) | [UFOGen Scheduler](#ufogen-scheduler) | - | [dg845](https://github.com/dg845) |
|
||||
| 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/) |
|
||||
| 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/) |
|
||||
|
||||
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.
|
||||
|
||||
@@ -1676,6 +1677,68 @@ image = pipe(prompt, image=input_image, strength=0.75,).images[0]
|
||||
image.save('tensorrt_img2img_new_zealand_hills.png')
|
||||
```
|
||||
|
||||
### Stable Diffusion BoxDiff
|
||||
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`.
|
||||
```py
|
||||
import torch
|
||||
from PIL import Image, ImageDraw
|
||||
from copy import deepcopy
|
||||
|
||||
from examples.community.pipeline_stable_diffusion_boxdiff import StableDiffusionBoxDiffPipeline
|
||||
|
||||
def draw_box_with_text(img, boxes, names):
|
||||
colors = ["red", "olive", "blue", "green", "orange", "brown", "cyan", "purple"]
|
||||
img_new = deepcopy(img)
|
||||
draw = ImageDraw.Draw(img_new)
|
||||
|
||||
W, H = img.size
|
||||
for bid, box in enumerate(boxes):
|
||||
draw.rectangle([box[0] * W, box[1] * H, box[2] * W, box[3] * H], outline=colors[bid % len(colors)], width=4)
|
||||
draw.text((box[0] * W, box[1] * H), names[bid], fill=colors[bid % len(colors)])
|
||||
return img_new
|
||||
|
||||
pipe = StableDiffusionBoxDiffPipeline.from_pretrained(
|
||||
"stabilityai/stable-diffusion-2-1-base",
|
||||
torch_dtype=torch.float16,
|
||||
)
|
||||
pipe.to("cuda")
|
||||
|
||||
# example 1
|
||||
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"
|
||||
phrases = [
|
||||
"aurora",
|
||||
"reindeer",
|
||||
"meadow",
|
||||
"lake",
|
||||
"mountain"
|
||||
]
|
||||
boxes = [[1,3,512,202], [75,344,421,495], [1,327,508,507], [2,217,507,341], [1,135,509,242]]
|
||||
|
||||
# example 2
|
||||
# prompt = "A rabbit wearing sunglasses looks very proud"
|
||||
# phrases = ["rabbit", "sunglasses"]
|
||||
# boxes = [[67,87,366,512], [66,130,364,262]]
|
||||
|
||||
boxes = [[x / 512 for x in box] for box in boxes]
|
||||
|
||||
images = pipe(
|
||||
prompt,
|
||||
boxdiff_phrases=phrases,
|
||||
boxdiff_boxes=boxes,
|
||||
boxdiff_kwargs={
|
||||
"attention_res": 16,
|
||||
"normalize_eot": True
|
||||
},
|
||||
num_inference_steps=50,
|
||||
guidance_scale=7.5,
|
||||
generator=torch.manual_seed(42),
|
||||
safety_checker=None
|
||||
).images
|
||||
|
||||
draw_box_with_text(images[0], boxes, phrases).save("output.png")
|
||||
```
|
||||
|
||||
|
||||
### Stable Diffusion Reference
|
||||
|
||||
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).
|
||||
|
||||
1700
examples/community/pipeline_stable_diffusion_boxdiff.py
Normal file
1700
examples/community/pipeline_stable_diffusion_boxdiff.py
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user