Ask Your Question
1

How to import a tensorflow model with dropout in opencv

asked 2018-04-09 09:42:02 -0500

AyushP123 gravatar image

Hi, I tried using the advice here -> https://github.com/opencv/opencv/issu.... Saving the checkpoint with dropout and saving the graph without dropout. Here is the issue that I face, along with the name of all the layers read by OpenCV 3.4.1:

Conv1/convolution1/Conv2D

Conv1/convolution1/Relu

Conv1/pooling_1

Conv2/convolution2/Conv2D

Conv2/convolution2/Relu

Conv2/pooling_2

Conv3/convolution3/Conv2D

Conv3/convolution3/Relu

Conv3/Reshape/nchw Conv3/Reshape

Dense1/fully_connected5/MatMul

Dense1/fully_connected5/Relu

Dense2/fully_connected6/MatMul

Dense2/fully_connected6/Relu

Dense2/output_node [ INFO:0]

Initialize OpenCL runtime...

OpenCV(3.4.1) Error: Backtrace (Can't infer a dim denoted by -1) in computeShapeByReshapeMask, file /home/ayushpandey/opencv/modules/dnn/src/layers/reshape_layer.cpp, line 136 terminate called after throwing an instance of 'cv::Exception'

what(): OpenCV(3.4.1) /home/ayushpandey/opencv/modules/dnn/src/layers/reshape_layer.cpp:136: error: (-1) Can't infer a dim denoted by -1 in function computeShapeByReshapeMask

Aborted (core dumped)

Here is my tensorflow code ->https://pastebin.com/mMgRbHV0 ( Commented lines are uncommented and the lines corresponding to uncommented pool1, pool2 and saver.save is commented for saving graph without dropout ).

Then I run the following commands to freeze the graph.

python freeze_graph.py --input_graph=./input_graph.pb --input_checkpoint=./with_dropout.ckpt --output_graph=./output_graph.pb --output_node=Dense2/output_node

followed by

python optimize_for_inference.py --input output_graph.pb --output without_dropout.pb --frozen_graph True --input_names input_node --output_names Dense2/output_node

and then I try to load the "without_dropout.pb" with OpenCV where I face the issue.

edit retag flag offensive close merge delete

Comments

@AyushP123, without_dropout.pb attached can save some time to reproduce the problem. Please put a reference if it's possible.

dkurt gravatar imagedkurt ( 2018-04-09 09:50:46 -0500 )edit

@AyushP123, please specify input dimensions.

dkurt gravatar imagedkurt ( 2018-04-10 03:15:00 -0500 )edit

[NONE, 125, 125, 1]

AyushP123 gravatar imageAyushP123 ( 2018-04-10 03:36:53 -0500 )edit

@AyushP123, please check it again because the model doesn't work even in TensorFlow with 125x125 inputs.

dkurt gravatar imagedkurt ( 2018-04-10 03:43:38 -0500 )edit

Sorry for the mistake .... the input size is [NONE, 250, 250, 1]

AyushP123 gravatar imageAyushP123 ( 2018-04-10 04:44:26 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2018-04-10 04:52:49 -0500

dkurt gravatar image

Try to use the master branch of OpenCV. I cannot reproduce the problem.

graph = 'without_dropout.pb'
cvNet = cv.dnn.readNet(graph)

with tf.gfile.FastGFile(graph) as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    print graph_def.node[0]
    print graph_def.node[-1]

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, 250, 250, 1]).astype(np.float32)
    out = sess.run(sess.graph.get_tensor_by_name('Dense2/output_node:0'),
                   feed_dict={'input_node:0': inp})

    cvNet.setInput(inp.transpose(0, 3, 1, 2))
    cvOut = cvNet.forward()

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

outputs 2.9802322e-07.

edit flag offensive delete link more

Comments

Sorry for wasting your time .... it turns out that I was using the wrong image size .... should I remove this question as its not useful??

AyushP123 gravatar imageAyushP123 ( 2018-04-10 05:40:44 -0500 )edit

@AyushP123, don't worry. Let's keep it.

dkurt gravatar imagedkurt ( 2018-04-10 06:27:35 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-04-09 09:42:02 -0500

Seen: 285 times

Last updated: Apr 10 '18