-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(prediction): add prediction metrics for displacement error
Signed-off-by: ktro2828 <[email protected]>
- Loading branch information
Showing
9 changed files
with
397 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
perception_eval/perception_eval/evaluation/metrics/prediction/path_displacement_error.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
# Copyright 2022 TIER IV, Inc. | ||
|
||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
|
||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from typing import List | ||
from typing import Optional | ||
|
||
import numpy as np | ||
from perception_eval.common.label import AutowareLabel | ||
from perception_eval.common.threshold import get_label_threshold | ||
from perception_eval.evaluation.matching.object_matching import MatchingMode | ||
from perception_eval.evaluation.result.object_result import DynamicObjectWithPerceptionResult | ||
|
||
|
||
class PathDisplacementError: | ||
"""[summary] | ||
A class to calculate path displacement errors for motion prediction task. | ||
Support metrics: | ||
ADE; Average Displacement Error | ||
FDE; Final Displacement Error | ||
Miss Rate | ||
Attributes: | ||
self.ade (float) | ||
self.fde (float) | ||
self.miss_rate (float) | ||
self.num_ground_truth (int) | ||
self.target_labels (List[AutowareLabel]) | ||
self.matching_mode (MatchingMode) | ||
self.matching_threshold_list (List[float]) | ||
self.num_path_frames (int) | ||
self.top_k (Optional[int]) | ||
self.miss_tolerance (float) | ||
""" | ||
|
||
def __init__( | ||
self, | ||
object_results: List[DynamicObjectWithPerceptionResult], | ||
num_ground_truth: int, | ||
target_labels: List[AutowareLabel], | ||
matching_mode: MatchingMode, | ||
matching_threshold_list: List[float], | ||
num_path_frames: int = 1, | ||
top_k: Optional[int] = 1, | ||
miss_tolerance: float = 2.0, | ||
) -> None: | ||
"""[summary] | ||
Args: | ||
object_results (List[DynamicObjectWithPerceptionResult]): | ||
num_ground_truth (int): | ||
target_labels (List[AutowareLabel]): | ||
matching_mode (MatchingMode): | ||
matching_threshold_list (List[float]) | ||
num_path_frames (int): Number of horizontal frames. Defaults to 10[frames]. | ||
top_k (Optional[int]): Number of top kth confidential paths. If None, calculate all paths. Defaults to None. | ||
miss_tolerance (float): Tolerance value to determine miss. Defaults to 2.0[m]. | ||
""" | ||
|
||
self.num_ground_truth: int = num_ground_truth | ||
|
||
self.target_labels: List[AutowareLabel] = target_labels | ||
self.matching_mode: MatchingMode = matching_mode | ||
self.matching_threshold_list: List[float] = matching_threshold_list | ||
|
||
all_object_results: List[DynamicObjectWithPerceptionResult] = [] | ||
if len(object_results) == 0 or not isinstance(object_results[0], list): | ||
all_object_results = object_results | ||
else: | ||
for obj_results in object_results: | ||
all_object_results += obj_results | ||
self.objects_results_num: int = len(all_object_results) | ||
self.num_path_frames: int = num_path_frames | ||
self.top_k: Optional[int] = top_k | ||
self.miss_tolerance: float = miss_tolerance | ||
|
||
displacements: np.ndarray = self.__get_displacements(all_object_results) | ||
if len(displacements) > 0: | ||
self.ade: float = np.linalg.norm(displacements[:, :, :, :2], axis=-1).mean() | ||
self.fde: float = displacements[:, :, -1, :2].mean() | ||
self.miss_rate: float = self.__get_miss_rate(displacements) | ||
self.isnan: bool = False | ||
else: | ||
self.ade = np.nan | ||
self.fde = np.nan | ||
self.miss_rate = np.nan | ||
self.isnan: bool = True | ||
|
||
def __get_displacements( | ||
self, | ||
object_results: List[DynamicObjectWithPerceptionResult], | ||
) -> np.ndarray: | ||
"""[summary] | ||
Returns the displacement errors. | ||
Args: | ||
object_results (List[DynamicObjectWithPerceptionResult]) | ||
Returns: | ||
numpy.ndarray: Tensor of errors, in shape (N, K, T, 3). | ||
N is number of objects, K is top k confidential paths, T is length of trajectory. | ||
""" | ||
displacements = [] | ||
for obj_result in object_results: | ||
matching_threshold: float = get_label_threshold( | ||
semantic_label=obj_result.estimated_object.semantic_label, | ||
target_labels=self.target_labels, | ||
threshold_list=self.matching_threshold_list, | ||
) | ||
is_result_correct: bool = obj_result.is_result_correct( | ||
matching_mode=self.matching_mode, | ||
matching_threshold=matching_threshold, | ||
) | ||
if not is_result_correct: | ||
continue | ||
estimation = obj_result.estimated_object | ||
ground_truth = obj_result.ground_truth_object | ||
# (K, T, 3) | ||
err: np.ndarray = estimation.get_path_error( | ||
ground_truth, self.top_k, self.num_path_frames | ||
) | ||
displacements.append(err.tolist()) | ||
|
||
# (N, K, T, 3) | ||
return np.array(displacements) | ||
|
||
def __get_miss_rate(self, displacements: np.ndarray) -> float: | ||
"""[summary] | ||
Returns miss rate. | ||
Args: | ||
displacements (numpy.ndarray): in shape (N, K, T, 3). | ||
Returns: | ||
float: Miss rate. | ||
""" | ||
# (N, K) | ||
max_displacements: np.ndarray = np.linalg.norm( | ||
displacements[:, :, :, :2], | ||
axis=-1, | ||
).max(axis=-1) | ||
is_miss: np.ndarray = max_displacements[max_displacements >= self.miss_tolerance] | ||
return is_miss.size / max_displacements.size |
Oops, something went wrong.