Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

@Hank538, Please try the following code:

import tensorflow as tf
import cv2 as cv
import numpy as np

graph = 'frozen.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='')

    inp = cv.imread('mnist_2_5008.jpg', cv.IMREAD_GRAYSCALE)

    out = sess.run(sess.graph.get_tensor_by_name('softmax:0'),
                   feed_dict={'input:0': inp.reshape(1, 28, 28, 1)})

    cvNet.setInput(cv.dnn.blobFromImage(inp))
    cvOut = cvNet.forward()

    print np.max(np.abs(cvOut - out))
    print cvOut
    print out

Maximal absolute difference is 9.791666e-39, outputs:

OpenCV: [[0.000000e+00 0.000000e+00 1.000000e+00 0.000000e+00 0.000000e+00
          0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 9.791666e-39]]

TensorFlow: [[0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]]

Both frameworks vote that there is 2 on the image image description.