1 | initial version |
Use optimize_for_inference.py tool to remove unused nodes. Your model receives 784-dimensional input vector then reshapes it to 28x28 image. We also remove this reshape to prevent conflicts between TensorFlow's NHWC and OpenCV's NCHW data layouts:
python ~/tensorflow/tensorflow/python/tools/optimize_for_inference.py \
--input frozen_graph.pb \
--output frozen_graph_opt.pb \
--input_names "Reshape" \
--output_names "softmax" \
--frozen_graph
Test:
graph = 'frozen_graph_opt.pb'
cvNet = cv.dnn.readNet(graph)
with tf.gfile.FastGFile(graph) as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
with tf.Session() as sess:
sess.graph.as_default()
tf.import_graph_def(graph_def, name='')
np.random.seed(234)
inp = np.random.standard_normal([1, 28, 28, 1]).astype(np.float32)
out = sess.run(sess.graph.get_tensor_by_name('softmax:0'),
feed_dict={'Reshape:0': inp})
cvNet.setInput(inp.transpose(0, 3, 1, 2))
cvOut = cvNet.forward()
print np.max(np.abs(cvOut - out))
gives 1.1920929e-07
maximal absolute difference.