0

I design a CNN network in order to work with "cifar10" dataset in keras. here is my code:

input_layer = Input(shape=(32,32,3))
x = Conv3D(32,(5,5,3),activation='relu',padding='same')(input_layer)
x = Conv3D(32,(5,5,3),activation='relu',padding='same')(x)
x = MaxPool3D(pool_size=2, padding='same')(x)
x = Conv3D(32,(5,5,3),activation='relu',padding='same')(x)
x = Conv3D(32,(5,5,3),activation='relu',padding='same')(x)
x = MaxPool3D(pool_size=2, padding='same')(x)
x = Flatten()(x)
x = Dense(128,kernel_initializer='random_normal', bias_initializer='zeros')(x)
x = Dense(128,kernel_initializer='random_normal', bias_initializer='zeros')(x)
output_layer = Dense(10,activation='softmax',kernel_initializer='random_normal', bias_initializer='zeros')(x)
Cifar10_CNN = Model(input_layer, output_layer)

When I build the model I get this error:

Input 0 is incompatible with layer conv3d_5: expected ndim=5, found ndim=4

How can I solve this?

  • giving the right input with ndim=5 to the function conv3d_5 and not an input with ndim=4. Question: where is the function conv_3d_5? which function call this last one? did you investigate? – Leos313 Dec 07 '19 at 09:34
  • another solution may be to use the function conv3d_4. Does it exist? well, you have a lot of different line to follow to solve the problem :) have a deep look in the rest of the code – Leos313 Dec 07 '19 at 09:36
  • @Leos313 yes, the number after the underline is not determine which layer cause the error. for the first time I run the code it was "1" and I think it refer to the first Conv3D layer. I change parameters and still get the same error. after running the code multiple times without changing it, the number just increased. after "11" run it got "11". So it does not refer to any layer. I was wonder what it is. – Hossein Shahbodaghkhan Dec 07 '19 at 09:43
  • Does this answer your question? [Keras ValueError: Input 0 is incompatible with layer conv2d\_1: expected ndim=4, found ndim=5](https://stackoverflow.com/questions/47665391/keras-valueerror-input-0-is-incompatible-with-layer-conv2d-1-expected-ndim-4) – Leos313 Dec 07 '19 at 09:45
  • possible duplicate https://stackoverflow.com/questions/47665391/keras-valueerror-input-0-is-incompatible-with-layer-conv2d-1-expected-ndim-4 – Leos313 Dec 07 '19 at 09:45
  • @Leos313 no. I checked it and some other related topics. but I got no answer. besides, I'm sure my input shape and kernel size are correct. the network works on RGB images with 32 * 32 pixels. – Hossein Shahbodaghkhan Dec 07 '19 at 09:52

1 Answers1

1

You probably should read up about differences between Conv2D, Conv3D. Though it can be confusing (given images are in fact 3 dimensional), they are still considered 2D (you don't consider the channel dimension when thinking about convolution in Keras. Convolution anyway happens on the channels dimension). So You don't need Conv3D for images, you need Conv2D.

from tensorflow.keras.layers import Input, Dense, Conv2D, MaxPool2D, Flatten
from tensorflow.keras.models import Model

input_layer = Input(shape=(32,32,3))
x = Conv2D(32,(5,5),activation='relu',padding='same')(input_layer)
x = Conv2D(32,(5,5),activation='relu',padding='same')(x)
x = MaxPool2D(pool_size=2, padding='same')(x)
x = Conv2D(32,(5,5),activation='relu',padding='same')(x)
x = Conv2D(32,(5,5),activation='relu',padding='same')(x)
x = MaxPool2D(pool_size=2, padding='same')(x)
x = Flatten()(x)
x = Dense(128,kernel_initializer='random_normal', bias_initializer='zeros')(x)
x = Dense(128,kernel_initializer='random_normal', bias_initializer='zeros')(x)
output_layer = Dense(10,activation='softmax',kernel_initializer='random_normal', bias_initializer='zeros')(x)
Cifar10_CNN = Model(input_layer, output_layer)
print(Cifar10_CNN.summary())
thushv89
  • 10,865
  • 1
  • 26
  • 39