File size: 22,414 Bytes
b678162
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
import time
from typing import List

import cv2
import numpy as np
import viser
import viser.transforms as vt
import hydra
import torch
from torch import Tensor
from jaxtyping import Float
from PIL import Image
from torchvision import transforms as TF
from einops import repeat
import matplotlib

from vggt.utils.pose_enc import pose_encoding_to_extri_intri

from dpm.model import VDPM
from util.transforms import transform_points


VIDEO_SAMPLE_HZ = 1.0


def assign_colours(pts3d, colour=[0, 0, 1]):
    num_points = pts3d.shape[0]
    colors = (
        np.tile(np.array([colour]), (num_points, 1)) * 255
    ).astype(np.uint8)
    return colors


def compute_box_edges(corners):
    """
    Compute all edges of a 3D bounding box

    Args:
        corners: torch tensor of shape (8, 3) containing the coordinates of the 8 corners
                 of a 3D bounding box

    Returns:
        edges: torch tensor of shape (12, 2, 3) containing the 12 edges of the box,
               each represented as a pair of 3D coordinates [start_point, end_point]
    """
    # Define the 12 edges of a cube by specifying pairs of corner indices
    edge_indices = torch.tensor([
        # Edges along x-axis
        [0, 1], [2, 3], [4, 5], [6, 7],
        # Edges along y-axis
        [0, 2], [1, 3], [4, 6], [5, 7],
        # Edges along z-axis
        [0, 4], [1, 5], [2, 6], [3, 7]
    ], dtype=torch.long)

    # Initialize edges tensor
    edges = torch.zeros((12, 2, 3), dtype=corners.dtype, device=corners.device)

    # Extract the start and end points for each edge
    for i, (start_idx, end_idx) in enumerate(edge_indices):
        edges[i, 0] = corners[start_idx]  # Start point
        edges[i, 1] = corners[end_idx]    # End point

    colors = torch.tensor([
        [255, 0, 0],      # Red
        [0, 255, 0],      # Green
        [0, 0, 255],      # Blue
        [255, 255, 0],    # Yellow
        [0, 255, 255],    # Cyan
        [255, 0, 255],    # Magenta
        [255, 128, 0],    # Orange
        [128, 0, 255],    # Purple
        [128, 255, 0],    # Lime
        [255, 0, 128],    # Pink
        [0, 128, 255],    # Teal
        [128, 0, 0]       # Maroon
    ], dtype=torch.uint8, device=corners.device)

    return edges, colors


class TrackVisualiser:
    def __init__(self,
         server: viser.ViserServer
    ):
        self._trail_length = 12
        self._server = server

    def remove_static_tracks(self,
        tracks: Float[Tensor, "t n 3"],
        threshold=0.025
    ) -> Float[Tensor, "t n 3"]:
        # delta = tracks[1:] - tracks[[0]]
        delta = tracks[None, ...] - tracks[:, None, ...]
        max_displ = torch.linalg.norm(delta.abs(), dim=-1).amax((0, 1))
        dynamic = max_displ > threshold
        tracks_filtered = tracks[:, dynamic, :]
        return tracks_filtered

    def set_data(self,
        tracks_xyz: Float[Tensor, "t n 3"],
    ):
        # TODO: filter tracks and assign colours
        tracks_xyz = tracks_xyz.numpy()
        print("num actual tracks", tracks_xyz.shape[0])
        num_tracks = min(1000, tracks_xyz.shape[1])
        indices = np.random.choice(tracks_xyz.shape[1], num_tracks, replace=False)
        tracks_xyz = tracks_xyz[:, indices]
        sorted_indices = np.argsort(tracks_xyz[0, ..., 1])
        tracks_xyz = tracks_xyz[:, sorted_indices]
        color_map = matplotlib.colormaps.get_cmap('hsv')
        cmap_norm = matplotlib.colors.Normalize(vmin=0, vmax=tracks_xyz.shape[1] - 1)
        colours = np.zeros((num_tracks, 3), dtype=np.float32)
        for t_idx in range(num_tracks):
            color = color_map(cmap_norm(t_idx))[:3]
            colours[t_idx] = color
        colours = colours[:, None, :].repeat(2, axis=1)

        n_frames = tracks_xyz.shape[0]
        segment_nodes: list[viser.LineSegmentsHandle] = []
        for k in range(1, n_frames):
            segment = tracks_xyz[k-1:k+1].swapaxes(0, 1)
            # colours = (0.0, 0.0, 1.0)
            segment_node = self._server.scene.add_line_segments(
                f"/track_vis/{k}",
                segment,
                colours
            )
            segment_node.visible = False
            segment_nodes.append(segment_node)
        self._segment_nodes = segment_nodes

    def set_current_frame(self, f_idx: int):
        start_idx = max(1, f_idx - self._trail_length + 1)
        for node in self._segment_nodes:
            node.visible = False
        for idx in range(start_idx, f_idx + 1):
            self._segment_nodes[idx-1].visible = True


class ViserViewer:
    def __init__(self, model, device, port=8080):
        self.device = device
        self.model = model
        self.port = port

        self.S = 5 # num_frames
        self.need_update = True
        self.need_sequence_change = False
        self.is_playing = False
        self.last_update_time = time.time()

        self.server = viser.ViserServer(port=self.port)
        self._setup_gui()
        self._setup_event_handlers()

        self._track_visualiser = TrackVisualiser(self.server)

    def _setup_gui(self):
        server = self.server
        server.gui.configure_theme(control_layout="floating", control_width="large", show_logo=False)
        self.seq_selector = server.gui.add_button("Next example")
        self.play_button = server.gui.add_button("Play")
        self.scene_label = server.gui.add_text(
            "Sequence ID",
            initial_value="",
            disabled=True
        )

        self.gui_point_size = server.gui.add_slider(
            "Point size",
            min=0.0005,
            max=0.002,
            step=0.0005,
            initial_value=0.001,
        )

        self.gui_timestep = server.gui.add_slider(
            "Time",
            min=0,
            max=self.S-1,
            step=1,
            initial_value=0,
        )
        self.conf_slider = server.gui.add_slider(
            "Confidence",
            min=0.0,
            max=1.0,
            step=0.01,
            initial_value=0.3,
        )
        self.prev_timestep = self.gui_timestep.value

        self.rgb0_vis = self.server.gui.add_image(
            np.ones((100,100,3), dtype=np.uint8) * 255,
            label="rgb_0"
        )
        self.rgbt_vis = self.server.gui.add_image(
            np.ones((100,100,3), dtype=np.uint8) * 255,
            label="rgb_t"
        )

    def set_scene_label(self, example_idx):
        seq_idx, frame_idx = self.dataset.idx_to_seq_frame_id(example_idx)
        scene_name = self.dataset.seq_keys()[seq_idx]
        self.scene_label.value = f"{scene_name}_{frame_idx}"
        print("setting scene label", f"{scene_name}_{frame_idx}")

    def _setup_event_handlers(self):
        @self.seq_selector.on_click
        def _(_) -> None:
            """Choose random sequence to display"""
            with self.server.atomic():
                num_scenes = len(self.dataset)
                example_idx = np.random.randint(num_scenes)
                self.set_scene_label(example_idx)
                views = self.dataset[example_idx]
                views = process_example(views, self.device)
                pointmaps, extrinsic, _, gt_extrinsic = compute_predictions(self, model, views)
                self.visualise_reconstruction(views, pointmaps, extrinsic, gt_extrinsic)
            self.server.flush()  # Optional!
            self.need_update = True
            self.need_sequence_change = True

        @self.play_button.on_click
        def _(_) -> None:
            self.is_playing = not self.is_playing
            self.play_button.text = "Pause" if self.is_playing else "Play"

        @self.gui_point_size.on_update
        def _(_):
            for node in self.point_nodes:
                node.point_size = self.gui_point_size.value

        @self.conf_slider.on_update
        def _(_):
            self.need_update = True

        @self.gui_timestep.on_update
        def _(_) -> None:
            """Toggle frame visibility when the timestep slider changes"""
            current_timestep = self.gui_timestep.value
            with self.server.atomic():
                # Toggle visibility.
                self.frame_nodes[current_timestep].visible = True
                self.frame_nodes[self.prev_timestep].visible = False
            self.prev_timestep = current_timestep
            if self._track_visualiser is not None:
                self._track_visualiser.set_current_frame(current_timestep)
            self._update_image_t()
            self.server.flush()  # Optional!

    def continue_loop(self):
        return not self.need_sequence_change

    def set_data(
        self,
        pts3d_v0_t1: Float[Tensor, "s h w 3"],
        confs: Float[Tensor, "h w"],
        img_v0: Float[Tensor, "3 h w"],
        imgs: List[Float[Tensor, "3 h w"]],
        instance_ids,
        panoptic_v0,
        extrinsic,
    ):
        self.S = pts3d_v0_t1.shape[0]
        self.gui_timestep.max = self.S - 1

        self.pts3d_v0_t1 = pts3d_v0_t1
        self.img_v0 = img_v0
        self.imgs = imgs
        self.panoptic_v0 = panoptic_v0
        self.instance_ids = instance_ids
        self.confs = confs
        self.extrinsic = extrinsic # [1, S, 3, 4]

        self.need_update = True
        self.need_sequence_change = False

    def update(self):
        if not self.need_update:
            return
        self._do_update()
        self.need_update = False

    def _do_update(self):
        self.server.scene.reset()

        img_v0 = self.img_v0
        rgb_v0 = (img_v0 * 255.0).to(torch.uint8).permute(1, 2, 0).numpy()

        def get_coloured_pointclouds(pts_img, color=None):
            return {
                "pts3d": pts_img.view(-1, 3),
                "rgb": rgb_v0.reshape(-1, 3) if color is None else color,
                "conf": self.confs.view(-1)
            }

        points3d = dict()
        for s in range(self.S):
            points3d[f"v0_t{s}"] = get_coloured_pointclouds(self.pts3d_v0_t1[s])
        point_size = float(self.gui_point_size.value)

        T = torch.tensor([
            [1, 0, 0, 0],
            [0, 0, 1, 0],
            [0, -1, 0, 0],
            [0, 0, 0, 1]
        ], dtype=torch.float32)

        view_colours = np.array([
            [0, 0, 1],     # blue
            [1, 0, 0],     # red
            [0, 1, 0],     # green
            [1, 1, 0],     # yellow
            [1, 0, 1],     # magenta
            [0, 1, 1],     # cyan
            [0.5, 0, 0],   # dark red
            [0, 0.5, 0],   # dark green
            [0, 0, 0.5],   # dark blue
            [0.5, 0.5, 0]  # olive
        ], np.float32)

        if self.extrinsic is not None:
            extrinsic = self.extrinsic # [1, S, 3, 4]
            S = extrinsic.shape[0]

            T_c2ws = [extrinsic[s] for s in range(S)]

            for v, T_c2w in enumerate(T_c2ws):
                T_c2w = (T @ T_c2w).numpy()
                H, W = img_v0.shape[1:3]
                f_x = 600
                fov = 2 * np.arctan2(W / 2, f_x)
                aspect = W / H
                self.server.scene.add_camera_frustum(
                    f"/frames/t{v}/camera/pred",
                    fov=fov,
                    aspect=aspect,
                    scale=0.1,
                    color=view_colours[0],
                    # image=rgb[::downsample_factor, ::downsample_factor],
                    wxyz=vt.SO3.from_matrix(T_c2w[:3, :3]).wxyz,
                    position=T_c2w[:3, -1],
                )

        for pts in points3d.values():
            pts["pts3d"] = transform_points(T, pts["pts3d"])

        # TODO: choose reference frame
        reference_frame_id = 0

        confs = points3d[f"v0_t{reference_frame_id}"]["conf"]
        thresh = confs[confs.argsort()][int(confs.size()[0] * self.conf_slider.value)].item()
        good_points = (confs > thresh).numpy()

        tracks = torch.stack([points3d[f"v0_t{s}"]["pts3d"] for s in range(self.S)])
        tracks = tracks[:, good_points, :]
        if self._track_visualiser is not None:
            tracks_filtered = self._track_visualiser.remove_static_tracks(tracks)
            self._track_visualiser.set_data(tracks_filtered)

        frame_nodes: list[viser.FrameHandle] = []
        point_nodes: list[viser.PointCloudHandle] = []
        for s in range(self.S):
            v = points3d[f"v0_t{s}"]
            pts3d = v["pts3d"]
            colours = v["rgb"]
            pts3d_ = pts3d.numpy()[good_points, :]
            colours_ = colours if isinstance(colours, tuple) else colours[good_points]
            point_node = self.server.scene.add_point_cloud(
                name=f"/frames/t{s}/xyz",
                points=pts3d_,
                colors=colours_,
                point_size=point_size,
            )
            point_nodes.append(point_node)
            frame_node = self.server.scene.add_frame(f"/frames/t{s}", show_axes=False)
            frame_node.visible = s == self.gui_timestep.value
            frame_nodes.append(frame_node)
        self.point_nodes = point_nodes
        self.frame_nodes = frame_nodes
        # Hide all but the current frame.

        scene_centre = points3d["v0_t0"]["pts3d"].mean(dim=0)

        for client in self.server.get_clients().values():
            camera = client.camera
            camera.look_at = scene_centre

        self.rgb0_vis.image = rgb_v0
        self._update_image_t()

    def _update_image_t(self):
        rgb_vt = (self.imgs[self.gui_timestep.value] * 255.0).to(torch.uint8).permute(1, 2, 0).numpy()
        self.rgbt_vis.image = rgb_vt

    def visualise_reconstruction(self, images, pred, extrinsic):
        S = len(pred)
        pts3d_all = [pr["pts3d"] for pr in pred]
        conf_all = [pr["conf"] for pr in pred]

        #                              tgt_id src_id
        #                                 |     |
        pts3d_v0 = torch.stack([pts3d_all[s][:, 0] for s in range(S)], dim=1)
        pred_dynamic = dict(pts3d=pts3d_v0)

        pred_pts_t1 = pred_dynamic["pts3d"]

        pts3d_t1 = pred_pts_t1[0].detach()

        indices = torch.arange(S).to(torch.int64)
        pts3d_t1 = pts3d_t1[indices]
        confs_t1 = conf_all[0][0, 0]
        if extrinsic is not None:
            extrinsic = extrinsic[indices, ...].cpu()

        H, W = images.shape[-2:]
        imgs = images.cpu()
        img_v0 = images[0] # [3 H W]

        panoptic_1 = torch.zeros((H, W), dtype=torch.uint8, device=self.device)
        valid_instances = []

        self.set_data(
            pts3d_t1.cpu(),
            confs_t1.cpu(),
            img_v0.cpu(),
            imgs,
            valid_instances,
            panoptic_1.cpu(),
            extrinsic,
        )

    def run(self):
        """Run the visualization event loop"""
        while True:
            current_time = time.time()
            if self.is_playing and current_time - self.last_update_time > 0.1:  # 0.5 seconds per frame
                self.gui_timestep.value = (self.gui_timestep.value + 1) % self.S
                self.last_update_time = current_time
            self.update()
            time.sleep(1e-3)

def process_example(views, device):
    tensors = ['img', 'camera_pose', 'T_WV_norm', 'camera_intrinsics', 'pts3d_t0', 'pts3d_t1', 'valid_mask_t0', 'valid_mask_t1'] #, "view_idxs"]
    for view in views:
        # print(view["view_idxs"])
        for name in tensors:
            if name not in view:
                continue
            view[name] = view[name][None, ...]
            if isinstance(view[name], np.ndarray):
                view[name] = torch.from_numpy(view[name])

    for view in views:
        for name in tensors:  # pseudo_focal
            if name not in view:
                continue
            view[name] = view[name].to(device, non_blocking=True)
    return views


def compute_predictions(model, images):
    print("model inference started")

    start = time.perf_counter()

    with torch.no_grad():
        result = model.inference(None, images=images.unsqueeze(0))
    print("model inference finished")
    end = time.perf_counter()
    print(f"Execution time: {end - start:.6f} seconds")

    pointmaps = result["pointmaps"]

    # Extrinsic and intrinsic matrices, following OpenCV convention (camera from world)
    pose_enc = result["pose_enc"]
    HW = pointmaps[0]["pts3d"].shape[2:4]
    extrinsic, intrinsic = pose_encoding_to_extri_intri(pose_enc, HW)
    extrinsic = extrinsic[0]
    S = extrinsic.shape[0]
    extrinsic_CW = torch.cat([extrinsic.cpu(), repeat(torch.tensor([0, 0, 0, 1]), "c -> s 1 c", s=S)], dim=1)
    extrinsic_WC = torch.linalg.inv(extrinsic_CW)

    return pointmaps, extrinsic_WC, intrinsic


def extract_frames(input_video):
    torch.cuda.empty_cache()

    video_path = input_video
    vs = cv2.VideoCapture(video_path)

    fps = float(vs.get(cv2.CAP_PROP_FPS) or 0.0)
    frame_interval = max(int(fps / max(VIDEO_SAMPLE_HZ, 1e-6)), 1)

    count = 0
    frame_num = 0
    images = []
    try:
        while True:
            gotit, frame = vs.read()
            if not gotit:
                break
            if count % frame_interval == 0:
                frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                images.append(frame)
                frame_num += 1
            count += 1
    finally:
        vs.release()

    return images


def preprocess_images(images_np, mode="crop"):
    # Check for empty list
    if len(images_np) == 0:
        raise ValueError("At least 1 image is required")

    # Validate mode
    if mode not in ["crop", "pad"]:
        raise ValueError("Mode must be either 'crop' or 'pad'")

    images = []
    shapes = set()
    to_tensor = TF.ToTensor()
    target_size = 518

    # First process all images and collect their shapes
    for img_np in images_np:

        # Open image
        img = Image.fromarray(img_np)

        # If there's an alpha channel, blend onto white background:
        if img.mode == "RGBA":
            # Create white background
            background = Image.new("RGBA", img.size, (255, 255, 255, 255))
            # Alpha composite onto the white background
            img = Image.alpha_composite(background, img)

        # Now convert to "RGB" (this step assigns white for transparent areas)
        img = img.convert("RGB")

        width, height = img.size

        if mode == "pad":
            # Make the largest dimension 518px while maintaining aspect ratio
            if width >= height:
                new_width = target_size
                new_height = round(height * (new_width / width) / 14) * 14  # Make divisible by 14
            else:
                new_height = target_size
                new_width = round(width * (new_height / height) / 14) * 14  # Make divisible by 14
        else:  # mode == "crop"
            # Original behavior: set width to 518px
            new_width = target_size
            # Calculate height maintaining aspect ratio, divisible by 14
            new_height = round(height * (new_width / width) / 14) * 14

        # Resize with new dimensions (width, height)
        img = img.resize((new_width, new_height), Image.Resampling.BICUBIC)
        img = to_tensor(img)  # Convert to tensor (0, 1)

        # Center crop height if it's larger than 518 (only in crop mode)
        if mode == "crop" and new_height > target_size:
            start_y = (new_height - target_size) // 2
            img = img[:, start_y : start_y + target_size, :]

        # For pad mode, pad to make a square of target_size x target_size
        if mode == "pad":
            h_padding = target_size - img.shape[1]
            w_padding = target_size - img.shape[2]

            if h_padding > 0 or w_padding > 0:
                pad_top = h_padding // 2
                pad_bottom = h_padding - pad_top
                pad_left = w_padding // 2
                pad_right = w_padding - pad_left

                # Pad with white (value=1.0)
                img = torch.nn.functional.pad(
                    img, (pad_left, pad_right, pad_top, pad_bottom), mode="constant", value=1.0
                )

        shapes.add((img.shape[1], img.shape[2]))
        images.append(img)

    # Check if we have different shapes
    # In theory our model can also work well with different shapes
    if len(shapes) > 1:
        print(f"Warning: Found images with different shapes: {shapes}")
        # Find maximum dimensions
        max_height = max(shape[0] for shape in shapes)
        max_width = max(shape[1] for shape in shapes)

        # Pad images if necessary
        padded_images = []
        for img in images:
            h_padding = max_height - img.shape[1]
            w_padding = max_width - img.shape[2]

            if h_padding > 0 or w_padding > 0:
                pad_top = h_padding // 2
                pad_bottom = h_padding - pad_top
                pad_left = w_padding // 2
                pad_right = w_padding - pad_left

                img = torch.nn.functional.pad(
                    img, (pad_left, pad_right, pad_top, pad_bottom), mode="constant", value=1.0
                )
            padded_images.append(img)
        images = padded_images

    images = torch.stack(images)  # concatenate images

    # Ensure correct shape when single image
    if len(images_np) == 1:
        # Verify shape is (1, C, H, W)
        if images.dim() == 3:
            images = images.unsqueeze(0)

    return images


def load_model(cfg, device) -> VDPM:
    model = VDPM(cfg).to(device)

    _URL = "https://huggingface.co/edgarsucar/vdpm/resolve/main/model.pt"
    sd = torch.hub.load_state_dict_from_url(
        _URL,
        file_name="vdpm_model.pt",
        progress=True
    )
    print(model.load_state_dict(sd, strict=True))

    model.eval()
    return model


@hydra.main(config_path="configs", config_name="visualise")
def main(cfg) -> None:
    device = 'cuda:0'
    torch.backends.cuda.matmul.allow_tf32 = True  # for gpu >= Ampere and pytorch >= 1.12

    model = load_model(cfg, device)

    viewer = ViserViewer(model, device, cfg.vis.port)

    input_video = cfg.vis.input_video
    frames = extract_frames(input_video)
    images = preprocess_images(frames).to(device)  # (N, 3, H, W)

    pointmaps, extrinsic, _ = compute_predictions(model, images)
    viewer.visualise_reconstruction(images, pointmaps, extrinsic)
    viewer.run()


if __name__ == "__main__":
    main()