Python ValueError:尺寸必须相等,但对于输入形状为 [1,400,400,1]、[1,3,3,1] 的“Conv2D”(操作:“Conv2D”)为 1 和 3

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41800643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 01:37:59  来源:igfitidea点击:

ValueError: Dimensions must be equal, but are 1 and 3 for 'Conv2D' (op: 'Conv2D') with input shapes: [1,400,400,1], [1,3,3,1]

pythonmachine-learningtensorflowconvolution

提问by MORagab

i am trying to create a simple one layer convolution for a 2D input the idea is just to have an input image , kernel and an output the code is :

我正在尝试为 2D 输入创建一个简单的一层卷积,这个想法只是有一个输入图像、内核和一个输出,代码是:

import tensorflow as tf
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt

filename_queue = tf.train.string_input_producer(['/home/ubuntu/test.png']) 

reader = tf.WholeFileReader()
key, value = reader.read(filename_queue)

my_img = tf.image.decode_png(value) 

init_op = tf.initialize_all_variables()
sess = tf.InteractiveSession()
with sess.as_default():
    sess.run(init_op)

coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)

for i in range(1): 
  image = my_img.eval() 
image = tf.cast(image, tf.float64)
image = tf.expand_dims(image, 0)

K=np.array([[0,1,0],[1,1,1],[0,1,0]]).astype(float)
K = tf.expand_dims(K, 2)
K = tf.expand_dims(K, 0)


conv = tf.nn.conv2d(
        image,
        K,
        strides=[3, 3, 3, 3],
        padding="SAME")

and i am getting this error:

我收到此错误:

Traceback (most recent call last):
  File "test4.py", line 35, in <module>
    padding="SAME")
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_nn_ops.py", line 396, in conv2d
    data_format=data_format, name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 759, in apply_op
    op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2242, in create_op
    set_shapes_for_outputs(ret)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1617, in set_shapes_for_outputs
    shapes = shape_func(op)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1568, in call_with_requiring
    return call_cpp_shape_fn(op, require_shape_fn=True)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/common_shapes.py", line 610, in call_cpp_shape_fn
    debug_python_shape_fn, require_shape_fn)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/common_shapes.py", line 675, in _call_cpp_shape_fn_impl
    raise ValueError(err.message)
ValueError: Dimensions must be equal, but are 1 and 3 for 'Conv2D' (op: 'Conv2D') with input shapes: [1,400,400,1], [1,3,3,1].

my input is 400x400x1 and the kernel is 3x3

我的输入是 400x400x1,内核是 3x3

回答by user1454804

Based on conv2d doc:

基于conv2d 文档

shape of input = [batch, in_height, in_width, in_channels]
shape of filter = [filter_height, filter_width, in_channels, out_channels]

Last dimension of input and third dimension of filter represents the number of input channels. In your case they are not equal.

输入的最后一维和过滤器的第三维表示输入通道的数量。在您的情况下,它们不相等。

You can change the shape of filter to [3, 3, 1, 1].