Search code examples
kerasdeep-learningmetricsconv-neural-networkprecision-recall

Getting Precision,Recall,Sensitivity and Specificity in keras CNN


I have created a CNN that does binary classification on images. The CNN is seen below:

def neural_network():
  classifier = Sequential()

  # Adding a first convolutional layer
  classifier.add(Convolution2D(48, 3, input_shape = (320, 320, 3), activation = 'relu'))
  classifier.add(MaxPooling2D())
  
  # Adding a second convolutional layer
  classifier.add(Convolution2D(48, 3, activation = 'relu'))
  classifier.add(MaxPooling2D())

  #Flattening
  classifier.add(Flatten())

  #Full connected
  classifier.add(Dense(256, activation = 'relu'))
  #Full connected
  classifier.add(Dense(1, activation = 'sigmoid'))


  # Compiling the CNN
  classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

  classifier.summary()


  train_datagen = ImageDataGenerator(rescale = 1./255,
                                    horizontal_flip = True,
                                    vertical_flip=True,
                                    brightness_range=[0.5, 1.5])

  test_datagen = ImageDataGenerator(rescale = 1./255)

  training_set = train_datagen.flow_from_directory('/content/drive/My Drive/data_sep/train',
                                                  target_size = (320, 320),
                                                  batch_size = 32,
                                                  class_mode = 'binary')

  test_set = test_datagen.flow_from_directory('/content/drive/My Drive/data_sep/validate',
                                              target_size = (320, 320),
                                              batch_size = 32,
                                              class_mode = 'binary')

  es = EarlyStopping(
      monitor="val_accuracy",
      patience=15,
      mode="max",
      baseline=None,
      restore_best_weights=True,
  )
  filepath  = "/content/drive/My Drive/data_sep/weightsbestval.hdf5"
  checkpoint = ModelCheckpoint(filepath, monitor='val_accuracy', verbose=1, save_best_only=True, mode='max')
  callbacks_list = [checkpoint]

  history = classifier.fit(training_set,
                          epochs  = 50,
                          validation_data = test_set,
                          callbacks= callbacks_list
                          )
  
  best_score = max(history.history['val_accuracy'])

  return best_score

The images in the folders are organized in the following way:

-train
  -healthy
  -patient
-validation
  -healthy
  -patient

Is there a way to calculate the metrics Precision,Recall,Sensitivity and Specificity or at least the true positives,true negatives,false positive and false negatives from this code?


Solution

  • from sklearn.metrics import classification_report
    
    test_set = test_datagen.flow_from_directory('/content/drive/My Drive/data_sep/validate',
                                              target_size = (320, 320),
                                              batch_size = 32,
                                              class_mode = 'binary')
    predictions = model.predict_generator(
        test_set,
        steps = np.math.ceil(test_set.samples / test_set.batch_size),
        )
    predicted_classes = np.argmax(predictions, axis=1)
    true_classes = test_set.classes
    class_labels = list(test_set.class_indices.keys())
    report = classification_report(true_classes, predicted_classes, target_names=class_labels)
    accuracy = metrics.accuracy_score(true_classes, predicted_classes)  
    

    & if you do print(report) ,it will print everything

    And if your whole data files are not divisible by your batch size, then use

    test_set = test_datagen.flow_from_directory('/content/drive/My Drive/data_sep/validate',
                                              target_size = (320, 320),
                                              batch_size = 1,
                                              class_mode = 'binary')