forked from divamgupta/stable-diffusion-tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathtext2image.py
95 lines (81 loc) · 2.02 KB
/
text2image.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
from tensorflow import keras
from stable_diffusion_tf.stable_diffusion import Text2Image
import argparse
from PIL import Image
parser = argparse.ArgumentParser()
parser.add_argument(
"--prompt",
type=str,
nargs="?",
default="a painting of a virus monster playing guitar",
help="the prompt to render",
)
parser.add_argument(
"--output",
type=str,
nargs="?",
default="output",
help="where to save the output image",
)
parser.add_argument(
"--H",
type=int,
default=512,
help="image height, in pixels",
)
parser.add_argument(
"--W",
type=int,
default=512,
help="image width, in pixels",
)
parser.add_argument(
"--scale",
type=float,
default=7.5,
help="unconditional guidance scale: eps = eps(x, empty) + scale * (eps(x, cond) - eps(x, empty))",
)
parser.add_argument(
"--steps", type=int, default=50, help="number of ddim sampling steps"
)
parser.add_argument(
"--seed",
type=int,
help="optionally specify a seed integer for reproducible results",
)
parser.add_argument(
"--batch_size",
type=int,
default=1,
help="how many images to generate",
)
parser.add_argument(
"--mp",
default=False,
action="store_true",
help="Enable mixed precision (fp16 computation)",
)
args = parser.parse_args()
if args.mp:
print("Using mixed precision.")
keras.mixed_precision.set_global_policy("mixed_float16")
generator = Text2Image(img_height=args.H, img_width=args.W, jit_compile=False)
img = generator.generate(
args.prompt,
num_steps=args.steps,
unconditional_guidance_scale=args.scale,
temperature=1,
batch_size=args.batch_size,
seed=args.seed,
)
fname = args.output
if fname.endswith(".png"):
fname = fname[:-4]
if args.batch_size == 1:
Image.fromarray(img[0]).save(args.output + ".png")
print(f"saved at {args.output}.png")
else:
for i in range(args.batch_size):
fname_i = f"{fname}_{i}.png"
Image.fromarray(img[i]).save(fname_i)
print(f"saved at {fname_i}")