File size: 6,852 Bytes
0297809
4be2365
0297809
 
4be2365
 
 
0297809
4be2365
 
37e13fe
4be2365
 
 
 
37e13fe
 
4be2365
 
 
 
 
 
 
 
37e13fe
 
0297809
 
 
4be2365
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0297809
 
 
 
4be2365
 
 
0297809
 
 
 
 
 
 
 
 
 
4be2365
0297809
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4be2365
 
0297809
 
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
import gc
import os
import spaces
import gradio as gr
import random
import tempfile
import time

from easydict import EasyDict
import numpy as np
import torch
from dav.pipelines import DAVPipeline
from dav.models import UNetSpatioTemporalRopeConditionModel
from diffusers import AutoencoderKLTemporalDecoder, FlowMatchEulerDiscreteScheduler
from dav.utils import img_utils


def seed_all(seed: int = 0):
    """
    Set random seeds for reproducibility.
    """
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)


examples = [
    ["demos/wooly_mammoth.mp4", 3, 32, 16, 16, 6, 960],
]


def load_models(model_base, device):
    vae = AutoencoderKLTemporalDecoder.from_pretrained(model_base, subfolder="vae")
    scheduler = FlowMatchEulerDiscreteScheduler.from_pretrained(
        model_base, subfolder="scheduler"
    )
    unet = UNetSpatioTemporalRopeConditionModel.from_pretrained(
        model_base, subfolder="unet"
    )
    unet_interp = UNetSpatioTemporalRopeConditionModel.from_pretrained(
        model_base, subfolder="unet_interp"
    )
    pipe = DAVPipeline(
        vae=vae,
        unet=unet,
        unet_interp=unet_interp,
        scheduler=scheduler,
    )
    pipe = pipe.to(device)
    return pipe


model_base = "hhyangcs/depth-any-video"
device_type = "cuda"
device = torch.device(device_type)
pipe = load_models(model_base, device)


@spaces.GPU(duration=140)
def infer_depth(
    file: str,
    denoise_steps: int = 3,
    num_frames: int = 32,
    decode_chunk_size: int = 16,
    num_interp_frames: int = 16,
    num_overlap_frames: int = 6,
    max_resolution: int = 1024,
    seed: int = 66,
    output_dir: str = "./outputs",
):
    seed_all(seed)

    max_frames = (num_interp_frames + 2 - num_overlap_frames) * (num_frames // 2)
    image, fps = img_utils.read_video(file, max_frames=max_frames)

    image = img_utils.imresize_max(image, max_resolution)
    image = img_utils.imcrop_multi(image)
    image_tensor = np.ascontiguousarray(
        [_img.transpose(2, 0, 1) / 255.0 for _img in image]
    )
    image_tensor = torch.from_numpy(image_tensor).to(device)
    print(f"==> video name: {file}, frames shape: {image_tensor.shape}")

    with torch.no_grad(), torch.autocast(device_type=device_type, dtype=torch.float16):
        pipe_out = pipe(
            image_tensor,
            num_frames=num_frames,
            num_overlap_frames=num_overlap_frames,
            num_interp_frames=num_interp_frames,
            decode_chunk_size=decode_chunk_size,
            num_inference_steps=denoise_steps,
        )

    disparity = pipe_out.disparity
    disparity_colored = pipe_out.disparity_colored
    image = pipe_out.image
    # (N, H, 2 * W, 3)
    merged = np.concatenate(
        [
            image,
            disparity_colored,
        ],
        axis=2,
    )

    file_name = os.path.splitext(os.path.basename(file))[0]
    os.makedirs(output_dir, exist_ok=True)
    output_path = os.path.join(output_dir, f"{file_name}_depth.mp4")
    img_utils.write_video(
        output_path,
        merged,
        fps,
    )

    # clear the cache for the next video
    gc.collect()
    torch.cuda.empty_cache()

    return output_path


def construct_demo():
    with gr.Blocks(analytics_enabled=False) as depthanyvideo_iface:

        with gr.Row(equal_height=True):
            with gr.Column(scale=1):
                input_video = gr.Video(label="Input Video")

            with gr.Column(scale=1):
                with gr.Row(equal_height=True):
                    output_video = gr.Video(
                        label="Ouput Video Depth",
                        interactive=False,
                        autoplay=True,
                        loop=True,
                        show_share_button=True,
                        scale=1,
                    )

        with gr.Row(equal_height=True):
            with gr.Column(scale=1):
                with gr.Row(equal_height=False):
                    with gr.Accordion("Advanced Settings", open=False):
                        denoise_steps = gr.Slider(
                            label="Denoise Steps",
                            minimum=1,
                            maximum=10,
                            value=3,
                            step=1,
                        )
                        num_frames = gr.Slider(
                            label="Number of Key Frames",
                            minimum=16,
                            maximum=32,
                            value=24,
                            step=2,
                        )
                        decode_chunk_size = gr.Slider(
                            label="Decode Chunk Size",
                            minimum=8,
                            maximum=32,
                            value=16,
                            step=1,
                        )
                        num_interp_frames = gr.Slider(
                            label="Number of Interpolation Frames",
                            minimum=8,
                            maximum=32,
                            value=16,
                            step=1,
                        )
                        num_overlap_frames = gr.Slider(
                            label="Number of Overlap Frames",
                            minimum=2,
                            maximum=10,
                            value=6,
                            step=1,
                        )
                        max_resolution = gr.Slider(
                            label="Maximum Resolution",
                            minimum=512,
                            maximum=2048,
                            value=1024,
                            step=32,
                        )
                    generate_btn = gr.Button("Generate")
            with gr.Column(scale=2):
                pass

        gr.Examples(
            examples=examples,
            inputs=[
                input_video,
                denoise_steps,
                num_frames,
                decode_chunk_size,
                num_interp_frames,
                num_overlap_frames,
                max_resolution,
            ],
            outputs=output_video,
            fn=infer_depth,
            cache_examples="lazy",
        )

        generate_btn.click(
            fn=infer_depth,
            inputs=[
                input_video,
                denoise_steps,
                num_frames,
                decode_chunk_size,
                num_interp_frames,
                num_overlap_frames,
                max_resolution,
            ],
            outputs=output_video,
        )

    return depthanyvideo_iface


demo = construct_demo()

if __name__ == "__main__":
    demo.queue()
    demo.launch(share=True)