Pixiv - KiraraShss
151 字
1 分钟
评价指标封装
有时候为了计算多个评价指标,但又不想改太多的结构时,可以将这些指标封装在一个函数中,并返回一个包含所有评价指标的集合。以下代码包含精确度(Accuracy)、IOU和F1分数的计算:
import torch
def calculate_evaluation_metrics(mask_true, mask_pred): intersection = torch.logical_and(mask_true, mask_pred).float().sum() union = torch.logical_or(mask_true, mask_pred).float().sum() iou = intersection / union
true_positive = torch.logical_and(mask_true, mask_pred).float().sum() false_positive = torch.logical_and(~mask_true, mask_pred).float().sum() false_negative = torch.logical_and(mask_true, ~mask_pred).float().sum()
precision = true_positive / (true_positive + false_positive) recall = true_positive / (true_positive + false_negative) f1_score = 2 * (precision * recall) / (precision + recall)
accuracy = (true_positive + torch.logical_and(~mask_true, ~mask_pred).float().sum()) / mask_true.numel()
metrics = { 'accuracy': accuracy.item(), 'iou': iou.item(), 'precision': precision.item(), 'recall': recall.item(), 'f1_score': f1_score.item() }
return metrics支持与分享
如果这篇文章对你有帮助,欢迎分享给更多人或赞助支持!
最后更新于 2025-11-09,距今已过 92 天
部分内容可能已过时