File size: 4,611 Bytes
1ea89dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import argparse
import json
import os

import numpy as np
import torch
import torch.nn as nn
from PIL import Image

from unik3d.models import UniK3D
from unik3d.utils.camera import (MEI, OPENCV, BatchCamera, Fisheye624, Pinhole,
                                 Spherical)
from unik3d.utils.visualization import colorize, save_file_ply

SAVE = False
BASE_PATH = os.path.join(
    os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "assets", "demo"
)


def infer(model, rgb_path, camera_path, rays=None):
    rgb = np.array(Image.open(rgb_path))
    rgb_torch = torch.from_numpy(rgb).permute(2, 0, 1)

    camera = None
    if camera_path is not None:
        with open(camera_path, "r") as f:
            camera_dict = json.load(f)

        params = torch.tensor(camera_dict["params"])
        name = camera_dict["name"]
        assert name in ["Fisheye624", "Spherical", "OPENCV", "Pinhole", "MEI"]
        camera = eval(name)(params=params)

    outputs = model.infer(rgb=rgb_torch, camera=camera, normalize=True, rays=rays)

    return rgb_torch, outputs


def infer_equirectangular(model, rgb_path):
    rgb = np.array(Image.open(rgb_path))
    rgb_torch = torch.from_numpy(rgb).permute(2, 0, 1)

    # assuming full equirectangular image horizontally
    H, W = rgb.shape[:2]
    hfov_half = np.pi
    vfov_half = np.pi * H / W
    assert vfov_half <= np.pi / 2

    params = [W, H, hfov_half, vfov_half]
    camera = Spherical(params=torch.tensor([1.0] * 4 + params))

    outputs = model.infer(rgb=rgb_torch, camera=camera, normalize=True)
    return rgb_torch, outputs


def save(rgb, outputs, name, base_path, save_pointcloud=False):
    depth = outputs["depth"]
    rays = outputs["rays"]
    points = outputs["points"]

    depth = depth.cpu().numpy()
    rays = ((rays + 1) * 127.5).clip(0, 255)

    Image.fromarray(colorize(depth.squeeze())).save(
        os.path.join(base_path, f"{name}_depth.png")
    )
    Image.fromarray(rgb.squeeze().permute(1, 2, 0).cpu().numpy()).save(
        os.path.join(base_path, f"{name}_rgb.png")
    )
    Image.fromarray(rays.squeeze().permute(1, 2, 0).byte().cpu().numpy()).save(
        os.path.join(base_path, f"{name}_rays.png")
    )

    if save_pointcloud:
        predictions_3d = points.permute(0, 2, 3, 1).reshape(-1, 3).cpu().numpy()
        rgb = rgb.permute(1, 2, 0).reshape(-1, 3).cpu().numpy()
        save_file_ply(predictions_3d, rgb, os.path.join(base_path, f"{name}.ply"))


def demo(model):
    # RGB + CAMERA
    rgb, outputs = infer(
        model,
        os.path.join(BASE_PATH, f"scannet.png"),
        os.path.join(BASE_PATH, "scannet.json"),
    )
    if SAVE:
        save(rgb, outputs, name="scannet", base_path=BASE_PATH)

    # get GT and pred
    pts_pred = outputs["points"].squeeze().cpu().permute(1, 2, 0).numpy()
    pts_gt = np.load("./assets/demo/scannet.npy").astype(float)
    mask = np.linalg.norm(pts_gt, axis=-1) > 0
    error = np.linalg.norm(pts_pred - pts_gt, axis=-1)
    error = np.mean(error[mask] ** 2) ** 0.5

    # Trade-off between speed and resolution
    model.resolution_level = 1
    rgb, outputs = infer(
        model,
        os.path.join(BASE_PATH, f"scannet.png"),
        os.path.join(BASE_PATH, "scannet.json"),
    )
    if SAVE:
        save(rgb, outputs, name="scannet_lowres", base_path=BASE_PATH)

    # RGB
    rgb, outputs = infer(model, os.path.join(BASE_PATH, f"poorthings.jpg"), None)
    if SAVE:
        save(rgb, outputs, name="poorthings", base_path=BASE_PATH)

    # RGB + CAMERA
    rgb, outputs = infer(
        model,
        os.path.join(BASE_PATH, f"dl3dv.png"),
        os.path.join(BASE_PATH, "dl3dv.json"),
    )
    if SAVE:
        save(rgb, outputs, name="dl3dv", base_path=BASE_PATH)

    # EQUIRECTANGULAR
    rgb, outputs = infer_equirectangular(
        model, os.path.join(BASE_PATH, f"equirectangular.jpg")
    )
    if SAVE:
        save(rgb, outputs, name="equirectangular", base_path=BASE_PATH)

    print("Output keys are", outputs.keys())

    if SAVE:
        print("Done! Results saved in", BASE_PATH)

    print(f"RMSE on 3D clouds for ScanNet++ sample: {100*error:.1f}cm")


if __name__ == "__main__":
    print("Torch version:", torch.__version__)
    type_ = "l"  # available types: s, b, l
    name = f"unik3d-vit{type_}"
    model = UniK3D.from_pretrained(f"lpiccinelli/{name}")

    # set resolution level in [0,10) and output interpolation
    model.resolution_level = 9
    model.interpolation_mode = "bilinear"

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    model = model.to(device).eval()

    demo(model)