使用category_report评估Keras模型

时间:2018-10-24 20:32:09

标签: python tensorflow scikit-learn keras

问题:

在训练期间,我的模型的性能看起来还不错。但是,来自sklearn的category_report的结果几乎在所有地方都会产生精度,查全率和f1为零。我在做这样的训练表现和推理失配时错了吗? (我将Keras与TensorFlow后端一起使用。)

我的代码:

我使用valiation_split参数生成两个生成器(训练,验证),如下所示:

train_datagen = ImageDataGenerator(
rescale=1. / 255, validation_split=0.15)

train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='categorical', subset="training")

validation_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='categorical', subset="validation", shuffle=False)

我在我的validate_generator中设置了shuffle=False,以确保它不会混合图像和标签之间的关系,以便以后进行评估。

接下来,我像这样训练我的模型:

history = model.fit_generator(
    train_generator,
    steps_per_epoch=nb_train_samples // batch_size,
    epochs=epochs,
    validation_data=validation_generator,
    validation_steps=nb_validation_samples // batch_size,
    verbose=1)

性能还可以:

Epoch 1/5
187/187 [==============================] - 44s 233ms/step - loss: 0.7835 - acc: 0.6744 - val_loss: 1.2918 - val_acc: 0.6079
Epoch 2/5
187/187 [==============================] - 42s 225ms/step - loss: 0.7578 - acc: 0.6901 - val_loss: 1.2962 - val_acc: 0.6149
Epoch 3/5
187/187 [==============================] - 40s 216ms/step - loss: 0.7535 - acc: 0.6907 - val_loss: 1.3426 - val_acc: 0.6061
Epoch 4/5
187/187 [==============================] - 41s 217ms/step - loss: 0.7388 - acc: 0.6977 - val_loss: 1.2866 - val_acc: 0.6149
Epoch 5/5
187/187 [==============================] - 41s 217ms/step - loss: 0.7282 - acc: 0.6960 - val_loss: 1.2988 - val_acc: 0.6297

现在,我按照此处https://github.com/keras-team/keras/issues/2607#issuecomment-302365916所建议的方法为分类报告提取必要的信息。这给了我以下内容:

validation_steps_per_epoch = np.math.ceil(validation_generator.samples / validation_generator.batch_size)

predictions = model.predict_generator(validation_generator, steps=validation_steps_per_epoch)
# Get most likely class
predicted_classes = np.argmax(predictions, axis=1) 

true_classes = validation_generator.classes
class_labels = list(validation_generator.class_indices.keys())  

最后,我使用以下命令输出分类报告:

from sklearn.metrics import classification_report
report = classification_report(true_classes, predicted_classes, target_names=class_labels)
print(report)    

在整个位置都会产生零(请参见下面的平均值):

                   precision    recall  f1-score   support
      micro avg       0.01      0.01      0.01      2100
      macro avg       0.01      0.01      0.01      2100
   weighted avg       0.01      0.01      0.01      2100

0 个答案:

没有答案