File size: 6,748 Bytes
2c76547
 
 
 
 
 
e0700fc
 
2c76547
e0700fc
 
 
 
 
2c76547
e0700fc
 
2c76547
e0700fc
 
 
 
2c76547
e0700fc
 
 
 
2c76547
e0700fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c76547
e0700fc
 
 
 
 
2c76547
 
e0700fc
 
 
 
 
 
 
2c76547
e0700fc
 
 
2c76547
e0700fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c76547
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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.

# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.

import sys
sys.path.append("../")

import argparse
import logging
from pathlib import Path
from tqdm import tqdm
import os
import torch
import torch.nn as nn
import torch.optim as optim

from munch import DefaultMunch
import json
from pytorch_lightning.lite import LightningLite
from torch.cuda.amp import GradScaler

from train_utils.utils import (
    run_test_eval,
    save_ims_to_tb,
    count_parameters,
)
from train_utils.logger import Logger
from models.core.dynamic_stereo import DynamicStereo
from models.core.sci_codec import sci_encoder
from evaluation.core.evaluator import Evaluator
from train_utils.losses import sequence_loss
import datasets.dynamic_stereo_datasets as datasets

class wrapper(nn.Module):
    def __init__(
            self, 
            sigma_range=[0, 1e-9],
            num_frames=8,
            in_channels=1,
            n_taps=2,
            resolution=[480, 640],
            mixed_precision=True,
            attention_type="self_stereo_temporal_update_time_update_space",
            update_block_3d=True,
            different_update_blocks=True,
            train_iters=16):

        super(wrapper, self).__init__()

        self.train_iters = train_iters

        self.sci_enc_L = sci_encoder(sigma_range=sigma_range,
                                     n_frame=num_frames,
                                     in_channels=in_channels,
                                     n_taps=n_taps,
                                     resolution=resolution)
        self.sci_enc_R = sci_encoder(sigma_range=sigma_range,
                                     n_frame=num_frames,
                                     in_channels=in_channels,
                                     n_taps=n_taps,
                                     resolution=resolution)

        self.stereo = DynamicStereo(max_disp=256,
                                    mixed_precision=mixed_precision,
                                    num_frames=num_frames,
                                    attention_type=attention_type,
                                    use_3d_update_block=update_block_3d,
                                    different_update_blocks=different_update_blocks)

    def forward(self, batch):
        # ---- ---- FORWARD PASS ---- ----
        # -- Modified by Chu King on 20th November 2025
        
        # -- print ("[INFO] batch[\"img\"].device: ", batch["img"].device)

        # 0) Convert to Gray
        def rgb_to_gray(x):
            weights = torch.tensor([0.2989, 0.5870, 0.1140], dtype=x.dtype, device=x.device)
            gray = (x * weights[None, None, :, None, None]).sum(dim=2)
            return gray # -- shape: [B, T, H, W]
        
        video_L = rgb_to_gray(batch["img"][:, :, 0]) # ~ (b, t, h, w)
        video_R = rgb_to_gray(batch["img"][:, :, 1]) # ~ (b, t, h, w)

        # -- print ("[INFO] video_L.device: ", video_L.device)
        
        # 1) Extract and normalize input videos.
        # -- min_max_norm = lambda x : 2. * (x / 255.) - 1.
        min_max_norm = lambda x: x / 255.
        video_L = min_max_norm(video_L) # ~ (b, t, h, w)
        video_R = min_max_norm(video_R) # ~ (b, t, h, w)
        # -- print ("[INFO] video_L.device: ", video_L.device)
        
        # 2) If the tensor is non-contiguous and we try .view() later, PyTorch will raise an error:
        video_L = video_L.contiguous()
        video_R = video_R.contiguous()

        # -- print ("[INFO] video_L.device: ", video_L.device)
        
        # 3) Coded exposure modeling.
        snapshot_L = self.sci_enc_L(video_L) # ~ (b, c, h, w) -- c=2 for 2 taps
        snapshot_R = self.sci_enc_R(video_R) # ~ (b, c, h, w) -- c=2 for 2 taps

        # -- print ("[INFO] self.sci_enc_L.device: ", next(self.sci_enc_R.parameters()).device)
        # -- print ("[INFO] snapshot_L.device: ", snapshot_L.device)
        
        # 4) Dynamic Stereo
        output = {}
        
        disparities = self.stereo(
            snapshot_L,
            snapshot_R,
            iters=self.train_iters,
            test_mode=False
        )
        
        n_views = len(batch["disp"][0]) # -- sample_len
        for i in range(n_views):
            seq_loss, metrics = sequence_loss(
                disparities[:, i], batch["disp"][:, i, 0], batch["valid_disp"][:, i, 0]
            )

            output[f"disp_{i}"] = {"loss": seq_loss / n_views, "metrics": metrics}
        output["disparity"] = {
            "predictions": torch.cat(
                [disparities[-1, i, 0] for i in range(n_views)], dim=1
            ).detach(),
        }
        return output

if __name__ == "__main__":
    eval_dataloader_dr = datasets.DynamicReplicaDataset(
        split="valid", sample_len=8, only_first_n_samples=1, VERBOSE=False, root="../dynamic_replica_data", t_step_validation=4
    )
    
    eval_dataloader_sintel_clean = datasets.SequenceSintelStereo(dstype="clean")
    eval_dataloader_sintel_final = datasets.SequenceSintelStereo(dstype="final")
    
    eval_dataloaders = [
        ("sintel_clean", eval_dataloader_sintel_clean),
        ("sintel_final", eval_dataloader_sintel_final),
        ("dynamic_replica", eval_dataloader_dr),
    ]
    
    evaluator = Evaluator()
    
    eval_vis_cfg = {
        "visualize_interval": 1,  # Use 0 for no visualization
        "exp_dir": "./"
    }
    eval_vis_cfg = DefaultMunch.fromDict(eval_vis_cfg, object())
    evaluator.setup_visualization(eval_vis_cfg)
    
    # ----------------------------------------- Model Instantiation -----------------------------------------------
    model = wrapper(sigma_range=[0, 1e-9],
                    num_frames=8,
                    in_channels=1,
                    n_taps=2,
                    resolution=[480, 640],
                    mixed_precision=True,
                    attention_type="self_stereo_temporal_update_time_update_space",
                    update_block_3d=True,
                    different_update_blocks=True,
                    train_iters=8)

    ckpt_path = "../dynamicstereo_sf_dr/model_dynamic-stereo_050895.pth"
    state_dict = torch.load(ckpt_path, map_location=torch.device('cpu'))
    model.load_state_dict(state_dict["model"], strict=True)
    model.eval()

    run_test_eval(
        ckpt_path="./",
        eval_type="valid",
        evaluator=evaluator,
        sci_enc_L=model.sci_enc_L,
        sci_enc_R=model.sci_enc_R,
        model=model.stereo,
        dataloaders=eval_dataloaders,
        writer=None,
        step=None,
        resolution=[480, 640]
    )