使用Tensorflow的MNIST教程,我尝试使用"面部数据库"创建一个用于人脸识别的卷积网络.
图像大小为112x92,我使用3个卷积层将其减少到6 x 5,如此处所示
我在卷积网络上非常新,我的大部分层声明是通过类比Tensorflow MNIST教程制作的,它可能有点笨拙,所以请随时向我提出建议.
x_image = tf.reshape(x, [-1, 112, 92, 1]) h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) h_pool1 = max_pool_2x2(h_conv1) 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) W_conv3 = weight_variable([5, 5, 64, 128]) b_conv3 = bias_variable([128]) h_conv3 = tf.nn.relu(conv2d(h_pool2, W_conv3) + b_conv3) h_pool3 = max_pool_2x2(h_conv3) W_conv4 = weight_variable([5, 5, 128, 256]) b_conv4 = bias_variable([256]) h_conv4 = tf.nn.relu(conv2d(h_pool3, W_conv4) + b_conv4) h_pool4 = max_pool_2x2(h_conv4) W_conv5 = weight_variable([5, 5, 256, 512]) b_conv5 = bias_variable([512]) h_conv5 = tf.nn.relu(conv2d(h_pool4, W_conv5) + b_conv5) h_pool5 = max_pool_2x2(h_conv5) W_fc1 = weight_variable([6 * 5 * 512, 1024]) b_fc1 = bias_variable([1024]) h_pool5_flat = tf.reshape(h_pool5, [-1, 6 * 5 * 512]) h_fc1 = tf.nn.relu(tf.matmul(h_pool5_flat, W_fc1) + b_fc1) keep_prob = tf.placeholder("float") h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) print orlfaces.train.num_classes # 40 W_fc2 = weight_variable([1024, orlfaces.train.num_classes]) b_fc2 = bias_variable([orlfaces.train.num_classes]) y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
当会话运行"correct_prediction"操作时,我的问题出现了
tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
至少我认为给出错误信息:
W tensorflow/core/common_runtime/executor.cc:1027] 0x19369d0 Compute status: Invalid argument: Incompatible shapes: [8] vs. [20] [[Node: Equal = Equal[T=DT_INT64, _device="/job:localhost/replica:0/task:0/cpu:0"](ArgMax, ArgMax_1)]] Traceback (most recent call last): File "./convolutional.py", line 133, intrain_accuracy = accuracy.eval(feed_dict = {x: batch[0], y_: batch[1], keep_prob: 1.0}) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 405, in eval return _eval_using_default_session(self, feed_dict, self.graph, session) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2728, in _eval_using_default_session return session.run(tensors, feed_dict) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 345, in run results = self._do_run(target_list, unique_fetch_targets, feed_dict_string) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 419, in _do_run e.code) tensorflow.python.framework.errors.InvalidArgumentError: Incompatible shapes: [8] vs. [20] [[Node: Equal = Equal[T=DT_INT64, _device="/job:localhost/replica:0/task:0/cpu:0"](ArgMax, ArgMax_1)]] Caused by op u'Equal', defined at: File "./convolutional.py", line 125, in correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_math_ops.py", line 328, in equal return _op_def_lib.apply_op("Equal", x=x, y=y, name=name) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/op_def_library.py", line 633, in apply_op op_def=op_def) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1710, in create_op original_op=self._default_original_op, op_def=op_def) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 988, in __init__ self._traceback = _extract_stack()
看起来y_conv输出一个形状为8 x batch_size的矩阵,而不是number_of_class x batch_size
如果我将批量大小从20更改为10,则错误消息保持不变,而是[8]与[20]相比,我得到[4]与[10].因此我得出结论,问题可能来自y_conv声明(上面代码的最后一行).
损失函数,优化器,训练等声明与MNIST教程中的相同:
cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv)) train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) sess.run((tf.initialize_all_variables())) for i in xrange(1000): batch = orlfaces.train.next_batch(20) 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: orlfaces.test.images, y_: orlfaces.test.labels, keep_prob: 1.0})
感谢您的阅读,祝您有个美好的一天
好吧,经过大量调试后,我发现我的问题是由于标签的实例化不好造成的.我没有创建充满零的数组并将一个值替换为一个,而是使用随机值创建它们!愚蠢的错误.如果有人想知道我做错了那里,我怎么解决它在这里是我所做的更改.
无论如何,在我做的所有调试中,为了发现这个错误,我发现了一些有用的信息来调试这类问题:
对于交叉熵声明,tensorflow的MNIST教程使用可导致NaN值的公式
这个公式是
cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv))
取而代之的是,我发现了两种以更安全的方式声明它的方法:
cross_entropy = -tf.reduce_sum(y_ * tf.log(tf.clip_by_value(y_conv, 1e-10, 1.0)))
或者:
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logit, y_))
正如迈里所说.打印张量的形状有助于检测形状异常.
要获得张量的形状,只需调用他的get_shape()方法,如下所示:
print "W shape:", W.get_shape()
此问题中的user1111929 使用调试打印,帮助我断言问题的来源.