OpenCV3.4 DNN forward custom and pre-trained Tensorflow

asked 2019-08-20 23:12:42 -0600

Ubuntu 18.04 , OpenCV 3.4, Python 3.6, Tensorflow ,

Windows Opencv4.1

Trying to use a tensorflow model and when attempting a forward() operation get the following error: Can't create layer "dropout/dropout/random_uniform/RandomUniform" of type "RandomUniform"

The model/logic is based on a ANPR chapter from Mastering OpenCV 4 3rd Edition, however have trained with new images.

The model works fine using Tensorflow.

Steps taken:

  1. Train Model
  2. Freeze graph python3 freeze_graph.py --input_graph=graph.pbtxt --input_checkpoint=model.ckpt-20000 --output_graph frozen_graph.pb --output_node_names=softmax_tensor

3 Transform graph: ~/tensorflow/bazel-bin/tensorflow/tools/graph_transforms/transform_graph --in_graph="frozen_graph.pb" --out_graph="frozen_cut_graph.pb" --inputs="IteratorGetNext" --outputs="softmax_tensor" --transforms='strip_unused_nodes(type=half, shape="1,20,20,1") fold_constants(ignore_errors=true) fold_batch_norms fold_old_batch_norms sort_by_execution_order'

  1. optimize for inference: python3 ~/tensorflow/tensorflow/python/tools/optimize_for_inference.py --input frozen_cut_graph.pb --output frozen_cut_graph_opt.pb --frozen_graph True --input_names IteratorGetNext --output_names softmax_tensor

Here's a test script - uses both TF and OpenCV:

import tensorflow as tf
import numpy as np
import cv2

x_image = cv2.imread('5.jpg', cv2.IMREAD_GRAYSCALE)
x_image = cv2.resize(x_image, dsize=(20, 20))

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


with open('frozen_cut_graph.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:
        for n in sess.graph.as_graph_def().node:
            print (n.name)
        data = sess.graph.get_tensor_by_name("IteratorGetNext:0")
        prediction = sess.graph.get_tensor_by_name("softmax_tensor:0")

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

        print(np.argmax(x_image_out, 1))

print ("OpenCV DNN")

inputBlob=cv2.dnn.blobFromImage(x_image)
net = cv2.dnn.readNetFromTensorflow('frozen_cut_graph.pb')
net.setInput(inputBlob)
result = net.forward()

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

Tensorflow inference works fine.

Using the model in a Windows app as well - same error opening using OpenCV 4.1 in .NET 4.7/VS2019:

Net dnn_net = Emgu.CV.Dnn.DnnInvoke.ReadNetFromTensorflow("frozen_cut_graph.pb");
var m= CvInvoke.Imread(@"5.jpg", Emgu.CV.CvEnum.ImreadModes.Grayscale);

Mat blob = DnnInvoke.BlobFromImage(m, 1, new Size(20, 20),default(MCvScalar),true,false);
dnn_net.SetInput(blob);
//
var detection = dnn_net.Forward();

All files, including results of Tensorflow training including checkpoints and resulting frozen and processed model files, sample image and test script can be found at https://github.com/steinborge/opencva....

Thanks in advance for any help.

edit retag flag offensive close merge delete

Comments

1

sorry but openCV wrappers EmguCV is off topics. Use http://www.emgu.com/forum/

LBerger gravatar imageLBerger ( 2019-08-21 02:23:12 -0600 )edit

I only added as additional information - happy to edit if it's a requirement for posting.

xmeister gravatar imagexmeister ( 2019-08-21 03:01:22 -0600 )edit

if you really want quick help on DL issues, open up a full report on github repo and @dkurt will jump in quite suddenly to see what can be done :)

StevenPuttemans gravatar imageStevenPuttemans ( 2019-08-21 06:01:55 -0600 )edit