Last active
March 28, 2019 23:18
-
-
Save donniet/0d0e8f604eb30aeb0215968dc4e20071 to your computer and use it in GitHub Desktop.
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from openvino.inference_engine import IENetwork, IEPlugin\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"device = \"CPU\"\n", | |
"cpu_extension_path = \"/opt/intel/computer_vision_sdk/inference_engine/lib/ubuntu_16.04/intel64/libcpu_extension_avx2.so\"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"precision = \"FP16\"\n", | |
"\n", | |
"if device == \"CPU\":\n", | |
" precision = \"FP32\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"network = '''\n", | |
"<?xml version=\"1.0\" ?>\n", | |
"<net batch=\"1\" name=\"ResNet-50-128\" version=\"3\">\n", | |
"\t<layers>\n", | |
"\t\t<layer id=\"0\" name=\"data\" precision=\"{precision}\" type=\"Input\">\n", | |
"\t\t\t<output>\n", | |
"\t\t\t\t<port id=\"0\">\n", | |
"\t\t\t\t\t<dim>1</dim>\n", | |
" <dim>2</dim>\n", | |
"\t\t\t\t</port>\n", | |
"\t\t\t</output>\n", | |
"\t\t</layer>\n", | |
"\t\t<layer id=\"1\" name=\"feat_normalize\" precision=\"{precision}\" type=\"GRN\">\n", | |
"\t\t\t<data bias=\"1e-10\" />\n", | |
"\t\t\t<input>\n", | |
"\t\t\t\t<port id=\"0\">\n", | |
"\t\t\t\t\t<dim>1</dim>\n", | |
"\t\t\t\t\t<dim>2</dim>\n", | |
"\t\t\t\t</port>\n", | |
"\t\t\t</input>\n", | |
"\t\t\t<output>\n", | |
"\t\t\t\t<port id=\"2\">\n", | |
"\t\t\t\t\t<dim>1</dim>\n", | |
"\t\t\t\t\t<dim>2</dim>\n", | |
"\t\t\t\t</port>\n", | |
"\t\t\t</output>\n", | |
"\t\t</layer>\n", | |
" </layers>\n", | |
" <edges>\n", | |
"\t\t<edge from-layer=\"0\" from-port=\"0\" to-layer=\"1\" to-port=\"0\"/>\n", | |
" </edges>\n", | |
" <meta_data>\n", | |
"\t\t<MO_version value=\"1.4.288.16472e37\"/>\n", | |
"\t\t<cli_parameters>\n", | |
"\t\t\t<batch value=\"1\"/>\n", | |
"\t\t\t<data_type value=\"FP32\"/>\n", | |
"\t\t\t<unset unset_cli_parameters=\"finegrain_fusing, freeze_placeholder_with_value, input, input_shape, mean_file, mean_file_offsets, model_name, output, scale\"/>\n", | |
"\t\t</cli_parameters>\n", | |
"\t</meta_data>\n", | |
"</net>\n", | |
"'''.format(precision=precision)\n", | |
"\n", | |
"with open('test_normalization.xml', 'w') as f:\n", | |
" f.write(network)\n", | |
"\n", | |
"with open('test_normalization.bin', 'w') as f:\n", | |
" f.write('')\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plugin = IEPlugin(device=device)\n", | |
"if device == \"CPU\":\n", | |
" plugin.add_cpu_extension(cpu_extension_path)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"net = IENetwork(model='test_normalization.xml', weights='test_normalization.bin')\n", | |
"exec_net = plugin.load(net)\n", | |
"input_blob = next(iter(net.inputs))\n", | |
"output_blob = next(iter(net.outputs))\n", | |
"del net" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"normalized: True input: [1, 1] output: [0.70710677 0.70710677]\n", | |
"normalized: True input: [2, -2] output: [ 0.70710677 -0.70710677]\n", | |
"normalized: True input: [1, 0] output: [1. 0.]\n", | |
"normalized: True input: [0, 1] output: [0. 1.]\n" | |
] | |
} | |
], | |
"source": [ | |
"inputs = [[1,1],[2,-2],[1,0],[0,1]]\n", | |
"\n", | |
"for k in inputs:\n", | |
" res = exec_net.infer(inputs={input_blob: [k]})\n", | |
" out = res[output_blob][0]\n", | |
" d2 = out[0] * out[0] + out[1] * out[1]\n", | |
" normalized = True\n", | |
" \n", | |
" if d2 < 0.999 or d2 > 1.001:\n", | |
" normalized = False\n", | |
" \n", | |
" print('normalized: {} input: {} output: {}'.format(normalized, k, out))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.7" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment