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

Refactor OpenVINO imports #45

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
2 changes: 1 addition & 1 deletion openvino_xai/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from typing import List

import openvino.runtime as ov
import openvino as ov

from openvino_xai.common.parameters import Method, Task
from openvino_xai.common.utils import IdentityPreprocessFN, has_xai, logger
Expand Down
13 changes: 6 additions & 7 deletions openvino_xai/explainer/explainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
# SPDX-License-Identifier: Apache-2.0

from enum import Enum
from typing import Callable, List, Tuple
from typing import Callable, List, Mapping, Tuple

import numpy as np
import openvino.runtime as ov
from openvino.runtime.utils.data_helpers.wrappers import OVDict
import openvino as ov

from openvino_xai import Task
from openvino_xai.common.parameters import Method
Expand Down Expand Up @@ -53,7 +52,7 @@ class Explainer:
(assume input images are already preprocessed by user).
:type preprocess_fn: Callable[[np.ndarray], np.ndarray]
:param postprocess_fn: Postprocessing functions, required for black-box.
:type postprocess_fn: Callable[[OVDict], np.ndarray]
:type postprocess_fn: Callable[[Mapping], np.ndarray]
:param explain_mode: Explain mode.
:type explain_mode: ExplainMode
:parameter explain_method: Explain method to use for model explanation.
Expand All @@ -71,7 +70,7 @@ def __init__(
model: ov.Model,
task: Task,
preprocess_fn: Callable[[np.ndarray], np.ndarray] = IdentityPreprocessFN(),
postprocess_fn: Callable[[OVDict], np.ndarray] = None,
postprocess_fn: Callable[[Mapping], np.ndarray] = None,
explain_mode: ExplainMode = ExplainMode.AUTO,
explain_method: Method | None = None,
target_layer: str | List[str] | None = None,
Expand All @@ -80,7 +79,7 @@ def __init__(
**kwargs,
) -> None:
self.model = model
self.compiled_model: ov.ie_api.CompiledModel | None = None
self.compiled_model: ov.CompiledModel | None = None
self.task = task

if isinstance(preprocess_fn, IdentityPreprocessFN):
Expand Down Expand Up @@ -228,7 +227,7 @@ def explain(
overlay_weight,
)

def model_forward(self, x: np.ndarray, preprocess: bool = True) -> OVDict:
def model_forward(self, x: np.ndarray, preprocess: bool = True) -> Mapping:
"""Forward pass of the compiled model."""
return self.method.model_forward(x, preprocess)

Expand Down
5 changes: 2 additions & 3 deletions openvino_xai/explainer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
# SPDX-License-Identifier: Apache-2.0
from enum import Enum
from functools import partial
from typing import Any, Callable, List, Tuple
from typing import Any, Callable, List, Mapping, Tuple

import cv2
import numpy as np
from openvino.runtime.utils.data_helpers.wrappers import OVDict


def convert_targets_to_numpy(targets):
Expand Down Expand Up @@ -118,7 +117,7 @@ def get_preprocess_fn(
)


def postprocess_fn(x: OVDict, logit_name="logits") -> np.ndarray:
def postprocess_fn(x: Mapping, logit_name="logits") -> np.ndarray:
"""Postprocess function."""
return x.get(logit_name, x[0]) # Models from OVC has no output names at times

Expand Down
5 changes: 2 additions & 3 deletions openvino_xai/methods/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
# SPDX-License-Identifier: Apache-2.0

from abc import ABC, abstractmethod
from typing import Callable
from typing import Callable, Mapping

import numpy as np
import openvino.runtime as ov
from openvino.runtime.utils.data_helpers.wrappers import OVDict

from openvino_xai.common.utils import IdentityPreprocessFN

Expand All @@ -33,7 +32,7 @@ def model_compiled(self) -> ov.ie_api.CompiledModel | None:
def prepare_model(self, load_model: bool = True) -> ov.Model:
"""Model preparation steps."""

def model_forward(self, x: np.ndarray, preprocess: bool = True) -> OVDict:
def model_forward(self, x: np.ndarray, preprocess: bool = True) -> Mapping:
"""Forward pass of the compiled model. Applies preprocess_fn."""
if not self._model_compiled:
raise RuntimeError("Model is not compiled. Call prepare_model() first.")
Expand Down
7 changes: 3 additions & 4 deletions openvino_xai/methods/black_box/rise.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# Copyright (C) 2023-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

from typing import Callable, List, Tuple
from typing import Callable, List, Mapping, Tuple

import cv2
import numpy as np
import openvino.runtime as ov
from openvino.runtime.utils.data_helpers.wrappers import OVDict
from tqdm import tqdm

from openvino_xai.common.utils import IdentityPreprocessFN, scaling
Expand All @@ -19,7 +18,7 @@ class RISE(BlackBoxXAIMethod):
:param model: OpenVINO model.
:type model: ov.Model
:param postprocess_fn: Preprocessing function that extract scores from IR model output.
:type postprocess_fn: Callable[[OVDict], np.ndarray]
:type postprocess_fn: Callable[[Mapping], np.ndarray]
:param preprocess_fn: Preprocessing function, identity function by default
(assume input images are already preprocessed by user).
:type preprocess_fn: Callable[[np.ndarray], np.ndarray]
Expand All @@ -32,7 +31,7 @@ class RISE(BlackBoxXAIMethod):
def __init__(
self,
model: ov.Model,
postprocess_fn: Callable[[OVDict], np.ndarray],
postprocess_fn: Callable[[Mapping], np.ndarray],
preprocess_fn: Callable[[np.ndarray], np.ndarray] = IdentityPreprocessFN(),
device_name: str = "CPU",
prepare_model: bool = True,
Expand Down
9 changes: 4 additions & 5 deletions openvino_xai/methods/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
# SPDX-License-Identifier: Apache-2.0

from abc import ABC, abstractmethod
from typing import Callable, List
from typing import Callable, List, Mapping

import numpy as np
import openvino.runtime as ov
from openvino.runtime.utils.data_helpers.wrappers import OVDict

from openvino_xai.common.parameters import Method, Task
from openvino_xai.common.utils import IdentityPreprocessFN, logger
Expand Down Expand Up @@ -183,7 +182,7 @@ def create_method(
cls,
task: Task,
model: ov.Model,
postprocess_fn: Callable[[OVDict], np.ndarray],
postprocess_fn: Callable[[Mapping], np.ndarray],
preprocess_fn: Callable[[np.ndarray], np.ndarray] = IdentityPreprocessFN(),
device_name: str = "CPU",
**kwargs,
Expand All @@ -197,7 +196,7 @@ def create_method(
@staticmethod
def create_classification_method(
model: ov.Model,
postprocess_fn: Callable[[OVDict], np.ndarray],
postprocess_fn: Callable[[Mapping], np.ndarray],
preprocess_fn: Callable[[np.ndarray], np.ndarray] = IdentityPreprocessFN(),
device_name: str = "CPU",
**kwargs,
Expand All @@ -207,7 +206,7 @@ def create_classification_method(
:param model: OV IR model.
:type model: ov.Model
:param postprocess_fn: Preprocessing function that extract scores from IR model output.
:type postprocess_fn: Callable[[OVDict], np.ndarray]
:type postprocess_fn: Callable[[Mapping], np.ndarray]
:param preprocess_fn: Preprocessing function, identity function by default
(assume input images are already preprocessed by user).
:type preprocess_fn: Callable[[np.ndarray], np.ndarray]
Expand Down
2 changes: 1 addition & 1 deletion tests/intg/test_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import cv2
import numpy as np
import openvino.runtime as ov
import openvino as ov
import pytest

import openvino_xai.api.api as xai
Expand Down
2 changes: 1 addition & 1 deletion tests/intg/test_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import addict
import cv2
import numpy as np
import openvino.runtime as ov
import openvino as ov
import pytest

from openvino_xai.common.parameters import Method, Task
Expand Down
2 changes: 1 addition & 1 deletion tests/perf/test_performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import cv2
import numpy as np
import openvino.runtime as ov
import openvino as ov
import pandas as pd
import pytest

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/common/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from pathlib import Path

import openvino.runtime as ov
import openvino as ov

from openvino_xai.api.api import insert_xai
from openvino_xai.common.parameters import Task
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/explanation/test_explainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import cv2
import numpy as np
import openvino.runtime as ov
import openvino as ov
import pytest

from openvino_xai.api.api import insert_xai
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/insertion/test_model_parser.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (C) 2023-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

import openvino.runtime as ov
import openvino as ov
import pytest

from openvino_xai.common.utils import retrieve_otx_model
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/methods/black_box/test_black_box_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import cv2
import numpy as np
import openvino.runtime as ov
import openvino as ov
import pytest

from openvino_xai.common.utils import retrieve_otx_model
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/methods/white_box/test_create_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from pathlib import Path

import openvino.runtime as ov
import openvino as ov
import pytest

from openvino_xai.common.parameters import Method, Task
Expand Down