【问题标题】:Getting Precision,Recall,Sensitivity and Specificity in keras CNN在 keras CNN 中获得精确度、召回率、灵敏度和特异性
【发布时间】:2021-03-04 16:26:01
【问题描述】:

我创建了一个对图像进行二元分类的 CNN。 CNN 如下图所示:

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

文件夹中的图像按以下方式组织:

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

有没有办法从这段代码中计算指标 Precision、Recall、Sensitivity 和 Specificity,或者至少是真阳性、真阴性、假阳性和假阴性?

【问题讨论】:

    标签: keras deep-learning metrics conv-neural-network precision-recall


    【解决方案1】:
    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)  
    

    & 如果你这样做 print(report) ,它将打印所有内容

    如果你的整个数据文件不能被你的批量大小整除,那么使用

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

    【讨论】:

    • 在两个类之一上,结果都为零。你知道是什么原因造成的吗?
    • 尝试仅使用“report = classification_report(true_classes, predicted_classes)”或检查您的“true_classes”和“predicted_classes”是否在同一范围内!
    • 如果你觉得这个有用@asimplecoder,请投票/标记正确
    • 不好意思昨天睡着了,有用谢谢
    猜你喜欢
    • 2018-07-04
    • 2011-04-20
    • 1970-01-01
    • 2017-08-21
    • 2018-05-24
    • 2020-09-02
    • 2018-09-14
    • 1970-01-01
    相关资源
    最近更新 更多