[英]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.