22
22
import warnings
23
23
import numpy as np
24
24
from openvino .runtime import Core , AsyncInferQueue , get_version , PartialShape , Type , Dimension
25
+ from openvino .preprocess import PrePostProcessor
25
26
from .dlsdk_launcher_config import (
26
27
HETERO_KEYWORD , MULTI_DEVICE_KEYWORD , NIREQ_REGEX , VPU_PLUGINS ,
27
28
get_cpu_extension ,
@@ -494,19 +495,24 @@ def _log_versions(self):
494
495
def _create_network (self , input_shapes = None ):
495
496
model_path = Path (self ._model )
496
497
compiled_model = model_path .suffix == '.blob'
498
+ self .out_tensor_name_to_node = {}
497
499
if compiled_model :
498
500
self .network = None
499
501
with open (str (self ._model ), 'rb' ) as f : #pylint:disable=unspecified-encoding
500
502
self .exec_network = self .ie_core .import_model (io .BytesIO (f .read ()), self ._device )
501
503
self .original_outputs = self .exec_network .outputs
502
504
model_batch = self ._get_model_batch_size ()
503
505
self ._batch = model_batch if model_batch is not None else 1
506
+ for out in self .original_outputs :
507
+ if not out .names :
508
+ continue
509
+ for name in out .names :
510
+ self .out_tensor_name_to_node [name ] = out .get_node ().friendly_name
504
511
return
505
512
if self ._weights is None and self ._model .suffix != '.onnx' :
506
513
self ._weights = model_path .parent / (model_path .name .split (model_path .suffix )[0 ] + '.bin' )
507
514
self .network = self .read_network (self ._model , self ._weights )
508
515
self .original_outputs = self .network .outputs
509
- self .out_tensor_name_to_node = {}
510
516
for out in self .original_outputs :
511
517
if not out .names :
512
518
continue
@@ -587,8 +593,10 @@ def load_network(self, network=None, log=False, preprocessing=None):
587
593
self .network = network
588
594
if self .network is not None :
589
595
self .dyn_input_layers , self ._partial_shapes = self .get_dynamic_inputs (self .network )
590
- self .input_to_tensor_name = self .get_input_tensor_name_mapping (self .network )
591
- self .input_to_index = {inp .get_node ().friendly_name : idx for idx , inp in enumerate (self .network .inputs )}
596
+ self .input_to_tensor_name = self .get_input_tensor_name_mapping (
597
+ self .network if self .network is not None else self .exec_network )
598
+ network_inputs = self .network .inputs if self .network is not None else self .exec_network .inputs
599
+ self .input_to_index = {inp .get_node ().friendly_name : idx for idx , inp in enumerate (network_inputs )}
592
600
if not self ._postpone_input_configuration :
593
601
self ._set_precision ()
594
602
self ._set_input_shape ()
@@ -597,12 +605,13 @@ def load_network(self, network=None, log=False, preprocessing=None):
597
605
self .print_input_output_info (self .network if self .network is not None else self .exec_network )
598
606
if preprocessing :
599
607
self ._set_preprocess (preprocessing )
608
+ self .dyn_input_layers , self ._partial_shapes = self .get_dynamic_inputs (self .network )
600
609
model_batch = self ._get_model_batch_size ()
601
610
model_batch = 1 if model_batch is None else model_batch
602
611
self ._batch = self .config .get ('batch' , model_batch )
603
612
self ._set_batch_size (self ._batch )
604
613
self .try_to_set_default_layout ()
605
- if self .network and not preprocessing and (not self .dyn_input_layers or self .is_dynamic ):
614
+ if self .network and (not self .dyn_input_layers or self .is_dynamic or self . disable_resize_to_input ):
606
615
self .exec_network = self .ie_core .compile_model (self .network , self ._device )
607
616
self .infer_request = self .exec_network .create_infer_request ()
608
617
@@ -838,11 +847,15 @@ def _data_to_blob(self, layer_shape, data, layout): # pylint:disable=R0911,R091
838
847
839
848
def _set_precision (self ):
840
849
config_inputs = self .config .get ('inputs' , [])
841
- for input_config in config_inputs :
842
- if 'precision' in input_config :
843
- if self .network :
844
- self .inputs [input_config ['name' ]].set_element_type (
845
- PRECISION_STR_TO_TYPE [input_config ['precision' ].upper ()])
850
+ has_precisions = ['precision' in inp for inp in config_inputs ]
851
+ if has_precisions and self .network :
852
+ preprocessor = PrePostProcessor (self .network )
853
+ for input_config in config_inputs :
854
+ if 'precision' in input_config :
855
+ name = input_config ['name' ]
856
+ element_type = PRECISION_STR_TO_TYPE [input_config ['precision' ].upper ()]
857
+ preprocessor .input (self .input_to_index [name ]).tensor ().set_element_type (element_type )
858
+ self .network = preprocessor .build ()
846
859
847
860
def _set_input_shape (self ):
848
861
if not self .network :
@@ -914,33 +927,28 @@ def _set_preprocess(self, preprocess):
914
927
preprocess_steps = preprocess .ie_preprocess_steps
915
928
if not preprocess_steps :
916
929
return
917
- for input_name , input_info in self .network .input_info .items ():
930
+ preprocessor = PrePostProcessor (self .network )
931
+ for input_name in self .inputs :
918
932
if input_name in self .const_inputs + self .image_info_inputs :
919
933
continue
934
+ input_id = self .input_to_index [input_name ]
920
935
for (name , value ) in preprocess_steps :
921
- setattr (input_info .preprocess_info , name , value )
922
- if preprocess .ie_processor .has_normalization ():
923
- channel_id = input_info .layout .find ('C' )
924
- if channel_id != - 1 :
925
- num_channels = input_info .input_data .shape [channel_id ]
926
- preprocess .ie_processor .set_normalization (num_channels , input_info .preprocess_info )
927
- self .disable_resize_to_input = preprocess .ie_processor .has_resize ()
928
- self ._use_set_blob = self .disable_resize_to_input
929
- self .load_network (self .network )
936
+ if name == 'resize_algorithm' :
937
+ preprocessor .input (input_id ).tensor ().set_spatial_dynamic_shape ()
938
+ preprocessor .input (input_id ).preprocess ().resize (value )
939
+ self .need_dyn_resolving = False
940
+ if name == 'convert_color_format' :
941
+ src , dst = value
942
+ preprocessor .input (input_id ).tensor ().set_color_format (src )
943
+ preprocessor .input (input_id ).preprocess ().convert_color (dst )
944
+ if name == 'mean_variant' :
945
+ mean , scale = value
946
+ if mean is not None :
947
+ preprocessor .input (input_id ).preprocess ().mean (mean )
948
+ if scale is not None :
949
+ preprocessor .input (input_id ).preprocess ().scale (scale )
950
+ self .network = preprocessor .build ()
930
951
self ._preprocess_steps = preprocess_steps
931
- return
932
- preprocess_info_by_input = {}
933
- preprocess_info = preprocess .preprocess_info
934
- for input_name in self .inputs :
935
- if input_name in self .const_inputs + self .image_info_inputs :
936
- continue
937
- if preprocess .ie_processor .has_normalization ():
938
- channel_id = self .inputs [input_name ].layout .find ('C' )
939
- if channel_id != - 1 :
940
- num_channels = self .inputs [input_name ].shape [channel_id ]
941
- preprocess .ie_processor .set_normalization (num_channels , preprocess_info )
942
- preprocess_info_by_input [input_name ] = preprocess_info
943
- self ._preprocess_info = preprocess_info_by_input
944
952
self .disable_resize_to_input = preprocess .ie_processor .has_resize ()
945
953
946
954
def get_model_file_type (self ):
@@ -958,8 +966,7 @@ def get_infer_queue(self, log=True):
958
966
debug ('Prepared async infer queue with {} requests' .format (len (queue )))
959
967
return queue
960
968
961
- def prepare_data_for_request (self ,
962
- inputs , batch_meta , batch_id , batch_input_ids ,
969
+ def prepare_data_for_request (self , inputs , batch_meta , batch_id , batch_input_ids ,
963
970
batch_annotation , batch_identifiers ):
964
971
infer_inputs = inputs [0 ]
965
972
feed_dict = {self .input_to_tensor_name [name ]: data for name , data in infer_inputs .items ()}
0 commit comments