Skip to content

Commit 8b5b2a3

Browse files
move result types
Signed-off-by: Ashwin Vaidya <[email protected]>
1 parent 4f3242a commit 8b5b2a3

File tree

10 files changed

+424
-247
lines changed

10 files changed

+424
-247
lines changed

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+
)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Classification result type."""
2+
3+
# Copyright (C) 2024 Intel Corporation
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
from __future__ import annotations
7+
8+
from typing import TYPE_CHECKING
9+
10+
from .utils import array_shape_to_str
11+
12+
if TYPE_CHECKING:
13+
import numpy as np
14+
15+
16+
class ClassificationResult:
17+
"""Results for classification models."""
18+
19+
def __init__(
20+
self,
21+
top_labels: list[tuple[int, str, float]] | None = None,
22+
saliency_map: np.ndarray | None = None,
23+
feature_vector: np.ndarray | None = None,
24+
raw_scores: np.ndarray | None = None,
25+
) -> None:
26+
self.top_labels = top_labels
27+
self.saliency_map = saliency_map
28+
self.feature_vector = feature_vector
29+
self.raw_scores = raw_scores
30+
31+
def __str__(self) -> str:
32+
assert self.top_labels is not None
33+
labels = ", ".join(f"{idx} ({label}): {confidence:.3f}" for idx, label, confidence in self.top_labels)
34+
return (
35+
f"{labels}, {array_shape_to_str(self.saliency_map)}, {array_shape_to_str(self.feature_vector)}, "
36+
f"{array_shape_to_str(self.raw_scores)}"
37+
)

0 commit comments

Comments
 (0)