简体   繁体   English

为什么conv2d层需要ndim = 4输入?

[英]Why is the conv2d layer requiring a ndim=4 input?

I want to use a 2d Convolutional layer in my network and as input I would like to give it pictures. 我想在我的网络中使用2D卷积层,并希望输入图片。 So I have a batch of pictures which mean a ndim=3 matrix, like this for exemple : 所以我有一批图片,表示ndim = 3矩阵,例如:

dimension of my input: 我输入的维度:

[10, 6, 7]

The 10 value is the batch size and the two others values are the image size. 10值是batch size ,其他两个值是图像大小。 So what is the fourth dimension the conv 2d layer is requiring ? 那么conv 2d层需要的第四个维度是什么?

Here the interesting lines of code : 这是有趣的代码行:

self.state_size = [6, 7]

self.inputs_    = tf.placeholder(tf.float32, shape=[None, *self.state_size],  name="inputs_")


# Conv2D layer 1
self.conv1   = tf.layers.conv2d(inputs = self.inputs_,
                                filters = 4,
                                kernel_size = [4, 4],
                                strides = [1, 1],
                      kernel_initializer=tf.contrib.layers.xavier_initializer_conv2d())

Here the error I get : 这是我得到的错误:

Input 0 of layer conv2d_1 is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [None, 6, 7]*

Here is short explanation about the dimensions of the input tensor to Convolutional 2D layer. 这是卷积2D层​​的输入张量尺寸的简短说明。

 tensor_shape = (BATCH_SIZE, WIDTH, HEIGHT, CHANNELS).

The fourth dimension is channels(color) dimension. 第四个维度是通道(颜色)维度。

Long answer would be: Convolutional 2D layer expects the input to have four dimensions. 长答案是:卷积2D层​​期望输入具有四个维度。 There are two image tensor formats in tensorflow . 张量流中有两种图像张量格式。
1. channels_last(NHWC) - Dimensions are ordered as (BATCH_SIZE, HEIGHT, WIDTH, CHANNEL) . 1. Channels_last(NHWC) -尺寸按(BATCH_SIZE, HEIGHT, WIDTH, CHANNEL)排序。
2. channels_first(NCHW) - Dimensions are ordered as BATCH_SIZE, CHANNELS, HEIGHT, WIDTH) . 2. channels_first(NCHW) -尺寸按BATCH_SIZE, CHANNELS, HEIGHT, WIDTH)排序。

Batch Size dimension 批次大小尺寸

In tensorflow(possibly in other machine learning libraries) once you have defined your model, you have two options to feed data to your model. 定义模型后,在tensorflow中(可能在其他机器学习库中),有两个选项可将数据馈送到模型中。 The first options is feeding the data points one at a time. 第一种选择是一次馈送数据点。 The second options is feed N number of data points at time to your model. 第二个选项是一次向模型馈送N个数据点。 This is possible becuase of the Batch size dimension 这可能是因为“ 批次大小”尺寸

Width dimension 宽度尺寸

This dimension specifies the width of the image. 此尺寸指定图像的宽度。

Height dimension 高度尺寸

This dimension specifies Height of the image 此尺寸指定图像的高度

Channels dimension 通道尺寸

The channel dimension in RGB image is the RGB values dimension. RGB图像中的通道尺寸是RGB值尺寸。

EDIT: 编辑:

To specify the dataformat of your input images tensor conv2d layer accepts data_format argument.The default is "channels_last". 要指定输入图像的数据格式,张量conv2d图层接受data_format参数。默认值为“ channels_last”。 You can find more here . 你可以在这里找到更多。 The following code shows input with channals_last data format 以下代码显示具有channals_last数据格式的输入

inputs_ = tf.placeholder(tf.float32, [None, 32, 32, 3])

conv1 = tf.layers.conv2d(inputs_,32, (3, 3), data_format="channals_last")  

for channels first 首先用于频道

conv1 = tf.layers.conv2d(inputs_,32, (3, 3), data_format="channels_first")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Keras Conv2D - ValueError:层序的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3 - Keras Conv2D - ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3 ValueError:层“conv2d”的输入 0 与层不兼容:预期 min_ndim=4,发现 ndim=3。 收到的完整形状:(28、28、1) - ValueError: Input 0 of layer "conv2d" is incompatible with the layer: expected min_ndim=4, found ndim=3. Full shape received: (28, 28, 1) ValueError:层 conv2d 的输入 0 与层不兼容:预期 ndim=4,发现 ndim=3。 收到的完整形状:[无,30,30] - ValueError: Input 0 of layer conv2d is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [None, 30, 30] 层“conv2d”的输入 0 与层不兼容:预期 min_ndim=4,发现 ndim=3。 收到的完整形状:(32、28、28) - Input 0 of layer "conv2d" is incompatible with the layer: expected min_ndim=4, found ndim=3. Full shape received: (32, 28, 28) ValueError:conv2d 层的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3。 收到的完整形状:(2240、70、3) - ValueError: Input 0 of layer conv2d is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: (2240, 70, 3) 为什么此Keras Conv2D层与输入不兼容? - Why is this Keras Conv2D layer not compatible with the input? 为什么只为第一个 Conv2D 层指定 Conv2D 层的 input_shape 属性? - Why should the input_shape property of a Conv2D layer be specified only for the first Conv2D layer? ValueError:conv2d 层的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3。 收到的完整形状:(无、400、50) - ValueError: Input 0 of layer conv2d is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: (None, 400, 50) Keras Conv1d输入形状问题,conv1d层的输入0与层不兼容::预期min_ndim=3,发现ndim=2 - Keras Conv1d input shape problem, Input 0 of layer conv1d is incompatible with the layer: : expected min_ndim=3, found ndim=2 Keras Conv1D ValueError:层顺序的输入 0 与层不兼容::预期 min_ndim=3,发现 ndim=2 - Keras Conv1D ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=3, found ndim=2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM