Ask Your Question
0

DNN module forward() problem

asked 2017-09-18 23:34:15 -0500

Juventi gravatar image

updated 2017-09-19 04:40:55 -0500

berak gravatar image

cv::dnn::Net net = cv::dnn::readNetFromTensorflow(modelTxt); cv::Mat output = net.forward();

when use dnn module, import a tensorflow model successfully, then do the forward() calculation, a error throw:

OpenCV Error: Assertion failed (start < (int)shape.size() && end <= (int)shape.size() && start <= en
d) in cv::dnn::experimental_dnn_v1::total, file D:\software\opencv\opencv-3.3.0\modules\dnn\include\
opencv2/dnn/shape_utils.hpp, line 159

the assert is here:

static inline int total(const MatShape& shape, int start = -1, int end = -1)
    ...
    CV_Assert(start < (int)shape.size() && end <= (int)shape.size() &&
              start <= end);
    ...

My Graph is

def cnn_net(_X, n_classes, imagesize, img_channel):

   with tf.name_scope('ConvLayer1'):

        W_conv1 = weight_variable([5,5,3,32])
        b_conv1 = bias_variable([32])
        l_conv1 = tf.nn.relu(tf.nn.conv2d(_X,W_conv1, strides=[1,1,1,1],padding='SAME') + b_conv1)
        l_pool1 = tf.nn.max_pool(l_conv1, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')

    with tf.name_scope('ConvLayer2'):

        W_conv2 = weight_variable([5,5,32,64])
        b_conv2 = bias_variable([64])
        l_conv2 = tf.nn.relu(tf.nn.conv2d(l_pool1, W_conv2, strides=[1,1,1,1], padding='SAME')+b_conv2)
        l_pool2 = tf.nn.max_pool(l_conv2, ksize=[1,2,2,1],strides=[1,2,2,1], padding='SAME')

    with tf.name_scope('fcLayer1'):

        W_fc1 = weight_variable([64*8*8, 1024])
        b_fc1 = bias_variable([1024])
        l_pool2_flat = tf.reshape(l_pool2, [-1, 64*8*8])
        l_fc1 = tf.nn.relu(tf.matmul(l_pool2_flat, W_fc1) + b_fc1)

    with tf.name_scope('fcLayer2'):

        W_fc2 = weight_variable([1024, n_classes])
        b_fc2 = bias_variable([n_classes])
        y_conv = tf.matmul(l_fc1, W_fc2) + b_fc2

    return y_conv


x = tf.placeholder(tf.float32, name='inputs_placeholder', shape = [None, 32,32,3])
y = tf.placeholder(tf.int32, name='labels_placeholder', shape = [None, n_classes])

pred = cnn_net(x,n_classes, imagesize, img_channel)  
Y_re = tf.nn.softmax(pred,  name="y_pre")


Input : inputs_placeholder
Output: y_pre

this is my graph, i've already exclude dropout and other components that cannot import in Opencv

and the model is imported successfully, but i cannot foward with an single input image

BTW, by which way can you show the .pb file in the text window( at Github before) ?

edit retag flag offensive close merge delete

Comments

@Juventi, it'll be easier to know the model. More detailed sample will be also great to see.

dkurt gravatar imagedkurt ( 2017-09-19 03:35:12 -0500 )edit
1

hi, i've posted my graph..

Juventi gravatar imageJuventi ( 2017-09-19 04:14:57 -0500 )edit

1 answer

Sort by » oldest newest most voted
2

answered 2017-09-19 06:20:43 -0500

dkurt gravatar image

updated 2017-09-19 06:23:25 -0500

@Juventi, thanks for the code! I have no problems.

There are my graph postprocessing:

python ~/tensorflow/tensorflow/python/tools/freeze_graph.py \
  --input_graph=graph.pb \
  --input_checkpoint=tmp.ckpt \
  --output_graph=frozen_graph.pb \
  --output_node_names=y_pre

python ~/tensorflow/tensorflow/python/tools/optimize_for_inference.py \
  --input frozen_graph.pb \
  --output opt_graph.pb \
  --frozen_graph True \
  --input_names inputs_placeholder \
  --output_names y_pre

And the invocation:

#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>

int main(int argc, char** argv) {
  cv::dnn::Net net = cv::dnn::readNetFromTensorflow("../opt_graph.pb");

  cv::Mat input(32, 32, CV_32FC3);
  net.setInput(cv::dnn::blobFromImage(input));
  net.forward();
  return 0;
}

Are you sure that the latest OpenCV version is used?

edit flag offensive delete link more

Comments

yes...opencv 3.3 i downloaded 2 days ago
it's strange a more complex tf_model is runing in opencv ok, but this simple one cannot work ……

Juventi gravatar imageJuventi ( 2017-09-19 23:15:39 -0500 )edit

@Juventi, can you reproduce these steps? How it works?

dkurt gravatar imagedkurt ( 2017-09-19 23:38:46 -0500 )edit

...I think i've fixed it.....

y_conv = tf.matmul(l_fc1, W_fc2) + b_fc2

should be repalced with:

Y_re = tf.matmul(l_fc1, W_fc2)

y_conv = tf.nn.bias_add(Y_re, b_fc2)

then the forward() can run...

Juventi gravatar imageJuventi ( 2017-09-20 01:19:35 -0500 )edit

the reason may be , if I use '+' or tf.add, the shape of bias is not certain? but tf.nn.bias_add can make sure the bias shape alone with the weights shape, is that true?

it seems that follow a strict code rule can prevent strange prblems...

Juventi gravatar imageJuventi ( 2017-09-20 01:30:33 -0500 )edit

@Juventi, I think you just need to use the latest master branch. We've already fixed it recently. May you try?

dkurt gravatar imagedkurt ( 2017-09-20 02:25:38 -0500 )edit

sure I will try it ..

Juventi gravatar imageJuventi ( 2017-09-20 20:41:13 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

Asked: 2017-09-18 23:34:15 -0500

Seen: 1,184 times

Last updated: Sep 19 '17