-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AC: add inputs and LSTM inputs processing for OpenCV launcher #3444
Open
APrigarina
wants to merge
1
commit into
openvinotoolkit:master
Choose a base branch
from
APrigarina:ac_opencv_inputs_processing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ | |
import numpy as np | ||
import cv2 | ||
|
||
from ..config import PathField, StringField, ConfigError, ListInputsField | ||
from ..config import PathField, StringField, ConfigError, ListInputsField, BoolField | ||
from ..logging import print_info | ||
from .launcher import Launcher, LauncherConfigValidator | ||
from ..utils import get_or_parse_value | ||
|
@@ -73,7 +73,8 @@ def parameters(cls): | |
regex=BACKEND_REGEX, choices=OpenCVLauncher.OPENCV_BACKENDS.keys(), | ||
optional=True, default='IE', | ||
description="Backend name: {}".format(', '.join(OpenCVLauncher.OPENCV_BACKENDS.keys()))), | ||
'inputs': ListInputsField(optional=False, description="Inputs.") | ||
'inputs': ListInputsField(optional=False, description="Inputs."), | ||
'allow_reshape_input': BoolField(optional=True, default=False, description="Allows reshape input.") | ||
}) | ||
|
||
return parameters | ||
|
@@ -104,8 +105,12 @@ def __init__(self, config_entry: dict, *args, **kwargs): | |
self.weights = self.get_value_from_config('weights') | ||
self.network = self.create_network(self.model, self.weights) | ||
self._inputs_shapes = self.get_inputs_from_config(self.config) | ||
self.allow_reshape_input = self.get_value_from_config('allow_reshape_input') | ||
self.network.setInputsNames(list(self._inputs_shapes.keys())) | ||
self.output_names = self.network.getUnconnectedOutLayersNames() | ||
self._lstm_inputs = None | ||
if '_list_lstm_inputs' in self.config: | ||
self._configure_lstm_inputs() | ||
|
||
@classmethod | ||
def validate_config(cls, config, delayed_model_loading=False, fetch_only=False, uri_prefix=''): | ||
|
@@ -130,6 +135,90 @@ def batch(self): | |
def output_blob(self): | ||
return next(iter(self.output_names)) | ||
|
||
def _configure_lstm_inputs(self): | ||
lstm_mapping = {} | ||
config_inputs = self.config.get('inputs', []) | ||
for input_config in config_inputs: | ||
if input_config['type'] == 'LSTM_INPUT': | ||
lstm_mapping[input_config['name']] = input_config['value'] | ||
self._lstm_inputs = lstm_mapping | ||
|
||
def _fill_lstm_inputs(self, infer_outputs=None): | ||
feed_dict = {} | ||
for i, lstm_var in enumerate(self._lstm_inputs.keys()): | ||
layer_shape = self._inputs_shapes[lstm_var] | ||
input_data = infer_outputs[i].reshape(layer_shape) if infer_outputs else np.zeros( | ||
layer_shape | ||
) | ||
feed_dict[lstm_var] = input_data | ||
return feed_dict | ||
|
||
def _data_to_blob(self, layer_shape, data, layout): # pylint:disable=R0911,R0912 | ||
data_shape = np.shape(data) | ||
if len(layer_shape) == 4: | ||
if len(data_shape) == 5: | ||
data = data[0] | ||
if len(data_shape) == 3: | ||
data = np.expand_dims(data, -1) | ||
data_shape = np.shape(data) | ||
if len(data_shape) < 4: | ||
if len(np.squeeze(np.zeros(layer_shape))) == len(np.squeeze(np.zeros(data_shape))): | ||
return np.resize(data, layer_shape) | ||
return np.transpose(data, layout) if layout is not None else data | ||
if len(layer_shape) == 2: | ||
if len(data_shape) == 1: | ||
return np.transpose([data]) | ||
if len(data_shape) > 2: | ||
if all(dim == 1 for dim in layer_shape) and all(dim == 1 for dim in data_shape): | ||
return np.resize(data, layer_shape) | ||
if len(np.squeeze(np.zeros(layer_shape))) == len(np.squeeze(np.zeros(data_shape))): | ||
return np.resize(data, layer_shape) | ||
if len(layer_shape) == 3 and len(data_shape) == 4: | ||
return np.transpose(data, layout)[0] if layout is not None else data[0] | ||
if len(layer_shape) == 1: | ||
return np.resize(data, layer_shape) | ||
if (len(data_shape) == 3) and (len(layer_shape) == 2) and (data_shape[0] == 1) and ( | ||
data_shape[1] == 1) and self.allow_reshape_input: | ||
return data[0] | ||
if layout is not None and len(layer_shape) == len(layout): | ||
return np.transpose(data, layout) | ||
if ( | ||
len(layer_shape) == 1 and len(data_shape) > 1 and | ||
len(np.squeeze(np.zeros(layer_shape))) == len(np.squeeze(np.zeros(data_shape))) | ||
): | ||
return np.resize(data, layer_shape) | ||
return np.array(data) | ||
|
||
def fit_to_input(self, data, layer_name, layout, precision, template=None): | ||
layer_shape = tuple(self._inputs_shapes[layer_name]) | ||
data = self._data_to_blob(layer_shape, data, layout) | ||
if precision: | ||
data = data.astype(precision) | ||
if data.shape != layer_shape: | ||
if self.allow_reshape_input: | ||
return data | ||
|
||
return data.reshape(layer_shape) | ||
|
||
def predict_sequential(self, inputs, metadata=None, **kwargs): | ||
lstm_inputs_feed = self._fill_lstm_inputs() | ||
results = [] | ||
for input_blobs in inputs: | ||
input_blobs.update(lstm_inputs_feed) | ||
for blob_name in input_blobs.keys(): | ||
input = input_blobs[blob_name].astype(np.float32) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ************* Module openvino.tools.accuracy_checker.launcher.opencv_launcher |
||
self.network.setInput(input, blob_name) | ||
list_prediction = self.network.forward(self.output_names) | ||
lstm_inputs_feed = self._fill_lstm_inputs(list_prediction) | ||
dict_result = dict(zip(self.output_names, list_prediction)) | ||
results.append(dict_result) | ||
|
||
if metadata is not None: | ||
for meta_ in metadata: | ||
meta_['input_shape'] = self.inputs_info_for_meta() | ||
|
||
return results | ||
|
||
def predict(self, inputs, metadata=None, **kwargs): | ||
""" | ||
Args: | ||
|
@@ -139,6 +228,9 @@ def predict(self, inputs, metadata=None, **kwargs): | |
raw data from network. | ||
""" | ||
results = [] | ||
if self._lstm_inputs: | ||
return self.predict_sequential(inputs, metadata) | ||
|
||
for input_blobs in inputs: | ||
for blob_name in self._inputs_shapes: | ||
self.network.setInput(input_blobs[blob_name].astype(np.float32), blob_name) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this key means that instead of resizing data to fit model, model should be reshaped to data side. Does opencv support model reshaping or dynamic model inference?