'keras CNN model predicts fine but only one label doesn't predict

After I train the model to predict the label of 24 class using 2800 images for each class and take 5000 images for validation I run some tests to see the quality of prediction for the label, I designed a program to take all the images in folder test and predict the label all the classes were good except class 19 which of a 1000 images for test none where predicted as 19

Does anyone have a solution?

this is the model architecture:

model = Sequential()

model.add(Conv2D(filters=32, kernel_size=2,padding='same',activation='relu',input_shape=(32,32,1)))

model.add(MaxPooling2D(pool_size=2))

model.add(Conv2D(filters=64, kernel_size=2, padding='same', activation='relu'))

model.add(MaxPooling2D(pool_size=2))

model.add(Flatten())

model.add(Dense(1024, activation='relu'))

model.add(Dropout(0.2))

model.add(Dense(24, activation='softmax'))

model.summary()

this is the optimizer and trainer:

optimizer = rmsprop(learning_rate=0.0001)

model.compile(loss='categorical_crossentropy',optimizer= optimizer,metrics=['accuracy'])

checkpointer = ModelCheckpoint(filepath='CNN_newData.hdf5',verbose=1,
                               save_best_only=True)
hist = model.fit(x_train,y_train,batch_size=128,epochs=100,
                 validation_data=(x_valid,y_valid),callbacks=[checkpointer],
                 verbose=2,shuffle=True)

this is how the images are prepared for prediction:

  for img in images:

            read_img = cv2.imread('test-images/' + file + '/' + img)
            read_img = cv2.cvtColor(read_img,cv2.COLOR_RGB2GRAY)
            read_img = read_img.reshape( -1,32, 32, 1)
            read_img = read_img.astype('float32')/255
            maxind = model.predict_classes(read_img)


Solution 1:[1]

  • I assume your data set is well-balanced?
  • Could you please upload your loss/accuracy curves?
  • Have you tried other optimizers? Your RMSprop learning_rate is lower than default and net rather shallow.
  • Is it possible for you to share the data? Are you at least sure, there's no contradictory knowledge there?

read_img = cv2.imread('test-images/' + file + '/' + img)

Please don't concat paths yourself. You'll run into troubles, once you'll push this model to some linux-based cloud. Check pathlib.

  • try running adam with default params
  • model.add(Dense(1024, activation='relu')) - it's rather large for only 24 labels after it. Try something smaller, like 240.
  • kernel_size looks weird, shouldn't conv kernel be odd? try kernel_size=3
  • try to add some regularization

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1