尺寸必须相等,但对于具有输入形状的'Conv2D_2'(op:'Conv2D'),尺寸必须为64和32:[?,7,7,64],[5,5,32,64]

问题描述 投票:0回答:3

这是一个5卷积分层代码:

 # Template program with one convolution layer and one fully connected hidden layer, 
    # dropout and Adam Optimizer
    from tensorflow.examples.tutorials.mnist import input_data
    mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

    import tensorflow as tf
    sess = tf.InteractiveSession()

    # xi is an image of size n. yi is the N labels of the image
    # X is mxn. Row xi of X is an image 
    # Y is mxN. Row yi of Y is the labels of xi
    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 Max Pool layer -- to resize the image to half of the image size
    orig_image = tf.reshape(X, [-1,28,28,1])
    h_pool0 = max_pool_2x2(orig_image)
    ### End of first max pool layer ###

    # First Convolutional Layer

    W_conv1 = weight_variable([5, 5, 1, 32])
    b_conv1 = bias_variable([32])

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

    # Second Convolutional 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)

    # Third Convolutional Layer

    W_conv3 = weight_variable([5, 5, 64, 128])
    b_conv3 = bias_variable([128])

    h_conv3 = tf.nn.relu(conv2d(h_conv2, W_conv2) + b_conv2)

    # Fourth Convolutional Layer

    W_conv3 = weight_variable([5, 5, 128, 256])
    b_conv3 = bias_variable([256])

    h_conv3 = tf.nn.relu(conv2d(h_conv2, W_conv2) + b_conv2)

    # Fifth Convolutional Layer

    W_conv3 = weight_variable([5, 5, 256, 512])
    b_conv3 = bias_variable([512])

    h_conv3 = tf.nn.relu(conv2d(h_conv2, W_conv2) + b_conv2)

    # Densely Connected Layer

    W_fc1 = weight_variable([7 * 7 * 512, 4096])
    b_fc1 = bias_variable([4096])

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

    # Dropout

    keep_rate = 0.8
    keep_prob = tf.placeholder(tf.float32)
    h_fc1_drop = tf.nn.dropout(h_fc1, keep_rate)

    # Readout Layer

    W_fc2 = weight_variable([4096, 10])
    b_fc2 = bias_variable([10])

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

    cross_entropy = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(labels=Y, logits=y_conv))
    #train_step = tf.train.GradientDescentOptimizer(0.005).minimize(cross_entropy)
    train_step = tf.train.AdamOptimizer().minimize(cross_entropy)

    correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(Y,1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    sess.run(tf.global_variables_initializer())
    for i in range(1000):
      batch = mnist.train.next_batch(100)
      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.0}))

我不知道为什么,但每当我运行它时,我都会收到此错误消息:

Traceback (most recent call last):
  File "C:\Users\Inshal\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 671, in _call_cpp_shape_fn_impl
    input_tensors_as_shapes, status)
  File "C:\Users\Inshal\AppData\Local\Programs\Python\Python35\lib\contextlib.py", line 66, in __exit__
    next(self.gen)
  File "C:\Users\Inshal\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 466, in raise_exception_on_not_ok_status
    pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Dimensions must be equal, but are 64 and 32 for 'Conv2D_2' (op: 'Conv2D') with input shapes: [?,7,7,64], [5,5,32,64].

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/Inshal/Desktop/Inshal Haq/UTD/Spring 2017/Computer Vision/Project2/mnist3.py", line 57, in <module>
    h_conv3 = tf.nn.relu(conv2d(h_conv2, W_conv2) + b_conv2)
  File "C:/Users/Inshal/Desktop/Inshal Haq/UTD/Spring 2017/Computer Vision/Project2/mnist3.py", line 25, in conv2d
    return tf.nn.conv2d(X, W, strides=[1, 1, 1, 1], padding='SAME')
  File "C:\Users\Inshal\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\gen_nn_ops.py", line 403, in conv2d
    data_format=data_format, name=name)
  File "C:\Users\Inshal\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 768, in apply_op
    op_def=op_def)
  File "C:\Users\Inshal\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 2338, in create_op
    set_shapes_for_outputs(ret)
  File "C:\Users\Inshal\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 1719, in set_shapes_for_outputs
    shapes = shape_func(op)
  File "C:\Users\Inshal\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 1669, in call_with_requiring
    return call_cpp_shape_fn(op, require_shape_fn=True)
  File "C:\Users\Inshal\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 610, in call_cpp_shape_fn
    debug_python_shape_fn, require_shape_fn)
  File "C:\Users\Inshal\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 676, in _call_cpp_shape_fn_impl
    raise ValueError(err.message)
ValueError: Dimensions must be equal, but are 64 and 32 for 'Conv2D_2' (op: 'Conv2D') with input shapes: [?,7,7,64], [5,5,32,64].

有谁知道我的代码有什么问题,我该如何解决?

谢谢任何人的意见!

python tensorflow mnist
3个回答
0
投票

您在每个图层中的过滤器大小都有问题,您应该打印每个图层的形状以进行跟踪和调试,在每次操作后使用get_shape(),如:

h_conv1 = tf.nn.relu(conv2d(h_pool0, W_conv1) + b_conv1)
print h_conv1.get_shape()
h_pool1 = max_pool_2x2(h_conv1)
print h_pool1.get_shape()

# Second Convolutional 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)
print h_conv2.get_shape()

然后你就可以意识到出了什么问题。


0
投票

阿里的答案是调试问题的好方法。您的具体问题是您的代码中有拼写错误 - 您在第三个卷积层中使用了错误的权重。

# Third Convolutional Layer

W_conv3 = weight_variable([5, 5, 64, 128])
b_conv3 = bias_variable([128])

# You wrote
# h_conv3 = tf.nn.relu(conv2d(h_conv2, W_conv2) + b_conv2)
# when you probably meant:
h_conv3 = tf.nn.relu(conv2d(h_conv2, W_conv3) + b_conv3)

希望有所帮助!


0
投票

你使用相同的图层名称错误地在第4和第5层进行“conv2d”操作。 enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.