在Keras中获得每个班级的精确度,召回率和F1分数 [英] Getting precision, recall and F1 score per class in Keras
问题描述
我已经使用Keras(2.1.5)中的TensorFlow后端训练了神经网络,并且还使用了keras-contrib(2.0.8)库来添加CRF层作为网络的输出. /p>
我想知道在使用NN对测试集进行预测后如何获得每个班级的精度,召回率和f1分数.
假定您具有一个函数get_model()
,该函数可以构建与您训练的模型完全相同的模型,并且路径weights_path
指向包含模型的HDF5文件重量:
model = get_model()
model.load_weights(weights_path)
这应该可以正确加载模型.然后,您只需定义测试数据的ImageDataGenerator
并拟合模型即可获得预测:
# Path to your folder testing data
testing_folder = ""
# Image size (set up the image size used for training)
img_size = 256
# Batch size (you should tune it based on your memory)
batch_size = 16
val_datagen = ImageDataGenerator(
rescale=1. / 255)
validation_generator = val_datagen.flow_from_directory(
testing_folder,
target_size=(img_size, img_size),
batch_size=batch_size,
shuffle=False,
class_mode='categorical')
然后,您可以使用model.predict_generator()
方法使模型生成整个数据集的所有预测:
# Number of steps corresponding to an epoch
steps = 100
predictions = model.predict_generator(validation_generator, steps=steps)
最后使用sklearn
包中的metrics.confusion_matrix()
方法创建混淆矩阵:
val_preds = np.argmax(predictions, axis=-1)
val_trues = validation_generator.classes
cm = metrics.confusion_matrix(val_trues, val_preds)
或使用sklearn
中的metrics.precision_recall_fscore_support()
方法获得所有类的所有精度,召回率和f1-分数(参数average=None
输出所有类的指标):
# label names
labels = validation_generator.class_indices.keys()
precisions, recall, f1_score, _ = metrics.precision_recall_fscore_support(val_trues, val_preds, labels=labels)
我还没有测试过,但是我想这会为您提供帮助.
I have trained a neural network using the TensorFlow backend in Keras (2.1.5) and I have also used the keras-contrib (2.0.8) library in order to add a CRF layer as an output for the network.
I would like to know how can I get the precision, recall and f1 score for each class after making the predictions on a test set using the NN.
Assume that you have a function get_model()
that builds a your exact same model you have trained and a path weights_path
pointing to your HDF5 file containing your model weights:
model = get_model()
model.load_weights(weights_path)
This should load your model properly. Then you just have to define a ImageDataGenerator
of your test data and fit the model to obtain predictions:
# Path to your folder testing data
testing_folder = ""
# Image size (set up the image size used for training)
img_size = 256
# Batch size (you should tune it based on your memory)
batch_size = 16
val_datagen = ImageDataGenerator(
rescale=1. / 255)
validation_generator = val_datagen.flow_from_directory(
testing_folder,
target_size=(img_size, img_size),
batch_size=batch_size,
shuffle=False,
class_mode='categorical')
Then you can make the model generate all predictions over your entire dataset using the model.predict_generator()
method:
# Number of steps corresponding to an epoch
steps = 100
predictions = model.predict_generator(validation_generator, steps=steps)
And finally create a confussion matrix using the metrics.confusion_matrix()
method from sklearn
package:
val_preds = np.argmax(predictions, axis=-1)
val_trues = validation_generator.classes
cm = metrics.confusion_matrix(val_trues, val_preds)
Or get all precisions, recalls and f1-scores for all classes using metrics.precision_recall_fscore_support()
method from sklearn
(argument average=None
outputs metrics for all classes):
# label names
labels = validation_generator.class_indices.keys()
precisions, recall, f1_score, _ = metrics.precision_recall_fscore_support(val_trues, val_preds, labels=labels)
I haven't tested it, but I guess this will help you.
这篇关于在Keras中获得每个班级的精确度,召回率和F1分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!