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

🐞 Fix inferencer in Gradio #332

Merged
merged 3 commits into from
May 25, 2022
Merged
Changes from 2 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
40 changes: 27 additions & 13 deletions tools/inference_gradio.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
from argparse import ArgumentParser, Namespace
from importlib import import_module
from pathlib import Path
from typing import Tuple, Union
from typing import Optional, Tuple

import gradio as gr
import gradio.inputs
import gradio.outputs
import numpy as np
from omegaconf import DictConfig, ListConfig
from skimage.segmentation import mark_boundaries

from anomalib.config import get_configurable_parameters
Expand Down Expand Up @@ -46,6 +45,12 @@ def infer(
def get_args() -> Namespace:
"""Get command line arguments.

Example:

>>> python tools/inference_gradio.py \
--config ./anomalib/models/padim/config.yaml \
--weight_path ./results/padim/mvtec/bottle/weights/model.ckpt

Returns:
Namespace: List of arguments.
"""
Expand All @@ -69,26 +74,35 @@ def get_args() -> Namespace:
return args


def get_inferencer(config_path: Path, weight_path: Path, meta_data_path: Path) -> Inferencer:
"""Parse args and open inferencer."""
config = get_configurable_parameters(config_path)
def get_inferencer(config_path: Path, weight_path: Path, meta_data_path: Optional[Path] = None) -> Inferencer:
"""Parse args and open inferencer.

Args:
config_path (Path): Path to model configuration file or the name of the model.
weight_path (Path): Path to model weights.
meta_data_path (Optional[Path], optional): Metadata is required for OpenVINO models. Defaults to None.

Raises:
ValueError: If unsupported model weight is passed.

Returns:
Inferencer: Torch or OpenVINO inferencer.
"""
config = get_configurable_parameters(config_path=config_path)

# Get the inferencer. We use .ckpt extension for Torch models and (onnx, bin)
# for the openvino models.
extension = weight_path.suffix
inferencer: Inferencer
if extension in (".ckpt"):
module = import_module("anomalib.deploy.inferencers.torch")
TorchInferencer = getattr(module, "TorchInferencer") # pylint: disable=invalid-name
inferencer = TorchInferencer(
config=config, model_source=weight_path, meta_data_path=meta_data
)
samet-akcay marked this conversation as resolved.
Show resolved Hide resolved
TorchInferencer = getattr(module, "TorchInferencer")
inferencer = TorchInferencer(config=config, model_source=weight_path, meta_data_path=meta_data_path)

elif extension in (".onnx", ".bin", ".xml"):
module = import_module("anomalib.deploy.inferencers.openvino")
OpenVINOInferencer = getattr(module, "OpenVINOInferencer") # pylint: disable=invalid-name
inferencer = OpenVINOInferencer(
config=config, path=weight_path, meta_data_path=meta_data
)
OpenVINOInferencer = getattr(module, "OpenVINOInferencer")
inferencer = OpenVINOInferencer(config=config, path=weight_path, meta_data_path=meta_data_path)

else:
raise ValueError(
Expand Down