Source code for anomalib.deploy.inferencers.openvino
"""This module contains inference-related abstract class and its Torch and OpenVINO implementations."""# Copyright (C) 2020 Intel Corporation## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions# and limitations under the License.fromimportlib.utilimportfind_specfrompathlibimportPathfromtypingimportDict,Optional,Tuple,Unionimportcv2importnumpyasnpfromomegaconfimportDictConfig,ListConfigfromanomalib.pre_processingimportPreProcessorfrom.baseimportInferenceriffind_spec("openvino")isnotNone:fromopenvino.inference_engineimport(# type: ignore # pylint: disable=no-name-in-moduleIECore,)
[docs]classOpenVINOInferencer(Inferencer):"""OpenVINO implementation for the inference. Args: config (DictConfig): Configurable parameters that are used during the training stage. path (Union[str, Path]): Path to the openvino onnx, xml or bin file. meta_data_path (Union[str, Path], optional): Path to metadata file. Defaults to None. """def__init__(self,config:Union[DictConfig,ListConfig],path:Union[str,Path,Tuple[bytes,bytes]],meta_data_path:Union[str,Path]=None,):self.config=configself.input_blob,self.output_blob,self.network=self.load_model(path)self.meta_data=super()._load_meta_data(meta_data_path)
[docs]defload_model(self,path:Union[str,Path,Tuple[bytes,bytes]]):"""Load the OpenVINO model. Args: path (Union[str, Path, Tuple[bytes, bytes]]): Path to the onnx or xml and bin files or tuple of .xml and .bin data as bytes. Returns: [Tuple[str, str, ExecutableNetwork]]: Input and Output blob names together with the Executable network. """ie_core=IECore()# If tuple of bytes is passedifisinstance(path,tuple):network=ie_core.read_network(model=path[0],weights=path[1],init_from_buffer=True)else:path=pathifisinstance(path,Path)elsePath(path)ifpath.suffixin(".bin",".xml"):ifpath.suffix==".bin":bin_path,xml_path=path,path.with_suffix(".xml")elifpath.suffix==".xml":xml_path,bin_path=path,path.with_suffix(".bin")network=ie_core.read_network(xml_path,bin_path)elifpath.suffix==".onnx":network=ie_core.read_network(path)else:raiseValueError(f"Path must be .onnx, .bin or .xml file. Got {path.suffix}")input_blob=next(iter(network.input_info))output_blob=next(iter(network.outputs))executable_network=ie_core.load_network(network=network,device_name="CPU")returninput_blob,output_blob,executable_network
[docs]defpre_process(self,image:np.ndarray)->np.ndarray:"""Pre process the input image by applying transformations. Args: image (np.ndarray): Input image. Returns: np.ndarray: pre-processed image. """config=self.config.transformif"transform"inself.config.keys()elseNoneimage_size=tuple(self.config.dataset.image_size)pre_processor=PreProcessor(config,image_size)processed_image=pre_processor(image=image)["image"]iflen(processed_image.shape)==3:processed_image=np.expand_dims(processed_image,axis=0)ifprocessed_image.shape[-1]==3:processed_image=processed_image.transpose(0,3,1,2)returnprocessed_image
[docs]defforward(self,image:np.ndarray)->np.ndarray:"""Forward-Pass input tensor to the model. Args: image (np.ndarray): Input tensor. Returns: np.ndarray: Output predictions. """returnself.network.infer(inputs={self.input_blob:image})
[docs]defpost_process(self,predictions:np.ndarray,meta_data:Optional[Union[Dict,DictConfig]]=None)->Tuple[np.ndarray,float]:"""Post process the output predictions. Args: predictions (np.ndarray): Raw output predicted by the model. meta_data (Dict, optional): Meta data. Post-processing step sometimes requires additional meta data such as image shape. This variable comprises such info. Defaults to None. Returns: np.ndarray: Post processed predictions that are ready to be visualized. """ifmeta_dataisNone:meta_data=self.meta_datapredictions=predictions[self.output_blob]anomaly_map=predictions.squeeze()pred_score=anomaly_map.reshape(-1).max()anomaly_map,pred_score=self._normalize(anomaly_map,pred_score,meta_data)if"image_shape"inmeta_dataandanomaly_map.shape!=meta_data["image_shape"]:anomaly_map=cv2.resize(anomaly_map,meta_data["image_shape"])returnanomaly_map,float(pred_score)