在keras CNN中获得精确性、召回率、敏感性和特异性

在keras CNN中获得精确性、召回率、敏感性和特异性,keras,deep-learning,metrics,cnn,precision-recall,Keras,Deep Learning,Metrics,Cnn,Precision Recall,我创建了一个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())

我创建了一个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
是否有一种方法可以计算该代码中的度量精度、召回率、敏感性和特异性,或者至少是真阳性、真阴性、假阳性和假阴性

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)  
&如果您执行
打印(报告)
,它将打印所有内容

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

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

在这两类中的一类上,结果都是零。你知道是什么原因造成的吗?试着用“报告=分类报告(真实类,预测类)”或检查你的“真实类”和“预测类”是否在同一范围内!如果你觉得这个有用@asimplecoder对不起,我昨天睡着了,它很有用,谢谢