Can not use tensorflow model in Opencv DNN

asked 2017-12-18 23:02:05 -0600

Hi , I am trying to use tensforflow Frozen model in Opencv using dnn.

But I am getting this error after loading the tensorFlow model in opencv dnn.

OpenCV Error: Assertion failed (start < (int)shape.size() && end <= (int)shape.s
ize() && start <= end) in cv::dnn::experimental_dnn_v1::total, file C:\build\mas
ter_winpack-build-win64-vc14\opencv\modules\dnn\include\opencv2/dnn/shape_utils.
hpp, line 159

Detail of my code is . . . .

My Model in python like this

def weight_variable(shape, name):
    initial = tf.random_normal(shape, mean=0.0, stddev=0.1, name=name,dtype=tf.float32)
    return tf.Variable(initial,dtype=tf.float32)
def bias_variable(shape, name):
    initial = tf.constant(0.1, shape=shape, name=name)
    return tf.Variable(initial,dtype=tf.float32)
def conv2d(xxx, W):
    return tf.nn.conv2d(xxx, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(xxx, name):
    return tf.nn.max_pool(xxx, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name=name)
def DNN(x):
    with tf.name_scope('conv1'):
        W_conv1 = weight_variable([3, 3, 1, 32], name='W_conv1')
        b_conv1 = bias_variable([32], name='b_conv1')
        conv = conv2d(x, W_conv1)
        h_conv1 = tf.nn.relu(conv + b_conv1, name='h_conv1')

    with tf.name_scope('pool1'):
        h_pool1 = max_pool_2x2(h_conv1, name='h_pool1')

    with tf.name_scope('conv2'):
        W_conv2 = weight_variable([3, 3, 32, 64], name='W_conv2')
        b_conv2 = bias_variable([64], name='b_conv2')
        h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2, name='h_conv2')

    with tf.name_scope('pool2'):
        h_pool2 = max_pool_2x2(h_conv2, name='h_pool2')

    with tf.name_scope('conv3'):
        W_conv3 =  weight_variable([3, 3, 64, 128], name='W_conv3')
        b_conv3 = bias_variable([128], name='b_conv3')
        h_conv3 = tf.nn.relu(conv2d(h_pool2, W_conv3) + b_conv3, name='h_conv3')

    with tf.name_scope('pool3'):
        h_pool3 = max_pool_2x2(h_conv3, name='h_pool3')

    with tf.name_scope('fc1'):
        W_fc1 = weight_variable([4*2*128, 1024], name='W_fc1')
        b_fc1 = bias_variable([1024], name='b_fc1')
        tf.transpose(h_pool3, [0, 3, 1, 2])
        h_pool3_flat = tf.reshape(h_pool3, [-1, 4*2*128], name='h_pool3_flat')
        h_fc1 = tf.nn.relu(tf.matmul(h_pool3_flat, W_fc1) + b_fc1, name='h_fc1')

with tf.name_scope('output'): W_fc2 = weight_variable([1024, 2], name='W_fc2') b_fc2 = bias_variable([2], name='b_fc2') y_conv = tf.matmul(h_fc1,W_fc2) + b_fc2 prediction = tf.nn.softmax(y_conv, name='prediction') return y_conv,prediction

with tf.name_scope('input'):
    x = tf.placeholder(tf.float32, [None, 32, 16, 1], name ='x_input')
    y_label = tf.placeholder(tf.float32, [None, 2],name='y_input')

And training is like this

 for i in range(total_batch):
        batch = train_mini_batch(batch_size)
        batch_xs = batch[0].values.reshape([batch_size, 32, 16, 1])
        batch_ys = batch[1]
        c, _ = sess.run([cross_entropy, train_step], feed_dict={x: batch_xs, y_label: batch_ys})

Saving Model and graph

save_path = saver.save(sess, model_path)
tf.train.write_graph(sess.graph_def, 'E:\temp5', 'trainModel_3.pb',as_text=False)

Saving Frozen Model

output_node_names = 'input/x_input,input/y_input,output/prediction'
constant_graph = tf.graph_util.convert_variables_to_constants(sess, sess.graph_def, output_node_names.split(','))
with tf.gfile.GFile('frozen_model_temp8.pb', "wb") as f:
    f.write(constant_graph.SerializeToString())

And opencv Code

#include <iostream>

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

int main() {
    cv::dnn::Net net = cv::dnn::readNetFromTensorflow("E:/frozen_model.pb");

    cv::Mat input = cv::imread("E:/test.jpg", 0);
    cv ...
(more)
edit retag flag offensive close merge delete

Comments

@mindHunter099 it looks like your model accepts 1 channel images with height 32 and width 16, right? But blobFromImage has cv::Size(32, 16) that is width 32 and height 16.

dkurt gravatar imagedkurt ( 2017-12-19 01:52:36 -0600 )edit

Yes you are right.

I also checked with this one cv::Size(16, 32) Height : 32 , Width :16

But still not working.

And in opencv my input image is also grayscale cv::Mat input = cv::imread("E:/test.jpg", 0);

Thank you so mcuh

mindHunter099 gravatar imagemindHunter099 ( 2017-12-19 03:37:29 -0600 )edit

Kindly also check this my input blob shape

Input Blob shape: 1 x 1 x 32 x 16

OpenCV Error: Assertion failed (start < (int)shape.size() && end <= (int)shape.s
ize() && start <= end) in cv::dnn::experimental_dnn_v1::total, file C:\build\mas
ter_winpack-build-win64-vc14\opencv\modules\dnn\include\opencv2/dnn/shape_utils.
hpp, line 159
Press any key to continue . . .

Thank you so mcuh and waiting for your help

mindHunter099 gravatar imagemindHunter099 ( 2017-12-19 03:47:24 -0600 )edit

@mindHunter099 please specify state of OpenCV source code. Release version or commit id. Try to use the latest master branch.

dkurt gravatar imagedkurt ( 2017-12-20 11:15:15 -0600 )edit