forked from modular/max
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_to_image.py
executable file
·178 lines (152 loc) · 5.61 KB
/
text_to_image.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
#!/usr/bin/env python3
# ===----------------------------------------------------------------------=== #
# Copyright (c) 2025, Modular Inc. All rights reserved.
#
# Licensed under the Apache License v2.0 with LLVM Exceptions:
# https://llvm.org/LICENSE.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===----------------------------------------------------------------------=== #
import signal
import sys
from argparse import ArgumentParser
from pathlib import Path
import numpy as np
from diffusers import PNDMScheduler
from huggingface_hub import snapshot_download
from max.engine import InferenceSession
from PIL import Image
from transformers import CLIPTokenizer
DESCRIPTION = "Generate an image based on the given prompt."
GUIDANCE_SCALE_FACTOR = 7.5
LATENT_SCALE_FACTOR = 0.18215
OUTPUT_HEIGHT = 512
OUTPUT_WIDTH = 512
LATENT_WIDTH = OUTPUT_WIDTH // 8
LATENT_HEIGHT = OUTPUT_HEIGHT // 8
LATENT_CHANNELS = 4
def run_stable_diffusion(
args, txt_encoder, img_decoder, img_diffuser, tokenizer, scheduler
):
# Tokenize inputs and run through text encoder.
print("Processing input...")
prompt_p = tokenizer(
args.prompt, padding="max_length", max_length=tokenizer.model_max_length
)
prompt_n = tokenizer(
args.negative_prompt,
padding="max_length",
max_length=tokenizer.model_max_length,
)
input_ids = np.stack((prompt_p.input_ids, prompt_n.input_ids)).astype(
np.int32
)
encoder_hidden_states = txt_encoder.execute_legacy(input_ids=input_ids)[
"last_hidden_state"
]
print("Input processed.\n")
# Initialize latent and scheduler.
print("Initializing latent...")
# Note: For onnx, shapes are given in NCHW format.
latent = np.random.normal(
size=(1, LATENT_CHANNELS, LATENT_HEIGHT, LATENT_WIDTH)
)
latent = latent * scheduler.init_noise_sigma
latent = latent.astype(np.float32)
# Loop through diffusion model.
scheduler.set_timesteps(args.num_steps)
for i, t in enumerate(scheduler.timesteps):
print(f"\rGenerating image: {i}/{args.num_steps}", end="")
# Duplicate input and scale based on scheduler.
sample = np.vstack((latent, latent))
sample = scheduler.scale_model_input(sample, timestep=t)
# Execute the diffusion model with bs=2. Both batches have same primary input and
# timestep, but the encoder_hidden_states (primary prompt vs negative) differs.
noise_pred = img_diffuser.execute_legacy(
sample=sample,
encoder_hidden_states=encoder_hidden_states,
timestep=np.array([t], dtype=np.int64),
)["out_sample"]
# Merge conditioned & unconditioned outputs.
noise_pred_text, noise_pred_uncond = np.split(noise_pred, 2)
noise_pred = noise_pred_uncond + GUIDANCE_SCALE_FACTOR * (
noise_pred_text - noise_pred_uncond
)
# Merge latent with previous iteration.
latent = scheduler.step(noise_pred, t, latent).prev_sample
# Decode finalized latent.
print("\n\nDecoding image...")
latent = latent * (1 / LATENT_SCALE_FACTOR)
decoded = img_decoder.execute_legacy(latent_sample=latent)["sample"]
image = np.clip(decoded / 2 + 0.5, 0, 1).squeeze()
image = (image.transpose(1, 2, 0) * 255).astype(np.uint8)
Image.fromarray(image, "RGB").save(args.output)
print(f"Image saved to {args.output}.")
return
def parse(args):
# Parse args.
parser = ArgumentParser(description=DESCRIPTION)
parser.add_argument(
"--prompt",
type=str,
metavar="<str>",
required=True,
help="Description of desired image.",
)
parser.add_argument(
"--negative-prompt",
type=str,
metavar="<str>",
default="",
help="Objects or styles to avoid in generated image.",
)
parser.add_argument(
"--num-steps",
type=int,
metavar="<int>",
default=25,
help="# of diffusion steps; trades-off speed vs quality",
)
parser.add_argument(
"--seed",
type=int,
metavar="<int>",
default=None,
help="Seed for psuedo-random number generation.",
)
parser.add_argument(
"--output",
"-o",
type=str,
metavar="<outfile>",
default="output.png",
help="Output filename.",
)
parsed_args = parser.parse_args(args)
signal.signal(signal.SIGINT, signal.SIG_DFL)
# Set seed if requested.
if parsed_args.seed:
np.random.seed(parsed_args.seed)
return parsed_args
def main():
args = parse(sys.argv[1:])
# Compile & load models - this may take a few minutes.
session = InferenceSession()
model_dir = Path(snapshot_download("modularai/stable-diffusion-1.5-onnx"))
print("Loading and compiling models...")
txt_encoder = session.load(model_dir / "text_encoder" / "model.onnx")
img_decoder = session.load(model_dir / "vae_decoder" / "model.onnx")
img_diffuser = session.load(model_dir / "unet" / "model.onnx")
print("Models compiled.\n")
# Instantiate tokenizer and scheduler.
tokenizer = CLIPTokenizer.from_pretrained(model_dir / "tokenizer")
scheduler = PNDMScheduler.from_pretrained(model_dir / "scheduler")
run_stable_diffusion(
args, txt_encoder, img_decoder, img_diffuser, tokenizer, scheduler
)
if __name__ == "__main__":
main()