Why is the Dense layer getting a shape of (None, 50176)?

Viewed 102

I am building a CNN that can detect numbers and the addition and subtraction symbols.

I was following DeepLizards Tutorial on CNNs.

And I wanted to use my own test images but I keep getting this error when I make predictions:

ValueError: Input 0 of layer dense_10 is incompatible with the layer: expected axis -1 of input shape to have value 53760 but received input with shape (None, 50176)

I used Keras's Image Generator to create my train and test set with a preprocessing function from the VGG16 model.

train_batch = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input).flow_from_directory(directory=train_path,target_size=(224,244),classes=['+','-','0','1','2','3','4','5','6','7','8','9'],batch_size=30)

test_batch = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input).flow_from_directory(directory=test_path,target_size=(224,244),classes=['+','-','0','1','2','3','4','5','6','7','8','9'],batch_size=30)

My updated model:

def model():
    model = Sequential()
    
    model.add(Conv2D(filters=32,kernel_size=(3,3),padding='same',input_shape=(224,244,3)))
    model.add(LeakyReLU(alpha=0.1))
    model.add(MaxPooling2D(pool_size=(2,2),strides=2))
    
    model.add(Conv2D(filters=16,kernel_size=(3,3),padding='same'))
    model.add(MaxPooling2D(pool_size=(2,2),strides=2))
    model.add(LeakyReLU(alpha=0.1))
    
    model.add(Conv2D(filters=64,kernel_size=(3,3),padding='same'))
    model.add(LeakyReLU(alpha=0.1))
    model.add(MaxPooling2D(pool_size=(2,2),strides=2))
    
    model.add(Flatten())
    
    model.add(Dense(units=1024))
    model.add(Dropout(0.7))
    
    model.add(Dense(units=12,activation='softmax'))
    
    model.compile(optimizer=Adam(0.0001),loss='binary_crossentropy',metrics=['accuracy'])
    
    return model

Then I preprocess my test image.

def preprocess(IMG):
    IMG = cv2.imread(IMG)
    IMG = cv2.resize(IMG,(244,244))
    IMG = np.expand_dims(IMG,axis=0)/255
    return IMG

I resize the image to a shape of (244,244,3) and expanded the dimensions to match the input given in my model.

Can someone explain where I went wrong and how I can fix it?

Can someone also explain how I can apply the same preprocessing function to my test images?

thanks in advance.

I mess around with my model a bit. I did not do anything major other than using LeakyReLU instead of ReLU and using a softmax function instead of a sigmoid function cause I have more than 2 classes. My model summary is

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 224, 244, 32)      896       
_________________________________________________________________
leaky_re_lu (LeakyReLU)      (None, 224, 244, 32)      0         
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 112, 122, 32)      0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 112, 122, 16)      4624      
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 56, 61, 16)        0         
_________________________________________________________________
leaky_re_lu_1 (LeakyReLU)    (None, 56, 61, 16)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 56, 61, 64)        9280      
_________________________________________________________________
leaky_re_lu_2 (LeakyReLU)    (None, 56, 61, 64)        0         
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 28, 30, 64)        0         
_________________________________________________________________
flatten (Flatten)            (None, 53760)             0         
_________________________________________________________________
dense (Dense)                (None, 1024)              55051264  
_________________________________________________________________
dropout (Dropout)            (None, 1024)              0         
_________________________________________________________________
dense_1 (Dense)              (None, 12)                12300     
=================================================================
Total params: 55,078,364
Trainable params: 55,078,364
Non-trainable params: 0
_________________________________________________________________
2 Answers
IMG = cv2.resize(IMG,(224,244))

The input of your network is (224,244,32) but image size is (244,244) which is not appropriate. Change resize parameter x from 244 to 224. Also, your network input contains 32 channel input. If it is 3-channel image data change network input to (224,244,3).

The input of the network and the size of data have to be same dimensions.

model.add(Conv2D(filters=32,kernel_size=(3,3),activation='relu',padding='same',input_shape=(224,244,3)))

The bug is here. You have (224, 244) instead of (244, 244).

Related