Skip to content

Commit

Permalink
Update openvino to 2024 ver
Browse files Browse the repository at this point in the history
  • Loading branch information
SamSamhuns committed Aug 7, 2024
1 parent b489b62 commit 2c59e8c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 31 deletions.
54 changes: 25 additions & 29 deletions face_detection_and_extraction/modules/openvino/model.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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


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,
Expand All @@ -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)

Expand All @@ -49,41 +47,39 @@ 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]]


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,
xml_path: str,
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)

Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 2c59e8c

Please sign in to comment.