Skip to content

Commit afdd7bb

Browse files
authored
[Community Pipeline] CLIPSeg + StableDiffusionInpainting (huggingface#1250)
* text inpainting * refactor
1 parent aa5c4c2 commit afdd7bb

File tree

2 files changed

+355
-0
lines changed

2 files changed

+355
-0
lines changed

examples/community/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ If a community doesn't work as expected, please open an issue and ping the autho
2020
| Imagic Stable Diffusion | Stable Diffusion Pipeline that enables writing a text prompt to edit an existing image| [Imagic Stable Diffusion](#imagic-stable-diffusion) | - | [Mark Rich](https://github.com/MarkRich) |
2121
| Multilingual Stable Diffusion| Stable Diffusion Pipeline that supports prompts in 50 different languages. | [Multilingual Stable Diffusion](#multilingual-stable-diffusion-pipeline) | - | [Juan Carlos Piñeros](https://github.com/juancopi81) |
2222
| Image to Image Inpainting Stable Diffusion | Stable Diffusion Pipeline that enables the overlaying of two images and subsequent inpainting| [Image to Image Inpainting Stable Diffusion](#image-to-image-inpainting-stable-diffusion) | - | [Alex McKinney](https://github.com/vvvm23) |
23+
| Text Based Inpainting Stable Diffusion | Stable Diffusion Inpainting Pipeline that enables passing a text prompt to generate the mask for inpainting| [Text Based Inpainting Stable Diffusion](#image-to-image-inpainting-stable-diffusion) | - | [Dhruv Karan](https://github.com/unography) |
2324

2425

2526

@@ -618,3 +619,37 @@ pipe = pipe.to("cuda")
618619
prompt = "Your prompt here!"
619620
image = pipe(prompt=prompt, image=init_image, inner_image=inner_image, mask_image=mask_image).images[0]
620621
```
622+
623+
### Text Based Inpainting Stable Diffusion
624+
625+
Use a text prompt to generate the mask for the area to be inpainted.
626+
Currently uses the CLIPSeg model for mask generation, then calls the standard Stable Diffusion Inpainting pipeline to perform the inpainting.
627+
628+
```python
629+
from transformers import CLIPSegProcessor, CLIPSegForImageSegmentation
630+
from diffusers import DiffusionPipeline
631+
632+
from PIL import Image
633+
import requests
634+
from torch import autocast
635+
636+
processor = CLIPSegProcessor.from_pretrained("CIDAS/clipseg-rd64-refined")
637+
model = CLIPSegForImageSegmentation.from_pretrained("CIDAS/clipseg-rd64-refined")
638+
639+
pipe = DiffusionPipeline.from_pretrained(
640+
"runwayml/stable-diffusion-inpainting",
641+
custom_pipeline="text_inpainting",
642+
segmentation_model=model,
643+
segmentation_processor=processor
644+
)
645+
pipe = pipe.to("cuda")
646+
647+
648+
url = "https://github.com/timojl/clipseg/blob/master/example_image.jpg?raw=true"
649+
image = Image.open(requests.get(url, stream=True).raw).resize((512, 512))
650+
text = "a glass" # will mask out this text
651+
prompt = "a cup" # the masked out region will be replaced with this
652+
653+
with autocast("cuda"):
654+
image = pipe(image=image, text=text, prompt=prompt).images[0]
655+
```

0 commit comments

Comments
 (0)