赞
踩
在使用自己的算法对数据集的精度进行提高时,发现mAP50的值已高达80甚至90以上,那对我们来说改进是很难的,就需要提高mAP75或mAP95的值来验证算法的有效性。
mAP的计算基于不同的IoU(交并比)阈值。常见的阈值包括0.5、0.75和0.9等。mAP50指的是使用IoU阈值为0.5时的mAP,它主要衡量了模型对目标的边界定位准确性。而mAP75和mAP90则使用更严格的IoU阈值,分别为0.75和0.9。
使用的源码为ultralytics的YOLO v8,githup地址为:https://github.com/ultralytics/ultralytics
添加后的结果如下图:
添加方式如下:
(1)更改ultralytics/utils/metrics.py
在class Metric(SimpleClass):中添加map75y与map90
- @property
- def ap75(self):
- """
- Returns the Average Precision (AP) at an IoU threshold of 0.75 for all classes.
- Returns:
- (np.ndarray, list): Array of shape (nc,) with AP75 values per class, or an empty list if not available.
- """
- return self.all_ap[:, 5] if len(self.all_ap) else []
-
- @property
- def ap90(self):
- """
- Returns the Average Precision (AP) at an IoU threshold of 0.90 for all classes.
- Returns:
- (np.ndarray, list): Array of shape (nc,) with AP90 values per class, or an empty list if not available.
- """
- return self.all_ap[:, 8] if len(self.all_ap) else []
- @property
- def map75(self):
- """
- Returns the mean Average Precision (mAP) at an IoU threshold of 0.75.
- Returns:
- (float): The mAP75 at an IoU threshold of 0.75.
- """
- return self.all_ap[:, 5].mean() if len(self.all_ap) else 0.0
-
- @property
- def map90(self):
- """
- Returns the mean Average Precision (mAP) at an IoU threshold of 0.90.
- Returns:
- (float): The mAP90 at an IoU threshold of 0.90.
- """
- return self.all_ap[:, 8].mean() if len(self.all_ap) else 0.0
替换def mean_results(self):
- def mean_results(self):
- """Mean of results, return mp, mr, map50, map."""
- return [self.mp, self.mr, self.map50, self.map75,self.map90,self.map]
替换 def fitness(self):
- def fitness(self):
- """Model fitness as a weighted combination of metrics."""
- w = [0.0, 0.0, 0.1, 0.2, 0.2, 0.5] # weights for [P, R, mAP@0.5, mAP@0.75, mAP@0.9, mAP@0.5:0.95]
- return (np.array(self.mean_results()) * w).sum()
在class SegmentMetrics(SimpleClass):中替换def keys(self):
- @property
- def keys(self):
- """Returns a list of keys for accessing metrics."""
- return [
- 'metrics/precision(B)', 'metrics/recall(B)', 'metrics/mAP50(B)', 'metrics/mAP75(B)', 'metrics/mAP90(B)', 'metrics/mAP50-95(B)',
- 'metrics/precision(M)', 'metrics/recall(M)', 'metrics/mAP50(M)', 'metrics/mAP75(M)', 'metrics/mAP90(M)', 'metrics/mAP50-95(M)']
在class DetMetrics(SimpleClass):替换def keys(self):
- @property
- def keys(self):
- """Returns a list of keys for accessing specific metrics."""
- return ['metrics/precision(B)', 'metrics/recall(B)', 'metrics/mAP50(B)','metrics/mAP75(B)','metrics/mAP90(B)', 'metrics/mAP50-95(B)']
在class PoseMetrics(SegmentMetrics):替换 def keys(self):
- @property
- def keys(self):
- """Returns list of evaluation metric keys."""
- return [
- 'metrics/precision(B)', 'metrics/recall(B)', 'metrics/mAP50(B)', 'metrics/mAP75(B)', 'metrics/mAP90(B)', 'metrics/mAP50-95(B)',
- 'metrics/precision(P)', 'metrics/recall(P)', 'metrics/mAP50(P)', 'metrics/mAP75(P)', 'metrics/mAP90(P)', 'metrics/mAP50-95(P)']
(2)更改ultralytics/models/yolo/detect/val.py(如果使用pose和segment方法类似)
在class DetectionValidator(BaseValidator):中替换 def get_desc(self):
- def get_desc(self):
- """Return a formatted string summarizing class metrics of YOLO model."""
- return ('%22s' + '%11s' * 8) % ('Class', 'Images', 'Instances', 'Box(P', 'R', 'mAP50','mAP75','mAP90', 'mAP50-95)')
在 def eval_json(self, stats):中替换
stats[self.metrics.keys[-1]],stats[self.metrics.keys[-2]],stats[self.metrics.keys[-3]], stats[self.metrics.keys[-4]] = eval.stats[:4] # update mAP50-95 and mAP50
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。