简体   繁体   中英

how to reducing validation loss and improving the test result in CNN Model

How may I improve the valid accuracy? Besides that, my test accuracy is also low. I am trying to do categorical image classification on pictures about weeds detection in the agriculture field.

Dataset: The total number of images is 5539 with 12 classes where 70% (3870 images) of Training set 15% (837 images) of Validation and 15% (832 images) of Testing set

#data augmentation by applying Augmentor

train_aug = Augmentor.Pipeline(source_directory="/content/dataset/train",
                      output_directory="/content/dataset/train")

# Defining augmentation parameters and generating 17600 samples 
train_aug.flip_left_right(probability=0.4) 
train_aug.flip_top_bottom(probability=0.8)
train_aug.rotate(probability=0.5, max_left_rotation=5, max_right_rotation=10)
train_aug.skew(0.4, 0.5) 
train_aug.zoom(probability = 0.2, min_factor = 1.1, max_factor = 1.5) 
train_aug.sample(17600)
    
def cnn_model():
  Model = tf.keras.models.Sequential([
         tf.keras.layers.Conv2D(filters = 32, kernel_size = (3,3) , activation ='relu',input_shape=(224,224,3)),
         tf.keras.layers.MaxPool2D(2,2),
         
        
         tf.keras.layers.Conv2D(filters = 96, kernel_size = (3,3) , activation ='relu'),
         tf.keras.layers.MaxPool2D(2,2),
         
         tf.keras.layers.Conv2D(filters = 150, kernel_size = (3,3) , activation ='relu'),
         tf.keras.layers.MaxPool2D(2,2),
        
         tf.keras.layers.Conv2D(filters = 64, kernel_size = (3,3) , activation ='relu'),
         tf.keras.layers.MaxPool2D(2,2),
         
         
         tf.keras.layers.Flatten() ,    
         tf.keras.layers.Dense(512,activation='relu') , 
         tf.keras.layers.Dropout(0.2),
         
         tf.keras.layers.Dense(416, [enter image description here][1]activation='relu') , 
         tf.keras.layers.Dropout(0.2),
         
         
         
         tf.keras.layers.Dense(12,activation='softmax') ,    
         ])
  
Model.summary()
  return Model

Model = cnn_model()

from tensorflow.keras.callbacks import ModelCheckpoint
checkpoint = ModelCheckpoint(filepath, monitor='accuracy', verbose=1, save_best_only=False, save_weights_only=True, mode='auto')
callbacks_list = [checkpoint]

model= Model.compile(tf.keras.optimizers.Adam(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy']) 

History = Model.fit_generator(generator= train_data, steps_per_epoch= 3333//BATCH_SIZE , epochs= NO_OF_EPOCHS , validation_data= valid_data, validation_steps=1 ,callbacks=callbacks_list)

How may I increase my valid accuracy where my training accuracy is 98% and validation accuracy is 71%?

I would adjust the number of filters to size to 32, then 64, 128, 256. Then I would replace the flatten layer with

tf.keras.layers.GlobalAveragePooling2D()

I would also remove the checkpoint callback and replace with

es=tf.keras.callbacks.EarlyStopping( monitor="val_loss", patience=3,
                                     verbose=1,  restore_best_weights=True)
rlronp=tf.keras.callbacks.ReduceLROnPlateau( monitor="val_loss", factor=0.5, patience=1,
                                             verbose=1)
callback_list=[es, rlronp]

the early stopping callback will monitor validation loss and if it fails to reduce after 3 consecutive epochs it will halt training and restore the weights from the best epoch to the model. The ReduceLROnPlateau callback will monitor validation loss and reduce the learning rate by a factor of.5 if the loss does not reduce at the end of an epoch. Run this and if it does not do much better you can try to use a class_weight dictionary to try to compensate for the class imbalance. To calculate the dictionary find the class that has the HIGHEST number of samples. Then the weight for each class is weight for class=highest number of samples/samples in class. So create a dictionary of the form class integer:weight. Be careful to keep the order of the classes correct. Also to help with the imbalance you can try image augmentation. If you use ImageDataGenerator.flow_from_directory to read in your data you can use the generator to provide image augmentation like horizontal flip. If not you can use the Keras augmentation layers directly in your model. Documentation is here. .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM