-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathinference.py
More file actions
164 lines (131 loc) · 4.48 KB
/
inference.py
File metadata and controls
164 lines (131 loc) · 4.48 KB
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
"""FireRed-Image-Edit Inference Demo."""
import argparse
from pathlib import Path
import torch
from PIL import Image
from diffusers import QwenImageEditPlusPipeline
from utils.fast_pipeline import load_fast_pipeline
def parse_args() -> argparse.Namespace:
"""Parse command line arguments."""
parser = argparse.ArgumentParser(
description="FireRed-Image-Edit inference script"
)
parser.add_argument(
"--model_path",
type=str,
default="FireRedTeam/FireRed-Image-Edit-1.0",
help="Path to the model or HuggingFace model ID",
)
parser.add_argument(
"--input_image",
type=Path,
nargs="+",
default=[Path("./examples/cola.png")],
help="Path(s) to the input image(s). Supports 1-N images. "
"When more than 3 images are given the agent will "
"automatically crop and stitch them into 2-3 composites.",
)
parser.add_argument(
"--output_image",
type=Path,
default=Path("output_edit.png"),
help="Path to save the output image",
)
parser.add_argument(
"--prompt",
type=str,
default="Transform the object into a realistic miniature product by carefully holding it between your thumb and forefinger.",
help="Editing prompt",
)
parser.add_argument(
"--seed",
type=int,
default=49,
help="Random seed for generation",
)
parser.add_argument(
"--true_cfg_scale",
type=float,
default=4.0,
help="True CFG scale",
)
parser.add_argument(
"--num_inference_steps",
type=int,
default=40,
help="Number of inference steps",
)
parser.add_argument(
"--recaption",
action="store_true",
default=False,
help="Enable agent-based recaption: expand the editing prompt to "
"~512 words/characters via Gemini for richer context. "
"Requires GEMINI_API_KEY environment variable.",
)
parser.add_argument(
"--optimized",
default=False,
help="Enable Int8, Cache, and Compile")
return parser.parse_args()
def load_pipeline(model_path: str) -> QwenImageEditPlusPipeline:
"""Load FireRed image edit pipeline."""
pipe = QwenImageEditPlusPipeline.from_pretrained(
model_path,
torch_dtype=torch.bfloat16,
)
pipe.to("cuda")
pipe.set_progress_bar_config(disable=None)
return pipe
def main() -> None:
"""Main entry point."""
args = parse_args()
if args.optimized:
pipeline = load_fast_pipeline(args.model_path)
else:
pipeline = load_pipeline(args.model_path)
print("Pipeline loaded.")
# ── Load all input images ──
images = [Image.open(p).convert("RGB") for p in args.input_image]
prompt = args.prompt
print(f"Loaded {len(images)} image(s).")
# ── Agent: stitch + recaption when needed ──
need_stitch = len(images) > 3
need_recaption = args.recaption
if need_stitch or need_recaption:
from agent import AgentPipeline
agent = AgentPipeline(verbose=True)
agent_result = agent.run(
images,
prompt,
enable_recaption=need_recaption or need_stitch,
)
images = agent_result.images
prompt = agent_result.prompt
print(f"Agent produced {len(images)} image(s).")
print(f"Rewritten prompt: {prompt[:200]}{'…' if len(prompt) > 200 else ''}")
inputs = {
"image": images,
"prompt": prompt,
"generator": torch.Generator(device="cuda").manual_seed(args.seed),
"true_cfg_scale": args.true_cfg_scale,
"negative_prompt": " ",
"num_inference_steps": args.num_inference_steps,
"num_images_per_prompt": 1,
}
if args.optimized:
print("NOTE: The first inference after compilation may take 1-2 minutes.")
with torch.inference_mode():
result = pipeline(**inputs)
output_image = result.images[0]
output_image.save(args.output_image)
print("Image saved at:", args.output_image.resolve())
# ── Replace with the desired case or scenario based on your specific needs ──
if args.optimized:
print("Subsequent runs will be significantly faster. Enjoy~")
with torch.inference_mode():
result = pipeline(**inputs)
output_image = result.images[0]
output_image.save(args.output_image)
if __name__ == "__main__":
main()