From 24b080fb21a65989730a3457bd46fdd4cee465b4 Mon Sep 17 00:00:00 2001 From: Peter Robicheaux Date: Tue, 26 Mar 2024 17:09:55 +0000 Subject: [PATCH 1/4] Use class agnostic nms value from request --- inference/core/entities/requests/groundingdino.py | 2 ++ inference/core/models/classification_base.py | 1 + inference/models/grounding_dino/grounding_dino.py | 10 ++-------- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/inference/core/entities/requests/groundingdino.py b/inference/core/entities/requests/groundingdino.py index d2d7f0af9..a94bfd8f4 100644 --- a/inference/core/entities/requests/groundingdino.py +++ b/inference/core/entities/requests/groundingdino.py @@ -3,6 +3,7 @@ from inference.core.entities.requests.dynamic_class_base import ( DynamicClassBaseInferenceRequest, ) +from inference.core.env import CLASS_AGNOSTIC_NMS class GroundingDINOInferenceRequest(DynamicClassBaseInferenceRequest): @@ -15,3 +16,4 @@ class GroundingDINOInferenceRequest(DynamicClassBaseInferenceRequest): box_threshold: Optional[float] = 0.5 grounding_dino_version_id: Optional[str] = "default" text_threshold: Optional[float] = 0.5 + class_agnostic_nms: Optional[bool] = CLASS_AGNOSTIC_NMS diff --git a/inference/core/models/classification_base.py b/inference/core/models/classification_base.py index ca5ba0cda..3d4c47019 100644 --- a/inference/core/models/classification_base.py +++ b/inference/core/models/classification_base.py @@ -136,6 +136,7 @@ def infer( Args: image (Any): The image or list of images to be processed. + - can be a BGR numpy array, filepath, InferenceRequestImage, PIL Image, byte-string, etc. disable_preproc_auto_orient (bool, optional): If true, the auto orient preprocessing step is disabled for this call. Default is False. disable_preproc_contrast (bool, optional): If true, the auto contrast preprocessing step is disabled for this call. Default is False. disable_preproc_grayscale (bool, optional): If true, the grayscale preprocessing step is disabled for this call. Default is False. diff --git a/inference/models/grounding_dino/grounding_dino.py b/inference/models/grounding_dino/grounding_dino.py index 7447feb9e..b35c9539c 100644 --- a/inference/models/grounding_dino/grounding_dino.py +++ b/inference/models/grounding_dino/grounding_dino.py @@ -42,9 +42,6 @@ def __init__( GROUNDING_DINO_CONFIG_PATH = os.path.join( GROUNDING_DINO_CACHE_DIR, "GroundingDINO_SwinT_OGC.py" ) - # GROUNDING_DINO_CHECKPOINT_PATH = os.path.join( - # GROUDNING_DINO_CACHE_DIR, "groundingdino_swint_ogc.pth" - # ) if not os.path.exists(GROUNDING_DINO_CACHE_DIR): os.makedirs(GROUNDING_DINO_CACHE_DIR) @@ -53,10 +50,6 @@ def __init__( url = "https://raw.githubusercontent.com/roboflow/GroundingDINO/main/groundingdino/config/GroundingDINO_SwinT_OGC.py" urllib.request.urlretrieve(url, GROUNDING_DINO_CONFIG_PATH) - # if not os.path.exists(GROUNDING_DINO_CHECKPOINT_PATH): - # url = "https://github.com/IDEA-Research/GroundingDINO/releases/download/v0.1.0-alpha/groundingdino_swint_ogc.pth" - # urllib.request.urlretrieve(url, GROUNDING_DINO_CHECKPOINT_PATH) - self.model = Model( model_config_path=GROUNDING_DINO_CONFIG_PATH, model_checkpoint_path=os.path.join( @@ -95,6 +88,7 @@ def infer( class_filter: list = None, box_threshold=0.5, text_threshold=0.5, + class_agnostic_nms=CLASS_AGNOSTIC_NMS, **kwargs ): """ @@ -121,7 +115,7 @@ def infer( self.class_names = text - if CLASS_AGNOSTIC_NMS: + if class_agnostic_nms: detections = detections.with_nms(class_agnostic=True) else: detections = detections.with_nms() From 48254dc989d2080874921c40983fe6253517da7c Mon Sep 17 00:00:00 2001 From: Peter Robicheaux Date: Tue, 26 Mar 2024 17:22:09 +0000 Subject: [PATCH 2/4] Style --- .../core/active_learning/post_processing.py | 22 ++++++------ .../core/entities/requests/groundingdino.py | 2 +- inference/core/exceptions.py | 1 - .../core/interfaces/camera/video_source.py | 3 +- .../interfaces/stream/inference_pipeline.py | 6 ++-- .../workflows/complier/flow_coordinator.py | 3 -- .../active_learning_middlewares.py | 1 - inference_sdk/http/client.py | 21 ++++++------ inference_sdk/http/utils/post_processing.py | 34 +++++++++---------- 9 files changed, 44 insertions(+), 49 deletions(-) diff --git a/inference/core/active_learning/post_processing.py b/inference/core/active_learning/post_processing.py index a8381fef4..f69cdf6ad 100644 --- a/inference/core/active_learning/post_processing.py +++ b/inference/core/active_learning/post_processing.py @@ -30,19 +30,19 @@ def adjust_prediction_to_client_scaling_factor( ): return prediction if prediction_type == INSTANCE_SEGMENTATION_TASK: - prediction["predictions"] = ( - adjust_prediction_with_bbox_and_points_to_client_scaling_factor( - predictions=prediction["predictions"], - scaling_factor=scaling_factor, - points_key="points", - ) + prediction[ + "predictions" + ] = adjust_prediction_with_bbox_and_points_to_client_scaling_factor( + predictions=prediction["predictions"], + scaling_factor=scaling_factor, + points_key="points", ) if prediction_type == OBJECT_DETECTION_TASK: - prediction["predictions"] = ( - adjust_object_detection_predictions_to_client_scaling_factor( - predictions=prediction["predictions"], - scaling_factor=scaling_factor, - ) + prediction[ + "predictions" + ] = adjust_object_detection_predictions_to_client_scaling_factor( + predictions=prediction["predictions"], + scaling_factor=scaling_factor, ) return prediction diff --git a/inference/core/entities/requests/groundingdino.py b/inference/core/entities/requests/groundingdino.py index a94bfd8f4..02941e439 100644 --- a/inference/core/entities/requests/groundingdino.py +++ b/inference/core/entities/requests/groundingdino.py @@ -16,4 +16,4 @@ class GroundingDINOInferenceRequest(DynamicClassBaseInferenceRequest): box_threshold: Optional[float] = 0.5 grounding_dino_version_id: Optional[str] = "default" text_threshold: Optional[float] = 0.5 - class_agnostic_nms: Optional[bool] = CLASS_AGNOSTIC_NMS + class_agnostic_nms: Optional[bool] = CLASS_AGNOSTIC_NMS diff --git a/inference/core/exceptions.py b/inference/core/exceptions.py index 3b3447fd5..951c138c8 100644 --- a/inference/core/exceptions.py +++ b/inference/core/exceptions.py @@ -79,7 +79,6 @@ class WorkspaceLoadError(Exception): class InputImageLoadError(Exception): - def __init__(self, message: str, public_message: str): super().__init__(message) self._public_message = public_message diff --git a/inference/core/interfaces/camera/video_source.py b/inference/core/interfaces/camera/video_source.py index 1a663d55c..57e13e9d1 100644 --- a/inference/core/interfaces/camera/video_source.py +++ b/inference/core/interfaces/camera/video_source.py @@ -119,7 +119,8 @@ class SourceMetadata: class VideoSourceMethod(Protocol): - def __call__(self, video_source: "VideoSource", *args, **kwargs) -> None: ... + def __call__(self, video_source: "VideoSource", *args, **kwargs) -> None: + ... def lock_state_transition( diff --git a/inference/core/interfaces/stream/inference_pipeline.py b/inference/core/interfaces/stream/inference_pipeline.py index b1f00a352..f68f6b220 100644 --- a/inference/core/interfaces/stream/inference_pipeline.py +++ b/inference/core/interfaces/stream/inference_pipeline.py @@ -697,9 +697,9 @@ def _execute_inference(self) -> None: def _dispatch_inference_results(self) -> None: while True: - inference_results: Optional[Tuple[dict, VideoFrame]] = ( - self._predictions_queue.get() - ) + inference_results: Optional[ + Tuple[dict, VideoFrame] + ] = self._predictions_queue.get() if inference_results is None: self._predictions_queue.task_done() break diff --git a/inference/enterprise/workflows/complier/flow_coordinator.py b/inference/enterprise/workflows/complier/flow_coordinator.py index 253085f38..051ec75f5 100644 --- a/inference/enterprise/workflows/complier/flow_coordinator.py +++ b/inference/enterprise/workflows/complier/flow_coordinator.py @@ -10,7 +10,6 @@ class StepExecutionCoordinator(metaclass=abc.ABCMeta): - @classmethod @abc.abstractmethod def init(cls, execution_graph: nx.DiGraph) -> "StepExecutionCoordinator": @@ -24,7 +23,6 @@ def get_steps_to_execute_next( class SerialExecutionCoordinator(StepExecutionCoordinator): - @classmethod def init(cls, execution_graph: nx.DiGraph) -> "StepExecutionCoordinator": return cls(execution_graph=execution_graph) @@ -61,7 +59,6 @@ def __establish_execution_order(self) -> None: class ParallelStepExecutionCoordinator(StepExecutionCoordinator): - @classmethod def init(cls, execution_graph: nx.DiGraph) -> "StepExecutionCoordinator": return cls(execution_graph=execution_graph) diff --git a/inference/enterprise/workflows/complier/steps_executors/active_learning_middlewares.py b/inference/enterprise/workflows/complier/steps_executors/active_learning_middlewares.py index d0b037940..4f95882bf 100644 --- a/inference/enterprise/workflows/complier/steps_executors/active_learning_middlewares.py +++ b/inference/enterprise/workflows/complier/steps_executors/active_learning_middlewares.py @@ -13,7 +13,6 @@ class WorkflowsActiveLearningMiddleware: - def __init__( self, cache: BaseCache, diff --git a/inference_sdk/http/client.py b/inference_sdk/http/client.py index e75356564..7d77a33b0 100644 --- a/inference_sdk/http/client.py +++ b/inference_sdk/http/client.py @@ -117,7 +117,6 @@ async def decorate(*args, **kwargs) -> Any: class InferenceHTTPClient: - @classmethod def init( cls, @@ -435,11 +434,11 @@ def infer_from_api_v1( parsed_response, request_data.image_scaling_factors ): if parsed_response_element.get("visualization") is not None: - parsed_response_element["visualization"] = ( - transform_base64_visualisation( - visualisation=parsed_response_element["visualization"], - expected_format=self.__inference_configuration.output_visualisation_format, - ) + parsed_response_element[ + "visualization" + ] = transform_base64_visualisation( + visualisation=parsed_response_element["visualization"], + expected_format=self.__inference_configuration.output_visualisation_format, ) parsed_response_element = adjust_prediction_to_client_scaling_factor( prediction=parsed_response_element, @@ -507,11 +506,11 @@ async def infer_from_api_v1_async( parsed_response, request_data.image_scaling_factors ): if parsed_response_element.get("visualization") is not None: - parsed_response_element["visualization"] = ( - transform_base64_visualisation( - visualisation=parsed_response_element["visualization"], - expected_format=self.__inference_configuration.output_visualisation_format, - ) + parsed_response_element[ + "visualization" + ] = transform_base64_visualisation( + visualisation=parsed_response_element["visualization"], + expected_format=self.__inference_configuration.output_visualisation_format, ) parsed_response_element = adjust_prediction_to_client_scaling_factor( prediction=parsed_response_element, diff --git a/inference_sdk/http/utils/post_processing.py b/inference_sdk/http/utils/post_processing.py index 247cee319..64dc442fc 100644 --- a/inference_sdk/http/utils/post_processing.py +++ b/inference_sdk/http/utils/post_processing.py @@ -137,27 +137,27 @@ def adjust_prediction_to_client_scaling_factor( if predictions_should_not_be_post_processed(prediction=prediction): return prediction if "points" in prediction["predictions"][0]: - prediction["predictions"] = ( - adjust_prediction_with_bbox_and_points_to_client_scaling_factor( - predictions=prediction["predictions"], - scaling_factor=scaling_factor, - points_key="points", - ) + prediction[ + "predictions" + ] = adjust_prediction_with_bbox_and_points_to_client_scaling_factor( + predictions=prediction["predictions"], + scaling_factor=scaling_factor, + points_key="points", ) elif "keypoints" in prediction["predictions"][0]: - prediction["predictions"] = ( - adjust_prediction_with_bbox_and_points_to_client_scaling_factor( - predictions=prediction["predictions"], - scaling_factor=scaling_factor, - points_key="keypoints", - ) + prediction[ + "predictions" + ] = adjust_prediction_with_bbox_and_points_to_client_scaling_factor( + predictions=prediction["predictions"], + scaling_factor=scaling_factor, + points_key="keypoints", ) elif "x" in prediction["predictions"][0] and "y" in prediction["predictions"][0]: - prediction["predictions"] = ( - adjust_object_detection_predictions_to_client_scaling_factor( - predictions=prediction["predictions"], - scaling_factor=scaling_factor, - ) + prediction[ + "predictions" + ] = adjust_object_detection_predictions_to_client_scaling_factor( + predictions=prediction["predictions"], + scaling_factor=scaling_factor, ) return prediction From cb5010b3d2f0985b7187e01b5dea4ababa6ce224 Mon Sep 17 00:00:00 2001 From: Peter Robicheaux Date: Tue, 26 Mar 2024 17:23:13 +0000 Subject: [PATCH 3/4] Revert "Style" This reverts commit 48254dc989d2080874921c40983fe6253517da7c. --- .../core/active_learning/post_processing.py | 22 ++++++------ .../core/entities/requests/groundingdino.py | 2 +- inference/core/exceptions.py | 1 + .../core/interfaces/camera/video_source.py | 3 +- .../interfaces/stream/inference_pipeline.py | 6 ++-- .../workflows/complier/flow_coordinator.py | 3 ++ .../active_learning_middlewares.py | 1 + inference_sdk/http/client.py | 21 ++++++------ inference_sdk/http/utils/post_processing.py | 34 +++++++++---------- 9 files changed, 49 insertions(+), 44 deletions(-) diff --git a/inference/core/active_learning/post_processing.py b/inference/core/active_learning/post_processing.py index f69cdf6ad..a8381fef4 100644 --- a/inference/core/active_learning/post_processing.py +++ b/inference/core/active_learning/post_processing.py @@ -30,19 +30,19 @@ def adjust_prediction_to_client_scaling_factor( ): return prediction if prediction_type == INSTANCE_SEGMENTATION_TASK: - prediction[ - "predictions" - ] = adjust_prediction_with_bbox_and_points_to_client_scaling_factor( - predictions=prediction["predictions"], - scaling_factor=scaling_factor, - points_key="points", + prediction["predictions"] = ( + adjust_prediction_with_bbox_and_points_to_client_scaling_factor( + predictions=prediction["predictions"], + scaling_factor=scaling_factor, + points_key="points", + ) ) if prediction_type == OBJECT_DETECTION_TASK: - prediction[ - "predictions" - ] = adjust_object_detection_predictions_to_client_scaling_factor( - predictions=prediction["predictions"], - scaling_factor=scaling_factor, + prediction["predictions"] = ( + adjust_object_detection_predictions_to_client_scaling_factor( + predictions=prediction["predictions"], + scaling_factor=scaling_factor, + ) ) return prediction diff --git a/inference/core/entities/requests/groundingdino.py b/inference/core/entities/requests/groundingdino.py index 02941e439..a94bfd8f4 100644 --- a/inference/core/entities/requests/groundingdino.py +++ b/inference/core/entities/requests/groundingdino.py @@ -16,4 +16,4 @@ class GroundingDINOInferenceRequest(DynamicClassBaseInferenceRequest): box_threshold: Optional[float] = 0.5 grounding_dino_version_id: Optional[str] = "default" text_threshold: Optional[float] = 0.5 - class_agnostic_nms: Optional[bool] = CLASS_AGNOSTIC_NMS + class_agnostic_nms: Optional[bool] = CLASS_AGNOSTIC_NMS diff --git a/inference/core/exceptions.py b/inference/core/exceptions.py index 951c138c8..3b3447fd5 100644 --- a/inference/core/exceptions.py +++ b/inference/core/exceptions.py @@ -79,6 +79,7 @@ class WorkspaceLoadError(Exception): class InputImageLoadError(Exception): + def __init__(self, message: str, public_message: str): super().__init__(message) self._public_message = public_message diff --git a/inference/core/interfaces/camera/video_source.py b/inference/core/interfaces/camera/video_source.py index 57e13e9d1..1a663d55c 100644 --- a/inference/core/interfaces/camera/video_source.py +++ b/inference/core/interfaces/camera/video_source.py @@ -119,8 +119,7 @@ class SourceMetadata: class VideoSourceMethod(Protocol): - def __call__(self, video_source: "VideoSource", *args, **kwargs) -> None: - ... + def __call__(self, video_source: "VideoSource", *args, **kwargs) -> None: ... def lock_state_transition( diff --git a/inference/core/interfaces/stream/inference_pipeline.py b/inference/core/interfaces/stream/inference_pipeline.py index f68f6b220..b1f00a352 100644 --- a/inference/core/interfaces/stream/inference_pipeline.py +++ b/inference/core/interfaces/stream/inference_pipeline.py @@ -697,9 +697,9 @@ def _execute_inference(self) -> None: def _dispatch_inference_results(self) -> None: while True: - inference_results: Optional[ - Tuple[dict, VideoFrame] - ] = self._predictions_queue.get() + inference_results: Optional[Tuple[dict, VideoFrame]] = ( + self._predictions_queue.get() + ) if inference_results is None: self._predictions_queue.task_done() break diff --git a/inference/enterprise/workflows/complier/flow_coordinator.py b/inference/enterprise/workflows/complier/flow_coordinator.py index 051ec75f5..253085f38 100644 --- a/inference/enterprise/workflows/complier/flow_coordinator.py +++ b/inference/enterprise/workflows/complier/flow_coordinator.py @@ -10,6 +10,7 @@ class StepExecutionCoordinator(metaclass=abc.ABCMeta): + @classmethod @abc.abstractmethod def init(cls, execution_graph: nx.DiGraph) -> "StepExecutionCoordinator": @@ -23,6 +24,7 @@ def get_steps_to_execute_next( class SerialExecutionCoordinator(StepExecutionCoordinator): + @classmethod def init(cls, execution_graph: nx.DiGraph) -> "StepExecutionCoordinator": return cls(execution_graph=execution_graph) @@ -59,6 +61,7 @@ def __establish_execution_order(self) -> None: class ParallelStepExecutionCoordinator(StepExecutionCoordinator): + @classmethod def init(cls, execution_graph: nx.DiGraph) -> "StepExecutionCoordinator": return cls(execution_graph=execution_graph) diff --git a/inference/enterprise/workflows/complier/steps_executors/active_learning_middlewares.py b/inference/enterprise/workflows/complier/steps_executors/active_learning_middlewares.py index 4f95882bf..d0b037940 100644 --- a/inference/enterprise/workflows/complier/steps_executors/active_learning_middlewares.py +++ b/inference/enterprise/workflows/complier/steps_executors/active_learning_middlewares.py @@ -13,6 +13,7 @@ class WorkflowsActiveLearningMiddleware: + def __init__( self, cache: BaseCache, diff --git a/inference_sdk/http/client.py b/inference_sdk/http/client.py index 7d77a33b0..e75356564 100644 --- a/inference_sdk/http/client.py +++ b/inference_sdk/http/client.py @@ -117,6 +117,7 @@ async def decorate(*args, **kwargs) -> Any: class InferenceHTTPClient: + @classmethod def init( cls, @@ -434,11 +435,11 @@ def infer_from_api_v1( parsed_response, request_data.image_scaling_factors ): if parsed_response_element.get("visualization") is not None: - parsed_response_element[ - "visualization" - ] = transform_base64_visualisation( - visualisation=parsed_response_element["visualization"], - expected_format=self.__inference_configuration.output_visualisation_format, + parsed_response_element["visualization"] = ( + transform_base64_visualisation( + visualisation=parsed_response_element["visualization"], + expected_format=self.__inference_configuration.output_visualisation_format, + ) ) parsed_response_element = adjust_prediction_to_client_scaling_factor( prediction=parsed_response_element, @@ -506,11 +507,11 @@ async def infer_from_api_v1_async( parsed_response, request_data.image_scaling_factors ): if parsed_response_element.get("visualization") is not None: - parsed_response_element[ - "visualization" - ] = transform_base64_visualisation( - visualisation=parsed_response_element["visualization"], - expected_format=self.__inference_configuration.output_visualisation_format, + parsed_response_element["visualization"] = ( + transform_base64_visualisation( + visualisation=parsed_response_element["visualization"], + expected_format=self.__inference_configuration.output_visualisation_format, + ) ) parsed_response_element = adjust_prediction_to_client_scaling_factor( prediction=parsed_response_element, diff --git a/inference_sdk/http/utils/post_processing.py b/inference_sdk/http/utils/post_processing.py index 64dc442fc..247cee319 100644 --- a/inference_sdk/http/utils/post_processing.py +++ b/inference_sdk/http/utils/post_processing.py @@ -137,27 +137,27 @@ def adjust_prediction_to_client_scaling_factor( if predictions_should_not_be_post_processed(prediction=prediction): return prediction if "points" in prediction["predictions"][0]: - prediction[ - "predictions" - ] = adjust_prediction_with_bbox_and_points_to_client_scaling_factor( - predictions=prediction["predictions"], - scaling_factor=scaling_factor, - points_key="points", + prediction["predictions"] = ( + adjust_prediction_with_bbox_and_points_to_client_scaling_factor( + predictions=prediction["predictions"], + scaling_factor=scaling_factor, + points_key="points", + ) ) elif "keypoints" in prediction["predictions"][0]: - prediction[ - "predictions" - ] = adjust_prediction_with_bbox_and_points_to_client_scaling_factor( - predictions=prediction["predictions"], - scaling_factor=scaling_factor, - points_key="keypoints", + prediction["predictions"] = ( + adjust_prediction_with_bbox_and_points_to_client_scaling_factor( + predictions=prediction["predictions"], + scaling_factor=scaling_factor, + points_key="keypoints", + ) ) elif "x" in prediction["predictions"][0] and "y" in prediction["predictions"][0]: - prediction[ - "predictions" - ] = adjust_object_detection_predictions_to_client_scaling_factor( - predictions=prediction["predictions"], - scaling_factor=scaling_factor, + prediction["predictions"] = ( + adjust_object_detection_predictions_to_client_scaling_factor( + predictions=prediction["predictions"], + scaling_factor=scaling_factor, + ) ) return prediction From 1058bcd32a36041f76d98979eec11b0ed8a0bd85 Mon Sep 17 00:00:00 2001 From: Peter Robicheaux Date: Tue, 26 Mar 2024 17:24:01 +0000 Subject: [PATCH 4/4] Style --- inference/core/entities/requests/groundingdino.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inference/core/entities/requests/groundingdino.py b/inference/core/entities/requests/groundingdino.py index a94bfd8f4..02941e439 100644 --- a/inference/core/entities/requests/groundingdino.py +++ b/inference/core/entities/requests/groundingdino.py @@ -16,4 +16,4 @@ class GroundingDINOInferenceRequest(DynamicClassBaseInferenceRequest): box_threshold: Optional[float] = 0.5 grounding_dino_version_id: Optional[str] = "default" text_threshold: Optional[float] = 0.5 - class_agnostic_nms: Optional[bool] = CLASS_AGNOSTIC_NMS + class_agnostic_nms: Optional[bool] = CLASS_AGNOSTIC_NMS