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 ,
@@ -499,7 +500,7 @@ def _create_network(self, input_shapes=None):
499
500
with open (str (self ._model ), 'rb' ) as f : #pylint:disable=unspecified-encoding
500
501
self .exec_network = self .ie_core .import_model (io .BytesIO (f .read ()), self ._device )
501
502
self .original_outputs = self .exec_network .outputs
502
- model_batch = self ._get_model_batch_size ()
503
+ model_batch = self ._get_remodel_batch_size ()
503
504
self ._batch = model_batch if model_batch is not None else 1
504
505
return
505
506
if self ._weights is None and self ._model .suffix != '.onnx' :
@@ -587,8 +588,10 @@ def load_network(self, network=None, log=False, preprocessing=None):
587
588
self .network = network
588
589
if self .network is not None :
589
590
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 )}
591
+ self .input_to_tensor_name = self .get_input_tensor_name_mapping (
592
+ self .network if self .network is not None else self .exec_network )
593
+ network_inputs = self .network .inputs if self .network is not None else self .exec_network .inputs
594
+ self .input_to_index = {inp .get_node ().friendly_name : idx for idx , inp in enumerate (network_inputs )}
592
595
if not self ._postpone_input_configuration :
593
596
self ._set_precision ()
594
597
self ._set_input_shape ()
@@ -597,12 +600,13 @@ def load_network(self, network=None, log=False, preprocessing=None):
597
600
self .print_input_output_info (self .network if self .network is not None else self .exec_network )
598
601
if preprocessing :
599
602
self ._set_preprocess (preprocessing )
603
+ self .dyn_input_layers , self ._partial_shapes = self .get_dynamic_inputs (self .network )
600
604
model_batch = self ._get_model_batch_size ()
601
605
model_batch = 1 if model_batch is None else model_batch
602
606
self ._batch = self .config .get ('batch' , model_batch )
603
607
self ._set_batch_size (self ._batch )
604
608
self .try_to_set_default_layout ()
605
- if self .network and not preprocessing and (not self .dyn_input_layers or self .is_dynamic ):
609
+ if self .network and (not self .dyn_input_layers or self .is_dynamic or self . disable_resize_to_input ):
606
610
self .exec_network = self .ie_core .compile_model (self .network , self ._device )
607
611
self .infer_request = self .exec_network .create_infer_request ()
608
612
@@ -838,11 +842,15 @@ def _data_to_blob(self, layer_shape, data, layout): # pylint:disable=R0911,R091
838
842
839
843
def _set_precision (self ):
840
844
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 ()])
845
+ has_precisions = ['precision' in inp for inp in config_inputs ]
846
+ if has_precisions and self .network :
847
+ preprocessor = PrePostProcessor (self .network )
848
+ for input_config in config_inputs :
849
+ if 'precision' in input_config :
850
+ name = input_config ['name' ]
851
+ element_type = PRECISION_STR_TO_TYPE [input_config ['precision' ].upper ()]
852
+ preprocessor .input (self .input_to_index [name ]).tensor ().set_element_type (element_type )
853
+ self .network = preprocessor .build ()
846
854
847
855
def _set_input_shape (self ):
848
856
if not self .network :
@@ -914,33 +922,28 @@ def _set_preprocess(self, preprocess):
914
922
preprocess_steps = preprocess .ie_preprocess_steps
915
923
if not preprocess_steps :
916
924
return
917
- for input_name , input_info in self .network .input_info .items ():
925
+ preprocessor = PrePostProcessor (self .network )
926
+ for input_name in self .inputs :
918
927
if input_name in self .const_inputs + self .image_info_inputs :
919
928
continue
929
+ input_id = self .input_to_index [input_name ]
920
930
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 )
931
+ if name == 'resize_algorithm' :
932
+ preprocessor .input (input_id ).tensor ().set_spatial_dynamic_shape ()
933
+ preprocessor .input (input_id ).preprocess ().resize (value )
934
+ self .need_dyn_resolving = False
935
+ if name == 'convert_color_format' :
936
+ src , dst = value
937
+ preprocessor .input (input_id ).tensor ().set_color_format (src )
938
+ preprocessor .input (input_id ).preprocess ().convert_color (dst )
939
+ if name == 'mean_variant' :
940
+ mean , scale = value
941
+ if mean is not None :
942
+ preprocessor .input (input_id ).preprocess ().mean (mean )
943
+ if scale is not None :
944
+ preprocessor .input (input_id ).preprocess ().scale (scale )
945
+ self .network = preprocessor .build ()
930
946
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
947
self .disable_resize_to_input = preprocess .ie_processor .has_resize ()
945
948
946
949
def get_model_file_type (self ):
0 commit comments