本文介绍了卷积神经网络 (CNN) 输入形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 CNN 的新手,我有一个关于 CNN 的问题.我对 CNN 的输入形状(特别是 Keras)有点困惑.我的数据是不同时隙中的二维数据(比如 10X10).因此,我有 3D 数据.我将把这些数据提供给我的模型来预测即将到来的时间段.因此,我将有一定数量的时隙用于预测(假设有 10 个时隙,到目前为止,我可能有 10X10X10 个数据).现在,我的问题是我必须将这些数据作为具有 10 个通道的 2D 图像(如 CNN 中的普通数据、RGB 图像)或作为 3D 数据处理.(Keras 中的 conv2D 或 conv3D).

I am new to CNN and I have a question regarding CNN. I am a bit confused about the input shape of CNN (specifically with Keras).My data is a 2D data (let's say 10X10) in different time slots. Therefore, I have 3D data.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).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. (conv2D or conv3D in Keras).

预先感谢您的帮助.

推荐答案

在您的情况下,Conv2D 会很有用.请参阅以下描述以了解使用 Conv2D 的卷积神经网络 (CNN) 的输入形状.

In your case,Conv2D will be useful. Please refer below description for understanding input shape of Convolution Neural Network (CNN) using Conv2D.

让我们看看输入形状的样子.CNN 的输入数据如下图所示.我们假设我们的数据是一组图像.

Let’s see how the input shape looks like. The input data to CNN will look like the following picture. We are assuming that our data is a collection of images.

输入形状有(batch_size, height, width, channels).RGB 图像的通道为 3greyscale 图像的通道为 1.

Input shape has (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.

我们看下面的代码

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

输出:

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

认为输入形状看起来像3D,但是您必须在拟合数据时传递一个4D数组,该数组应该类似于(批量大小,10, 10, 3).由于 input_shape 参数中没有批量大小值,我们可以在拟合数据时使用任何批量大小.

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). Since there is no batch size value in the input_shape argument, we could go with any batch size while fitting the data.

输出形状为(None, 10, 10, 64).第一个维度表示批量大小,目前为 None.因为网络事先不知道批量大小.

The output shape is (None, 10, 10, 64). The first dimension represents the batch size, which is None at the moment. Because the network does not know the batch size in advance.

注意: 一旦你拟合了数据,None 将被你在拟合数据时给出的批大小替换.

Note: Once you fit the data, None would be replaced by the batch size you give while fitting the data.

我们来看另一个带有batch Size的代码

Let’s look at another code with 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()

输出:

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

这里我用 batch_input_shape 替换了 input_shape 参数.顾名思义,这个参数会提前询问你的batch size,你不能在拟合数据时提供任何其他batch size.

Here I have replaced input_shape argument with 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.

这篇关于卷积神经网络 (CNN) 输入形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-12 01:48