Skip to content

Commit 8ee23f2

Browse files
committed
update composable diffusion for an updated diffuser library
1 parent 784beee commit 8ee23f2

File tree

2 files changed

+467
-221
lines changed

2 files changed

+467
-221
lines changed

examples/community/README.md

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -355,43 +355,45 @@ out = pipe(
355355
import torch as th
356356
import numpy as np
357357
import torchvision.utils as tvu
358+
358359
from diffusers import DiffusionPipeline
359360

361+
import argparse
362+
363+
parser = argparse.ArgumentParser()
364+
parser.add_argument("--prompt", type=str, default="mystical trees | A magical pond | dark",
365+
help="use '|' as the delimiter to compose separate sentences.")
366+
parser.add_argument("--steps", type=int, default=50)
367+
parser.add_argument("--scale", type=float, default=7.5)
368+
parser.add_argument("--weights", type=str, default="7.5 | 7.5 | -7.5")
369+
parser.add_argument("--seed", type=int, default=2)
370+
parser.add_argument("--model_path", type=str, default="CompVis/stable-diffusion-v1-4")
371+
parser.add_argument("--num_images", type=int, default=1)
372+
args = parser.parse_args()
373+
360374
has_cuda = th.cuda.is_available()
361375
device = th.device('cpu' if not has_cuda else 'cuda')
362376

377+
prompt = args.prompt
378+
scale = args.scale
379+
steps = args.steps
380+
363381
pipe = DiffusionPipeline.from_pretrained(
364-
"CompVis/stable-diffusion-v1-4",
365-
use_auth_token=True,
382+
args.model_path,
366383
custom_pipeline="composable_stable_diffusion",
367384
).to(device)
368385

369-
370-
def dummy(images, **kwargs):
371-
return images, False
372-
373-
pipe.safety_checker = dummy
374-
375-
images = []
376-
generator = torch.Generator("cuda").manual_seed(0)
377-
378-
seed = 0
379-
prompt = "a forest | a camel"
380-
weights = " 1 | 1" # Equal weight to each prompt. Can be negative
386+
pipe.safety_checker = None
381387

382388
images = []
383-
for i in range(4):
384-
res = pipe(
385-
prompt,
386-
guidance_scale=7.5,
387-
num_inference_steps=50,
388-
weights=weights,
389-
generator=generator)
390-
image = res.images[0]
391-
images.append(image)
389+
generator = th.Generator("cuda").manual_seed(args.seed)
390+
for i in range(args.num_images):
391+
image = pipe(prompt, guidance_scale=scale, num_inference_steps=steps,
392+
weights=args.weights, generator=generator).images[0]
393+
images.append(th.from_numpy(np.array(image)).permute(2, 0, 1) / 255.)
394+
grid = tvu.make_grid(th.stack(images, dim=0), nrow=4, padding=0)
395+
tvu.save_image(grid, f'{prompt}_{args.weights}' + '.png')
392396

393-
for i, img in enumerate(images):
394-
img.save(f"./composable_diffusion/image_{i}.png")
395397
```
396398

397399
### Imagic Stable Diffusion

0 commit comments

Comments
 (0)