From 3d4795e3f2e7c07f2e3ad0682977886518b52e5f Mon Sep 17 00:00:00 2001
From: Illia Oleksiienko <io@ece.au.dk>
Date: Mon, 12 Dec 2022 16:37:46 +0000
Subject: [PATCH] Update camera uncertainty

---
 .gitignore                                    |  2 ++
 run/eval_tracking_3d.py                       |  6 ++---
 src/opendr/engine/target.py                   | 20 ++++++++--------
 .../pytorch/core/box_torch_ops.py             |  6 +++--
 .../second_detector/run.py                    | 23 +++++++++++++++++--
 5 files changed, 40 insertions(+), 17 deletions(-)

diff --git a/.gitignore b/.gitignore
index 58ae399224..eebccb6543 100644
--- a/.gitignore
+++ b/.gitignore
@@ -71,3 +71,5 @@ temp/
 projects/opendr_ws/.catkin_workspace
 projects/opendr_ws/devel/
 projects/python/control/eagerx/eagerx_ws/
+
+models
\ No newline at end of file
diff --git a/run/eval_tracking_3d.py b/run/eval_tracking_3d.py
index e5603ae09d..a0fc1e632f 100644
--- a/run/eval_tracking_3d.py
+++ b/run/eval_tracking_3d.py
@@ -397,9 +397,11 @@ def test_vnn_tapp(
 
 def all_save_detection_inference(start=0):
     models = [
+        ("tanet_car", test_tanet),
         ("tapp_car", test_tapp),
         ("pointpillars_car", test_pointpillars),
-        ("tanet_car", test_tanet),
+        *[("vnn_tanet_car_s" + str(i), test_vnn_tanet) for i in range(1, 3 + 1)],
+        *[("vnn_tapp_car_s" + str(i), test_vnn_tapp) for i in range(1, 3 + 1)],
         *[
             ("vnn_pointpillars_car_s" + str(i), test_vnn_pointpillars)
             for i in range(1, 7 + 1)
@@ -408,8 +410,6 @@ def all_save_detection_inference(start=0):
             ("vnna_pointpillars_car_s" + str(i), test_vnna_pointpillars)
             for i in range(1, 4 + 1)
         ],
-        *[("vnn_tanet_car_s" + str(i), test_vnn_tanet) for i in range(1, 3 + 1)],
-        *[("vnn_tapp_car_s" + str(i), test_vnn_tapp) for i in range(1, 3 + 1)],
     ]
 
     for i, (model_name, test_func) in enumerate(models):
diff --git a/src/opendr/engine/target.py b/src/opendr/engine/target.py
index c7f7af8f4f..6edb2d3cbc 100644
--- a/src/opendr/engine/target.py
+++ b/src/opendr/engine/target.py
@@ -1252,7 +1252,7 @@ class UncertaintyBoundingBox3D(BoundingBox3D):
 
         result = super().to_kitti_tracking_string(frame)
 
-        result += " ".join(map(str, [
+        result += "," + " ".join(map(str, [
             float(self.variance_truncated),
             float(self.variance_occluded),
             float(self.variance_alpha),
@@ -1267,15 +1267,15 @@ class UncertaintyBoundingBox3D(BoundingBox3D):
 
     def to_ab3dmot_string(self, frame):
         
-        result = super().to_ab3dmot_string(frame)
-
-        result = ",".join(map(str, [
-            *([0,0,0,0] if self.variance_bbox2d is None else self.variance_bbox2d),
-            float(self.variance_confidence),
-            *self.variance_dimensions[[1, 2, 0]],  # w h l -> h w l ?
-            *self.variance_location,  # x y z
-            self.variance_rotation_y,
-            self.variance_alpha,
+        result = super().to_ab3dmot_string(frame)  # 14
+
+        result +="," + ",".join(map(str, [
+            *([0,0,0,0] if self.variance_bbox2d is None else self.variance_bbox2d),  #15, 16, 17, 18 
+            float(self.variance_confidence),  # 19
+            *self.variance_dimensions[[1, 2, 0]],  # w h l -> h w l ? 20 21 22
+            *self.variance_location,  # x y z 23 24 25
+            self.variance_rotation_y,  # 26
+            self.variance_alpha,  # 27
         ]))
 
         return result
diff --git a/src/opendr/perception/object_detection_3d/voxel_object_detection_3d/second_detector/pytorch/core/box_torch_ops.py b/src/opendr/perception/object_detection_3d/voxel_object_detection_3d/second_detector/pytorch/core/box_torch_ops.py
index 49ae77aa9b..06152b88f1 100644
--- a/src/opendr/perception/object_detection_3d/voxel_object_detection_3d/second_detector/pytorch/core/box_torch_ops.py
+++ b/src/opendr/perception/object_detection_3d/voxel_object_detection_3d/second_detector/pytorch/core/box_torch_ops.py
@@ -132,7 +132,7 @@ def second_box_decode_uncertainty(
         mxt, myt, mzt, mwt, mlt, mht, mrt = torch.split(box_mean_encodings, 1, dim=-1)
 
     def exp_var(m, s):
-        return (torch.exp(s ** 2) - 1) * (torch.exp(2 * m  + s ** 2))
+        return torch.exp(2*m + 2 * s) - torch.exp(2*m + s)
 
     za = za + ha / 2
     diagonal = torch.sqrt(la**2 + wa**2)
@@ -465,6 +465,7 @@ def lidar_to_camera(points, r_rect, velo2cam):
 
 
 def box_camera_to_lidar(data, r_rect, velo2cam):
+    data = data.type(velo2cam.dtype)
     xyz = data[..., 0:3]
     l, h, w = data[..., 3:4], data[..., 4:5], data[..., 5:6]
     r = data[..., 6:7]
@@ -473,6 +474,7 @@ def box_camera_to_lidar(data, r_rect, velo2cam):
 
 
 def uncertainty_box_camera_to_lidar(data, r_rect, velo2cam):
+    data = data.type(velo2cam.dtype)
     xyz = data[..., 0:3]
     l, h, w = data[..., 3:4], data[..., 4:5], data[..., 5:6]
     r = data[..., 6:7]
@@ -481,7 +483,7 @@ def uncertainty_box_camera_to_lidar(data, r_rect, velo2cam):
 
 
 def box_lidar_to_camera(data, r_rect, velo2cam):
-    data = data.type(torch.float64)
+    data = data.type(velo2cam.dtype)
     xyz_lidar = data[..., 0:3]
     w, l, h = data[..., 3:4], data[..., 4:5], data[..., 5:6]
     r = data[..., 6:7]
diff --git a/src/opendr/perception/object_detection_3d/voxel_object_detection_3d/second_detector/run.py b/src/opendr/perception/object_detection_3d/voxel_object_detection_3d/second_detector/run.py
index 3043a47873..ef82bb5bed 100644
--- a/src/opendr/perception/object_detection_3d/voxel_object_detection_3d/second_detector/run.py
+++ b/src/opendr/perception/object_detection_3d/voxel_object_detection_3d/second_detector/run.py
@@ -544,12 +544,20 @@ def comput_kitti_output(
             box_preds = preds_dict["box3d_camera"].detach().cpu().numpy()
             scores = preds_dict["scores"].detach().cpu().numpy()
             box_preds_lidar = preds_dict["box3d_lidar"].detach().cpu().numpy()
+
+            if return_uncertainty:
+                box_preds_var = preds_dict["box3d_camera_var"].detach().cpu().numpy()
+                scores_var = preds_dict["scores_var"].detach().cpu().numpy()
+            else:
+                box_preds_var = [None] * len(box_preds)
+                scores_var = [None] * len(box_preds)
+
             # write pred to file
             label_preds = preds_dict["label_preds"].detach().cpu().numpy()
             anno = kitti.get_start_result_anno(return_uncertainty)
             num_example = 0
-            for box, box_lidar, bbox, score, label in zip(
-                    box_preds, box_preds_lidar, box_2d_preds, scores,
+            for box, box_var, box_lidar, bbox, score, score_var, label in zip(
+                    box_preds, box_preds_var, box_preds_lidar, box_2d_preds, scores, scores_var,
                     label_preds):
                 if not lidar_input:
                     if bbox[0] > image_shape[1] or bbox[1] > image_shape[0]:
@@ -572,6 +580,17 @@ def comput_kitti_output(
                 anno["dimensions"].append(box[3:6])
                 anno["location"].append(box[:3])
                 anno["rotation_y"].append(box[6])
+
+                if return_uncertainty:
+                    anno["variance_truncated"].append(0.0)
+                    anno["variance_occluded"].append(0)
+                    anno["variance_alpha"].append(0)
+                    anno["variance_bbox"].append(None)
+                    anno["variance_dimensions"].append(box_var[3:6])
+                    anno["variance_location"].append(box_var[:3])
+                    anno["variance_rotation_y"].append(box_var[6])
+                    anno["variance_score"].append(np.mean(score_var))
+
                 if global_set is not None:
                     for i in range(100000):
                         if score in global_set:
-- 
GitLab