-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathinfer.py
More file actions
executable file
·369 lines (331 loc) · 10.7 KB
/
infer.py
File metadata and controls
executable file
·369 lines (331 loc) · 10.7 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import argparse
import math
import os
import subprocess
from datetime import datetime
import cv2
import numpy as np
import pandas as pd
import torch
from PIL import Image
from diffsynth import ModelManager, WanVideoPipeline
from diffsynth.data import save_video
from diffsynth.models.camer import CameraDemo
from diffsynth.models.face_align import FaceAlignment
from diffsynth.models.pdf import (FanEncoder, det_landmarks,
get_drive_expression_pd_fgc)
from diffsynth.pipelines.wan_video import PortraitAdapter
from utils import merge_audio_to_video
def find_replacement(a):
while a > 0:
if (a - 1) % 4 == 0:
return a
a -= 1
return 0
def get_emo_feature(
video_path, face_aligner, pd_fpg_motion, device=torch.device("cuda")
):
pd_fpg_motion = pd_fpg_motion.to(device)
cap = cv2.VideoCapture(video_path)
fps = cap.get(cv2.CAP_PROP_FPS)
frame_list = []
ret, frame = cap.read()
while ret:
resized_frame = frame
frame_list.append(resized_frame.copy())
ret, frame = cap.read()
cap.release()
num_frames = min(len(frame_list), args.num_frames)
num_frames = find_replacement(num_frames)
frame_list = frame_list[:num_frames]
landmark_list = det_landmarks(face_aligner, frame_list)[1]
emo_list = get_drive_expression_pd_fgc(
pd_fpg_motion, frame_list, landmark_list, device
)
emo_feat_list = []
head_emo_feat_list = []
for emo in emo_list:
headpose_emb = emo["headpose_emb"]
eye_embed = emo["eye_embed"]
emo_embed = emo["emo_embed"]
mouth_feat = emo["mouth_feat"]
emo_feat = torch.cat([eye_embed, emo_embed, mouth_feat], dim=1)
head_emo_feat = torch.cat([headpose_emb, emo_feat], dim=1)
emo_feat_list.append(emo_feat)
head_emo_feat_list.append(head_emo_feat)
emo_feat_all = torch.cat(emo_feat_list, dim=0)
head_emo_feat_all = torch.cat(head_emo_feat_list, dim=0)
return emo_feat_all, head_emo_feat_all, fps, num_frames
def parse_args():
parser = argparse.ArgumentParser(description="Simple example of a training script.")
parser.add_argument(
"--prompt",
type=str,
default=None,
required=False,
help="prompt.",
)
parser.add_argument(
"--output_path",
type=str,
default="./output",
help="Path to save the model.",
)
parser.add_argument(
"--height",
type=int,
default=480,
help="Image height.",
)
parser.add_argument(
"--width",
type=int,
default=832,
help="Image width.",
)
parser.add_argument(
"--portrait_scale",
type=float,
default=1.0,
help="Image width.",
)
parser.add_argument(
"--cfg_scale",
type=float,
default=5.0,
required=False,
help="The cfg of prompt.",
)
parser.add_argument(
"--portrait_cfg_scale",
type=float,
default=5.0,
required=False,
help="The emo cfg.",
)
parser.add_argument(
"--scale_image",
type=bool,
default=True,
required=False,
help="If scale the image.",
)
parser.add_argument(
"--portrait_in_dim",
type=int,
default=768,
help="The portrait in dim.",
)
parser.add_argument(
"--portrait_proj_dim",
type=int,
default=2048,
help="The portrait proj dim.",
)
parser.add_argument(
"--portrait_checkpoint",
type=str,
default=None,
required=True,
help="The ckpt of FantasyPortrait",
)
parser.add_argument(
"--alignment_model_path",
type=str,
default=None,
required=True,
help="The face landmark of pd-fgc.",
)
parser.add_argument(
"--det_model_path",
type=str,
default=None,
required=True,
help="The det model of pd-fgc.",
)
parser.add_argument(
"--pd_fpg_model_path",
type=str,
default=None,
required=True,
help="The motion model of pd-fgc.",
)
parser.add_argument(
"--wan_model_path",
type=str,
default=None,
required=True,
help="The wan model path.",
)
parser.add_argument(
"--num_frames",
type=int,
default=81,
required=False,
help="The number of frames.",
)
parser.add_argument(
"--seed",
type=int,
default=42,
required=False,
help="The generative seed.",
)
parser.add_argument(
"--max_size",
type=int,
default=720,
help="The max size to scale.",
)
parser.add_argument(
"--input_image_path",
type=str,
default=None,
required=True,
help="The input image path.",
)
parser.add_argument(
"--driven_video_path",
type=str,
default=None,
required=True,
help="The driven video path.",
)
args = parser.parse_args()
return args
args = parse_args()
def load_wan_video():
# Load models
model_manager = ModelManager(device="cpu")
model_manager.load_models(
[
[
os.path.join(
args.wan_model_path,
"diffusion_pytorch_model-00001-of-00007.safetensors",
),
os.path.join(
args.wan_model_path,
"diffusion_pytorch_model-00002-of-00007.safetensors",
),
os.path.join(
args.wan_model_path,
"diffusion_pytorch_model-00003-of-00007.safetensors",
),
os.path.join(
args.wan_model_path,
"diffusion_pytorch_model-00004-of-00007.safetensors",
),
os.path.join(
args.wan_model_path,
"diffusion_pytorch_model-00005-of-00007.safetensors",
),
os.path.join(
args.wan_model_path,
"diffusion_pytorch_model-00006-of-00007.safetensors",
),
os.path.join(
args.wan_model_path,
"diffusion_pytorch_model-00007-of-00007.safetensors",
),
],
os.path.join(
args.wan_model_path,
"models_clip_open-clip-xlm-roberta-large-vit-huge-14.pth",
),
os.path.join(args.wan_model_path, "models_t5_umt5-xxl-enc-bf16.pth"),
os.path.join(args.wan_model_path, "Wan2.1_VAE.pth"),
],
# torch_dtype=torch.float8_e4m3fn, # You can set `torch_dtype=torch.bfloat16` to disable FP8 quantization.
torch_dtype=torch.bfloat16, # You can set `torch_dtype=torch.bfloat16` to disable FP8 quantization.
)
pipe = WanVideoPipeline.from_model_manager(
model_manager, torch_dtype=torch.bfloat16, device="cuda"
)
pipe.enable_vram_management(
num_persistent_param_in_dit=None
) # You can set `num_persistent_param_in_dit` to a small number to reduce VRAM required.
return pipe
def load_pd_fgc_model():
face_aligner = CameraDemo(
face_alignment_module=FaceAlignment(
gpu_id=None,
alignment_model_path=args.alignment_model_path,
det_model_path=args.det_model_path,
),
reset=False,
)
pd_fpg_motion = FanEncoder()
pd_fpg_checkpoint = torch.load(args.pd_fpg_model_path, map_location="cpu")
m, u = pd_fpg_motion.load_state_dict(pd_fpg_checkpoint, strict=False)
pd_fpg_motion = pd_fpg_motion.eval()
return face_aligner, pd_fpg_motion
os.makedirs(args.output_path, exist_ok=True)
# Load models
pipe = load_wan_video()
face_aligner, pd_fpg_motion = load_pd_fgc_model()
device = torch.device("cuda")
portrait_model = PortraitAdapter(
pipe.dit, args.portrait_in_dim, args.portrait_proj_dim
).to("cuda")
portrait_model.load_portrait_adapter(args.portrait_checkpoint, pipe.dit)
pipe.dit.to("cuda")
print(f"FantasyPortrait model load from checkpoint:{args.portrait_checkpoint}")
image = Image.open(args.input_image_path).convert("RGB")
width, height = image.size
if args.scale_image:
scale = args.max_size / max(width, height)
width, height = (int(width * scale), int(height * scale))
image = image.resize([width, height], Image.LANCZOS)
with torch.no_grad():
emo_feat_all, head_emo_feat_all, fps, num_frames = get_emo_feature(
args.driven_video_path, face_aligner, pd_fpg_motion
)
emo_feat_all, head_emo_feat_all = emo_feat_all.unsqueeze(
0
), head_emo_feat_all.unsqueeze(0)
adapter_proj = portrait_model.get_adapter_proj(head_emo_feat_all.to(device))
pos_idx_range = portrait_model.split_audio_adapter_sequence(
adapter_proj.size(1), num_frames=num_frames
)
proj_split, context_lens = portrait_model.split_tensor_with_padding(
adapter_proj, pos_idx_range, expand_length=0
)
negative_prompt = "人物静止不动,静止,色调艳丽,过曝,静态,细节模糊不清,字幕,风格,作品,画作,画面,静止,整体发灰,最差质量,低质量,JPEG压缩残留,丑陋的,残缺的,多余的手指,画得不好的手部,画得不好的脸部,畸形的,毁容的,形态畸形的肢体,手指融合,静止不动的画面,杂乱的背景,三条腿,背景人很多,倒着走"
video_audio = pipe(
prompt=args.prompt,
negative_prompt=negative_prompt,
input_image=image,
width=width,
height=height,
num_frames=num_frames,
num_inference_steps=30,
seed=args.seed,
tiled=True,
ip_scale=args.portrait_scale,
cfg_scale=args.cfg_scale,
ip_cfg_scale=args.portrait_cfg_scale,
adapter_proj=proj_split,
adapter_context_lens=context_lens,
latents_num_frames=(num_frames - 1) // 4 + 1,
)
now = datetime.now()
timestamp_str = now.strftime("%Y%m%d_%H%M%S")
image_name = args.input_image_path.split("/")[-1]
video_name = args.driven_video_path.split("/")[-1]
save_image_name = image_name + os.path.basename(args.input_image_path).split(".")[0][:8]
save_video_name = (
video_name + os.path.basename(args.driven_video_path).split(".")[0][:8]
)
save_name = f"{timestamp_str}_{save_image_name}_{save_video_name}"
save_video_path = os.path.join(args.output_path, f"{save_name}.mp4")
save_video(
video_audio, os.path.join(args.output_path, f"{save_name}.mp4"), fps=fps, quality=5
)
# add Driven Audio to the Result video.
save_video_path_with_audio = os.path.join(
args.output_path, f"{save_name}_with_audio.mp4"
)
merge_audio_to_video(
args.driven_video_path, save_video_path, save_video_path_with_audio
)