0

I want to build a CNN model that takes 3 successive images insetead of one, so the input takes the shape: (3,height, width, channels=3) :

from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, Dropout, Dense, 
Flatten,Convolution2D
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import Adam

def build_cnn_model(frames_number,height,width,channel, nb_actions):

    model = Sequential()

    model.add( Input((frames_number,height,width,channel),name='Input') )
    model.add( Conv2D(96, (3,3), strides=(4,4), activation='relu', name='Conv2D_1', 
    input_shape = (frames_number,height,width,channel) ) )
    model.add( MaxPooling2D((2, 2), name='MaxPooling2D_1') )
    model.add( Dropout(0.2,name='Dropout_1'))

    model.add( Conv2D(192, (3, 3), activation='relu', name='Conv2D_2') )
    model.add( MaxPooling2D((2, 2), name='MaxPooling2D_2')  )
    model.add( Dropout(0.2, name='Dropout_2'))

    model.add( Flatten(name='Flatten_1')) 
    model.add( Dense(1500, activation='relu', name='Dense_1') )
    model.add( Dropout(0.5, name='Dropout_DNN_1'))

    model.add(Dense(nb_actions, activation='linear', name='Output') )

    return model

model = build_cnn_model(3,220,300,3,6)

The structure seems to be logic for me, but I got :

ValueError: Input 0 of layer Conv2D_1 is incompatible with the layer: expected ndim=4, found ndim=5. Full shape received: [None, 3, 210, 160, 3]

Note, I know it is possible also to change the data shape, so that the 3 images can be put in one single image of 3*3 channels. but I can't apply that solution in my program. I want to passe input of (3, height, width, 3).

desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • 1
    It seems u are looking for Conv3D – Marco Cerliani Mar 14 '21 at 10:36
  • `tf.keras.layers.Conv2D` expects input of 4D, where `tf.keras.layers.Conv3D` expects input 5+D tensor with shape: `batch_shape + (channels, conv_dim1, conv_dim2, conv_dim3)`. Looks like your input is of 5D. Change your code accordingly. Thanks! –  Aug 04 '21 at 06:31

0 Answers0