-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathinference_syncammaster.py
More file actions
281 lines (245 loc) · 10.5 KB
/
Copy pathinference_syncammaster.py
File metadata and controls
281 lines (245 loc) · 10.5 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
import sys
import torch
import torch.nn as nn
from diffsynth import ModelManager, WanVideoSynCamMasterPipeline, save_video, VideoData
import torch, os, imageio, argparse
from torchvision.transforms import v2
from einops import rearrange
import pandas as pd
import torchvision
from PIL import Image
import numpy as np
import json
from diffsynth.models.wan_video_dit import SelfAttention
import re
class Camera(object):
def __init__(self, c2w):
c2w_mat = np.array(c2w).reshape(4, 4)
self.c2w_mat = c2w_mat
self.w2c_mat = np.linalg.inv(c2w_mat)
class TextCameraDataset(torch.utils.data.Dataset):
def __init__(self, base_path, metadata_path, args, max_num_frames=81, frame_interval=1, num_frames=81, height=480, width=832, is_i2v=False):
metadata = pd.read_csv(metadata_path)
self.text = metadata["text"].to_list()
self.max_num_frames = max_num_frames
self.frame_interval = frame_interval
self.num_frames = num_frames
self.height = height
self.width = width
self.is_i2v = is_i2v
self.args = args
self.cam_type = self.args.cam_type
self.frame_process = v2.Compose([
v2.CenterCrop(size=(height, width)),
v2.Resize(size=(height, width), antialias=True),
v2.ToTensor(),
v2.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
])
def crop_and_resize(self, image):
width, height = image.size
scale = max(self.width / width, self.height / height)
image = torchvision.transforms.functional.resize(
image,
(round(height*scale), round(width*scale)),
interpolation=torchvision.transforms.InterpolationMode.BILINEAR
)
return image
def load_frames_using_imageio(self, file_path, max_num_frames, start_frame_id, interval, num_frames, frame_process):
reader = imageio.get_reader(file_path)
if reader.count_frames() < max_num_frames or reader.count_frames() - 1 < start_frame_id + (num_frames - 1) * interval:
reader.close()
return None
frames = []
first_frame = None
for frame_id in range(num_frames):
frame = reader.get_data(start_frame_id + frame_id * interval)
frame = Image.fromarray(frame)
frame = self.crop_and_resize(frame)
if first_frame is None:
first_frame = np.array(frame)
frame = frame_process(frame)
frames.append(frame)
reader.close()
frames = torch.stack(frames, dim=0)
frames = rearrange(frames, "T C H W -> C T H W")
if self.is_i2v:
return frames, first_frame
else:
return frames
def is_image(self, file_path):
file_ext_name = file_path.split(".")[-1]
if file_ext_name.lower() in ["jpg", "jpeg", "png", "webp"]:
return True
return False
def load_video(self, file_path):
start_frame_id = torch.randint(0, self.max_num_frames - (self.num_frames - 1) * self.frame_interval, (1,))[0]
frames = self.load_frames_using_imageio(file_path, self.max_num_frames, start_frame_id, self.frame_interval, self.num_frames, self.frame_process)
return frames
def parse_matrix(self, matrix_str):
rows = matrix_str.strip().split('] [')
matrix = []
for row in rows:
row = row.replace('[', '').replace(']', '')
matrix.append(list(map(float, row.split())))
return np.array(matrix)
def get_relative_pose(self, cam_params):
abs_w2cs = [cam_param.w2c_mat for cam_param in cam_params]
abs_c2ws = [cam_param.c2w_mat for cam_param in cam_params]
cam_to_origin = 0
target_cam_c2w = np.array([
[1, 0, 0, 0],
[0, 1, 0, -cam_to_origin],
[0, 0, 1, 0],
[0, 0, 0, 1]
])
abs2rel = target_cam_c2w @ abs_w2cs[0]
ret_poses = [target_cam_c2w, ] + [abs2rel @ abs_c2w for abs_c2w in abs_c2ws[1:]]
ret_poses = np.array(ret_poses, dtype=np.float32)
return ret_poses
def __getitem__(self, data_id):
text = self.text[data_id]
data = {"text": text}
# load camera
tgt_camera_path = "./example_test_data/cameras/camera_extrinsics.json"
with open(tgt_camera_path, 'r') as file:
cam_data = json.load(file)
multiview_c2ws = []
cam_idx = list(range(81))[::4]
if self.cam_type == "az":
tgt_idx = 1
cond_idx = 3
elif self.cam_type == "el":
tgt_idx = 3
cond_idx = 7
elif self.cam_type == "dis":
tgt_idx = 9
cond_idx = 10
for view_idx in [cond_idx, tgt_idx]:
traj = [self.parse_matrix(cam_data[f"frame{idx}"][f"cam{view_idx:02d}"]) for idx in cam_idx]
traj = np.stack(traj).transpose(0, 2, 1)
c2ws = []
for c2w in traj:
c2w = c2w[:, [1, 2, 0, 3]]
c2w[:3, 1] *= -1.
c2w[:3, 3] /= 200
c2ws.append(c2w)
multiview_c2ws.append(c2ws)
cond_cam_params = [Camera(cam_param) for cam_param in multiview_c2ws[0]]
tgt_cam_params = [Camera(cam_param) for cam_param in multiview_c2ws[1]]
relative_poses = []
for i in range(len(tgt_cam_params)):
relative_pose = self.get_relative_pose([tgt_cam_params[i], cond_cam_params[i]])
relative_poses.append(torch.as_tensor(relative_pose)[:,:3,:])
pose_embedding = torch.stack(relative_poses, dim=1) # v,21,3,4
pose_embedding = rearrange(pose_embedding, 'v f c d -> v f (c d)')
data['camera'] = pose_embedding.to(torch.bfloat16)
return data
def __len__(self):
return len(self.text)
def parse_args():
parser = argparse.ArgumentParser(description="SynCamMaster Inference")
parser.add_argument(
"--dataset_path",
type=str,
default="./example_test_data",
help="The path of the Dataset.",
)
parser.add_argument(
"--ckpt_path",
type=str,
default="./models/SynCamMaster/checkpoints/step20000.ckpt",
help="Path to save the model.",
)
parser.add_argument(
"--output_dir",
type=str,
default="./results",
help="Path to save the results.",
)
parser.add_argument(
"--dataloader_num_workers",
type=int,
default=1,
help="Number of subprocesses to use for data loading. 0 means that the data will be loaded in the main process.",
)
parser.add_argument(
"--cam_type",
type=str,
default='az',
)
parser.add_argument(
"--cfg_scale",
type=float,
default=5.0,
)
args = parser.parse_args()
return args
if __name__ == '__main__':
args = parse_args()
# 1. Load Wan2.1 pre-trained models
model_manager = ModelManager(torch_dtype=torch.bfloat16, device="cpu")
model_manager.load_models([
"models/Wan-AI/Wan2.1-T2V-1.3B/diffusion_pytorch_model.safetensors",
"models/Wan-AI/Wan2.1-T2V-1.3B/models_t5_umt5-xxl-enc-bf16.pth",
"models/Wan-AI/Wan2.1-T2V-1.3B/Wan2.1_VAE.pth",
])
pipe = WanVideoSynCamMasterPipeline.from_model_manager(model_manager, device="cuda")
# 2. Initialize additional modules introduced in SynCamMaster
dim=pipe.dit.blocks[0].self_attn.q.weight.shape[0]
for block in pipe.dit.blocks:
block.cam_encoder = nn.Linear(12, dim)
block.projector = nn.Linear(dim, dim)
block.cam_encoder.weight.data.zero_()
block.cam_encoder.bias.data.zero_()
block.projector.weight = nn.Parameter(torch.zeros(dim, dim))
block.projector.bias = nn.Parameter(torch.zeros(dim))
block.norm_mvs = nn.LayerNorm(dim, eps=block.norm1.eps, elementwise_affine=False)
block.modulation_mvs = nn.Parameter(torch.randn(1, 3, dim) / dim**0.5)
block.mvs_attn = SelfAttention(dim, block.self_attn.num_heads, block.self_attn.norm_q.eps)
block.modulation_mvs.data = block.modulation.data[:, :3, :].clone()
block.mvs_attn.load_state_dict(block.self_attn.state_dict(), strict=True)
# 3. Load SynCamMaster checkpoint
state_dict = torch.load(args.ckpt_path, map_location="cpu")
pipe.dit.load_state_dict(state_dict, strict=True)
pipe.to("cuda")
pipe.to(dtype=torch.bfloat16)
step_number = re.search(r"step(\d+)", args.ckpt_path).group(1)
output_dir = os.path.join(args.output_dir, f"step{step_number}", f"cam_type_{args.cam_type}")
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# 4. Prepare test data (source video, target camera, target trajectory)
dataset = TextCameraDataset(
args.dataset_path,
os.path.join(args.dataset_path, "metadata.csv"),
args,
)
dataloader = torch.utils.data.DataLoader(
dataset,
shuffle=False,
batch_size=1,
num_workers=args.dataloader_num_workers
)
# 5. Inference
for batch_idx, batch in enumerate(dataloader):
text = batch["text"]
camera = batch["camera"]
camera = rearrange(camera, 'b v ... -> (b v) ...')
video1, video2 = pipe(
prompt=text,
negative_prompt="色调艳丽,过曝,静态,细节模糊不清,字幕,风格,作品,画作,画面,静止,整体发灰,最差质量,低质量,JPEG压缩残留,丑陋的,残缺的,多余的手指,画得不好的手部,画得不好的脸部,畸形的,毁容的,形态畸形的肢体,手指融合,静止不动的画面,杂乱的背景,三条腿,背景人很多,倒着走",
camera=camera,
cfg_scale=args.cfg_scale,
num_inference_steps=50,
seed=0, tiled=True
)
merged_videos = []
for img1, img2 in zip(video1, video2):
width1, height1 = img1.size
width2, height2 = img2.size
merged_image = Image.new("RGB", (width1 + width2, height1))
merged_image.paste(img1, (0, 0))
merged_image.paste(img2, (width1, 0))
merged_videos.append(merged_image)
save_video(merged_videos, os.path.join(output_dir, f"prompt{batch_idx}_merged.mp4"), fps=30, quality=5)
# save_video(video1, os.path.join(output_dir, f"prompt{batch_idx}_view1.mp4"), fps=30, quality=5)
# save_video(video2, os.path.join(output_dir, f"prompt{batch_idx}_view2.mp4"), fps=30, quality=5)