Skip to content

Commit 146b702

Browse files
🧹 Move result types to separate files (#236)
* move result types Signed-off-by: Ashwin Vaidya <[email protected]> * fix Signed-off-by: Ashwin Vaidya <[email protected]> * add typing + fix str Signed-off-by: Ashwin Vaidya <[email protected]> * convert id to int in Detection class constructor Signed-off-by: Ashwin Vaidya <[email protected]> --------- Signed-off-by: Ashwin Vaidya <[email protected]>
1 parent 7d2fb1b commit 146b702

File tree

11 files changed

+432
-250
lines changed

11 files changed

+432
-250
lines changed

model_api/python/model_api/models/detection_model.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#
55

66
from .image_model import ImageModel
7+
from .result_types import Detection
78
from .types import ListValue, NumericalValue, StringValue
89
from .utils import load_labels
910

@@ -37,6 +38,7 @@ def __init__(self, inference_adapter, configuration: dict = {}, preload=False):
3738
"""
3839
super().__init__(inference_adapter, configuration, preload)
3940
self.path_to_labels: str
41+
self.confidence_threshold: float
4042
if not self.image_blob_name:
4143
self.raise_error(
4244
f"The Wrapper supports only one image input, but {len(self.image_blob_names)} found",
@@ -63,7 +65,7 @@ def parameters(cls):
6365

6466
return parameters
6567

66-
def _resize_detections(self, detections, meta):
68+
def _resize_detections(self, detections: list[Detection], meta):
6769
"""Resizes detection bounding boxes according to initial image shape.
6870
6971
It implements image resizing depending on the set `resize_type`(see `ImageModel` for details).
@@ -117,7 +119,7 @@ def _clamp_and_round(val, min_value, max_value):
117119

118120
return detections
119121

120-
def _filter_detections(self, detections, box_area_threshold=0.0):
122+
def _filter_detections(self, detections: list[Detection], box_area_threshold=0.0):
121123
"""Filters detections by confidence threshold and box size threshold
122124
123125
Args:
@@ -138,7 +140,7 @@ def _filter_detections(self, detections, box_area_threshold=0.0):
138140

139141
return filtered_detections
140142

141-
def _add_label_names(self, detections):
143+
def _add_label_names(self, detections: list[Detection]):
142144
"""Adds labels names to detections if they are available
143145
144146
Args:

model_api/python/model_api/models/result_types.py

Lines changed: 0 additions & 246 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""Result types."""
2+
3+
# Copyright (C) 2024 Intel Corporation
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
from .anomaly import AnomalyResult
7+
from .classification import ClassificationResult
8+
from .detection import Detection, DetectionResult
9+
from .keypoint import DetectedKeypoints
10+
from .segmentation import (
11+
Contour,
12+
ImageResultWithSoftPrediction,
13+
InstanceSegmentationResult,
14+
SegmentedObject,
15+
SegmentedObjectWithRects,
16+
)
17+
from .visual_prompting import PredictedMask, VisualPromptingResult, ZSLVisualPromptingResult
18+
19+
__all__ = [
20+
"AnomalyResult",
21+
"ClassificationResult",
22+
"Contour",
23+
"Detection",
24+
"DetectionResult",
25+
"DetectedKeypoints",
26+
"SegmentedObject",
27+
"SegmentedObjectWithRects",
28+
"ImageResultWithSoftPrediction",
29+
"InstanceSegmentationResult",
30+
"PredictedMask",
31+
"VisualPromptingResult",
32+
"ZSLVisualPromptingResult",
33+
]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""Anomaly result type."""
2+
3+
# Copyright (C) 2024 Intel Corporation
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
from __future__ import annotations
7+
8+
import numpy as np
9+
10+
11+
class AnomalyResult:
12+
"""Results for anomaly models."""
13+
14+
def __init__(
15+
self,
16+
anomaly_map: np.ndarray | None = None,
17+
pred_boxes: np.ndarray | None = None,
18+
pred_label: str | None = None,
19+
pred_mask: np.ndarray | None = None,
20+
pred_score: float | None = None,
21+
) -> None:
22+
self.anomaly_map = anomaly_map
23+
self.pred_boxes = pred_boxes
24+
self.pred_label = pred_label
25+
self.pred_mask = pred_mask
26+
self.pred_score = pred_score
27+
28+
def _compute_min_max(self, tensor: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
29+
"""Computes min and max values of the tensor."""
30+
return tensor.min(), tensor.max()
31+
32+
def __str__(self) -> str:
33+
assert self.anomaly_map is not None
34+
assert self.pred_mask is not None
35+
anomaly_map_min, anomaly_map_max = self._compute_min_max(self.anomaly_map)
36+
pred_mask_min, pred_mask_max = self._compute_min_max(self.pred_mask)
37+
return (
38+
f"anomaly_map min:{anomaly_map_min} max:{anomaly_map_max};"
39+
f"pred_score:{np.round(self.pred_score, 1) if self.pred_score else 0.0};"
40+
f"pred_label:{self.pred_label};"
41+
f"pred_mask min:{pred_mask_min} max:{pred_mask_max};"
42+
)

0 commit comments

Comments
 (0)