Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I trained a cnn model and save as .pb file. got wrong answer using opencv3.41 dnn module

using tensorflow, got correct prediction

inference using tensorflow

import tensorflow as tf import numpy as np import cv2

x_image = cv2.imread('characters/test_A.jpg', cv2.IMREAD_GRAYSCALE) x_image = cv2.resize(x_image, dsize=(40, 16)) x_image = x_image / 255. x_image = x_image.astype(np.float32)

inputBlob = np.reshape(x_image, [-1, 40, 16, 1])

with open('Model/frozen_modelx.pb', 'rb') as f: out_graph_def = tf.GraphDef() out_graph_def.ParseFromString(f.read()) tf.import_graph_def(out_graph_def, name="")

with tf.Session() as sess:
    data = sess.graph.get_tensor_by_name("input/x_input:0")
    prediction = sess.graph.get_tensor_by_name("softmax/prediction:0")

    sess.run(tf.global_variables_initializer())
    x_image_out = sess.run(prediction, feed_dict={data: inputBlob})

    print(x_image_out)
    print(np.argmax(x_image_out, 1))

[[1.6884116e-13 3.1422300e-07 7.3862933e-28 6.9958590e-16 6.0640429e-07 7.8472853e-12 2.0985551e-07 4.8857016e-17 3.7977627e-19 3.5136532e-21 9.9999881e-01 1.1159207e-21 9.9667357e-32 1.7269920e-31 7.0009942e-21 5.0873650e-25 1.1708389e-13 2.7525333e-24 1.1056422e-19 2.8127509e-08 2.8245915e-14 2.0870798e-23 1.0649909e-11 0.0000000e+00 3.8043613e-11 1.7576119e-15 7.1381208e-14 7.3414647e-08 2.4327422e-20 9.1099174e-17 3.2708306e-19 1.3278245e-14 3.0282866e-22 6.0491925e-24]] [10]

but, using dnn module got wrong prediction

inference using opencv

import numpy as np

import cv2

x_image = cv2.imread('characters/test_A.jpg', cv2.IMREAD_GRAYSCALE)

x_image = cv2.resize(x_image, dsize=(16, 40))

x_image = x_image.astype(np.float32)

x_image = x_image / 255.

read model and inference

inputBlob=cv2.dnn.blobFromImage(x_image)

net = cv2.dnn.readNetFromTensorflow('model/frozen_modelx.pb')

net.setInput(inputBlob)

result = net.forward()

print(result)

print(np.argmax(result, 1))

[[1.3639329e-02 3.8205376e-01 4.1754208e-10 1.0312299e-08 4.3897840e-01 2.1898718e-06 1.0198972e-06 1.3872443e-04 1.2519218e-08 3.5530825e-06 4.7783997e-02 2.8187374e-08 5.5049820e-20 3.5439733e-09 1.4154615e-12 2.4284447e-15 2.9786484e-06 2.5385313e-07 1.6377919e-08 1.9443828e-06 5.2226778e-10 3.8744084e-04 1.8799990e-04 1.3955707e-15 1.1665202e-01 4.2024007e-07 1.0303499e-10 3.1994839e-06 6.9113579e-05 4.2022770e-05 2.7376987e-05 2.3721248e-05 5.4692578e-07 1.5755222e-10]] [4]

Can someone help me to indicate where should be modified! Thank you

click to hide/show revision 2
None

updated 2018-05-25 03:09:01 -0600

berak gravatar image

I trained a cnn model and save as .pb file. got wrong answer using opencv3.41 dnn module

using tensorflow, got correct prediction

# inference using tensorflow

tensorflow import tensorflow as tf import numpy as np import cv2

cv2 x_image = cv2.imread('characters/test_A.jpg', cv2.IMREAD_GRAYSCALE) x_image = cv2.resize(x_image, dsize=(40, 16)) x_image = x_image / 255. x_image = x_image.astype(np.float32)

x_image.astype(np.float32) inputBlob = np.reshape(x_image, [-1, 40, 16, 1])

1]) with open('Model/frozen_modelx.pb', 'rb') as f: out_graph_def = tf.GraphDef() out_graph_def.ParseFromString(f.read()) tf.import_graph_def(out_graph_def, name="")

name="")
with tf.Session() as sess:
 data = sess.graph.get_tensor_by_name("input/x_input:0")
 prediction = sess.graph.get_tensor_by_name("softmax/prediction:0")
 sess.run(tf.global_variables_initializer())
  x_image_out = sess.run(prediction, feed_dict={data: inputBlob})
 print(x_image_out)
  print(np.argmax(x_image_out, 1))

[[1.6884116e-13 3.1422300e-07 7.3862933e-28 6.9958590e-16 6.0640429e-07 7.8472853e-12 2.0985551e-07 4.8857016e-17 3.7977627e-19 3.5136532e-21 9.9999881e-01 1.1159207e-21 9.9667357e-32 1.7269920e-31 7.0009942e-21 5.0873650e-25 1.1708389e-13 2.7525333e-24 1.1056422e-19 2.8127509e-08 2.8245915e-14 2.0870798e-23 1.0649909e-11 0.0000000e+00 3.8043613e-11 1.7576119e-15 7.1381208e-14 7.3414647e-08 2.4327422e-20 9.1099174e-17 3.2708306e-19 1.3278245e-14 3.0282866e-22 6.0491925e-24]] [10]

[10]

but, using dnn module got wrong prediction

# inference using opencv

opencv: import numpy as np

import cv2

np import cv2 x_image = cv2.imread('characters/test_A.jpg', cv2.IMREAD_GRAYSCALE)

cv2.IMREAD_GRAYSCALE) x_image = cv2.resize(x_image, dsize=(16, 40))

x_image = x_image.astype(np.float32)

40)) x_image = x_image.astype(np.float32) x_image = x_image / 255.

255. # read model and inference

inputBlob=cv2.dnn.blobFromImage(x_image)

inference inputBlob=cv2.dnn.blobFromImage(x_image) net = cv2.dnn.readNetFromTensorflow('model/frozen_modelx.pb')

net.setInput(inputBlob)

cv2.dnn.readNetFromTensorflow('model/frozen_modelx.pb') net.setInput(inputBlob) result = net.forward()

print(result)

net.forward() print(result) print(np.argmax(result, 1))

1)) [[1.3639329e-02 3.8205376e-01 4.1754208e-10 1.0312299e-08 4.3897840e-01 2.1898718e-06 1.0198972e-06 1.3872443e-04 1.2519218e-08 3.5530825e-06 4.7783997e-02 2.8187374e-08 5.5049820e-20 3.5439733e-09 1.4154615e-12 2.4284447e-15 2.9786484e-06 2.5385313e-07 1.6377919e-08 1.9443828e-06 5.2226778e-10 3.8744084e-04 1.8799990e-04 1.3955707e-15 1.1665202e-01 4.2024007e-07 1.0303499e-10 3.1994839e-06 6.9113579e-05 4.2022770e-05 2.7376987e-05 2.3721248e-05 5.4692578e-07 1.5755222e-10]] [4]

[4]

Can someone help me to indicate where should be modified! Thank you

I trained a cnn model and save as .pb file. got wrong answer using opencv3.41 dnn module

here is my pb file and some test images.

using tensorflow, got correct prediction

# inference using tensorflow
import tensorflow as tf
import numpy as np
import cv2

x_image = cv2.imread('characters/test_A.jpg', cv2.IMREAD_GRAYSCALE)
x_image = cv2.resize(x_image, dsize=(40, 16))
x_image = x_image / 255.
x_image = x_image.astype(np.float32)


inputBlob = np.reshape(x_image, [-1, 40, 16, 1])

with open('Model/frozen_modelx.pb', 'rb') as f:
    out_graph_def = tf.GraphDef()
    out_graph_def.ParseFromString(f.read())
    tf.import_graph_def(out_graph_def, name="")

    with tf.Session() as sess:
        data = sess.graph.get_tensor_by_name("input/x_input:0")
        prediction = sess.graph.get_tensor_by_name("softmax/prediction:0")

        sess.run(tf.global_variables_initializer())
        x_image_out = sess.run(prediction, feed_dict={data: inputBlob})

        print(x_image_out)
        print(np.argmax(x_image_out, 1))


[[1.6884116e-13 3.1422300e-07 7.3862933e-28 6.9958590e-16 6.0640429e-07
  7.8472853e-12 2.0985551e-07 4.8857016e-17 3.7977627e-19 3.5136532e-21
  9.9999881e-01 1.1159207e-21 9.9667357e-32 1.7269920e-31 7.0009942e-21
  5.0873650e-25 1.1708389e-13 2.7525333e-24 1.1056422e-19 2.8127509e-08
  2.8245915e-14 2.0870798e-23 1.0649909e-11 0.0000000e+00 3.8043613e-11
  1.7576119e-15 7.1381208e-14 7.3414647e-08 2.4327422e-20 9.1099174e-17
  3.2708306e-19 1.3278245e-14 3.0282866e-22 6.0491925e-24]]
[10]

but, using dnn module got wrong prediction

# inference using opencv:  
import numpy as np
import cv2

x_image = cv2.imread('characters/test_A.jpg', cv2.IMREAD_GRAYSCALE)
x_image = cv2.resize(x_image, dsize=(16, 40))
x_image = x_image.astype(np.float32)
x_image = x_image / 255.

# read model and inference
inputBlob=cv2.dnn.blobFromImage(x_image)
net = cv2.dnn.readNetFromTensorflow('model/frozen_modelx.pb')
net.setInput(inputBlob)
result = net.forward()

print(result)
print(np.argmax(result, 1))


[[1.3639329e-02 3.8205376e-01 4.1754208e-10 1.0312299e-08 4.3897840e-01
  2.1898718e-06 1.0198972e-06 1.3872443e-04 1.2519218e-08 3.5530825e-06
  4.7783997e-02 2.8187374e-08 5.5049820e-20 3.5439733e-09 1.4154615e-12
  2.4284447e-15 2.9786484e-06 2.5385313e-07 1.6377919e-08 1.9443828e-06
  5.2226778e-10 3.8744084e-04 1.8799990e-04 1.3955707e-15 1.1665202e-01
  4.2024007e-07 1.0303499e-10 3.1994839e-06 6.9113579e-05 4.2022770e-05
  2.7376987e-05 2.3721248e-05 5.4692578e-07 1.5755222e-10]]
[4]

Can someone help me to indicate where should be modified! Thank you