Using tools sklearn Yes, it can be calculated F1,recall,precision,iou,kappa Coefficient of these indicators , But when there are many pictures to evaluate , It often leads to insufficient memory . So right. F1,recall,precision,iou,kappa The calculation method of the coefficient is analyzed , It is found that the prediction results of each picture only need to be added to the confusion matrix , Therefore, it can evaluate any capacity data , And attach the code for testing the image directory .
The specific implementation method is shown in the following code , Each line of code has corresponding comments , The core part is _fast_confusion_matrix function ( Used to calculate label_pred And label_true The confusion matrix ), Call... When in use add_batch or add_data Function to add data , When you need to get results , Call directly evaluate Function .
import numpy as np
class SegMetric:
def __init__(self, num_classes,ignore_index=None):
self.num_classes = num_classes
self.confusion_matrix = np.zeros((num_classes, num_classes))
self.ignore_index=ignore_index
# Calculation label_pred Follow label_true Confusion matrix of categories
def _fast_confusion_matrix(self, label_pred, label_true):
# prevent label Value spillover ( Because sometimes people who participate in training label The value is different from the actual value label Values are not the same )
# Users can ignore specific categories to participate in the calculation
if self.ignore_index is not None: