Difference in prediction for TensorFlow and OpenCV DNN (Inception V1))

asked 2019-02-04 15:33:08 -0600

I am getting different predictions for similar image on native TensorFlow vs OpenCV dnn inference.

I have solved issues with TensorFlow graph using https://github.com/opencv/opencv/issu...

.pb .pbtxt and test images can be obtained at https://drive.google.com/open?id=1WHi...

TensorFlow Inference

import tensorflow as tf
import cv2
import numpy as np
import time

frozen_graph = './transformed_frozen_graph.pb'
if __name__ == "__main__":
    with tf.gfile.GFile(frozen_graph_filename, "rb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
    with tf.Graph().as_default() as graph:
        tf.import_graph_def(graph_def, name='80ms')


x = graph.get_tensor_by_name('80ms/x:0')
keep_prob = graph.get_tensor_by_name('80ms/p:0')
y_hat = graph.get_tensor_by_name('80ms/inception_v1/fully_connected/prob_output:0')

test_image = cv2.imread("./user01_fluorescent@5:139661764.png")
cv2.imshow("test_image", test_image)
cv2.waitKey(50)
test_image = cv2.resize(test_image, (224, 224))
batch_of_size_one = np.zeros((1, 224, 224, 3), dtype=np.float32)
batch_of_size_one[0, :, :, :] = test_image

batch_of_size_one[:, :, :, 0] = batch_of_size_one[:, :, :, 0] - 13.05
batch_of_size_one[:, :, :, 1] = batch_of_size_one[:, :, :, 1] - 13.07
batch_of_size_one[:, :, :, 2] = batch_of_size_one[:, :, :, 2] - 12.39

batch_of_size_one[:, :, :, 0] = batch_of_size_one[:, :, :, 0] / 56.20
batch_of_size_one[:, :, :, 1] = batch_of_size_one[:, :, :, 1] / 56.24
batch_of_size_one[:, :, :, 2] = batch_of_size_one[:, :, :, 2] / 54.82

with tf.Session(graph=graph) as sess:
    start_time = time.time()
    print(sess.run(y_hat, feed_dict={x:batch_of_size_one, keep_prob:

OpenCV Inference

import cv2
import numpy as np
import time

nn_2 = cv2.dnn.readNetFromTensorflow("./transformed_frozen_graph.pb", "./cv_graph.pbtxt")

test_image = cv2.imread("./user01_fluorescent@4:127784861.png")
test_image = cv2.resize(test_image, (224, 224))

blob = cv2.dnn.blobFromImage(test_image, 55.02, (224, 224), (13.05, 13.05, 12.39), False)

nn_2.setInput(blob)
start_time = time.time()
out = nn_2.forward()
print("Eval time:{}".format(time.time() - start_time))


out = out.flatten()
print(out)
classId = np.argmax(out)
print(classId)
confidence = out[classId]
print(confidence)

Number after @ in image name is the correct class. TensorFlow predictions are correct but OpenCV predictions are incorrect.

edit retag flag offensive close merge delete

Comments

Hey did you solve this issue ever? I think i might be having a similar issue where if i run my model on just a batch of images it works great but with openCV i just get the whole screen classified as one category for no apparent reason.

Axees gravatar imageAxees ( 2019-05-10 14:29:03 -0600 )edit