【问题标题】:Adapting TensorFlow code droput layer to allow import into openCV调整 TensorFlow 代码丢弃层以允许导入到 openCV
【发布时间】:2018-05-04 23:48:20
【问题描述】:

背景

我想创建和训练一个卷积神经网络,并使用 c++ 在另一台设备上实现训练好的模型。

在研究了可能的解决方案后,我决定使用 keras 创建和训练模型,将训练后的模型导出为 .pb,然后将其导入到 openCV 网络模型。

openCV importNetFromTensorFLow 函数在我的情况下不起作用,因为它无法正确转换 dropout 层。已经发布了解决方法/修复here。在我看来,不幸的是,我将不得不使用 tensorflow 而不是 keras 创建一个模型来实现解决方法。

问题

我使用tutorial 制作了一个 cnn,以尝试应用上述解决方法/修复。但我无法让它工作。

from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.python.framework import function
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

import tensorflow as tf
sess = tf.InteractiveSession();

isTraining = tf.placeholder(tf.bool, name='isTraining')

@function.Defun(tf.float32, func_name='Dropout')
def my_dropout(x):
    return tf.layers.dropout(x, rate=0.1, training=isTraining)


x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])

def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)

def bias_variable(shape):
    initial = tf.constant(0.1,shape=shape)
    return tf.Variable(initial)

def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1,1,1,1],padding='SAME')

def max_pool_2x2(x):
    return tf.nn.max_pool(x, ksize=[1,2,2,1],
                        strides=[1,2,2,1], padding='SAME')

#First conv layer                           
W_conv1 = weight_variable([5,5,1,32])
b_conv1 = bias_variable([32])

x_image = tf.reshape(x,[-1,28,28,1])

h_conv1 = tf.nn.relu(conv2d(x_image,W_conv1)+ b_conv1)
h_pool1 = max_pool_2x2(h_conv1)

#Second conv layer
W_conv2 = weight_variable([5,5,32,64])
b_conv2 = bias_variable([64])

h_conv2 = tf.nn.relu(conv2d(h_pool1,W_conv2)+ b_conv2)
h_pool2 = max_pool_2x2(h_conv2)

#Dense layer
W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])

h_pool2_flat = tf.reshape(h_pool2, [-1,7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1)+ b_fc1)

#Dropout layer
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = my_dropout(h_conv2)

#Readout Layer
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])

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

错误:您必须为占位符张量“isTraining”提供一个值 数据类型布尔

如果有人可以帮助我将此修复程序集成到教程中,将不胜感激,谢谢。

编辑

#train
cross_entropy = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correction_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correction_prediction, tf.float32)) 

with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(20000):
    batch = mnist.train.next_batch(50)
    if i % 100 == 0:
        train_accuracy = accuracy.eval(feed_dict={
            x: batch[0], y_: batch[1], keep_prob: 1.0})
        print('step %d, training accuracy %g' % (i, train_accuracy))
    train_step.run(feed_dict={x: batch[0], y_:batch[1],keep_prob:0.5})

print('test accuracy %g' % accuracy.eval(feed_dict={
    x: mnist.test.images, y_: mnist.test.labels,keep_prob:1}))

编辑2:新错误

在第 66 行引起了我的 MatMul_1

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

错误

In[0] is not a matrix 

【问题讨论】:

  • 您能发布您用于执行培训/评估的行吗?您可能需要将 isTraining 占位符添加到您的 feeddict。
  • 我已将其发布为对我最初问题的编辑
  • 对不起,我指的是你运行train_step张量的部分。
  • 我的错,现在添加它

标签: python opencv tensorflow keras


【解决方案1】:

当您运行 cross_entropytrain_step 张量时,您需要在 feed dict 中添加 isTraining 占位符:

train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob:0.5, isTraining: True})

(当然,训练时通过True,不训练时通过False)。

编辑:你还需要

accuracy.eval(feed_dict={
x: mnist.test.images, y_: mnist.test.labels, keep_prob:1, isTraining: False}))

您可以完全删除代码和提要字典中的 keep_prob 占位符,因为您从不使用它。

第二次编辑:

我漏了一行,你也需要补充一下

        train_accuracy = accuracy.eval(feed_dict={
            x: batch[0], y_: batch[1], keep_prob: 1.0, isTraining: False})

上面。

【讨论】:

  • 感谢您解决了我的问题,尽管发生了新错误,我会将其添加到我原来的问题中,如果您想再次提供帮助,您可以。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-18
  • 2011-07-10
  • 2011-05-28
  • 2020-11-19
相关资源
最近更新 更多