Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧹 Move result types to separate files #236

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions model_api/python/model_api/models/detection_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#

from .image_model import ImageModel
from .result_types import Detection
from .types import ListValue, NumericalValue, StringValue
from .utils import load_labels

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

return parameters

def _resize_detections(self, detections, meta):
def _resize_detections(self, detections: list[Detection], meta):
"""Resizes detection bounding boxes according to initial image shape.

It implements image resizing depending on the set `resize_type`(see `ImageModel` for details).
Expand Down Expand Up @@ -117,7 +119,7 @@ def _clamp_and_round(val, min_value, max_value):

return detections

def _filter_detections(self, detections, box_area_threshold=0.0):
def _filter_detections(self, detections: list[Detection], box_area_threshold=0.0):
"""Filters detections by confidence threshold and box size threshold

Args:
Expand All @@ -138,7 +140,7 @@ def _filter_detections(self, detections, box_area_threshold=0.0):

return filtered_detections

def _add_label_names(self, detections):
def _add_label_names(self, detections: list[Detection]):
"""Adds labels names to detections if they are available

Args:
Expand Down
246 changes: 0 additions & 246 deletions model_api/python/model_api/models/result_types.py

This file was deleted.

33 changes: 33 additions & 0 deletions model_api/python/model_api/models/result_types/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Result types."""

# Copyright (C) 2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

from .anomaly import AnomalyResult
from .classification import ClassificationResult
from .detection import Detection, DetectionResult
from .keypoint import DetectedKeypoints
from .segmentation import (
Contour,
ImageResultWithSoftPrediction,
InstanceSegmentationResult,
SegmentedObject,
SegmentedObjectWithRects,
)
from .visual_prompting import PredictedMask, VisualPromptingResult, ZSLVisualPromptingResult

__all__ = [
"AnomalyResult",
"ClassificationResult",
"Contour",
"Detection",
"DetectionResult",
"DetectedKeypoints",
"SegmentedObject",
"SegmentedObjectWithRects",
"ImageResultWithSoftPrediction",
"InstanceSegmentationResult",
"PredictedMask",
"VisualPromptingResult",
"ZSLVisualPromptingResult",
]
42 changes: 42 additions & 0 deletions model_api/python/model_api/models/result_types/anomaly.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""Anomaly result type."""

# Copyright (C) 2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

from __future__ import annotations

import numpy as np


class AnomalyResult:
"""Results for anomaly models."""

def __init__(
self,
anomaly_map: np.ndarray | None = None,
pred_boxes: np.ndarray | None = None,
pred_label: str | None = None,
pred_mask: np.ndarray | None = None,
pred_score: float | None = None,
) -> None:
self.anomaly_map = anomaly_map
self.pred_boxes = pred_boxes
self.pred_label = pred_label
self.pred_mask = pred_mask
self.pred_score = pred_score

def _compute_min_max(self, tensor: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
"""Computes min and max values of the tensor."""
return tensor.min(), tensor.max()

def __str__(self) -> str:
assert self.anomaly_map is not None
assert self.pred_mask is not None
anomaly_map_min, anomaly_map_max = self._compute_min_max(self.anomaly_map)
pred_mask_min, pred_mask_max = self._compute_min_max(self.pred_mask)
return (
f"anomaly_map min:{anomaly_map_min} max:{anomaly_map_max};"
f"pred_score:{np.round(self.pred_score, 1) if self.pred_score else 0.0};"
f"pred_label:{self.pred_label};"
f"pred_mask min:{pred_mask_min} max:{pred_mask_max};"
)
Loading
Loading