-
Notifications
You must be signed in to change notification settings - Fork 7
/
depth_splatting_inference.py
305 lines (262 loc) · 10.4 KB
/
depth_splatting_inference.py
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
import gc
import cv2
import os
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision.io import write_video
from diffusers.training_utils import set_seed
from fire import Fire
from decord import VideoReader, cpu
from dependency.DepthCrafter.depthcrafter.depth_crafter_ppl import DepthCrafterPipeline
from dependency.DepthCrafter.depthcrafter.unet import DiffusersUNetSpatioTemporalConditionModelDepthCrafter
from dependency.DepthCrafter.depthcrafter.utils import vis_sequence_depth
from Forward_Warp import forward_warp
def read_video_frames(video_path, process_length, target_fps, max_res, dataset="open"):
if dataset == "open":
print("==> processing video: ", video_path)
vid = VideoReader(video_path, ctx=cpu(0))
print("==> original video shape: ", (len(vid), *vid.get_batch([0]).shape[1:]))
original_height, original_width = vid.get_batch([0]).shape[1:3]
height = round(original_height / 64) * 64
width = round(original_width / 64) * 64
if max(height, width) > max_res:
scale = max_res / max(original_height, original_width)
height = round(original_height * scale / 64) * 64
width = round(original_width * scale / 64) * 64
else:
height = dataset_res_dict[dataset][0]
width = dataset_res_dict[dataset][1]
vid = VideoReader(video_path, ctx=cpu(0), width=width, height=height)
fps = vid.get_avg_fps() if target_fps == -1 else target_fps
stride = round(vid.get_avg_fps() / fps)
stride = max(stride, 1)
frames_idx = list(range(0, len(vid), stride))
print(
f"==> downsampled shape: {len(frames_idx), *vid.get_batch([0]).shape[1:]}, with stride: {stride}"
)
if process_length != -1 and process_length < len(frames_idx):
frames_idx = frames_idx[:process_length]
print(
f"==> final processing shape: {len(frames_idx), *vid.get_batch([0]).shape[1:]}"
)
frames = vid.get_batch(frames_idx).asnumpy().astype("float32") / 255.0
return frames, fps, original_height, original_width
class DepthCrafterDemo:
def __init__(
self,
unet_path: str,
pre_trained_path: str,
cpu_offload: str = "model",
):
unet = DiffusersUNetSpatioTemporalConditionModelDepthCrafter.from_pretrained(
unet_path,
low_cpu_mem_usage=True,
torch_dtype=torch.float16,
)
# load weights of other components from the provided checkpoint
self.pipe = DepthCrafterPipeline.from_pretrained(
pre_trained_path,
unet=unet,
torch_dtype=torch.float16,
variant="fp16",
)
# for saving memory, we can offload the model to CPU, or even run the model sequentially to save more memory
if cpu_offload is not None:
if cpu_offload == "sequential":
# This will slow, but save more memory
self.pipe.enable_sequential_cpu_offload()
elif cpu_offload == "model":
self.pipe.enable_model_cpu_offload()
else:
raise ValueError(f"Unknown cpu offload option: {cpu_offload}")
else:
self.pipe.to("cuda")
# enable attention slicing and xformers memory efficient attention
try:
self.pipe.enable_xformers_memory_efficient_attention()
except Exception as e:
print(e)
print("Xformers is not enabled")
self.pipe.enable_attention_slicing()
def infer(
self,
input_video_path: str,
output_video_path: str,
process_length: int = -1,
num_denoising_steps: int = 8,
guidance_scale: float = 1.2,
window_size: int = 70,
overlap: int = 25,
max_res: int = 1024,
dataset: str = "open",
target_fps: int = -1,
seed: int = 42,
track_time: bool = False,
save_depth: bool = False,
):
set_seed(seed)
frames, target_fps, original_height, original_width = read_video_frames(
input_video_path,
process_length,
target_fps,
max_res,
dataset,
)
# inference the depth map using the DepthCrafter pipeline
with torch.inference_mode():
res = self.pipe(
frames,
height=frames.shape[1],
width=frames.shape[2],
output_type="np",
guidance_scale=guidance_scale,
num_inference_steps=num_denoising_steps,
window_size=window_size,
overlap=overlap,
track_time=track_time,
).frames[0]
# convert the three-channel output to a single channel depth map
res = res.sum(-1) / res.shape[-1]
# resize the depth to the original size
tensor_res = torch.tensor(res).unsqueeze(1).float().contiguous().cuda()
res = F.interpolate(tensor_res, size=(original_height, original_width), mode='bilinear', align_corners=False)
res = res.cpu().numpy()[:,0,:,:]
# normalize the depth map to [0, 1] across the whole video
res = (res - res.min()) / (res.max() - res.min())
# visualize the depth map and save the results
vis = vis_sequence_depth(res)
# save the depth map and visualization with the target FPS
save_path = os.path.join(
os.path.dirname(output_video_path), os.path.splitext(os.path.basename(output_video_path))[0]
)
os.makedirs(os.path.dirname(save_path), exist_ok=True)
if save_depth:
np.savez_compressed(save_path + ".npz", depth=res)
write_video(save_path + "_depth_vis.mp4", vis*255.0, fps=target_fps, video_codec="h264", options={"crf": "16"})
return res, vis
class ForwardWarpStereo(nn.Module):
def __init__(self, eps=1e-6, occlu_map=False):
super(ForwardWarpStereo, self).__init__()
self.eps = eps
self.occlu_map = occlu_map
self.fw = forward_warp()
def forward(self, im, disp):
"""
:param im: BCHW
:param disp: B1HW
:return: BCHW
detach will lead to unconverge!!
"""
im = im.contiguous()
disp = disp.contiguous()
# weights_map = torch.abs(disp)
weights_map = disp - disp.min()
weights_map = (
1.414
) ** weights_map # using 1.414 instead of EXP for avoding numerical overflow.
flow = -disp.squeeze(1)
dummy_flow = torch.zeros_like(flow, requires_grad=False)
flow = torch.stack((flow, dummy_flow), dim=-1)
res_accum = self.fw(im * weights_map, flow)
# mask = self.fw(weights_map, flow.detach())
mask = self.fw(weights_map, flow)
mask.clamp_(min=self.eps)
res = res_accum / mask
if not self.occlu_map:
return res
else:
ones = torch.ones_like(disp, requires_grad=False)
occlu_map = self.fw(ones, flow)
occlu_map.clamp_(0.0, 1.0)
occlu_map = 1.0 - occlu_map
return res, occlu_map
def DepthSplatting(
input_video_path,
output_video_path,
video_depth,
depth_vis,
max_disp,
process_length,
batch_size):
'''
Depth-Based Video Splatting Using the Video Depth.
Args:
input_video_path: Path to the input video.
output_video_path: Path to the output video.
video_depth: Video depth with shape of [T, H, W] in [0, 1].
depth_vis: Visualized video depth with shape of [T, H, W, 3] in [0, 1].
process_length: The length of video to process.
batch_size: The batch size for splatting to save GPU memory.
'''
vid_reader = VideoReader(input_video_path, ctx=cpu(0))
original_fps = vid_reader.get_avg_fps()
input_frames = vid_reader[:].asnumpy() / 255.0
if process_length != -1 and process_length < len(input_frames):
input_frames = input_frames[:process_length]
video_depth = video_depth[:process_length]
depth_vis = depth_vis[:process_length]
stereo_projector = ForwardWarpStereo(occlu_map=True).cuda()
num_frames = len(input_frames)
height, width, _ = input_frames[0].shape
# Initialize OpenCV VideoWriter
out = cv2.VideoWriter(
output_video_path,
cv2.VideoWriter_fourcc(*"mp4v"),
original_fps,
(width * 2, height * 2)
)
for i in range(0, num_frames, batch_size):
batch_frames = input_frames[i:i+batch_size]
batch_depth = video_depth[i:i+batch_size]
batch_depth_vis = depth_vis[i:i+batch_size]
left_video = torch.from_numpy(batch_frames).permute(0, 3, 1, 2).float().cuda()
disp_map = torch.from_numpy(batch_depth).unsqueeze(1).float().cuda()
disp_map = disp_map * 2.0 - 1.0
disp_map = disp_map * max_disp
with torch.no_grad():
right_video, occlusion_mask = stereo_projector(left_video, disp_map)
right_video = right_video.cpu().permute(0, 2, 3, 1).numpy()
occlusion_mask = occlusion_mask.cpu().permute(0, 2, 3, 1).numpy().repeat(3, axis=-1)
for j in range(len(batch_frames)):
video_grid_top = np.concatenate([batch_frames[j], batch_depth_vis[j]], axis=1)
video_grid_bottom = np.concatenate([occlusion_mask[j], right_video[j]], axis=1)
video_grid = np.concatenate([video_grid_top, video_grid_bottom], axis=0)
video_grid_uint8 = np.clip(video_grid * 255.0, 0, 255).astype(np.uint8)
video_grid_bgr = cv2.cvtColor(video_grid_uint8, cv2.COLOR_RGB2BGR)
out.write(video_grid_bgr)
# Free up GPU memory
del left_video, disp_map, right_video, occlusion_mask
torch.cuda.empty_cache()
gc.collect()
out.release()
def main(
input_video_path: str,
output_video_path: str,
unet_path: str,
pre_trained_path: str,
max_disp: float = 20.0,
process_length = -1,
batch_size = 10
):
depthcrafter_demo = DepthCrafterDemo(
unet_path=unet_path,
pre_trained_path=pre_trained_path
)
video_depth, depth_vis = depthcrafter_demo.infer(
input_video_path,
output_video_path,
process_length
)
DepthSplatting(
input_video_path,
output_video_path,
video_depth,
depth_vis,
max_disp,
process_length,
batch_size
)
if __name__ == "__main__":
Fire(main)