From 3e5ead0b44df73e689575c3fbc0cd7ffa8dc2a03 Mon Sep 17 00:00:00 2001 From: MMathisLab Date: Thu, 16 Apr 2026 17:05:37 +0200 Subject: [PATCH] Port functional utility fixes from Xiaohang workspace. Apply renderer camera-translation safety fixes, harden task wrapper timing/error handling, and add metric guards for empty thresholds and zero-visibility cases. Made-with: Cursor --- prima/utils/evaluate_metric.py | 6 ++++-- prima/utils/mesh_renderer.py | 5 +++-- prima/utils/misc.py | 11 +++++------ prima/utils/renderer.py | 5 +++-- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/prima/utils/evaluate_metric.py b/prima/utils/evaluate_metric.py index db80c73..af73a52 100644 --- a/prima/utils/evaluate_metric.py +++ b/prima/utils/evaluate_metric.py @@ -110,6 +110,8 @@ def compute_pck(self, output: Dict, batch: Dict, pck_threshold: Union[List, None if pck_threshold is not None: for i in range(len(pck_threshold)): self.pck_threshold_list.append(torch.tensor([pck_threshold[i]] * len(pred_keypoints_2d), dtype=torch.float32)) + if len(self.pck_threshold_list) == 0: + return torch.tensor([], dtype=torch.float32) pcks = [] # Use mask area if available, otherwise use full image area @@ -118,7 +120,7 @@ def compute_pck(self, output: Dict, batch: Dict, pck_threshold: Union[List, None else: # Use full image area as fallback seg_area = torch.tensor([self.image_size * self.image_size] * len(pred_keypoints_2d), dtype=torch.float32).unsqueeze(-1) - total_visible = torch.sum(conf, dim=-1) + total_visible = torch.sum(conf, dim=-1).clamp_min(1e-6) for th in self.pck_threshold_list: dist = torch.norm(pred_keypoints_2d - gt_keypoints_2d, dim=-1) @@ -147,7 +149,7 @@ def eval_3d(self, output: Dict, batch: Dict): Returns: evaluate metric """ if batch['has_smal_params']["betas"].sum() == 0: - return 0., 0., 0., [0., 0.], 0. + return 0., 0. pred_keypoints_3d = output["pred_keypoints_3d"].detach() pred_keypoints_3d = pred_keypoints_3d[:, None, :, :] diff --git a/prima/utils/mesh_renderer.py b/prima/utils/mesh_renderer.py index ef33579..0c5b82a 100644 --- a/prima/utils/mesh_renderer.py +++ b/prima/utils/mesh_renderer.py @@ -231,7 +231,8 @@ def __call__(self, vertices, camera_translation, image, focal_length, text=None, alphaMode='OPAQUE', baseColorFactor=baseColorFactor) - camera_translation[0] *= -1. + camera_translation_local = camera_translation.copy() + camera_translation_local[0] *= -1. mesh = trimesh.Trimesh(vertices.copy(), self.faces.copy()) if side_view: @@ -248,7 +249,7 @@ def __call__(self, vertices, camera_translation, image, focal_length, text=None, scene.add(mesh, 'mesh') camera_pose = np.eye(4) - camera_pose[:3, 3] = camera_translation + camera_pose[:3, 3] = camera_translation_local camera_center = [image.shape[1] / 2., image.shape[0] / 2.] camera = pyrender.IntrinsicsCamera(fx=focal_length, fy=focal_length, cx=camera_center[0], cy=camera_center[1], diff --git a/prima/utils/misc.py b/prima/utils/misc.py index ffcfe78..fc83761 100644 --- a/prima/utils/misc.py +++ b/prima/utils/misc.py @@ -29,13 +29,12 @@ def task_wrapper(task_func: Callable) -> Callable: """ def wrap(cfg: DictConfig): - - # apply extra utilities - extras(cfg) - - # execute the task + start_time = time.time() try: - start_time = time.time() + # apply extra utilities + extras(cfg) + + # execute the task ret = task_func(cfg=cfg) except Exception as ex: log.exception("") # save exception to `.log` file diff --git a/prima/utils/renderer.py b/prima/utils/renderer.py index 2efd238..2939dd2 100644 --- a/prima/utils/renderer.py +++ b/prima/utils/renderer.py @@ -202,7 +202,8 @@ def __call__(self, alphaMode='OPAQUE', baseColorFactor=(*mesh_base_color, 1.0)) - camera_translation[0] *= -1. + camera_translation_local = camera_translation.copy() + camera_translation_local[0] *= -1. mesh = trimesh.Trimesh(vertices.copy(), self.faces.copy()) if side_view: @@ -219,7 +220,7 @@ def __call__(self, scene.add(mesh, 'mesh') camera_pose = np.eye(4) - camera_pose[:3, 3] = camera_translation + camera_pose[:3, 3] = camera_translation_local camera_center = [image.shape[1] / 2., image.shape[0] / 2.] camera = pyrender.IntrinsicsCamera(fx=focal_length_to_use, fy=focal_length_to_use, cx=camera_center[0], cy=camera_center[1], zfar=1e12)