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

make device configurable in OpenVINO inference #755

Merged
merged 2 commits into from
Dec 1, 2022
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
4 changes: 3 additions & 1 deletion anomalib/deploy/inferencers/openvino_inferencer.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def __init__(
config: Union[str, Path, DictConfig, ListConfig],
path: Union[str, Path, Tuple[bytes, bytes]],
meta_data_path: Union[str, Path] = None,
device: Optional[str] = "CPU",
):
# Check and load the configuration
if isinstance(config, (str, Path)):
Expand All @@ -46,6 +47,7 @@ def __init__(
else:
raise ValueError(f"Unknown config type {type(config)}")

self.device = device
self.input_blob, self.output_blob, self.network = self.load_model(path)
self.meta_data = super()._load_meta_data(meta_data_path)

Expand Down Expand Up @@ -80,7 +82,7 @@ def load_model(self, path: Union[str, Path, Tuple[bytes, bytes]]):

input_blob = next(iter(network.input_info))
output_blob = next(iter(network.outputs))
executable_network = ie_core.load_network(network=network, device_name="CPU")
executable_network = ie_core.load_network(network=network, device_name=self.device)

return input_blob, output_blob, executable_network

Expand Down
2 changes: 2 additions & 0 deletions docs/source/tutorials/inference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ To run OpenVINO inference, first make sure that your model has been exported to
| meta_data | True | Path to the JSON file containing the model's meta data (e.g. normalization |
| | | parameters and anomaly score threshold). |
+-----------+----------+--------------------------------------------------------------------------------------+
| device | False | Device on which OpenVINO will perform the computations (``CPU``, ``GPU`` or ``VPU``) |
+-----------+----------+--------------------------------------------------------------------------------------+

For correct inference results, the ``meta_data`` argument should be specified and point to the ``meta_data.json`` file that was generated when exporting the OpenVINO IR model. The file is stored in the same folder as the ``.xml`` and ``.bin`` files of the model.

Expand Down
14 changes: 12 additions & 2 deletions tools/inference/openvino_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ def get_args() -> Namespace:
default="classification",
choices=["classification", "segmentation"],
)
parser.add_argument(
"--device",
type=str,
required=False,
help="Hardware device on which the model will be deployed",
default="CPU",
choices=["CPU", "GPU", "VPU"],
)
parser.add_argument(
"--visualization_mode",
type=str,
Expand Down Expand Up @@ -71,7 +79,9 @@ def infer() -> None:
args = get_args()

# Get the inferencer.
inferencer = OpenVINOInferencer(config=args.config, path=args.weights, meta_data_path=args.meta_data)
inferencer = OpenVINOInferencer(
config=args.config, path=args.weights, meta_data_path=args.meta_data, device=args.device
)
visualizer = Visualizer(mode=args.visualization_mode, task=args.task)

filenames = get_image_filenames(path=args.input)
Expand All @@ -86,7 +96,7 @@ def infer() -> None:
)

if args.output:
file_path = generate_output_image_filename(input_path=args.input, output_path=args.output)
file_path = generate_output_image_filename(input_path=filename, output_path=args.output)
visualizer.save(file_path=file_path, image=output)

# Show the image in case the flag is set by the user.
Expand Down