From 2c59e8ca43fdc04246e420b11d9e1fd6d547439b Mon Sep 17 00:00:00 2001 From: SamSamhuns Date: Wed, 7 Aug 2024 14:51:14 +0400 Subject: [PATCH] Update openvino to 2024 ver --- .../modules/openvino/model.py | 54 +++++++++---------- .../tests/image_tests/test_openvino.py | 4 +- 2 files changed, 27 insertions(+), 31 deletions(-) diff --git a/face_detection_and_extraction/modules/openvino/model.py b/face_detection_and_extraction/modules/openvino/model.py index 8e5d3c1..a86ac1c 100644 --- a/face_detection_and_extraction/modules/openvino/model.py +++ b/face_detection_and_extraction/modules/openvino/model.py @@ -1,5 +1,5 @@ import numpy as np -from openvino.inference_engine import IECore +import openvino as ov from modules.models.base import Model from modules.utils.image import pad_resize_image @@ -7,7 +7,7 @@ class OVModel(Model): - __slots__ = ["OVExec", "in_layer", "out_layer", "in_shape", "out_shape"] + __slots__ = ["compiled_model", "out_layer_ir", "in_shape", "out_shape"] def __init__( self, @@ -17,26 +17,24 @@ def __init__( bbox_area_thres: float, device: str = "CPU", verbose: bool = False): - OVIE = IECore() - OVNet = OVIE.read_network(model=xml_path, weights=bin_path) - - self.OVExec = OVIE.load_network(network=OVNet, device_name=device) + core = ov.Core() + model = core.read_model(model=xml_path, weights=bin_path) + self.compiled_model = core.compile_model(model=model, device_name=device) self.det_thres = det_thres self.bbox_area_thres = bbox_area_thres # get input/output layer information - self.in_layer = next(iter(OVNet.input_info)) - self.out_layer = next(iter(OVNet.outputs)) - self.in_shape = OVNet.input_info[self.in_layer].input_data.shape - self.out_shape = OVNet.outputs[self.out_layer].shape + self.out_layer_ir = self.compiled_model.output(model.outputs[0].names.pop()) + self.in_shape = model.inputs[0].shape + self.out_shape = model.outputs[0].shape Model.__init__(self, self.in_shape[2:][::-1], det_thres, bbox_area_thres) if verbose: # print model input/output info and shapes - print("Available Devices: ", OVIE.available_devices) - print("Input Layer: ", self.in_layer) - print("Output Layer: ", self.out_layer) + print("Available Devices: ", core.available_devices) + print("Input Layer: ", model.inputs) + print("Output Layer: ", model.outputs) print("Input Shape: ", self.in_shape) print("Output Shape: ", self.out_shape) @@ -49,8 +47,8 @@ def __call__( resized = resized.transpose((2, 0, 1)) # HWC to CHW in_image = resized.reshape((N, C, H, W)) # openVINO expects BGR format - detections = self.OVExec.infer(inputs={self.in_layer: in_image}) - detections = detections[self.out_layer][0][0] + detections = self.compiled_model([in_image]) + detections = detections[self.out_layer_ir][0][0] # reorder dets to have [xmin, ymin, xmax, ymax, conf] format # from a [_, _, conf, xmin, ymin, xmax, ymax] fmt return detections[:, [3, 4, 5, 6, 2]] @@ -58,7 +56,7 @@ def __call__( class OVFeatModel(Model): - __slots__ = ["OVExec", "in_layer", "out_layer", "in_shape", "out_shape"] + __slots__ = ["compiled_model", "out_layer_ir", "in_shape", "out_shape"] def __init__( self, @@ -66,24 +64,22 @@ def __init__( bin_path: str, device: str = "CPU", verbose: bool = False): - OVIE = IECore() - OVNet = OVIE.read_network(model=xml_path, weights=bin_path) - - self.OVExec = OVIE.load_network(network=OVNet, device_name=device) + core = ov.Core() + model = core.read_model(model=xml_path, weights=bin_path) + self.compiled_model = core.compile_model(model=model, device_name=device) # get input/output layer information - self.in_layer = next(iter(OVNet.input_info)) - self.out_layer = next(iter(OVNet.outputs)) - self.in_shape = OVNet.input_info[self.in_layer].input_data.shape - self.out_shape = OVNet.outputs[self.out_layer].shape + self.out_layer_ir = self.compiled_model.output(model.outputs[0].names.pop()) + self.in_shape = model.inputs[0].shape + self.out_shape = model.outputs[0].shape Model.__init__(self, self.in_shape[2:][::-1], det_thres=None, bbox_area_thres=None) if verbose: # print model input/output info and shapes - print("Available Devices: ", OVIE.available_devices) - print("Input Layer: ", self.in_layer) - print("Output Layer: ", self.out_layer) + print("Available Devices: ", core.available_devices) + print("Input Layer: ", model.inputs) + print("Output Layer: ", model.outputs) print("Input Shape: ", self.in_shape) print("Output Shape: ", self.out_shape) @@ -96,6 +92,6 @@ def __call__( resized = resized.transpose((2, 0, 1)) # HWC to CHW in_image = resized.reshape((N, C, H, W)) # openVINO expects BGR format - detections = self.OVExec.infer(inputs={self.in_layer: in_image}) - detections = detections[self.out_layer].squeeze() + detections = self.compiled_model([in_image]) + detections = detections[self.out_layer_ir].squeeze() return detections diff --git a/face_detection_and_extraction/tests/image_tests/test_openvino.py b/face_detection_and_extraction/tests/image_tests/test_openvino.py index e339423..97c12fe 100644 --- a/face_detection_and_extraction/tests/image_tests/test_openvino.py +++ b/face_detection_and_extraction/tests/image_tests/test_openvino.py @@ -36,11 +36,11 @@ def test_3_faces_jpg(mock_openvino_model, mock_3_faces_image): dets, (w, h), (iw, ih), model.det_thres, model.bbox_area_thres) gt_areas = np.array([ - 0.0377225852, 0.0099964741, 0.0082628834 + 0.0099964741, 0.0377225852, 0.0082628834 ]) gt_boxes = np.array([ - [513, 203, 634, 365], [408, 213, 469, 299], + [513, 203, 634, 365], [285, 231, 342, 307], ], dtype=np.float32)