-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_pie.py
153 lines (135 loc) · 4.64 KB
/
run_pie.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import argparse
import os
import torch
from PIL import Image
import numpy as np
from torchvision import transforms
from pipeline_stable_diffusion_pie import StableDiffusionPIEPipeline
from diffusers import AutoencoderKL, DDPMScheduler, DiffusionPipeline, UNet2DConditionModel
def set_all_seeds(SEED):
# REPRODUCIBILITY
torch.manual_seed(SEED)
np.random.seed(SEED)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def parse_args(input_args=None):
parser = argparse.ArgumentParser(description="Simple example of a PIE inference script.")
parser.add_argument(
"--pretrained_model_name_or_path",
type=str,
default="runwayml/stable-diffusion-v1-5",
required=True,
help="Path to pretrained model or model identifier from huggingface.co/models.",
)
parser.add_argument(
"--revision",
type=str,
default=None,
required=False,
help=(
"Revision of pretrained model identifier from huggingface.co/models. Trainable model components should be"
" float32 precision."
),
)
parser.add_argument(
"--finetuned_path",
type=str,
default=None,
required=False,
help="Path to domain specific finetuned unet from any healthcare text-to-image dataset",
)
parser.add_argument(
"--image_path",
type=str,
default=None,
required=True,
help="Path to the input instance images.",
)
parser.add_argument(
"--mask_path",
type=str,
default=None,
required=False,
help="Path to mask.",
)
parser.add_argument(
"--prompt",
type=str,
default=None,
required=True,
help="The prompt with identifier specifying the instance",
)
parser.add_argument("--step", type=int, default=10, help="N in the paper, Number to images / steps for PIE generation")
parser.add_argument("--strength", type=float, default=0.5, help="Roll back ratio garmma")
parser.add_argument("--guidance_scale", type=float, default=7.5, help="guidance scale")
parser.add_argument(
"--output_dir",
type=str,
default="./simulation",
help="The output directory.",
)
parser.add_argument("--seed", type=int, default=None, help="A seed for reproducible training.")
parser.add_argument(
"--resolution",
type=int,
default=512,
help=(
"The resolution for input images, all the images in the train/validation dataset will be resized to this"
" resolution"
),
)
args = parser.parse_args()
return args
def main(args):
seed = args.seed
set_all_seeds(seed)
image_path = args.image_path
mask_path = args.mask_path
prompt = args.prompt
model_id_or_path = args.pretrained_model_name_or_path
finetuned_path = args.finetuned_path
resolution = args.resolution
ddim_times = args.step
strength = args.strength
guidance_scale = args.guidance_scale
output_dir = args.output_dir
if not os.path.exists(output_dir):
os.mkdir(output_dir)
device = "cuda"
pipe = StableDiffusionPIEPipeline.from_pretrained(model_id_or_path, torch_dtype=torch.float32, cache_dir="./checkpoints", safety_checker=None)
if finetuned_path != None:
unet = UNet2DConditionModel.from_pretrained(
finetuned_path, subfolder="text_encoder"
)
pipe.unet = unet
pipe = pipe.to(device)
image_transforms = transforms.Compose(
[
transforms.Resize(resolution, interpolation=transforms.InterpolationMode.BILINEAR),
transforms.CenterCrop(resolution)
]
)
images = []
step_i = 0
init_image = Image.open(image_path).convert("RGB") # The unedited image
init_image = image_transforms(init_image)
init_image.save(os.path.join(output_dir, str(step_i) + ".png"))
if mask_path != None:
mask = Image.open(mask_path).convert("RGB")
mask = image_transforms(mask)
mask.save(os.path.join(output_dir, "mask" + ".png"))
else:
mask = None
step_i += 1
img = init_image
images.append(img)
while step_i <= ddim_times:
img = pipe(prompt=prompt, image=img, mask=mask, init_image=init_image, strength=strength, guidance_scale=guidance_scale).images[0]
images.append(img)
img.save(os.path.join(output_dir, str(step_i) + ".png"))
step_i += 1
duration = 1000
images[0].save('output.gif', save_all=True, append_images=images[1:], duration=duration)
if __name__ == "__main__":
args = parse_args()
main(args)