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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
import os
import torch
import numpy as np
from PIL import Image
import imageio
import json
from diffsynth import WanVideoReCamMasterPipeline, ModelManager
import argparse
from torchvision.transforms import v2
from einops import rearrange
import torch.nn as nn


def load_encoded_video_from_pth(pth_path, start_frame=0, num_frames=10):
    """
    从pth文件加载预编码的视频数据
    Args:
        pth_path: pth文件路径
        start_frame: 起始帧索引(基于压缩后的latent帧数)
        num_frames: 需要的帧数(基于压缩后的latent帧数)
    Returns:
        condition_latents: [C, T, H, W] 格式的latent tensor
    """
    print(f"Loading encoded video from {pth_path}")
    
    # 加载编码数据
    encoded_data = torch.load(pth_path, weights_only=False, map_location="cpu")
    
    # 获取latent数据
    full_latents = encoded_data['latents']  # [C, T, H, W]
    
    print(f"Full latents shape: {full_latents.shape}")
    print(f"Extracting frames {start_frame} to {start_frame + num_frames}")
    
    # 检查帧数是否足够
    if start_frame + num_frames > full_latents.shape[1]:
        raise ValueError(f"Not enough frames: requested {start_frame + num_frames}, available {full_latents.shape[1]}")
    
    # 提取指定帧数
    condition_latents = full_latents[:, start_frame:start_frame + num_frames, :, :]
    
    print(f"Extracted condition latents shape: {condition_latents.shape}")
    
    return condition_latents, encoded_data


def compute_relative_pose(pose_a, pose_b, use_torch=False):
    """
    计算相机B相对于相机A的相对位姿矩阵
    """
    assert pose_a.shape == (4, 4), f"相机A外参矩阵形状应为(4,4),实际为{pose_a.shape}"
    assert pose_b.shape == (4, 4), f"相机B外参矩阵形状应为(4,4),实际为{pose_b.shape}"
    
    if use_torch:
        if not isinstance(pose_a, torch.Tensor):
            pose_a = torch.from_numpy(pose_a).float()
        if not isinstance(pose_b, torch.Tensor):
            pose_b = torch.from_numpy(pose_b).float()
        
        pose_a_inv = torch.inverse(pose_a)
        relative_pose = torch.matmul(pose_b, pose_a_inv)
    else:
        if not isinstance(pose_a, np.ndarray):
            pose_a = np.array(pose_a, dtype=np.float32)
        if not isinstance(pose_b, np.ndarray):
            pose_b = np.array(pose_b, dtype=np.float32)
        
        pose_a_inv = np.linalg.inv(pose_a)
        relative_pose = np.matmul(pose_b, pose_a_inv)
    
    return relative_pose


def generate_camera_poses_from_data(cam_data, start_frame, condition_frames, target_frames):
    """
    从实际相机数据生成pose embeddings
    Args:
        cam_data: 相机外参数据
        start_frame: 起始帧(原始帧索引)
        condition_frames: 条件帧数(压缩后)
        target_frames: 目标帧数(压缩后)
    """
    time_compression_ratio = 4
    total_frames = condition_frames + target_frames
    
    # 获取相机外参序列
    cam_extrinsic = cam_data  # [N, 4, 4]
    
    # 计算原始帧索引
    start_frame_original = start_frame * time_compression_ratio
    end_frame_original = (start_frame + total_frames) * time_compression_ratio
    
    print(f"Using camera data from frame {start_frame_original} to {end_frame_original}")
    
    # 计算相对pose
    relative_poses = []
    for i in range(total_frames):
        frame_idx = start_frame_original + i * time_compression_ratio
        next_frame_idx = frame_idx + time_compression_ratio
        
      
        cam_prev = cam_extrinsic[frame_idx]
      
            

        relative_poses.append(torch.as_tensor(cam_prev))  # 取前3行

        print(cam_prev)
    # 组装pose embedding
    pose_embedding = torch.stack(relative_poses, dim=0)
    # print('pose_embedding init:',pose_embedding[0])
    print('pose_embedding:',pose_embedding)
    # assert False

    # pose_embedding = rearrange(pose_embedding, 'b c d -> b (c d)')  # [frames, 12]
    
    # 添加mask信息
    mask = torch.zeros(total_frames, dtype=torch.float32)
    mask[:condition_frames] = 1.0  # condition frames
    mask = mask.view(-1, 1)
    
    # 组合pose和mask
    camera_embedding = torch.cat([pose_embedding, mask], dim=1)  # [frames, 13]
    
    print(f"Generated camera embedding shape: {camera_embedding.shape}")
    
    return camera_embedding.to(torch.bfloat16)


def generate_camera_poses(direction="forward", target_frames=10, condition_frames=20):
    """
    根据指定方向生成相机pose序列(合成数据)
    """
    time_compression_ratio = 4
    total_frames = condition_frames + target_frames
    
    poses = []
    
    for i in range(total_frames):
        t = i / max(1, total_frames - 1)  # 0 to 1
        
        # 创建变换矩阵
        pose = np.eye(4, dtype=np.float32)
        
        if direction == "forward":
            # 前进:沿z轴负方向移动
            pose[2, 3] = -t * 0.04
            print('forward!')
            
        elif direction == "backward":
            # 后退:沿z轴正方向移动
            pose[2, 3] = t * 2.0
            
        elif direction == "left_turn":
            # 左转:前进 + 绕y轴旋转
            pose[2, 3] = -t * 0.03  # 前进
            pose[0, 3] = t * 0.02   # 左移
            # 添加旋转
            yaw = t * 1
            pose[0, 0] = np.cos(yaw)
            pose[0, 2] = np.sin(yaw)
            pose[2, 0] = -np.sin(yaw)
            pose[2, 2] = np.cos(yaw)
            
        elif direction == "right_turn":
            # 右转:前进 + 绕y轴反向旋转
            pose[2, 3] = -t * 0.03 # 前进
            pose[0, 3] = -t * 0.02 # 右移
            # 添加旋转
            yaw = - t * 1
            pose[0, 0] = np.cos(yaw)
            pose[0, 2] = np.sin(yaw)
            pose[2, 0] = -np.sin(yaw)
            pose[2, 2] = np.cos(yaw)
            
        poses.append(pose)
    
    # 计算相对pose
    relative_poses = []
    for i in range(len(poses) - 1):
        relative_pose = compute_relative_pose(poses[i], poses[i + 1])
        relative_poses.append(torch.as_tensor(relative_pose[:3, :]))  # 取前3行
    
    # 为了匹配模型输入,需要确保帧数正确
    if len(relative_poses) < total_frames:
        # 补充最后一帧
        relative_poses.append(relative_poses[-1])
    
    pose_embedding = torch.stack(relative_poses[:total_frames], dim=0)
    
    print('pose_embedding init:',pose_embedding[0])

    print('pose_embedding:',pose_embedding[-5:])

    pose_embedding = rearrange(pose_embedding, 'b c d -> b (c d)')  # [frames, 12]
    
    # 添加mask信息
    mask = torch.zeros(total_frames, dtype=torch.float32)
    mask[:condition_frames] = 1.0  # condition frames
    mask = mask.view(-1, 1)
    
    # 组合pose和mask
    camera_embedding = torch.cat([pose_embedding, mask], dim=1)  # [frames, 13]
    
    print(f"Generated {direction} movement poses:")
    print(f"  Total frames: {total_frames}")
    print(f"  Camera embedding shape: {camera_embedding.shape}")
    
    return camera_embedding.to(torch.bfloat16)


def inference_sekai_video_from_pth(
    condition_pth_path,
    dit_path,
    output_path="sekai/infer_results/output_sekai.mp4",
    start_frame=0,
    condition_frames=10,  # 压缩后的帧数
    target_frames=2,      # 压缩后的帧数
    device="cuda",
    prompt="a robotic arm executing precise manipulation tasks on a clean, organized desk",
    direction="forward",
    use_real_poses=True
):
    """
    从pth文件进行Sekai视频推理
    """
    os.makedirs(os.path.dirname(output_path), exist_ok=True)

    print(f"Setting up models for {direction} movement...")
    
    # 1. Load 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 = WanVideoReCamMasterPipeline.from_model_manager(model_manager, device="cuda")

    # Add camera components to DiT
    dim = pipe.dit.blocks[0].self_attn.q.weight.shape[0]
    for block in pipe.dit.blocks:
        block.cam_encoder = nn.Linear(30, dim)  # 13维embedding (12D pose + 1D mask)
        block.projector = nn.Linear(dim, dim)
        block.cam_encoder.weight.data.zero_()
        block.cam_encoder.bias.data.zero_()
        block.projector.weight = nn.Parameter(torch.eye(dim))
        block.projector.bias = nn.Parameter(torch.zeros(dim))
    
    # Load trained DiT weights
    dit_state_dict = torch.load(dit_path, map_location="cpu")
    pipe.dit.load_state_dict(dit_state_dict, strict=True)
    pipe = pipe.to(device)
    pipe.scheduler.set_timesteps(50)
    
    print("Loading condition video from pth...")
    
    # Load condition video from pth
    condition_latents, encoded_data = load_encoded_video_from_pth(
        condition_pth_path, 
        start_frame=start_frame,
        num_frames=condition_frames
    )
    
    condition_latents = condition_latents.unsqueeze(0).to(device, dtype=pipe.torch_dtype)
    
    print("Processing poses...")
    
    # 生成相机pose embedding
    if use_real_poses and 'cam_emb' in encoded_data:
        print("Using real camera poses from data")
        camera_embedding = generate_camera_poses_from_data(
            encoded_data['cam_emb'],
            start_frame=start_frame,
            condition_frames=condition_frames,
            target_frames=target_frames
        )
    else:
        print(f"Using synthetic {direction} poses")
        camera_embedding = generate_camera_poses(
            direction=direction,
            target_frames=target_frames,
            condition_frames=condition_frames
        )


    
    camera_embedding = camera_embedding.unsqueeze(0).to(device, dtype=torch.bfloat16)
    
    print(f"Camera embedding shape: {camera_embedding.shape}")
    
    print("Encoding prompt...")
    
    # Encode text prompt
    prompt_emb = pipe.encode_prompt(prompt)
    
    print("Generating video...")
    
    # Generate target latents
    batch_size = 1
    channels = condition_latents.shape[1]
    latent_height = condition_latents.shape[3]
    latent_width = condition_latents.shape[4]
    
    # 空间裁剪以节省内存(如果需要)
    target_height, target_width = 64, 64
    
    if latent_height > target_height or latent_width > target_width:
        # 中心裁剪
        h_start = (latent_height - target_height) // 2
        w_start = (latent_width - target_width) // 2
        condition_latents = condition_latents[:, :, :, 
                        h_start:h_start+target_height, 
                        w_start:w_start+target_width]
        latent_height = target_height
        latent_width = target_width
                
    # Initialize target latents with noise
    target_latents = torch.randn(
        batch_size, channels, target_frames, latent_height, latent_width,
        device=device, dtype=pipe.torch_dtype
    )
    
    print(f"Condition latents shape: {condition_latents.shape}")
    print(f"Target latents shape: {target_latents.shape}")
    print(f"Camera embedding shape: {camera_embedding.shape}")
    
    # Combine condition and target latents
    combined_latents = torch.cat([condition_latents, target_latents], dim=2)
    print(f"Combined latents shape: {combined_latents.shape}")

    # Prepare extra inputs
    extra_input = pipe.prepare_extra_input(combined_latents)
    
    # Denoising loop
    timesteps = pipe.scheduler.timesteps
        
    for i, timestep in enumerate(timesteps):
        print(f"Denoising step {i+1}/{len(timesteps)}")
        
        # Prepare timestep
        timestep_tensor = timestep.unsqueeze(0).to(device, dtype=pipe.torch_dtype)
        
        # Predict noise
        with torch.no_grad():
            noise_pred = pipe.dit(
                combined_latents,
                timestep=timestep_tensor,
                cam_emb=camera_embedding,
                **prompt_emb,
                **extra_input
            )
        
        # Update only target part
        target_noise_pred = noise_pred[:, :, condition_frames:, :, :]
        target_latents = pipe.scheduler.step(target_noise_pred, timestep, target_latents)
        
        # Update combined latents
        combined_latents[:, :, condition_frames:, :, :] = target_latents
    
    print("Decoding video...")
    
    # Decode final video
    final_video = torch.cat([condition_latents, target_latents], dim=2)
    decoded_video = pipe.decode_video(final_video, tiled=True, tile_size=(34, 34), tile_stride=(18, 16))

    # Save video
    print(f"Saving video to {output_path}")

    # Convert to numpy and save
    video_np = decoded_video[0].to(torch.float32).permute(1, 2, 3, 0).cpu().numpy()
    video_np = (video_np * 0.5 + 0.5).clip(0, 1)  # Denormalize
    video_np = (video_np * 255).astype(np.uint8)

    with imageio.get_writer(output_path, fps=20) as writer:
        for frame in video_np:
            writer.append_data(frame)

    print(f"Video generation completed! Saved to {output_path}")


def main():
    parser = argparse.ArgumentParser(description="Sekai Video Generation Inference from PTH")
    parser.add_argument("--condition_pth", type=str,
                       default="/share_zhuyixuan05/zhuyixuan05/rlbench/OpenBox_demo_49/encoded_video.pth")
    parser.add_argument("--start_frame", type=int, default=0,
                       help="Starting frame index (compressed latent frames)")
    parser.add_argument("--condition_frames", type=int, default=8,
                       help="Number of condition frames (compressed latent frames)")
    parser.add_argument("--target_frames", type=int, default=8,
                       help="Number of target frames to generate (compressed latent frames)")
    parser.add_argument("--direction", type=str, default="left_turn",
                       choices=["forward", "backward", "left_turn", "right_turn"],
                       help="Direction of camera movement (if not using real poses)")
    parser.add_argument("--use_real_poses",  default=False,
                       help="Use real camera poses from data")
    parser.add_argument("--dit_path", type=str, default="/home/zhuyixuan05/ReCamMaster/RLBench-train/step2000_dynamic.ckpt",
                       help="Path to trained DiT checkpoint")
    parser.add_argument("--output_path", type=str, default='/home/zhuyixuan05/ReCamMaster/rlbench/infer_results/output_rl_2.mp4',
                       help="Output video path")
    parser.add_argument("--prompt", type=str, 
                       default="a robotic arm executing precise manipulation tasks on a clean, organized desk",
                       help="Text prompt for generation")
    parser.add_argument("--device", type=str, default="cuda",
                       help="Device to run inference on")
    
    args = parser.parse_args()
    
    # 生成输出路径
    if args.output_path is None:
        pth_filename = os.path.basename(args.condition_pth)
        name_parts = os.path.splitext(pth_filename)
        output_dir = "rlbench/infer_results"
        os.makedirs(output_dir, exist_ok=True)
        
        if args.use_real_poses:
            output_filename = f"{name_parts[0]}_real_poses_{args.start_frame}_{args.condition_frames}_{args.target_frames}.mp4"
        else:
            output_filename = f"{name_parts[0]}_{args.direction}_{args.start_frame}_{args.condition_frames}_{args.target_frames}.mp4"
        
        output_path = os.path.join(output_dir, output_filename)
    else:
        output_path = args.output_path

    print(f"Input pth: {args.condition_pth}")
    print(f"Start frame: {args.start_frame} (compressed)")
    print(f"Condition frames: {args.condition_frames} (compressed, original: {args.condition_frames * 4})")
    print(f"Target frames: {args.target_frames} (compressed, original: {args.target_frames * 4})")
    print(f"Use real poses: {args.use_real_poses}")
    print(f"Output video will be saved to: {output_path}")
    
    inference_sekai_video_from_pth(
        condition_pth_path=args.condition_pth,
        dit_path=args.dit_path,
        output_path=output_path,
        start_frame=args.start_frame,
        condition_frames=args.condition_frames,
        target_frames=args.target_frames,
        device=args.device,
        prompt=args.prompt,
        direction=args.direction,
        use_real_poses=args.use_real_poses
    )


if __name__ == "__main__":
    main()