简体   繁体   English

如何使用 MobileNet 计算图像分类的精度和召回率

[英]How to compute precision and recall on image classification with MobileNet

I am trying to compute precision, recall, and f1 score on my test dataset.我正在尝试在我的测试数据集上计算精度、召回率和 f1 分数。 However, I am using ImageDataGenerator format, not using train_test_split (x_train, y_train, x_test and y_test).但是,我使用的是 ImageDataGenerator 格式,而不是使用train_test_split (x_train、y_train、x_test 和 y_test)。 That's why I couldn't find any references online.这就是为什么我在网上找不到任何参考资料。

IMAGE_SIZE = 224
BATCH_SIZE = 64

EPOCH = 30
CHANNEL = 3
CLASSES = 10

train_path = "/Users/ba/Documents/mycodes/datasets/DS/train"
valid_path = "/Users/ba/Documents/mycodes/datasets/DS/val"
test_path = "/Users/ba/Documents/mycodes/datasets/DS/test"

train_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.mobilenet_v3.preprocess_input) \
    .flow_from_directory(directory=train_path, target_size=(IMAGE_SIZE,IMAGE_SIZE), batch_size=BATCH_SIZE)
valid_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.mobilenet_v3.preprocess_input) \
    .flow_from_directory(directory=valid_path, target_size=(IMAGE_SIZE,IMAGE_SIZE), batch_size=BATCH_SIZE)
test_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.mobilenet_v3.preprocess_input) \
    .flow_from_directory(directory=test_path, target_size=(IMAGE_SIZE,IMAGE_SIZE), batch_size=BATCH_SIZE, shuffle=False)

Then I tried to calculate precision, recall, and f1 in the following way down below:然后我尝试按以下方式计算精度、召回率和 f1:

from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import f1_score

y_pred_logits = model.predict(test_batches)
y_pred = tf.math.argmax(y_pred_logits)

test_classes = test_batches.classes


# accuracy: (tp + tn) / (p + n)
accuracy = accuracy_score(test_classes, y_pred)
print('Accuracy: %f' % accuracy)
# precision tp / (tp + fp)
precision = precision_score(test_classes, y_pred)
print('Precision: %f' % precision)
# recall: tp / (tp + fn)
recall = recall_score(test_classes, y_pred)
print('Recall: %f' % recall)
# f1: 2 tp / (2 tp + fp + fn)
f1 = f1_score(test_classes, y_pred)
print('F1 score: %f' % f1)

Unfortunately it throws this error message:不幸的是,它抛出了这个错误信息:

ValueError: Found input variables with inconsistent numbers of samples: [1887, 10]

Can you help me re-write the code, or any other references using ImageDataGenerator format I used?你能帮我重写代码,或者使用我使用的 ImageDataGenerator 格式的任何其他参考吗?

So the problem were on y_true (test_classes) and y_pred.所以问题出在 y_true (test_classes) 和 y_pred 上。 With this, one can also calculate the confusion matrix.有了这个,也可以计算混淆矩阵。

#Making prediction
y_true = test_batches.classes
predictions = model.predict(x=test_batches, steps=len(test_batches), verbose=0)
y_pred = predictions.argmax(axis=1)

print("Precision Score: ",precision_score(y_true, y_pred, pos_label='positive', average='micro'))
print("Recall Score: ",recall_score(y_true, y_pred, pos_label='positive', average='micro'))
print("F1 Score: ",f1_score(y_true, y_pred, pos_label='positive', average='micro'))
print("Accuracy Score: ",accuracy_score(y_true, y_pred))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 MobileNet 计算 Multiclass-problem 的准确率、召回率、F1 和混淆矩阵 - How to compute precision, recall, F1 and confusion matrix of Multiclass-problem with MobileNet 如何从 Tensorflow 二值图像分类中获得召回率和精度 - How to get Recall and Precision from Tensorflow binary image classification 计算多 label 分类 keras 的召回精度和 F1 分数 - compute the recall precision and F1 score for a multi label classification keras 如何从分类报告中计算准确率和召回率 - How to calculate precision and recall from classification report 召回后的分类精度和精度 - Classification accuracy after recall and precision 如何在Decision tree sklearn中计算精确回忆? - How to compute precision-recall in Decision tree sklearn? Keras-精度和召回率大于1(多种分类) - Keras - Precision and Recall is greater than 1 (Multi classification) 为什么F1-score,Recall,Precision都等于1? (图像分类linearSVM) - Why F1-score, Recall, Precision all equal to 1? (image classification linearSVM) Tensorflow:计算精度、召回率、F1 分数 - Tensorflow: Compute Precision, Recall, F1 Score 如何告诉scikit-了解给出F-1 /精确/召回得分的标签(二进制分类)? - How to tell scikit-learn for which label the F-1/precision/recall score is given (in binary classification)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM