一、环境配置
1、下载OPENVINO—TOOLKIT
https: // software.intel.com / en - us / openvino - toolkit / choose - download
2、安装OPENVINO
按照默认的路径安装,全部勾选,直接下一步到完成
3、安装CMake,并添加到环境变量
4、安装PYthon,并添加到环境变量
5、设置环境
1) cd
C:\Program
Files(x86)\IntelSWTools\openvino\bin \
2) setupvars.bat
6、安装对应框架的优化
1)cd
C:\Program
Files(x86)\IntelSWTools\openvino\deployment_tools\model_optimizer\install_prerequisites
2)install_prerequisites_tf.bat (由于使用的是TensorFlow )
7、测试
1)cd
C:\Program
Files(x86)\IntelSWTools\openvino\deployment_tools\demo \
2)demo_squeezenet_download_convert_run.bat (不报错就是安装成功了)
二、模型转换
1、训练模型(以手写体识别为例)
原始TensorFlow手写体识别程序
"""A deep MNIST classifier using convolutional layers.
See extensive documentation at
https://www.tensorflow.org/get_started/mnist/pros
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import sys
import tempfile
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
FLAGS = None
def deepnn(x):
"""deepnn builds the graph for a deep net for classifying digits.
Args:
x: an input tensor with the dimensions (N_examples, 784), where 784 is the
number of pixels in a standard MNIST image.
Returns:
A tuple (y, keep_prob). y is a tensor of shape (N_examples, 10), with values
equal to the logits of classifying the digit into one of 10 classes (the
digits 0-9). keep_prob is a scalar placeholder for the probability of
dropout.
"""
with tf.name_scope('reshape'):
x_image = tf.reshape(x, [-1, 28, 28, 1])
with tf.name_scope('conv1'):
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
with tf.name_scope('pool1'):
h_pool1 = max_pool_2x2(h_conv1)
with tf.name_scope('conv2'):
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
with tf.name_scope('pool2'):
h_pool2 = max_pool_2x2(h_conv2)
with tf.name_scope('fc1'):
W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
with tf.name_scope('dropout'):
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
with tf.name_scope('fc2'):
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
return y_conv, keep_prob
def conv2d(x, W):
"""conv2d returns a 2d convolution layer with full stride."""
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
"""max_pool_2x2 downsamples a feature map by 2X."""
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
def weight_variable(shape):
"""weight_variable generates a weight variable of a given shape."""
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
"""bias_variable generates a bias variable of a given shape."""
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def main(_):
mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)
x = tf.placeholder(tf.float32, [None, 784], name="input")
y_ = tf.placeholder(tf.float32, [None, 10])
y_conv, keep_prob = deepnn(x)
with tf.name_scope('loss'):
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=y_,
logits=y_conv)
cross_entropy = tf.reduce_mean(cross_entropy)
with tf.name_scope('adam_optimizer'):
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
with tf.name_scope('accuracy'):
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
correct_prediction = tf.cast(correct_prediction, tf.float32)
accuracy = tf.reduce_mean(correct_prediction)
graph_location = tempfile.mkdtemp()
print('Saving graph to: %s' % graph_location)
train_writer = tf.summary.FileWriter(graph_location)
train_writer.add_graph(tf.get_default_graph())
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(2000):
batch = mnist.train.next_batch(50)
if i % 100 == 0:
train_accuracy = accuracy.eval(feed_dict={
x: batch[0], y_: batch[1], keep_prob: 1.0})
print('step %d, training accuracy %g' % (i, train_accuracy))
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
print('test accuracy %g' % accuracy.eval(feed_dict={
x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
graph_location = "."
save_path = saver.save(sess, graph_location + "/mnist_model")
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--data_dir', type=str,
default='/tmp/tensorflow/mnist/input_data',
help='Directory for storing input data')
FLAGS, unparsed = parser.parse_known_args()
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
2、保存模型
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import sys
import tempfile
import tensorflow as tf
FLAGS = None
def deepnn(x):
with tf.name_scope('reshape'):
x_image = tf.reshape(x, [-1, 28, 28, 1])
with tf.name_scope('conv1'):
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
with tf.name_scope('pool1'):
h_pool1 = max_pool_2x2(h_conv1)
with tf.name_scope('conv2'):
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
with tf.name_scope('pool2'):
h_pool2 = max_pool_2x2(h_conv2)
with tf.name_scope('fc1'):
W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
with tf.name_scope('fc2'):
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_conv = tf.matmul(h_fc1, W_fc2) + b_fc2
return y_conv
def conv2d(x, W):
"""conv2d returns a 2d convolution layer with full stride."""
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
"""max_pool_2x2 downsamples a feature map by 2X."""
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
def weight_variable(shape):
"""weight_variable generates a weight variable of a given shape."""
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
"""bias_variable generates a bias variable of a given shape."""
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def main(_):
x = tf.placeholder(tf.float32, [None, 784], name="input")
y_conv = deepnn(x)
output = tf.nn.softmax(y_conv, name='output')
saver = tf.train.Saver(tf.global_variables())
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
saver.restore(sess, '.' + '/mnist_model')
saver.save(sess, 'C:/Users/ym/Desktop/model' + '/mnist_inference')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--data_dir', type=str,
default='/tmp/tensorflow/mnist/input_data',
help='Directory for storing input data')
FLAGS, unparsed = parser.parse_known_args()
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
3、模型转换为中间件
模型存储后mnist_inference.index、mnist_inference.data - 00000 - of - 00001、mnist_inference.meta
将这三个文件通过TensorFlow自带的freezeing功能将模型存为.pb文件
将.pb文件转化为神经计算棒能够识别的模型
1、管理员身份运行cmd
2、cd
C:\Program
Files(x86)\IntelSWTools\openvino_2019
.2
.275\deployment_tools\model_optimizer
3、python
mo_tf.py - -input_model
C:\Users\ym\Desktop\model\model1.pb - -input_shape[1, 28, 28, 1] - -data_type
FP16
最后生成三个文件.bin文件.mapping文件.xml文件
4、C + +调用
using
namespace
InferenceEngine;
int
main()
{
const
file_name_t
input_model
{"C:/Users/ym/Desktop/model/cnn_model.xml"};
const
file_name_t
input_image_path
{"C:/Users/ym/Desktop/22/q/Y_200402_26__17.jpg"};
const
std::string
device_name
{"MYRIAD"}; // 设备名字
MYRIAD是神经计算棒,CPU是电脑自带CPU
// -----------------------------------------------------------------------------------------------------
// --------------------------- 1.
Load
inference
engine
instance - ------------------------------------
Core
ie;
// -----------------------------------------------------------------------------------------------------
// --------------------------- 2.
Read
IR
Generated
by
ModelOptimizer(.xml and.bin
files) ------------
CNNNetReader
network_reader;
network_reader.ReadNetwork(fileNameToString(input_model));
network_reader.ReadWeights(fileNameToString(input_model).substr(0, input_model.size() - 4) + ".bin");
network_reader.getNetwork().setBatchSize(1);
CNNNetwork
network = network_reader.getNetwork();
// -----------------------------------------------------------------------------------------------------
// --------------------------- 3.
Configure
input & output - --------------------------------------------
// --------------------------- Prepare
input
blobs - ----------------------------------------------------
InputInfo::Ptr
input_info = network.getInputsInfo().begin()->second;
std::string
input_name = network.getInputsInfo().begin()->first;
/ *Mark
input as resizable
by
setting
of
a
resize
algorithm.
* In
this
case
we
will
be
able
to
set
an
input
blob
of
any
shape
to
an
infer
request.
* Resize and layout
conversions
are
executed
automatically
during
inference * /
input_info->getPreProcess().setResizeAlgorithm(RESIZE_BILINEAR);
input_info->setLayout(Layout::NHWC);
input_info->setPrecision(Precision::U8);
// --------------------------- Prepare
output
blobs - ---------------------------------------------------
DataPtr
output_info = network.getOutputsInfo().begin()->second;
std::string
output_name = network.getOutputsInfo().begin()->first;
output_info->setPrecision(Precision::FP32);
// -----------------------------------------------------------------------------------------------------
// --------------------------- 4.
Loading
model
to
the
device - -----------------------------------------
ExecutableNetwork
executable_network = ie.LoadNetwork(network, device_name);
// -----------------------------------------------------------------------------------------------------
// --------------------------- 5.
Create
infer
request - ------------------------------------------------
InferRequest
infer_request = executable_network.CreateInferRequest();
// -----------------------------------------------------------------------------------------------------
// --------------------------- 6.
Prepare
input - -------------------------------------------------------
/ * Read
input
image
to
a
blob and set
it
to
an
infer
request
without
resize and layout
conversions. * /
cv::Mat
image = cv::imread(input_image_path, -1);
Blob::Ptr
imgBlob = wrapMat2Blob(image); // just
wrap
Mat
data
by
Blob::Ptr
without
allocating
of
new
memory
infer_request.SetBlob(input_name, imgBlob); // infer_request
accepts
input
blob
of
any
size
- -----------------------------------------------------------------
// --------------------------- 7.
Do
inference - -------------------------------------------------------
/ * Running
the
request
synchronously * /
infer_request.Infer();
// -----------------------------------------------------------------------------------------------------
// --------------------------- 8.
Process
output - -----------------------------------------------------
Blob::Ptr
output2 = infer_request.GetBlob(output_name);
// Print
classification
results
ClassificationResult
classificationResult(output, {fileNameToString(input_image_path2)});
classificationResult.print();
// -----------------------------------------------------------------------------------------------------
return EXIT_SUCCESS;
}
参考
[1]
https: // docs.openvinotoolkit.org / latest / _docs_install_guides_installing_openvino_windows.html
[2]
https: // movidius.github.io / ncsdk / tf_compile_guidance.html