-
Notifications
You must be signed in to change notification settings - Fork 15
/
generate_experimental_images.py
181 lines (149 loc) · 4.97 KB
/
generate_experimental_images.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"""
Use this script to generate and report a batch of images to W&B
with a single prompt through multiple weights of Stable Diffusion.
This is particularly useful when you have multiple fine-tuned weight
files. It works for the both cases: (a) diffusion model only and
(b) text encoder + diffusion model
Usage:
# Find weight files(.h5) under "." location.
# Generate 4 images with a nested loop over
# num_steps x ugs combinations.
$ python generate_experimental_images.py \
--base_root_dir "." \
--caption "A photo of sks dog in a bucket" \
--num_image_gen 4 \
--num_steps 75 100 150 \
--ugs 15 30 \
--wandb_project_id "my-wandb-project"
Depending on the unique identifier and class you used, you'd need
to change the caption accordingly. In the above case, the unique identifier
is "sks" and the class is "dog".
If the fine-tuned weights are stored as artifacts in WandB, then you can
use this script: https://gist.github.com/sayakpaul/0d83d7fd7c3939ce2ddc2292b6d4f173
"""
import tensorflow as tf
tf.keras.mixed_precision.set_global_policy("mixed_float16")
import argparse
import glob
import keras_cv
import PIL
import wandb
def generate_report(
sd_model,
weights_dict,
caption,
num_image_gen,
num_steps,
unconditional_guidance_scales,
wandb_project,
):
"Generates images and report the results to WandB."
for key in list(weights_dict.keys()):
print(f"Generating images for model({key}).")
wandb.init(project=wandb_project, name=key)
unet_params_path = weights_dict[key]["unet"]
sd_model.diffusion_model.load_weights(unet_params_path)
if "text_encoder" in weights_dict[key]:
text_encoder_params_path = weights_dict[key]["text_encoder"]
sd_model.text_encoder.load_weights(text_encoder_params_path)
for steps in num_steps:
for ugs in unconditional_guidance_scales:
images = sd_model.text_to_image(
caption,
batch_size=num_image_gen,
num_steps=steps,
unconditional_guidance_scale=ugs,
)
wandb.log(
{
f"num_steps@{steps}-ugs@{ugs}": [
wandb.Image(
PIL.Image.fromarray(image), caption=f"{i}: {caption}"
)
for i, image in enumerate(images)
]
}
)
wandb.finish()
def find_weights(base_root_dir):
"""Finds weights per model name."""
weights_dict = {}
for file in glob.glob(f"{base_root_dir}/*.h5"):
if "@True" in file:
rindex = file.rindex("@True")
key = file[: rindex + len("@True")]
else:
rindex = file.rindex("@False")
key = file[: rindex + len("@False")]
if key not in weights_dict:
weights_dict[key] = {}
if "text_encoder" in file[rindex:]:
weights_dict[key]["text_encoder"] = file
else:
weights_dict[key]["unet"] = file
return weights_dict
def run(args):
"""Finds weights, generate images based on them"""
# Initialize the SD model.
img_height = img_width = 512
sd_model = keras_cv.models.StableDiffusion(
img_width=img_width, img_height=img_height, jit_compile=True
)
# Find weights per model.
weights_dict = find_weights(args.base_root_dir)
# Run image generations.
generate_report(
sd_model,
weights_dict,
args.caption,
args.num_image_gen,
args.num_steps,
args.ugs,
args.wandb_project_id,
)
def parse_args():
parser = argparse.ArgumentParser(
description="Script to perform image generating experimentations."
)
parser.add_argument(
"--base_root_dir",
type=str,
default=".",
help="base directory to search for weight files",
)
parser.add_argument(
"--caption",
type=str,
default="A photo of sks person without mustache, handsome, ultra realistic, 4k, 8k",
help="prompt to use to generate images",
)
parser.add_argument(
"--num_image_gen",
type=int,
default=16,
help="number of images to generate per model",
)
parser.add_argument(
"--num_steps",
nargs="+",
type=int,
default=[75, 100, 150],
help="list of num_steps",
)
parser.add_argument(
"--ugs",
nargs="+",
type=int,
default=[15, 30],
help="list of unconditional guidance scale",
)
parser.add_argument(
"--wandb_project_id",
type=str,
default="dreambooth-generate-dog",
help="W&B project id to log",
)
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
run(args)