Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

@leyendecker321, OpenCV's output matches TensorFlow's one.

graph = 'graph.pb'
cvNet = cv.dnn.readNetFromTensorflow(graph)

with tf.gfile.FastGFile(graph) as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())

with tf.Session() as sess:
    # Restore session
    sess.graph.as_default()
    tf.import_graph_def(graph_def, name='')

    np.random.seed(324)
    inp = np.random.standard_normal([1, 1, 28, 28]).astype(np.float32)

    out = sess.run(sess.graph.get_tensor_by_name('activation_4/Softmax:0'),
                   feed_dict={'conv2d_1_input:0': inp.transpose(0, 2, 3, 1)})
    cvNet.setInput(inp)
    cvOut = cvNet.forward()

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

Output:

2.98023e-08

So you need to find a bug in your application.

@leyendecker321, OpenCV's output matches TensorFlow's one.

graph = 'graph.pb'
cvNet = cv.dnn.readNetFromTensorflow(graph)

with tf.gfile.FastGFile(graph) as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())

with tf.Session() as sess:
    # Restore session
    sess.graph.as_default()
    tf.import_graph_def(graph_def, name='')

    np.random.seed(324)
    inp = np.random.standard_normal([1, 1, 28, 28]).astype(np.float32)

    out = sess.run(sess.graph.get_tensor_by_name('activation_4/Softmax:0'),
                   feed_dict={'conv2d_1_input:0': inp.transpose(0, 2, 3, 1)})
    cvNet.setInput(inp)
    cvOut = cvNet.forward()

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

Output:

2.98023e-08

So you need to find a bug in your application.application. Try to start from input image. You have to create a 4D blob from an image using blobFromImage. Follow one of [tutorials] (https://docs.opencv.org/master/d5/de7/tutorial_dnn_googlenet.html).

@leyendecker321, OpenCV's output matches TensorFlow's one.

graph = 'graph.pb'
cvNet = cv.dnn.readNetFromTensorflow(graph)

with tf.gfile.FastGFile(graph) as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())

with tf.Session() as sess:
    # Restore session
    sess.graph.as_default()
    tf.import_graph_def(graph_def, name='')

    np.random.seed(324)
    inp = np.random.standard_normal([1, 1, 28, 28]).astype(np.float32)

    out = sess.run(sess.graph.get_tensor_by_name('activation_4/Softmax:0'),
                   feed_dict={'conv2d_1_input:0': inp.transpose(0, 2, 3, 1)})
    cvNet.setInput(inp)
    cvOut = cvNet.forward()

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

Output:

2.98023e-08

So you need to find a bug in your application. Try to start from input image. You have to create a 4D blob from an image using blobFromImage. Follow one of [tutorials] (https://docs.opencv.org/master/d5/de7/tutorial_dnn_googlenet.html).tutorials.