Reputation: 35
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?
Upvotes: 0
Views: 688
Reputation: 11333
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())
Upvotes: 1