使用Opencv评估张量流模型失败 [英] Evaluation of tensorflow model with Opencv fails

查看:80
本文介绍了使用Opencv评估张量流模型失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下简单的卷积网络:

I am using the following simple convolutional network:

def forward_pass(train_batch):

    with tf.name_scope('input'):
        inputData = tf.identity(train_batch)

    #network definition
    #image input 32 x 32 x 3
    #weights for the first convolution layer

    with tf.name_scope('conv1'):
        W_conv1 = weight_variable([5, 5, 3, 6])
        b_conv1 = bias_variable([6])
        #structure of the first convolution layer: convolution, activation, pooling
        h_conv1 = tf.nn.tanh(tf.nn.bias_add(conv2d(inputData, W_conv1), b_conv1))

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

    #image input 14 x 14 x 6
    #weights for the second convolution layer 

    with tf.name_scope('conv2'):
        W_conv2 = weight_variable([5, 5, 6, 16])
        b_conv2 = bias_variable([16])
        #structure of the second convolution layer: convolution, activation, pooling
        h_conv2 = tf.nn.tanh(tf.nn.bias_add(conv2d(h_pool1, W_conv2), b_conv2))

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

    #image input 10 x 10 x 16

    with tf.name_scope('reshape'):
        h_pool2_flat = tf.reshape(h_pool2, [-1, 5 * 5 * 16])


    #fully connected layer
    with tf.name_scope('fc'):
        W_fc1 = weight_variable([5 * 5 * 16, 46])
        b_fc1 = bias_variable([46])
        y_re = tf.matmul(h_pool2_flat, W_fc1)
        fc_output = tf.nn.bias_add(y_re, b_fc1)

    with tf.name_scope('output'):
        output = tf.multiply(fc_output, 1.)


    return output

要训练网络,我使用Adam优化器,如下所示:

To train the network I use the Adam optimizer as follows:

output = forward_pass(train_batch)

with tf.name_scope('one-hot'):
    label_batch_vector = tf.one_hot(label_batch, 46)

with tf.name_scope('loss'):
    cross_entropy = tf.reduce_mean(
     tf.nn.softmax_cross_entropy_with_logits(labels=label_batch_vector, logits=output))

with tf.name_scope('adam-optimizer'):
    train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

要保存我最后使用的网络:

To save the network I use at the end:

tf.train.write_graph(sess.graph.as_graph_def(), "", 'graph.pb')

使用TensorFlow进行培训会产生良好的效果,但是现在我想使用OpenCV中的网络.

Training with TensorFlow yields good results but now I would like to use the network from OpenCV.

我对网络进行如下转换:

I transform the network as follows:

python3 $TF_TOOLS/freeze_graph.py \
  --input_graph=graph.pb \
  --input_checkpoint=Model.ckpt \
  --output_graph=frozen_graph.pb \
  --output_node_names=output/Mul

python3 $TF_TOOLS/optimize_for_inference.py \
  --input frozen_graph.pb \
  --output opt_graph.pb \
   --frozen_graph True \
   --input_names input/Identity \
  --output_names output/Mul

我正在使用opt_graph.pb用cv :: dnn :: readNetFromTensorflow加载模型.然后,我在训练数据上进行测试,得到2%的识别率.有人可以帮忙吗?是否有可能我没有正确地将权重保存在opt_graph.pb中?在我看来,训练数据已经丢失.

I am using then opt_graph.pb to load the model with cv::dnn::readNetFromTensorflow. Then I am testing on my training data and I get a 2% recognition rate. Can anyone help ? Is it possible that I did not save the weights properly in opt_graph.pb ? To me it seems like the training data has been lost.

推荐答案

解决方案是在tf.reshape操作之前添加一个tf.transpose(h_pool2,[0,3,1,2])操作.在 https://github.com/opencv/opencv/issues/10065.

The solution is to add a tf.transpose(h_pool2, [0, 3, 1, 2]) operation before the tf.reshape operation. See additional info in https://github.com/opencv/opencv/issues/10065.

这篇关于使用Opencv评估张量流模型失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆