File size: 10,915 Bytes
daa6779
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ba0984
daa6779
8ba0984
 
daa6779
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
-----------------------------------------------------------------------------
Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved.

NVIDIA CORPORATION and its licensors retain all intellectual property
and proprietary rights in and to this software, related documentation
and any modifications thereto. Any use, reproduction, disclosure or
distribution of this software and related documentation without an express
license agreement from NVIDIA CORPORATION is strictly prohibited.
-----------------------------------------------------------------------------
"""

import os
from functools import wraps
from typing import Literal

import numpy as np
import torch
import trimesh
from kiui.mesh_utils import clean_mesh, decimate_mesh


# Adapted from https://github.com/Tencent/Hunyuan3D-2/blob/main/hy3dgen/shapegen/utils.py#L38
class sync_timer:
    """
    Synchronized timer to count the inference time of `nn.Module.forward` or else.
    set env var TIMER=1 to enable logging!

    Example as context manager:
    ```python
    with timer('name'):
        run()
    ```

    Example as decorator:
    ```python
    @timer('name')
    def run():
        pass
    ```
    """

    def __init__(self, name=None, flag_env="TIMER"):
        self.name = name
        self.flag_env = flag_env

    def __enter__(self):
        if os.environ.get(self.flag_env, "0") == "1":
            self.start = torch.cuda.Event(enable_timing=True)
            self.end = torch.cuda.Event(enable_timing=True)
            self.start.record()
            return lambda: self.time

    def __exit__(self, exc_type, exc_value, exc_tb):
        if os.environ.get(self.flag_env, "0") == "1":
            self.end.record()
            torch.cuda.synchronize()
            self.time = self.start.elapsed_time(self.end)
            if self.name is not None:
                print(f"{self.name} takes {self.time} ms")

    def __call__(self, func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            with self:
                result = func(*args, **kwargs)
            return result

        return wrapper


@torch.no_grad()
def calculate_iou(pred: torch.Tensor, gt: torch.Tensor, target_value: int, thresh: float = 0) -> torch.Tensor:
    """Calculate the Intersection over Union (IoU) between two volumes.

    Args:
        pred (torch.Tensor): [*] continuous value between 0 and 1
        gt (torch.Tensor): [*] discrete value of 0 or 1
        target_value (int): The value to be considered as the target class

    Returns:
        torch.Tensor: IoU value
    """
    # Ensure volumes have the same shape
    assert pred.shape == gt.shape, "Volumes must have the same shape"

    # binarize
    pred_binary = pred > thresh
    gt = gt > thresh

    # Convert the volumes to boolean tensors for logical operations
    intersection = torch.logical_and(pred_binary == target_value, gt == target_value).sum().float()
    union = torch.logical_or(pred_binary == target_value, gt == target_value).sum().float()

    # Compute IoU
    iou = intersection / union if union != 0 else torch.tensor(0.0)
    return iou


@torch.no_grad()
def calculate_metrics(
    pred: torch.Tensor, gt: torch.Tensor, target_value: int = 1, thresh: float = 0.5
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
    """Calculate Precision, Recall, and F1 between two volumes.

    Args:
        pred (torch.Tensor): [*] continuous value between 0 and 1
        gt (torch.Tensor): [*] discrete value of 0 or 1
        target_value (int): The value to be considered as the target class

    Returns:
        tuple: Precision, Recall, F1 values
    """
    assert pred.shape == gt.shape, f"Pred {pred.shape} and gt {gt.shape} must have the same shape"

    # Binarize prediction
    pred_binary = pred > thresh
    gt = gt > thresh

    # True Positive (TP): pred == target_value and gt == target_value
    true_positive = torch.logical_and(pred_binary == target_value, gt == target_value).sum().float()

    # False Positive (FP): pred == target_value and gt != target_value
    false_positive = torch.logical_and(pred_binary == target_value, gt != target_value).sum().float()

    # False Negative (FN): pred != target_value and gt == target_value
    false_negative = torch.logical_and(pred_binary != target_value, gt == target_value).sum().float()

    # Precision: TP / (TP + FP), best to detect False Positives
    precision = (
        true_positive / (true_positive + false_positive) if (true_positive + false_positive) != 0 else torch.tensor(0.0)
    )

    # Recall: TP / (TP + FN), best to detect False Negatives
    recall = (
        true_positive / (true_positive + false_negative) if (true_positive + false_negative) != 0 else torch.tensor(0.0)
    )

    # f1: 2 / (1 / precision + 1 / recall)
    f1 = 2 / (1 / precision + 1 / recall) if (precision != 0 and recall != 0) else torch.tensor(0.0)

    return precision, recall, f1


# Adapted from https://github.com/Stability-AI/stablediffusion/blob/main/ldm/modules/distributions/distributions.py#L24
class DiagonalGaussianDistribution:
    """VAE latent"""

    def __init__(self, mean, logvar, deterministic=False):
        # mean, logvar: [B, L, D] x 2
        self.mean, self.logvar = mean, logvar
        self.logvar = torch.clamp(self.logvar, -30.0, 20.0)
        self.deterministic = deterministic
        self.std = torch.exp(0.5 * self.logvar)
        self.var = torch.exp(self.logvar)
        if self.deterministic:
            self.var = self.std = torch.zeros_like(self.mean, device=self.mean.device, dtype=self.mean.dtype)

    def sample(self, weight: float = 1.0):
        sample = weight * torch.randn(self.mean.shape, device=self.mean.device, dtype=self.mean.dtype)
        x = self.mean + self.std * sample
        return x

    def kl(self, other=None, dims=[1, 2]):
        if self.deterministic:
            return torch.Tensor([0.0])
        else:
            if other is None:
                return 0.5 * torch.mean(torch.pow(self.mean, 2) + self.var - 1.0 - self.logvar, dim=dims)
            else:
                return 0.5 * torch.mean(
                    torch.pow(self.mean - other.mean, 2) / other.var
                    + self.var / other.var
                    - 1.0
                    - self.logvar
                    + other.logvar,
                    dim=dims,
                )

    def nll(self, sample, dims=[1, 2]):
        if self.deterministic:
            return torch.Tensor([0.0])
        logtwopi = np.log(2.0 * np.pi)
        return 0.5 * torch.mean(logtwopi + self.logvar + torch.pow(sample - self.mean, 2) / self.var, dim=dims)

    def mode(self):
        return self.mean


class DummyLatent:
    def __init__(self, mean):
        self.mean = mean

    def sample(self, weight=0):
        # simply perturb the mean
        if weight > 0:
            noise = torch.randn_like(self.mean) * weight
        else:
            noise = 0
        return self.mean + noise

    def mode(self):
        return self.mean

    def kl(self):
        # just an l2 penalty
        return 0.5 * torch.mean(torch.pow(self.mean, 2))


def construct_grid_points(
    resolution: int,
    indexing: str = "ij",
):
    """Generate dense grid points in [-1, 1]^3.

    Args:
        resolution (int): resolution of the grid
        indexing (str, optional): indexing of the grid. Defaults to "ij".

    Returns:
        torch.Tensor: grid points (resolution + 1, resolution + 1, resolution + 1, 3), inside bbox.
    """
    x = np.linspace(-1, 1, resolution + 1, dtype=np.float32)
    y = np.linspace(-1, 1, resolution + 1, dtype=np.float32)
    z = np.linspace(-1, 1, resolution + 1, dtype=np.float32)
    [xs, ys, zs] = np.meshgrid(x, y, z, indexing=indexing)
    xyzs = np.stack((xs, ys, zs), axis=-1)
    xyzs = torch.from_numpy(xyzs).float()
    return xyzs


_diso_session = None  # lazy session for reuse


@sync_timer("extract_mesh")
def extract_mesh(
    grid_vals: torch.Tensor,
    resolution: int,
    isosurface_level: float = 0,
    backend: Literal["mcubes", "diso"] = "mcubes",
):
    """Extract mesh from grid occupancy.

    Args:
        grid_vals (torch.Tensor): [resolution + 1, resolution + 1, resolution + 1], assume to be TSDF in [-1, 1] (inner is positive)
        resolution (int, optional): Grid resolution.
        isosurface_level (float, optional): Iso-surface level. Defaults to 0.
        backend (Literal["mcubes", "diso"], optional): Backend for mesh extraction. Defaults to "diso", which uses GPU and is faster.
    Returns:
        vertices (np.ndarray): [N, 3], float32, in [-1, 1]
        faces (np.ndarray): [M, 3], int32
    """

    grid_vals = grid_vals.view(resolution + 1, resolution + 1, resolution + 1)

    if backend == "mcubes":
        try:
            import mcubes
        except ImportError:
            os.system("pip install pymcubes")
            import mcubes
        grid_vals = grid_vals.float().cpu().numpy()
        verts, faces = mcubes.marching_cubes(grid_vals, isosurface_level)
        verts = 2 * verts / resolution - 1.0  # normalize to [-1, 1]
    elif backend == "diso":
        try:
            import diso
        except ImportError:
            os.system("pip install diso")
            import diso
        global _diso_session
        if _diso_session is None:
            _diso_session = diso.DiffDMC(dtype=torch.float32).cuda()

        grid_vals = -grid_vals.float().cuda()  # diso assumes inner is NEGATIVE!
        verts, faces = _diso_session(grid_vals, deform=None, normalize=True)  # verts in [0, 1]
        verts = verts.cpu().numpy() * 2 - 1.0  # normalize to [-1, 1]
        faces = faces.cpu().numpy()

    return verts, faces


@sync_timer("postprocess_mesh")
def postprocess_mesh(mesh: trimesh.Trimesh, decimate_target=100000):
    vertices = mesh.vertices
    triangles = mesh.faces

    if vertices.shape[0] > 0 and triangles.shape[0] > 0:
        vertices, triangles = clean_mesh(vertices, triangles, remesh=False, min_f=25, min_d=5)
    if decimate_target > 0 and triangles.shape[0] > decimate_target:
        vertices, triangles = decimate_mesh(vertices, triangles, decimate_target, optimalplacement=False)
        if vertices.shape[0] > 0 and triangles.shape[0] > 0:
            vertices, triangles = clean_mesh(vertices, triangles, remesh=False, min_f=25, min_d=5)

    mesh.vertices = vertices
    mesh.faces = triangles

    return mesh


def sphere_normalize(vertices):
    bmin = vertices.min(axis=0)
    bmax = vertices.max(axis=0)
    bcenter = (bmax + bmin) / 2
    radius = np.linalg.norm(vertices - bcenter, axis=-1).max()
    vertices = (vertices - bcenter) / radius  # to [-1, 1]
    return vertices


def box_normalize(vertices, bound=0.95):
    bmin = vertices.min(axis=0)
    bmax = vertices.max(axis=0)
    bcenter = (bmax + bmin) / 2
    vertices = bound * (vertices - bcenter) / (bmax - bmin).max()
    return vertices