|
| 1 | +from threading import Lock |
| 2 | +from time import perf_counter |
| 3 | +from typing import Any, Generic, List, Optional, Tuple, Union |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +from inference_exp.models.base.object_detection import Detections, ObjectDetectionModel |
| 7 | +from inference_exp.models.base.types import ( |
| 8 | + PreprocessedInputs, |
| 9 | + PreprocessingMetadata, |
| 10 | + RawPrediction, |
| 11 | +) |
| 12 | + |
| 13 | +from inference.core.entities.responses.inference import ( |
| 14 | + InferenceResponseImage, |
| 15 | + ObjectDetectionInferenceResponse, |
| 16 | + ObjectDetectionPrediction, |
| 17 | +) |
| 18 | +from inference.core.env import API_KEY |
| 19 | +from inference.core.logger import logger |
| 20 | +from inference.core.models.base import Model |
| 21 | +from inference.core.utils.image_utils import load_image_rgb |
| 22 | +from inference.models.aliases import resolve_roboflow_model_alias |
| 23 | + |
| 24 | + |
| 25 | +class InferenceExpObjectDetectionModelAdapter(Model): |
| 26 | + def __init__(self, model_id: str, api_key: str = None, **kwargs): |
| 27 | + super().__init__() |
| 28 | + |
| 29 | + self.metrics = {"num_inferences": 0, "avg_inference_time": 0.0} |
| 30 | + |
| 31 | + self.api_key = api_key if api_key else API_KEY |
| 32 | + model_id = resolve_roboflow_model_alias(model_id=model_id) |
| 33 | + |
| 34 | + self.task_type = "object-detection" |
| 35 | + |
| 36 | + # Lazy import to avoid hard dependency if flag disabled |
| 37 | + from inference_exp import AutoModel # type: ignore |
| 38 | + |
| 39 | + self._exp_model: ObjectDetectionModel = AutoModel.from_pretrained( |
| 40 | + model_id_or_path=model_id, api_key=self.api_key |
| 41 | + ) |
| 42 | + if hasattr(self._exp_model, "optimize_for_inference"): |
| 43 | + self._exp_model.optimize_for_inference() |
| 44 | + |
| 45 | + self.class_names = list(self._exp_model.class_names) |
| 46 | + |
| 47 | + def map_inference_kwargs(self, kwargs: dict) -> dict: |
| 48 | + return kwargs |
| 49 | + |
| 50 | + def preprocess(self, image: Any, **kwargs): |
| 51 | + is_batch = isinstance(image, list) |
| 52 | + images = image if is_batch else [image] |
| 53 | + np_images: List[np.ndarray] = [ |
| 54 | + load_image_rgb( |
| 55 | + v, |
| 56 | + disable_preproc_auto_orient=kwargs.get( |
| 57 | + "disable_preproc_auto_orient", False |
| 58 | + ), |
| 59 | + ) |
| 60 | + for v in images |
| 61 | + ] |
| 62 | + mapped_kwargs = self.map_inference_kwargs(kwargs) |
| 63 | + return self._exp_model.pre_process(np_images, **mapped_kwargs) |
| 64 | + |
| 65 | + def predict(self, img_in, **kwargs): |
| 66 | + mapped_kwargs = self.map_inference_kwargs(kwargs) |
| 67 | + return self._exp_model.forward(img_in, **mapped_kwargs) |
| 68 | + |
| 69 | + def postprocess( |
| 70 | + self, |
| 71 | + predictions: Tuple[np.ndarray, ...], |
| 72 | + preprocess_return_metadata: PreprocessingMetadata, |
| 73 | + **kwargs, |
| 74 | + ) -> List[Detections]: |
| 75 | + mapped_kwargs = self.map_inference_kwargs(kwargs) |
| 76 | + detections_list = self._exp_model.post_process( |
| 77 | + predictions, preprocess_return_metadata, **mapped_kwargs |
| 78 | + ) |
| 79 | + |
| 80 | + responses: List[ObjectDetectionInferenceResponse] = [] |
| 81 | + for preproc_metadata, det in zip(preprocess_return_metadata, detections_list): |
| 82 | + H = preproc_metadata.original_size.height |
| 83 | + W = preproc_metadata.original_size.width |
| 84 | + |
| 85 | + xyxy = det.xyxy.detach().cpu().numpy() |
| 86 | + confs = det.confidence.detach().cpu().numpy() |
| 87 | + class_ids = det.class_id.detach().cpu().numpy() |
| 88 | + |
| 89 | + predictions: List[ObjectDetectionPrediction] = [] |
| 90 | + |
| 91 | + for (x1, y1, x2, y2), conf, class_id in zip(xyxy, confs, class_ids): |
| 92 | + cx = (float(x1) + float(x2)) / 2.0 |
| 93 | + cy = (float(y1) + float(y2)) / 2.0 |
| 94 | + w = float(x2) - float(x1) |
| 95 | + h = float(y2) - float(y1) |
| 96 | + class_id_int = int(class_id) |
| 97 | + class_name = ( |
| 98 | + self.class_names[class_id_int] |
| 99 | + if 0 <= class_id_int < len(self.class_names) |
| 100 | + else str(class_id_int) |
| 101 | + ) |
| 102 | + predictions.append( |
| 103 | + ObjectDetectionPrediction( |
| 104 | + x=cx, |
| 105 | + y=cy, |
| 106 | + width=w, |
| 107 | + height=h, |
| 108 | + confidence=float(conf), |
| 109 | + **{"class": class_name}, |
| 110 | + class_id=class_id_int, |
| 111 | + ) |
| 112 | + ) |
| 113 | + |
| 114 | + responses.append( |
| 115 | + ObjectDetectionInferenceResponse( |
| 116 | + predictions=predictions, |
| 117 | + image=InferenceResponseImage(width=W, height=H), |
| 118 | + ) |
| 119 | + ) |
| 120 | + |
| 121 | + return responses |
| 122 | + |
| 123 | + def clear_cache(self, delete_from_disk: bool = True) -> None: |
| 124 | + """Clears any cache if necessary. TODO: Implement this to delete the cache from the experimental model. |
| 125 | +
|
| 126 | + Args: |
| 127 | + delete_from_disk (bool, optional): Whether to delete cached files from disk. Defaults to True. |
| 128 | + """ |
| 129 | + pass |
0 commit comments