hyoungwoncho
commited on
Commit
•
78e8159
1
Parent(s):
36de266
Upload openpose_example.py
Browse files- openpose_example.py +62 -0
openpose_example.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import torch
|
3 |
+
|
4 |
+
from diffusers import ControlNetModel, UniPCMultistepScheduler, StableDiffusionControlNetPipeline
|
5 |
+
from diffusers.utils import load_image, make_image_grid
|
6 |
+
from diffusers.utils.torch_utils import randn_tensor
|
7 |
+
from controlnet_aux import OpenposeDetector
|
8 |
+
|
9 |
+
controlnet = ControlNetModel.from_pretrained(
|
10 |
+
"lllyasviel/sd-controlnet-openpose",
|
11 |
+
torch_dtype=torch.float16
|
12 |
+
)
|
13 |
+
|
14 |
+
pipe = StableDiffusionControlNetPipeline.from_pretrained(
|
15 |
+
"runwayml/stable-diffusion-v1-5",
|
16 |
+
custom_pipeline="/home/cvlab04/project/hyoungwon/sd_perturbed_attention_guidance_controlnet",
|
17 |
+
controlnet=controlnet,
|
18 |
+
torch_dtype=torch.float16
|
19 |
+
)
|
20 |
+
|
21 |
+
device="cuda"
|
22 |
+
pipe = pipe.to(device)
|
23 |
+
|
24 |
+
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
|
25 |
+
|
26 |
+
openpose = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")
|
27 |
+
original_image = load_image(
|
28 |
+
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/person.png"
|
29 |
+
)
|
30 |
+
openpose_image = openpose(original_image)
|
31 |
+
|
32 |
+
prompts=""
|
33 |
+
|
34 |
+
base_dir = "./results/openpose/"
|
35 |
+
|
36 |
+
if not os.path.exists(base_dir):
|
37 |
+
os.makedirs(base_dir)
|
38 |
+
|
39 |
+
latent_input = randn_tensor(shape=(1,4,64,64),generator=None, device=device, dtype=torch.float16)
|
40 |
+
|
41 |
+
output_baseline = pipe(
|
42 |
+
prompts,
|
43 |
+
image=openpose_image,
|
44 |
+
num_inference_steps=50,
|
45 |
+
guidance_scale=0.0,
|
46 |
+
pag_scale=0.0,
|
47 |
+
pag_applied_layers_index=["m0"],
|
48 |
+
latents=latent_input
|
49 |
+
).images[0]
|
50 |
+
|
51 |
+
output_pag = pipe(
|
52 |
+
prompts,
|
53 |
+
image=openpose_image,
|
54 |
+
num_inference_steps=50,
|
55 |
+
guidance_scale=0.0,
|
56 |
+
pag_scale=4.0,
|
57 |
+
pag_applied_layers_index=["m0"],
|
58 |
+
latents=latent_input
|
59 |
+
).images[0]
|
60 |
+
|
61 |
+
grid_image = make_image_grid([original_image, openpose_image, output_baseline, output_pag], rows=1, cols=4)
|
62 |
+
grid_image.save(base_dir + "sample.png")
|