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 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
48 changes: 32 additions & 16 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,13 +45,21 @@ def infer(
def get_args() -> Namespace:
"""Get command line arguments.

Example:

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

Returns:
Namespace: List of arguments.
"""
parser = ArgumentParser()
parser.add_argument("--config", type=Path, required=True, help="Path to a model config file")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could keep config to be consistent with other entrypoints?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure what to call it. We can either pass the model config or the model name now. If I switch back to config should I drop calling inferencer with just model name?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--config would be inline with the new PL CLI, so I would prefer that.

If I switch back to config should I drop calling inferencer with just model name?

Not sure if I get this part

Copy link
Collaborator Author

@ashwinvaidya17 ashwinvaidya17 May 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I mean is that initially with config parameter we had to pass the yaml file to the inferencer. I changed it to model so that we can either pass yaml or only the model name. So, I wasn't sure sure what to call this parameter. If I change it back to config then passing just the model name might not match with the parameter name. In which case we can drop passing only the model name and keep yaml as the only option. It might be better in some sense as it will force people to ensure that their train config matches with the config they use for inference. Otherwise inferencer might pick up the default config with just the model name which might not match with the path for config that was used to train with.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Passing only config would ensure the right config file is passed. Otherwise, it would be the default config, which may not be the same as the one that is used to train the model.

parser.add_argument("--config_path", type=Path, required=True, help="Path to a model config file")
parser.add_argument("--weight_path", type=Path, required=True, help="Path to a model weights")
parser.add_argument("--meta_data", type=Path, required=False, help="Path to JSON file containing the metadata.")
parser.add_argument(
"--meta_data_path", type=Path, required=False, help="Path to JSON file containing the metadata."
)

parser.add_argument(
"--threshold",
Expand All @@ -69,26 +76,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 All @@ -102,7 +118,7 @@ def get_inferencer(config_path: Path, weight_path: Path, meta_data_path: Path) -
if __name__ == "__main__":
session_args = get_args()

gradio_inferencer = get_inferencer(session_args.config, session_args.weight_path, session_args.meta_data)
gradio_inferencer = get_inferencer(session_args.config_path, session_args.weight_path, session_args.meta_data_path)

interface = gr.Interface(
fn=lambda image, threshold: infer(image, gradio_inferencer, threshold),
Expand Down