Skip to content

Commit 86670db

Browse files
committed
initial commit to add imagic to stable diffusion community pipelines
1 parent de00c63 commit 86670db

File tree

4 files changed

+420
-2
lines changed

4 files changed

+420
-2
lines changed

examples/community/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ If a community doesn't work as expected, please open an issue and ping the autho
1616
| Speech to Image | Using automatic-speech-recognition to transcribe text and Stable Diffusion to generate images | [Speech to Image](#speech-to-image) | - | [Mikail Duzenli](https://github.com/MikailINTech)
1717
| Wild Card Stable Diffusion | Stable Diffusion Pipeline that supports prompts that contain wildcard terms (indicated by surrounding double underscores), with values instantiated randomly from a corresponding txt file or a dictionary of possible values | [Wildcard Stable Diffusion](#wildcard-stable-diffusion) | - | [Shyam Sudhakaran](https://github.com/shyamsn97) |
1818
| Composable Stable Diffusion| Stable Diffusion Pipeline that supports prompts that contain "|" in prompts (as an AND condition) and weights (separated by "|" as well) to positively / negatively weight prompts. | [Composable Stable Diffusion](#composable-stable-diffusion) | - | [Mark Rich](https://github.com/MarkRich) |
19+
| 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) |
1920

2021

2122
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.
@@ -371,3 +372,67 @@ for i in range(4):
371372
for i, img in enumerate(images):
372373
img.save(f"./composable_diffusion/image_{i}.png")
373374
```
375+
376+
### Imagic Stable Diffusion
377+
Allows you to edit an image using stable diffusion.
378+
379+
380+
```python
381+
382+
383+
import torch as th
384+
import numpy as np
385+
import requests
386+
from PIL import Image
387+
from io import BytesIO
388+
import torch
389+
from diffusers import DiffusionPipeline, DDIMScheduler
390+
391+
has_cuda = th.cuda.is_available()
392+
device = th.device('cpu' if not has_cuda else 'cuda')
393+
394+
pipe = DiffusionPipeline.from_pretrained(
395+
"CompVis/stable-diffusion-v1-4",
396+
use_auth_token=True,
397+
custom_pipeline="imagic_stable_diffusion",
398+
scheduler = DDIMScheduler(beta_start=0.00085, beta_end=0.012, beta_schedule="scaled_linear", clip_sample=False, set_alpha_to_one=False)
399+
).to(device)
400+
401+
402+
def dummy(images, **kwargs):
403+
return images, False
404+
405+
pipe.safety_checker = dummy
406+
407+
images = []
408+
generator = th.Generator("cuda").manual_seed(0)
409+
410+
seed = 0
411+
prompt = "A photo of Barack Obama smiling with a big grin"
412+
413+
images = []
414+
415+
url = 'https://www.dropbox.com/s/6tlwzr73jd1r9yk/obama.png?dl=1'
416+
417+
response = requests.get(url)
418+
init_image = Image.open(BytesIO(response.content)).convert("RGB")
419+
init_image = init_image.resize((512, 512))
420+
421+
res = pipe.train(
422+
prompt,
423+
init_image,
424+
guidance_scale=7.5,
425+
num_inference_steps=50,
426+
generator=generator)
427+
res = pipe(alpha=1)
428+
image = res.images[0]
429+
image.save('./imagic/imagic_image_alpha_1.png')
430+
431+
res = pipe(alpha=1.5)
432+
image = res.images[0]
433+
image.save('./imagic/imagic_image_alpha_1_5.png')
434+
435+
res = pipe(alpha=2)
436+
image = res.images[0]
437+
image.save('./imagic/imagic_image_alpha_2.png')
438+
```

0 commit comments

Comments
 (0)