简体   繁体   English

卷积神经网络 (CNN) 输入形状

[英]Convolutional Neural Network (CNN) input shape

I am new to CNN and I have a question regarding CNN.我是 CNN 的新手,我有一个关于 CNN 的问题。 I am a bit confused about the input shape of CNN (specifically with Keras).我对 CNN 的输入形状(特别是 Keras)有点困惑。 My data is a 2D data (let's say 10X10) in different time slots.我的数据是不同时隙中的二维数据(比如 10X10)。 Therefore, I have 3D data.因此,我有 3D 数据。 I am going to feed this data to my model to predict the coming time slot.我将把这些数据提供给我的模型来预测即将到来的时间段。 So, I will have a certain number of time slots for prediction (let's say 10 slots, so far, I may have a 10X10X10 data).因此,我将有一定数量的时隙用于预测(假设有 10 个时隙,到目前为止,我可能有 10X10X10 个数据)。 Now, my question is that I have to deal with this data as a 2D image with 10 channels (like ordinary kinds of data in CNN, RGB images) or as a 3D data.现在,我的问题是我必须将这些数据作为具有 10 个通道的 2D 图像(如 CNN 中的普通数据、RGB 图像)或作为 3D 数据处理。 (conv2D or conv3D in Keras). (Keras 中的 conv2D 或 conv3D)。

Thank you in advance for your help.预先感谢您的帮助。

In your case, Conv2D will be useful.在您的情况下, Conv2D会很有用。 Please refer below description for understanding input shape of Convolution Neural Network (CNN) using Conv2D .请参阅以下描述以了解使用Conv2D的卷积神经网络 (CNN) 的输入形状。

Let's see how the input shape looks like.让我们看看输入形状的样子。 The input data to CNN will look like the following picture. CNN 的输入数据如下图所示。 We are assuming that our data is a collection of images.我们假设我们的数据是一组图像。

在此处输入图片说明

Input shape has (batch_size, height, width, channels) .输入形状有(batch_size, height, width, channels) Incase of RGB image would have a channel of 3 and the greyscale image would have a channel of 1 . RGB图像的通道为3greyscale图像的通道为1

Let's look at the following code我们看下面的代码

import tensorflow as tf
from tensorflow.keras.layers import Conv2D

model=tf.keras.models.Sequential()
model.add(Conv2D(filters=64, kernel_size=1, input_shape=(10,10,3)))
model.summary()

Output:输出:

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 10, 10, 64)        256       
=================================================================

Thought it looks like out input shape is 3D , but you have to pass a 4D array at the time of fitting the data which should be like (batch_size, 10, 10, 3) .认为它看起来像输入形状是3D ,但你必须在拟合数据时传递一个4D数组,它应该是(batch_size, 10, 10, 3) Since there is no batch size value in the input_shape argument, we could go with any batch size while fitting the data.由于 input_shape 参数中没有批量大小值,我们可以在拟合数据时使用任何批量大小。

The output shape is (None, 10, 10, 64) .输出形状是(None, 10, 10, 64) The first dimension represents the batch size, which is None at the moment.第一个维度表示批量大小,目前为None Because the network does not know the batch size in advance.因为网络事先不知道批量大小。

Note: Once you fit the data, None would be replaced by the batch size you give while fitting the data. Note:拟合数据后, None将被您在拟合数据时提供的批量大小替换。

Let's look at another code with batch Size让我们看看另一个带有batch Size的代码

    import tensorflow as tf
    from tensorflow.keras.layers import Conv2D

    model=tf.keras.models.Sequential()
    model.add(Conv2D(filters=64, kernel_size=1, batch_input_shape=(16,10,10,3)))
    model.summary()

Output:输出:

Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (16, 10, 10, 64)          256       
=================================================================

Here I have replaced input_shape argument with batch_input_shape .在这里,我已经取代input_shape与参数batch_input_shape As the name suggests, this argument will ask you the batch size in advance, and you can not provide any other batch size at the time of fitting the data.顾名思义,这个参数会提前询问你的batch size,你不能在拟合数据的时候提供任何其他的batch size。

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

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