File size: 6,420 Bytes
5f5f544
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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.

from collections import defaultdict
from typing import Dict, List, Sequence

import cv2
import numpy as np
import torch
import torch.nn.functional as F
from prettytable import PrettyTable
from sapiens.engine.evaluators import BaseEvaluator
from sapiens.registry import MODELS


@MODELS.register_module()
class NormalEvaluator(BaseEvaluator):
    def __init__(
        self,
        angle_thresholds: list[float] = [5.0, 11.25, 22.5, 30.0],
        hist_bin_size_deg: float = 0.5,
        hist_max_deg: float = 180.0,
    ):
        super().__init__()
        self.angle_thresholds = angle_thresholds
        self.hist_bin_size_deg = float(hist_bin_size_deg)
        self.hist_max_deg = float(hist_max_deg)

        # number of histogram bins, edges computed on demand
        self._num_bins = int(
            torch.ceil(torch.tensor(self.hist_max_deg / self.hist_bin_size_deg)).item()
        )

    @torch.no_grad()
    def process(self, predictions: torch.Tensor, data_samples: dict, accelerator=None):
        """
        Process a single batch of predictions and ground truth data.

        Args:
            predictions (tuple): A tuple containing the predicted pointmap and scale.
            data_samples (List[Dict]): A list of dictionaries, each containing ground truth data.
        """
        assert accelerator is not None, "evaluation process expects an accelerator"
        pred_normals = predictions  ## pred normals, B x 3 x H_low x W_low
        gt_masks = data_samples["mask"]  # B x 1 x H x W
        gt_normals = data_samples["gt_normal"]  # B x 3 x H x W

        if pred_normals.shape[2:] != gt_normals.shape[2:]:
            pred_normals = F.interpolate(
                input=pred_normals,
                size=gt_normals.shape[2:],
                mode="bilinear",
                align_corners=False,
                antialias=False,
            )

        ## normalize
        eps = 1e-6
        pred_normals = pred_normals / pred_normals.norm(dim=1, keepdim=True).clamp_min(
            eps
        )
        gt_normals = gt_normals / gt_normals.norm(dim=1, keepdim=True).clamp_min(eps)

        B = gt_normals.shape[0]
        HN = self._num_bins

        # packed vector layout:
        # [ sum_angle, sum_angle2, N, counts(<th_0..A-1), hist(0..HN-1) ]
        per_sample_vecs = []

        for i in range(B):
            valid = gt_masks[i, 0] > 0
            n_valid = int(valid.sum().item())

            assert n_valid > 0, "no valid pixels found"

            gt = gt_normals[i].permute(1, 2, 0)[valid]  # (N,3)
            pr = pred_normals[i].permute(1, 2, 0)[valid]  # (N,3)

            dot = (gt * pr).sum(dim=1)  # (N,)
            dot = dot.clamp(-1.0, 1.0)
            angle = torch.acos(dot) * (180.0 / torch.pi)  # (N,)

            ## sums
            sum_angle = angle.sum().to(torch.float64).unsqueeze(0)  # shape (1,)
            sum_angle2 = (
                (angle * angle).sum().to(torch.float64).unsqueeze(0)
            )  # shape (1,)
            N_tensor = torch.tensor(
                [float(n_valid)], dtype=torch.float64, device=pred_normals.device
            )  # (1,)

            ## thresholds
            th_counts = torch.stack(
                [(angle < t).sum().to(torch.float64) for t in self.angle_thresholds],
                dim=0,
            )

            ## histogram
            idx = torch.floor(angle / self.hist_bin_size_deg).long().clamp_(0, HN - 1)
            hist = torch.bincount(idx, minlength=HN).to(torch.float64)

            vec = torch.cat(
                [sum_angle, sum_angle2, N_tensor, th_counts, hist], dim=0
            )  # (K,)
            per_sample_vecs.append(vec)

        # (B_local, K)
        pack = torch.stack(per_sample_vecs, dim=0)
        gpack = accelerator.gather_for_metrics(pack)  # (B_global_this_step, K)
        step_totals = gpack.sum(dim=0)  # (K,)

        if accelerator.is_main_process:
            self.results.append(step_totals)  # store one vector per step on rank-0
        return

    def evaluate(self, logger=None, accelerator=None) -> Dict[str, float]:
        """
        Compute and log the final metrics after processing all batches.

        Returns:
            Dict[str, float]: A dictionary of the final computed metrics.
        """
        assert accelerator is not None, "evaluation aggregation expects an accelerator"

        if not accelerator.is_main_process:
            self.reset()
            return {}

        if not self.results:
            if logger is not None:
                logger.info("No results to evaluate.")
            return {}

        totals_vec = torch.stack(self.results, dim=0).sum(dim=0)  # (K,)
        A = len(self.angle_thresholds)
        HN = self._num_bins

        idx = 0
        sum_angle = totals_vec[idx]
        idx += 1
        sum_angle2 = totals_vec[idx]
        idx += 1
        n_total = totals_vec[idx]
        idx += 1
        ang_counts = totals_vec[idx : idx + A]
        idx += A
        hist_counts = totals_vec[idx : idx + HN]
        idx += HN

        # Core metrics
        mae = (sum_angle / n_total).item()
        rmse = torch.sqrt(sum_angle2 / n_total).item()
        within = (ang_counts / n_total * 100.0).tolist()

        # Global median from histogram (bin center)
        cdf = torch.cumsum(hist_counts, dim=0)
        mid = 0.5 * n_total
        bin_idx = torch.searchsorted(cdf, mid).clamp(max=HN - 1).item()
        bin_lo = bin_idx * self.hist_bin_size_deg
        bin_hi = (bin_idx + 1) * self.hist_bin_size_deg
        median = 0.5 * (bin_lo + bin_hi)

        # Assemble metrics dict
        metrics: Dict[str, float] = {
            "normal_mae": mae,
            "normal_median_deg": float(median),
            "normal_rmse": rmse,
        }
        for j, t in enumerate(self.angle_thresholds):
            suf = str(t).replace(".", "_")
            metrics[f"within_{suf}_deg"] = float(within[j])

        # Pretty print
        table = PrettyTable()
        table.field_names = list(metrics.keys())
        table.add_row([f"{float(v):.5f}" for v in metrics.values()])
        if logger is not None:
            logger.info("\n" + table.get_string())

        self.reset()
        return metrics