From 55c00345bf8c5a25befec9aceb65680ac7613bec Mon Sep 17 00:00:00 2001 From: Haroon <106879583+sky0walker99@users.noreply.github.com> Date: Tue, 26 Nov 2024 17:14:10 +0530 Subject: [PATCH 1/4] Added Attributes of POT and NNCF on CompressionType in export.py and updated test_export Signed-off-by: Haroon <106879583+sky0walker99@users.noreply.github.com> --- src/anomalib/deploy/export.py | 2 ++ src/anomalib/engine/engine.py | 5 +++++ tests/integration/cli/test_cli.py | 13 +++++++++++-- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/anomalib/deploy/export.py b/src/anomalib/deploy/export.py index aae359c035..80de3733e8 100644 --- a/src/anomalib/deploy/export.py +++ b/src/anomalib/deploy/export.py @@ -58,6 +58,8 @@ class CompressionType(str, Enum): INT8 = "int8" INT8_PTQ = "int8_ptq" INT8_ACQ = "int8_acq" + POT = "pot" + NNCF = "nncf" class InferenceModel(nn.Module): diff --git a/src/anomalib/engine/engine.py b/src/anomalib/engine/engine.py index b537819729..b8bf570ab4 100644 --- a/src/anomalib/engine/engine.py +++ b/src/anomalib/engine/engine.py @@ -975,6 +975,11 @@ def export( else: logging.error(f"Export type {export_type} is not supported yet.") + if compression_type and export_type != ExportType.OPENVINO: + msg = f"Compression type {compression_type} is only applicable for OpenVIVO Export Type." + logging.error(f"Compression type {compression_type} is only applicable for OpenVIVO Export Type.") + raise ValueError(msg) + if exported_model_path: logging.info(f"Exported model to {exported_model_path}") return exported_model_path diff --git a/tests/integration/cli/test_cli.py b/tests/integration/cli/test_cli.py index 3a9b001903..e1fe081353 100644 --- a/tests/integration/cli/test_cli.py +++ b/tests/integration/cli/test_cli.py @@ -12,7 +12,7 @@ import torch from anomalib.cli import AnomalibCLI -from anomalib.deploy import ExportType +from anomalib.deploy import CompressionType, ExportType class TestCLI: @@ -155,7 +155,16 @@ def test_predict_with_image_path(self, project_path: Path) -> None: ) torch.cuda.empty_cache() - @pytest.mark.parametrize("export_type", [ExportType.TORCH, ExportType.ONNX, ExportType.OPENVINO]) + @pytest.mark.parametrize( + ("export_type", "compression_type"), + [ + (ExportType.TORCH, None), + (ExportType.ONNX, None), + (ExportType.OPENVIVO, None), + (ExportType.OPENVIVO, CompressionType.POT), + (ExportType.OPENVIVO, CompressionType.NNCF), + ], + ) def test_export( self, project_path: Path, From 22d0558ca274eccddccbe76c92137a334e656cc8 Mon Sep 17 00:00:00 2001 From: Haroon <106879583+sky0walker99@users.noreply.github.com> Date: Tue, 26 Nov 2024 17:31:34 +0530 Subject: [PATCH 2/4] Add Support for POT and NNCF Compression in OpenVINO Export Signed-off-by: Haroon <106879583+sky0walker99@users.noreply.github.com> --- src/anomalib/engine/engine.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/anomalib/engine/engine.py b/src/anomalib/engine/engine.py index b8bf570ab4..0817f658d0 100644 --- a/src/anomalib/engine/engine.py +++ b/src/anomalib/engine/engine.py @@ -915,6 +915,7 @@ def export( Raises: ValueError: If Dataset, Datamodule, and transform are not provided. + ValueError: If an unsupported compression type is specified for non-OpenVINO export types. TypeError: If path to the transform file is not a string or Path. CLI Usage: From edf2b51ad27b6f5cc3b8c6e6c1cc24ec7e73b78c Mon Sep 17 00:00:00 2001 From: Haroon <106879583+sky0walker99@users.noreply.github.com> Date: Tue, 26 Nov 2024 18:57:17 +0530 Subject: [PATCH 3/4] Add Support for POT and NNCF Compression in OpenVINO Export Signed-off-by: Haroon <106879583+sky0walker99@users.noreply.github.com> --- tests/integration/cli/test_cli.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/tests/integration/cli/test_cli.py b/tests/integration/cli/test_cli.py index e1fe081353..7ac247e372 100644 --- a/tests/integration/cli/test_cli.py +++ b/tests/integration/cli/test_cli.py @@ -160,15 +160,16 @@ def test_predict_with_image_path(self, project_path: Path) -> None: [ (ExportType.TORCH, None), (ExportType.ONNX, None), - (ExportType.OPENVIVO, None), - (ExportType.OPENVIVO, CompressionType.POT), - (ExportType.OPENVIVO, CompressionType.NNCF), + (ExportType.OPENVINO, None), + (ExportType.OPENVINO, CompressionType.POT), + (ExportType.OPENVINO, CompressionType.NNCF), ], ) def test_export( self, project_path: Path, export_type: ExportType, + compression_type: CompressionType | None, ) -> None: """Test the export method of the CLI. @@ -176,17 +177,19 @@ def test_export( dataset_path (Path): Root of the synthetic/original dataset. project_path (Path): Path to temporary project folder. export_type (ExportType): Export type. + compression_type (CompressionType): Compression type (if any). """ - AnomalibCLI( - args=[ - "export", - "--export_type", - export_type, - *self._get_common_cli_args(None, project_path), - "--ckpt_path", - f"{project_path}/Padim/MVTec/dummy/v0/weights/lightning/model.ckpt", - ], - ) + args = [ + "export", + "--export_type", + export_type, + *self._get_common_cli_args(None, project_path), + "--ckpt_path", + f"{project_path}/Padim/MVTec/dummy/v0/weights/lightning/model.ckpt", + ] + if compression_type: + args += ["--compression_type", str(compression_type)] + AnomalibCLI(args=args) @staticmethod def _get_common_cli_args(dataset_path: Path | None, project_path: Path) -> list[str]: From c4a11d5d6c97e9e273f5c02977de16eb36e909f2 Mon Sep 17 00:00:00 2001 From: Haroon <106879583+sky0walker99@users.noreply.github.com> Date: Mon, 2 Dec 2024 23:41:35 +0530 Subject: [PATCH 4/4] Resolved Unecessary modification Signed-off-by: Haroon <106879583+sky0walker99@users.noreply.github.com> --- src/anomalib/deploy/export.py | 2 -- src/anomalib/engine/engine.py | 6 ------ 2 files changed, 8 deletions(-) diff --git a/src/anomalib/deploy/export.py b/src/anomalib/deploy/export.py index 80de3733e8..aae359c035 100644 --- a/src/anomalib/deploy/export.py +++ b/src/anomalib/deploy/export.py @@ -58,8 +58,6 @@ class CompressionType(str, Enum): INT8 = "int8" INT8_PTQ = "int8_ptq" INT8_ACQ = "int8_acq" - POT = "pot" - NNCF = "nncf" class InferenceModel(nn.Module): diff --git a/src/anomalib/engine/engine.py b/src/anomalib/engine/engine.py index 0817f658d0..b537819729 100644 --- a/src/anomalib/engine/engine.py +++ b/src/anomalib/engine/engine.py @@ -915,7 +915,6 @@ def export( Raises: ValueError: If Dataset, Datamodule, and transform are not provided. - ValueError: If an unsupported compression type is specified for non-OpenVINO export types. TypeError: If path to the transform file is not a string or Path. CLI Usage: @@ -976,11 +975,6 @@ def export( else: logging.error(f"Export type {export_type} is not supported yet.") - if compression_type and export_type != ExportType.OPENVINO: - msg = f"Compression type {compression_type} is only applicable for OpenVIVO Export Type." - logging.error(f"Compression type {compression_type} is only applicable for OpenVIVO Export Type.") - raise ValueError(msg) - if exported_model_path: logging.info(f"Exported model to {exported_model_path}") return exported_model_path