简体   繁体   中英

Dimensions must be equal, but are 64 and 32 for 'Conv2D_2' (op: 'Conv2D') with input shapes: [?,7,7,64], [5,5,32,64]

This is a 5 convolutional-layered code:

 # 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}))

And I am not sure why, but I am receiving this error message whenever I run it:

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].

Does anyone know what is the issue with my code, and how can I fix it?

Thank you for anyone's input!

You have a problem with your filter size in each layer, you should print the shape of each layer to trace and debug, use get_shape() after each operation like:

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()

Then you can realize what's going wrong.

Ali's answer is a good way to debug the problem. Your specific problem is you have a typo in your code --- you are using the wrong weights in the third convolutional layer.

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

Hope that helps!

you use the same layer name to do "conv2d" operation in the 4th and 5th layers wrongly. enter image description here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM