-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinfer.py
More file actions
183 lines (155 loc) · 5.45 KB
/
infer.py
File metadata and controls
183 lines (155 loc) · 5.45 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
# SPDX-License-Identifier: Apache-2.0
import os
import argparse
import torch
import numpy as np
from PIL import Image
from modules.common.face_encoder import FaceEncoderArcFace, get_landmarks_from_image
from modules.common.inference_utils import SubjectInfo, VideoStyleInfo, dtype_mapping
from modules.full.lynx_infer import LynxWanInfer
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Simple single-GPU inference for Lynx (Wan + IPA + Ref)"
)
# Required-ish (with defaults matching README layout)
parser.add_argument(
"--base_model_path",
type=str,
default="models/Wan2.1-T2V-14B-Diffusers",
help="Path to Wan2.1 base model directory",
)
parser.add_argument(
"--adapter_path",
type=str,
default="models/lynx_full",
help="Path to Lynx adapter directory (resampler/ip/ref layers)",
)
# Minimal inputs
parser.add_argument(
"--subject_image",
type=str,
required=True,
help="Path to the subject face image (single image)",
)
parser.add_argument(
"--prompt",
type=str,
required=True,
help="Text prompt for video generation",
)
parser.add_argument(
"--negative_prompt",
type=str,
default="Bright tones, overexposed, blurred background, static, subtitles, style, works, paintings, images, overall gray, worst quality, low quality, JPEG compression residue, ugly, incomplete, extra fingers, poorly drawn hands, poorly drawn faces, deformed, disfigured, misshapen limbs, fused fingers, still picture, messy background, three legs, many people in the background, walking backwards",
help="Optional negative prompt",
)
# Output
parser.add_argument(
"--output_dir",
type=str,
default="results",
help="Output directory for generated video",
)
parser.add_argument(
"--ext",
type=str,
default="mp4",
choices=["mp4", "webp"],
help="Output format",
)
parser.add_argument(
"--name",
type=str,
default="demo",
help="Style name used in output filename",
)
# Generation parameters (defaults mirror repo examples)
parser.add_argument("--num_frames", type=int, default=81)
parser.add_argument("--fps", type=int, default=16)
parser.add_argument("--height", type=int, default=480)
parser.add_argument("--width", type=int, default=832)
parser.add_argument("--seed", type=int, default=42)
parser.add_argument("--num_inference_steps", type=int, default=50)
parser.add_argument("--guidance_scale", type=float, default=5.0)
parser.add_argument("--guidance_scale_i", type=float, default=2.0)
parser.add_argument("--ip_scale", type=float, default=1.0)
parser.add_argument("--ref_scale", type=float, default=1.0)
# Runtime
parser.add_argument(
"--torch_dtype",
type=str,
default="bf16",
choices=["bf16", "fp16", "fp32"],
help="Model precision",
)
parser.add_argument(
"--device",
type=str,
default="cuda:0" if torch.cuda.is_available() else "cpu",
help="Inference device (single GPU)",
)
return parser.parse_args()
def build_subject_info(image_path: str, device: str) -> SubjectInfo:
image_pil = Image.open(image_path).convert("RGB")
# Landmarks
landmarks = get_landmarks_from_image(image_pil)
# Face embedding via ArcFace
face_encoder = FaceEncoderArcFace()
face_encoder.init_encoder_model("cuda" if device.startswith("cuda") else device)
embeds = face_encoder(image_pil, need_proc=True, landmarks=landmarks)
embeds = np.array(embeds.squeeze(0).cpu())
name = os.path.splitext(os.path.basename(image_path))[0]
return SubjectInfo(
name=name,
image_pil=image_pil,
landmarks=landmarks,
face_embeds=embeds,
)
def build_style_info(args: argparse.Namespace) -> VideoStyleInfo:
if os.path.isfile(args.prompt):
with open(args.prompt, 'r') as f:
args.prompt = f.read().strip()
return VideoStyleInfo(
style_name=args.name,
num_frames=args.num_frames,
seed=args.seed,
guidance_scale=args.guidance_scale,
guidance_scale_i=args.guidance_scale_i,
num_inference_steps=args.num_inference_steps,
width=args.width,
height=args.height,
prompt=args.prompt,
negative_prompt=args.negative_prompt,
)
def main():
args = parse_args()
# Prepare subject/style
subject = build_subject_info(args.subject_image, args.device)
style = build_style_info(args)
# Init pipeline
infer = LynxWanInfer(
adapter_path=args.adapter_path,
base_model_path=args.base_model_path,
dtype=dtype_mapping[args.torch_dtype],
device=args.device,
)
# Generate
infer.generate_t2v(
subject_info=subject,
style_info=style,
output_dir=args.output_dir,
ext=args.ext,
fps=args.fps,
height=args.height,
width=args.width,
num_frames=args.num_frames,
num_inference_steps=args.num_inference_steps,
guidance_scale=args.guidance_scale,
guidance_scale_i=args.guidance_scale_i,
ip_scale=args.ip_scale,
ref_scale=args.ref_scale,
seed=args.seed,
)
if __name__ == "__main__":
main()