Warm tip: This article is reproduced from stackoverflow.com, please click
conv-neural-network deep-learning keras tensorflow

Convolutional Neural Network (CNN) input shape

发布于 2020-04-23 15:18:05

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

Thank you in advance for your help.

Questioner
Hamed
Viewed
9
Veeru 2020-04-07 18:52

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

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.

enter image description here

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.

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

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.

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

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

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