-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Add openvino support #149
Open
r-or
wants to merge
5
commits into
nwojke:master
Choose a base branch
from
r-or:openvino
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
Add openvino support #149
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
83fc559
Add option to bypass preprocessing step.
r-or 552944e
Add initial support for Openvino.
r-or cb8020f
Add a FPS counter to the command line
r-or 2ea4866
Update readme for openvino
r-or df7d69b
Add openvino / tensorflow test application
r-or 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
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
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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import timeit | ||
import math | ||
|
||
from openvino.inference_engine import IENetwork, IEPlugin | ||
import numpy as np | ||
import tensorflow as tf | ||
|
||
all_batch_size = 1 | ||
np.random.seed(seed=69) | ||
|
||
def _run_in_batches(f, data_dict, out, batch_size): | ||
data_len = len(out) | ||
num_batches = int(data_len / batch_size) | ||
|
||
s, e = 0, 0 | ||
for i in range(num_batches): | ||
s, e = i * batch_size, (i + 1) * batch_size | ||
batch_data_dict = {k: v[s:e] for k, v in data_dict.items()} | ||
out[s:e] = f(batch_data_dict) | ||
if e < len(out): | ||
batch_data_dict = {k: v[e:] for k, v in data_dict.items()} | ||
out[e:] = f(batch_data_dict) | ||
|
||
modelname = 'resources/networks/mars-small128' | ||
|
||
# OV configuration | ||
ov_net = IENetwork(model=modelname + '.xml', weights=modelname + '.bin') | ||
ov_net.batch_size = all_batch_size | ||
ov_plugin = IEPlugin(device='CPU') | ||
|
||
# TF configuration | ||
tf_session = tf.Session() | ||
with tf.gfile.GFile(modelname + '.pb', 'rb') as gfile: | ||
tf_graph = tf.GraphDef() | ||
tf_graph.ParseFromString(gfile.read()) | ||
tf.import_graph_def(tf_graph, name='net') | ||
tf_input_node = tf.get_default_graph().get_tensor_by_name('net/images:0') | ||
tf_output_node = tf.get_default_graph().get_tensor_by_name('net/features:0') | ||
|
||
|
||
# ?x128x64x3 | ||
testinput = np.random.random_sample((all_batch_size, 128, 64, 3)) | ||
testinput2 = testinput[:, :, :, ::-1] | ||
print(testinput - testinput2) | ||
# openvino expects colors major | ||
ov_testinput = np.transpose(testinput, (0, 3, 1, 2)) | ||
ov_testinput2 = np.transpose(testinput2, (0, 3, 1, 2)) | ||
|
||
# run OV | ||
ov_input_blob = next(iter(ov_net.inputs)) | ||
ov_out_blob = next(iter(ov_net.outputs)) | ||
ov_exec_net = ov_plugin.load(network=ov_net) | ||
|
||
def run_ov(inp): | ||
return ov_exec_net.infer(inputs={ov_input_blob: inp}) | ||
|
||
ov_res = next(iter(run_ov(ov_testinput).values())) | ||
ov_res2 = next(iter(run_ov(ov_testinput2).values())) | ||
|
||
# run TF | ||
def run_tf(inp): | ||
tf_output = np.zeros((all_batch_size, 128), np.float32) | ||
_run_in_batches(lambda x: tf_session.run(tf_output_node, feed_dict=x), | ||
{tf_input_node: inp}, tf_output, all_batch_size) | ||
return tf_output | ||
|
||
tf_res = run_tf(testinput) | ||
tf_res2 = run_tf(testinput2) | ||
|
||
def compare(vec1, vec2): | ||
print('Diff abs (0.0 is exactly same):\n', vec1 - vec2) | ||
print('Diff rel (1.0 is exactly same):\n', vec1 / vec2) | ||
|
||
comp = 'PASSED' if np.allclose(vec1, vec2) else 'FAILED' | ||
print('Comparison: {}'.format(comp)) | ||
|
||
# compare different results | ||
print('TF: RGB vs BGR') | ||
compare(tf_res, tf_res2) | ||
print('') | ||
|
||
print('OV: RGB vs BGR') | ||
compare(ov_res, ov_res2) | ||
print('') | ||
|
||
print('TF vs OV') | ||
compare(tf_res, ov_res) | ||
print('') | ||
|
||
# timing | ||
iterations = int(300 / all_batch_size) | ||
print('Batch size {}, {} iterations:'.format(all_batch_size, iterations)) | ||
print(' OV: {:.5f}s'.format(timeit.timeit('run_ov(ov_testinput)', number=iterations, globals=globals()))) | ||
print(' TF: {:.5f}s'.format(timeit.timeit('run_tf(testinput)', number=iterations, globals=globals()))) |
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
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
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.
The
_preprocess()
function changes input order (BGR to RGB). Is this handled anywhere in the code?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.
No... but it seems to not matter for the computation.
I wrote a test script for that:
https://gist.github.com/r-or/e1b85c47e1906763b6e0e7a209812dda
Apart from tensorflow and openvino not creating exactly the same results (which is to be expected) swapping BGR and RGB seems to not do anything.
Anyway, even though the results are slightly different, the tracker worked perfectly fine
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.
But it wouldn't hurt to convert from BGR to RGB before handing the image over from NumPy to TensorFlow, right?
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.
Also, thanks for posting test results.
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.
True, it wouldn't hurt either. I'll add it for consistency.