Skip to content

Commit 3f561eb

Browse files
authored
AC: add support OV2.0 in custom evaluators (#2938)
1 parent e828f2b commit 3f561eb

23 files changed

+1972
-756
lines changed

tools/accuracy_checker/openvino/tools/accuracy_checker/evaluators/custom_evaluators/asr_custom_encoder_decoder_joint.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import math
1818
import numpy as np
1919
from .asr_encoder_prediction_joint_evaluator import ASREvaluator
20-
from .base_models import create_model, BaseCascadeModel, BaseDLSDKModel, BaseONNXModel
20+
from .base_models import create_model, BaseCascadeModel, BaseDLSDKModel, BaseONNXModel, BaseOpenVINOModel
2121
from ...adapters import create_adapter
2222
from ...utils import generate_layer_name, contains_all, contains_any
2323
from ...config import ConfigError
@@ -261,27 +261,75 @@ def predict(self, identifiers, input_data):
261261
raise NotImplementedError
262262

263263

264+
class CommonOpenVINOModel(BaseOpenVINOModel):
265+
def __init__(self, network_info, launcher, suffix=None, delayed_model_loading=False):
266+
self.select_inputs_outputs(network_info)
267+
self.reset()
268+
super().__init__(network_info, launcher, suffix, delayed_model_loading)
269+
270+
def set_input_and_output(self):
271+
inputs = self.exec_network.inputs if self.exec_network is not None else self.network.inputs
272+
input_blob = next(iter(inputs)).get_node().friendly_name
273+
with_prefix = input_blob.startswith(self.default_model_suffix)
274+
if with_prefix != self.with_prefix:
275+
self.input_names = [
276+
generate_layer_name(
277+
inp_name, self.default_model_suffix + '_', with_prefix) for inp_name in self.input_names
278+
]
279+
self.output_names = [
280+
generate_layer_name(
281+
out_name, self.default_model_suffix + '_', with_prefix) for out_name in self.output_names
282+
]
283+
self.with_prefix = with_prefix
284+
285+
286+
def predict(self, identifiers, input_data):
287+
raise NotImplementedError
288+
289+
290+
264291
class DLSDKEncoder(Encoder, CommonDLSDKModel):
265292
def __init__(self, network_info, launcher, suffix=None, delayed_model_loading=False):
266293
self.default_inputs = ['input_0', 'input_1', 'input_2']
267294
self.default_outputs = ['output_0', 'output_1', 'output_2']
268295
super().__init__(network_info, launcher, suffix, delayed_model_loading)
269296

270297

298+
class OVEncoder(Encoder, CommonOpenVINOModel):
299+
def __init__(self, network_info, launcher, suffix=None, delayed_model_loading=False):
300+
self.default_inputs = ['input_0', 'input_1', 'input_2']
301+
self.default_outputs = ['output_0/sink_port_0', 'output_1/sink_port_0', 'output_2/sink_port_0']
302+
super().__init__(network_info, launcher, suffix, delayed_model_loading)
303+
304+
271305
class DLSDKDecoder(Decoder, CommonDLSDKModel):
272306
def __init__(self, network_info, launcher, suffix=None, delayed_model_loading=False):
273307
self.default_inputs = ['input_0', 'input_1', 'input_2']
274308
self.default_outputs = ['output_0', 'output_1', 'output_2']
275309
super().__init__(network_info, launcher, suffix, delayed_model_loading)
276310

277311

312+
class OVDecoder(Decoder, CommonOpenVINOModel):
313+
def __init__(self, network_info, launcher, suffix=None, delayed_model_loading=False):
314+
self.default_inputs = ['input_0', 'input_1', 'input_2']
315+
self.default_outputs = ['output_0/sink_port_0', 'output_1/sink_port_0', 'output_2/sink_port_0']
316+
super().__init__(network_info, launcher, suffix, delayed_model_loading)
317+
318+
278319
class DLSDKJoint(Joint, CommonDLSDKModel):
279320
def __init__(self, network_info, launcher, suffix=None, delayed_model_loading=False):
280321
self.default_inputs = ['0', '1']
281322
self.default_outputs = ['8']
282323
super().__init__(network_info, launcher, suffix, delayed_model_loading)
283324

284325

326+
class OVJoint(Joint, CommonOpenVINOModel):
327+
def __init__(self, network_info, launcher, suffix=None, delayed_model_loading=False):
328+
self.default_inputs = ['0', '1']
329+
self.default_outputs = ['8/sink_port']
330+
super().__init__(network_info, launcher, suffix, delayed_model_loading)
331+
332+
285333
class CommonONNXModel(BaseONNXModel):
286334
def __init__(self, network_info, launcher, suffix=None, delayed_model_loading=False):
287335
self.select_inputs_outputs(network_info)
@@ -352,14 +400,17 @@ def __init__(self, network_info, adapter_config, launcher, models_args, is_blob,
352400
raise ConfigError('network_info should contain encoder, prediction and joint fields')
353401
self._decoder_mapping = {
354402
'dlsdk': DLSDKDecoder,
403+
'openvino': OVDecoder,
355404
'onnx_runtime': ONNXDecoder
356405
}
357406
self._encoder_mapping = {
358407
'dlsdk': DLSDKEncoder,
408+
'openvino': OVEncoder,
359409
'onnx_runtime': ONNXEncoder
360410
}
361411
self._joint_mapping = {
362412
'dlsdk': DLSDKJoint,
413+
'openvino': OVJoint,
363414
'onnx_runtime': ONNXJoint
364415
}
365416
self.encoder = create_model(network_info['encoder'], launcher, self._encoder_mapping, 'encoder',

tools/accuracy_checker/openvino/tools/accuracy_checker/evaluators/custom_evaluators/asr_encoder_decoder_evaluator.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121

2222

2323
from .base_custom_evaluator import BaseCustomEvaluator
24-
from .base_models import BaseCascadeModel, BaseDLSDKModel, BaseONNXModel, create_model, create_encoder
24+
from .base_models import (
25+
BaseCascadeModel, BaseDLSDKModel, BaseONNXModel, BaseOpenVINOModel,
26+
create_model, create_encoder)
2527
from ...adapters import create_adapter
2628
from ...config import ConfigError
2729
from ...utils import contains_all, contains_any, extract_image_representations, read_pickle
@@ -79,10 +81,12 @@ def __init__(self, network_info, launcher, models_args, is_blob, delayed_model_l
7981
raise ConfigError('network_info should contain encoder and decoder fields')
8082
self._decoder_mapping = {
8183
'dlsdk': DecoderDLSDKModel,
84+
'openvino': DecoderOVModel,
8285
'onnx_runtime': DecoderONNXModel
8386
}
8487
self._encoder_mapping = {
8588
'dlsdk': EncoderDLSDKModel,
89+
'openvino': EncoderOVModel,
8690
'onnx_runtime': EncoderONNXModel,
8791
'dummy': DummyEncoder
8892
}
@@ -138,6 +142,13 @@ def predict(self, identifiers, input_data):
138142
return results, results[self.output_blob]
139143

140144

145+
class EncoderOVModel(BaseOpenVINOModel):
146+
def predict(self, identifiers, input_data):
147+
input_data = self.fit_to_input(input_data)
148+
results = self.infer(input_data)
149+
return results, results[self.output_blob]
150+
151+
141152
class DecoderDLSDKModel(BaseDLSDKModel):
142153
def __init__(self, network_info, launcher, suffix=None, delayed_model_loading=False):
143154
self.adapter = create_adapter(network_info.get('adapter', 'ctc_greedy_decoder'))
@@ -156,6 +167,24 @@ def set_input_and_output(self):
156167
self.adapter.output_blob = self.output_blob
157168

158169

170+
class DecoderOVModel(BaseOpenVINOModel):
171+
def __init__(self, network_info, launcher, suffix=None, delayed_model_loading=False):
172+
self.adapter = create_adapter(network_info.get('adapter', 'ctc_greedy_decoder'))
173+
super().__init__(network_info, launcher, suffix, delayed_model_loading)
174+
self.adapter.output_blob = self.output_blob
175+
176+
def predict(self, identifiers, input_data):
177+
feed_dict = self.fit_to_input(input_data)
178+
results = self.infer(feed_dict)
179+
result = self.adapter.process([results], identifiers, [{}])
180+
181+
return results, result
182+
183+
def set_input_and_output(self):
184+
super().set_input_and_output()
185+
self.adapter.output_blob = self.output_blob
186+
187+
159188
class EncoderONNXModel(BaseONNXModel):
160189
def predict(self, identifiers, input_data):
161190
results = self.inference_session.run((self.output_blob.name, ), self.fit_to_input(input_data))

tools/accuracy_checker/openvino/tools/accuracy_checker/evaluators/custom_evaluators/asr_encoder_prediction_joint_evaluator.py

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121

2222
from ...adapters import create_adapter
2323
from ...config import ConfigError
24-
from ...utils import contains_all, contains_any, read_pickle
24+
from ...utils import contains_all, contains_any, read_pickle, parse_partial_shape
2525
from .asr_encoder_decoder_evaluator import AutomaticSpeechRecognitionEvaluator
26-
from .base_models import BaseCascadeModel, BaseDLSDKModel, BaseONNXModel, create_model, create_encoder
26+
from .base_models import (
27+
BaseCascadeModel, BaseDLSDKModel, BaseOpenVINOModel, BaseONNXModel, create_model, create_encoder
28+
)
2729

2830

2931
class ASREvaluator(AutomaticSpeechRecognitionEvaluator):
@@ -59,15 +61,18 @@ def __init__(self, network_info, launcher, models_args, is_blob, adapter_info, d
5961
raise ConfigError('network_info should contain encoder, prediction and joint fields')
6062
self._encoder_mapping = {
6163
'dlsdk': EncoderDLSDKModel,
64+
'openvino': EncoderOVMOdel,
6265
'onnx_runtime': EncoderONNXModel,
6366
'dummy': DummyEncoder
6467
}
6568
self._prediction_mapping = {
6669
'dlsdk': PredictionDLSDKModel,
70+
'openvino': PredictionOVModel,
6771
'onnx_runtime': PredictionONNXModel
6872
}
6973
self._joint_mapping = {
7074
'dlsdk': JointDLSDKModel,
75+
'openvino': JointOVModel,
7176
'onnx_runtime': JointONNXModel
7277
}
7378
self.encoder = create_encoder(network_info['encoder'], launcher, self._encoder_mapping, delayed_model_loading)
@@ -249,27 +254,105 @@ def set_input_and_output(self):
249254
)
250255

251256

257+
class CommonOVModel(BaseOpenVINOModel):
258+
def __init__(self, network_info, launcher, suffix=None, delayed_model_loading=False):
259+
self.input_layers = network_info.get('inputs', self.default_input_layers)
260+
self.output_layers = network_info.get('outputs', self.default_output_layers)
261+
if len(self.input_layers) == 1:
262+
self.input_blob = self.input_layers[0]
263+
if len(self.output_layers) == 1:
264+
self.output_blob = self.output_layers[0]
265+
super().__init__(network_info, launcher, suffix, delayed_model_loading)
266+
267+
def predict(self, identifiers, input_data, callback=None):
268+
input_data = self.fit_to_input(input_data)
269+
results = self.infer(input_data)
270+
return results, results[self.output_blob]
271+
272+
def fit_to_input(self, input_data):
273+
if isinstance(input_data, dict):
274+
fitted = {}
275+
for input_blob in self.inputs.keys():
276+
fitted.update(self.fit_one_input(input_blob, input_data[input_blob]))
277+
else:
278+
fitted = self.fit_one_input(self.input_blob, input_data)
279+
return fitted
280+
281+
def fit_one_input(self, input_blob, input_data):
282+
if (input_blob in self.dynamic_inputs or parse_partial_shape(
283+
self.inputs[input_blob].get_partial_shape()) != np.shape(input_data)):
284+
self._reshape_input({input_blob: np.shape(input_data)})
285+
286+
return {input_blob: np.array(input_data)}
287+
288+
def set_input_and_output(self):
289+
input_blob = next(iter(self.inputs))
290+
with_prefix = input_blob.startswith(self.default_model_suffix)
291+
if self.input_blob is None or with_prefix != self.with_prefix:
292+
if self.output_blob is None:
293+
output_blob = next(iter(self.exec_network.outputs)).get_node().friendly_name
294+
else:
295+
output_blob = (
296+
'_'.join([self.default_model_suffix, self.output_blob])
297+
if with_prefix else self.output_blob.split(self.default_model_suffix + '_')[-1]
298+
)
299+
self.input_blob = input_blob
300+
self.output_blob = output_blob
301+
self.with_prefix = with_prefix
302+
for idx, inp in enumerate(self.input_layers):
303+
self.input_layers[idx] = (
304+
'_'.join([self.default_model_suffix, inp])
305+
if with_prefix else inp.split(self.default_model_suffix)[-1]
306+
)
307+
for idx, out in enumerate(self.output_layers):
308+
self.output_layers[idx] = (
309+
'_'.join([self.default_model_suffix, out])
310+
if with_prefix else out.split(self.default_model_suffix)[-1]
311+
)
312+
313+
252314
class EncoderDLSDKModel(CommonDLSDKModel):
253315
def __init__(self, network_info, launcher, suffix=None, delayed_model_loading=False):
254316
self.default_input_layers = []
255317
self.default_output_layers = ['472']
256318
super().__init__(network_info, launcher, suffix, delayed_model_loading)
257319

258320

321+
class EncoderOVMOdel(CommonOVModel):
322+
def __init__(self, network_info, launcher, suffix=None, delayed_model_loading=False):
323+
self.default_input_layers = []
324+
self.default_output_layers = ['472/sink_port_0']
325+
super().__init__(network_info, launcher, suffix, delayed_model_loading)
326+
327+
259328
class PredictionDLSDKModel(CommonDLSDKModel):
260329
def __init__(self, network_info, launcher, suffix=None, delayed_model_loading=False):
261330
self.default_input_layers = ['input.1', '1', '2']
262331
self.default_output_layers = ['151', '152', '153']
263332
super().__init__(network_info, launcher, suffix, delayed_model_loading)
264333

265334

335+
class PredictionOVModel(CommonOVModel):
336+
def __init__(self, network_info, launcher, suffix=None, delayed_model_loading=False):
337+
self.default_input_layers = ['input.1', '1', '2']
338+
self.default_output_layers = ['151/sink_port_0', '152/sink_port_0', '153/sink_port_0']
339+
super().__init__(network_info, launcher, suffix, delayed_model_loading)
340+
341+
266342
class JointDLSDKModel(CommonDLSDKModel):
267343
def __init__(self, network_info, launcher, suffix=None, delayed_model_loading=False):
268344
self.default_input_layers = ['0', '1']
269345
self.default_output_layers = []
270346
super().__init__(network_info, launcher, suffix, delayed_model_loading)
271347

272348

349+
class JointOVModel(CommonOVModel):
350+
def __init__(self, network_info, launcher, suffix=None, delayed_model_loading=False):
351+
self.default_input_layers = ['0', '1']
352+
self.default_output_layers = []
353+
super().__init__(network_info, launcher, suffix, delayed_model_loading)
354+
355+
273356
class CommonONNXModel(BaseONNXModel):
274357
def predict(self, identifiers, input_data, callback=None):
275358
fitted = self.fit_to_input(input_data)

0 commit comments

Comments
 (0)