-
Notifications
You must be signed in to change notification settings - Fork 228
/
text2image.py
88 lines (75 loc) · 1.9 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
from tensorflow import keras
from stable_diffusion_tf.stable_diffusion import StableDiffusion
import argparse
from PIL import Image
from PIL.PngImagePlugin import PngInfo
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(
"--negative-prompt",
type=str,
help="the negative prompt to use (if any)",
)
parser.add_argument(
"--output",
type=str,
nargs="?",
default="output.png",
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(
"--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 = StableDiffusion(img_height=args.H, img_width=args.W, jit_compile=False)
img = generator.generate(
args.prompt,
negative_prompt=args.negative_prompt,
num_steps=args.steps,
unconditional_guidance_scale=args.scale,
temperature=1,
batch_size=1,
seed=args.seed,
)
pnginfo = PngInfo()
pnginfo.add_text('prompt', args.prompt)
Image.fromarray(img[0]).save(args.output, pnginfo=pnginfo)
print(f"saved at {args.output}")