Ask Your Question
0

cv exception using DNN [closed]

asked 2018-04-10 04:16:49 -0600

Hank538 gravatar image

Hi

I test frozen_graph.pb CNN model fine in tensorflow code for MNIST.

But when I test with OpenCV, it output error log below.

error: C:\projects\opencv-python\opencv\modules\dnn\src\tensorflow\tf_importer.cpp:1487: error: (-2) Unknown layer type NoOp in op init in function cv::dnn::experimental_dnn_v3::`anonymous-namespace'::TFImporter::populateNet

frozen_graph.pb here.

https://drive.google.com/file/d/1C0ak...

Test image here.

https://drive.google.com/file/d/1rna2...

Please help.

Thanks

Hank

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Hank538
close date 2018-04-10 23:10:31.114297

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-04-10 04:39:44 -0600

dkurt gravatar image

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.

edit flag offensive delete link more

Comments

Finally, it works! Appreciate your support. Thanks!

Hank538 gravatar imageHank538 ( 2018-04-10 23:10:17 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-04-10 04:16:49 -0600

Seen: 571 times

Last updated: Apr 10 '18