mirror of
https://github.com/hanzoai/anomalib.git
synced 2026-10-30 18:48:11 +00:00
The previous commit was meant to remove .github/workflows and removed every tracked path instead — 892 files, the whole repo. This puts back the 874 that were never in scope; the eighteen workflow files stay gone, for the reasons that commit gives. Content is byte-identical to 9d2aec9 outside .github/workflows.
88 lines
2.7 KiB
Python
88 lines
2.7 KiB
Python
"""Test Gradio inference entrypoint script."""
|
|
|
|
# Copyright (C) 2023-2024 Intel Corporation
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
import sys
|
|
from collections.abc import Callable
|
|
from importlib.util import find_spec
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from anomalib import TaskType
|
|
from anomalib.deploy import OpenVINOInferencer, TorchInferencer
|
|
from anomalib.models import Padim
|
|
|
|
sys.path.append("tools/inference")
|
|
|
|
|
|
class TestGradioInferenceEntrypoint:
|
|
"""This tests whether the entrypoints run without errors without quantitative measure of the outputs.
|
|
|
|
Note: This does not launch the gradio server. It only checks if the right inferencer is called.
|
|
"""
|
|
|
|
@pytest.fixture()
|
|
@staticmethod
|
|
def get_functions() -> tuple[Callable, Callable]:
|
|
"""Get functions from Gradio_inference.py."""
|
|
if find_spec("gradio_inference") is not None:
|
|
from tools.inference.gradio_inference import get_inferencer, get_parser
|
|
else:
|
|
msg = "Unable to import gradio_inference.py for testing"
|
|
raise ImportError(msg)
|
|
return get_parser, get_inferencer
|
|
|
|
@staticmethod
|
|
def test_torch_inference(
|
|
get_functions: tuple[Callable, Callable],
|
|
ckpt_path: Callable[[str], Path],
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""Test gradio_inference.py."""
|
|
# Set TRUST_REMOTE_CODE environment variable for the test
|
|
monkeypatch.setenv("TRUST_REMOTE_CODE", "1")
|
|
|
|
_ckpt_path = ckpt_path("Padim")
|
|
parser, inferencer = get_functions
|
|
model = Padim.load_from_checkpoint(_ckpt_path)
|
|
|
|
# export torch model
|
|
model.to_torch(
|
|
export_root=_ckpt_path.parent.parent.parent,
|
|
)
|
|
|
|
arguments = parser().parse_args(
|
|
[
|
|
"--weights",
|
|
str(_ckpt_path.parent.parent) + "/torch/model.pt",
|
|
],
|
|
)
|
|
assert isinstance(inferencer(arguments.weights), TorchInferencer)
|
|
|
|
@staticmethod
|
|
def test_openvino_inference(
|
|
get_functions: tuple[Callable, Callable],
|
|
ckpt_path: Callable[[str], Path],
|
|
) -> None:
|
|
"""Test gradio_inference.py."""
|
|
_ckpt_path = ckpt_path("Padim")
|
|
parser, inferencer = get_functions
|
|
model = Padim.load_from_checkpoint(_ckpt_path)
|
|
|
|
# export OpenVINO model
|
|
model.to_openvino(
|
|
export_root=_ckpt_path.parent.parent.parent,
|
|
ov_args={},
|
|
task=TaskType.SEGMENTATION,
|
|
)
|
|
|
|
arguments = parser().parse_args(
|
|
[
|
|
"--weights",
|
|
str(_ckpt_path.parent.parent) + "/openvino/model.bin",
|
|
],
|
|
)
|
|
assert isinstance(inferencer(arguments.weights), OpenVINOInferencer)
|