Views
No views yet
CogVideoControlNetModel for Sketch3DVE, a novel sketch-based 3D-aware video editing method. Sketch3DVE enables detailed local manipulation of videos, even with significant viewpoint changes, by handling novel view content consistency, preserving unedited regions, and translating sparse 2D sketch inputs into realistic 3D video outputs.diffusers library.1import os
2import tqdm
3import torch
4import numpy as np
5from PIL import Image
6from diffusers.utils import export_to_video
7
8from video_diffusion.pipeline_control_cogvideo import CogVideoXControlNetPipeline
9from video_diffusion.controlnet.controlnet_self_attn import CogVideoControlNetModel
10
11from diffusers import (
12 AutoencoderKLCogVideoX,
13 CogVideoXDDIMScheduler,
14)
15from decord import VideoReader
16
17from diffusers import (
18 AutoencoderKLCogVideoX,
19 CogVideoXDDIMScheduler,
20)
21from decord import VideoReader
22
23# Load video diffusion models
24basemodel_path = '/home/jovyan/data/liufenglin/Diffusion_models/CogVideoX-2b'
25controlnet_path = '/home/jovyan/old/liufenglin/code/CogVideo/viewcrafter_editing/control-ini-new/viewcrafter_editing_10_blocks/checkpoint-15000/controlnet'
26root_dir = './examples/cake'
27seed=40
28guidance_scale=10.0
29
30controlnet = CogVideoControlNetModel.from_pretrained(controlnet_path, torch_dtype=torch.float16, use_safetensors=True)
31pipeline = CogVideoXControlNetPipeline.from_pretrained(
32 basemodel_path, controlnet=controlnet, torch_dtype=torch.float16, use_safetensors=True
33)
34
35device = 'cuda:0'
36pipeline.scheduler = CogVideoXDDIMScheduler.from_config(pipeline.scheduler.config)
37pipeline = pipeline.to(device)
38pipeline.vae.enable_tiling()
39
40# prepare input file paths
41validation_prompts_path = os.path.join(root_dir, "editing.txt")
42validation_pointcloud_video_path = os.path.join(root_dir, "edited_render.mp4")
43validation_ref_video_path = os.path.join(root_dir, "editing_ori.png")
44input_video_path = os.path.join(root_dir, "original.mp4")
45input_video_mask = os.path.join(root_dir, "mask_box/box_render.mp4")
46
47output_dir = os.path.join(root_dir, "result")
48if not os.path.exists(output_dir):
49 os.mkdir(output_dir)
50
51# 1. Read the pointcloud video
52vr = VideoReader(uri=validation_pointcloud_video_path, height=-1, width=-1)
53ori_vlen = len(vr)
54temp_frms = vr.get_batch(np.arange(0, ori_vlen))
55tensor_frms = torch.from_numpy(temp_frms.asnumpy()) if type(temp_frms) is not torch.Tensor else temp_frms
56tensor_frms = tensor_frms.permute(3, 0, 1, 2) # [T, H, W, C] -> [C, T, H, W]
57condition_pc_input = (tensor_frms - 127.5) / 127.5
58condition_pc_input = condition_pc_input.unsqueeze(0)
59
60# 2. Read the original video
61temp_frms = Image.open(validation_ref_video_path)
62temp_frms = torch.from_numpy(np.array(temp_frms)).unsqueeze(0)
63temp_frms = temp_frms[:,:,:,0:3]
64temp_frms = temp_frms.permute(3, 0, 1, 2) # [T, H, W, C] -> [C, T, H, W]
65condition_ref_image_input = (temp_frms - 127.5) / 127.5
66condition_ref_image_input = condition_ref_image_input.unsqueeze(0)
67
68# 3. Read the input video
69vr = VideoReader(uri=input_video_path, height=-1, width=-1)
70ori_vlen = len(vr)
71temp_frms = vr.get_batch(np.arange(0, ori_vlen))
72tensor_frms = torch.from_numpy(temp_frms.asnumpy()) if type(temp_frms) is not torch.Tensor else temp_frms
73tensor_frms = tensor_frms.permute(3, 0, 1, 2) # [T, H, W, C] -> [C, T, H, W]
74input_image_input = (tensor_frms - 127.5) / 127.5
75input_image_input = input_image_input.unsqueeze(0)
76
77# 4. Read the input mask
78vr = VideoReader(uri=input_video_mask, height=-1, width=-1)
79ori_vlen = len(vr)
80temp_frms = vr.get_batch(np.arange(0, ori_vlen))
81tensor_frms = torch.from_numpy(temp_frms.asnumpy()) if type(temp_frms) is not torch.Tensor else temp_frms
82tensor_frms = tensor_frms.permute(3, 0, 1, 2) # [T, H, W, C] -> [C, T, H, W]
83input_mask_input = tensor_frms / 255
84input_mask_input = input_mask_input.unsqueeze(0)
85
86# 5. Read the caption
87with open(validation_prompts_path, "r") as f: # 打开文件
88 validation_prompt = f.read() # 读取文件
89
90control_scale = 1.0
91
92front_path = os.path.join(output_dir, "60000_test_video_")
93back_path = str(seed) + "_g" + str(guidance_scale) + "_c" + str(control_scale) + ".mp4"
94output_path = front_path + back_path
95generator = torch.Generator().manual_seed(seed)
96
97# 2. Inference the video results
98video = pipeline(
99 prompt=validation_prompt, # Text prompt
100 pc_image=condition_pc_input, # Control point cloud video
101 ref_image=condition_ref_image_input, # Control ref images
102
103 input_image=input_image_input, # input video
104 input_mask=input_mask_input, # input mask video
105
106 num_videos_per_prompt=1, # Number of videos to generate per prompt
107 num_inference_steps=50, # Number of inference steps
108 num_frames=49, # Number of frames to generate,changed to 49 for diffusers version `0.31.0` and after.
109 use_dynamic_cfg=True, ## This id used for DPM Sechduler, for DDIM scheduler, it should be False
110 guidance_scale=guidance_scale, # Guidance scale for classifier-free guidance, can set to 7 for DPM scheduler
111 generator=generator, # Set the seed for reproducibility
112
113 controlnet_conditioning_scale=control_scale,
114).frames[0]
115
116export_to_video(video, output_path, fps=8)