当前位置:   article > 正文

yolo v8实现添加mAP75与mAP90_yolo ap75

yolo ap75

       在使用自己的算法对数据集的精度进行提高时,发现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

  1. @property
  2. def ap75(self):
  3. """
  4. Returns the Average Precision (AP) at an IoU threshold of 0.75 for all classes.
  5. Returns:
  6. (np.ndarray, list): Array of shape (nc,) with AP75 values per class, or an empty list if not available.
  7. """
  8. return self.all_ap[:, 5] if len(self.all_ap) else []
  9. @property
  10. def ap90(self):
  11. """
  12. Returns the Average Precision (AP) at an IoU threshold of 0.90 for all classes.
  13. Returns:
  14. (np.ndarray, list): Array of shape (nc,) with AP90 values per class, or an empty list if not available.
  15. """
  16. return self.all_ap[:, 8] if len(self.all_ap) else []
  1. @property
  2. def map75(self):
  3. """
  4. Returns the mean Average Precision (mAP) at an IoU threshold of 0.75.
  5. Returns:
  6. (float): The mAP75 at an IoU threshold of 0.75.
  7. """
  8. return self.all_ap[:, 5].mean() if len(self.all_ap) else 0.0
  9. @property
  10. def map90(self):
  11. """
  12. Returns the mean Average Precision (mAP) at an IoU threshold of 0.90.
  13. Returns:
  14. (float): The mAP90 at an IoU threshold of 0.90.
  15. """
  16. return self.all_ap[:, 8].mean() if len(self.all_ap) else 0.0

替换def mean_results(self):

  1. def mean_results(self):
  2. """Mean of results, return mp, mr, map50, map."""
  3. return [self.mp, self.mr, self.map50, self.map75,self.map90,self.map]

替换    def fitness(self):

  1. def fitness(self):
  2. """Model fitness as a weighted combination of metrics."""
  3. 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]
  4. return (np.array(self.mean_results()) * w).sum()

在class SegmentMetrics(SimpleClass):中替换def keys(self):

  1. @property
  2. def keys(self):
  3. """Returns a list of keys for accessing metrics."""
  4. return [
  5. 'metrics/precision(B)', 'metrics/recall(B)', 'metrics/mAP50(B)', 'metrics/mAP75(B)', 'metrics/mAP90(B)', 'metrics/mAP50-95(B)',
  6. '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):

  1. @property
  2. def keys(self):
  3. """Returns a list of keys for accessing specific metrics."""
  4. 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):

  1. @property
  2. def keys(self):
  3. """Returns list of evaluation metric keys."""
  4. return [
  5. 'metrics/precision(B)', 'metrics/recall(B)', 'metrics/mAP50(B)', 'metrics/mAP75(B)', 'metrics/mAP90(B)', 'metrics/mAP50-95(B)',
  6. '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):

  1. def get_desc(self):
  2. """Return a formatted string summarizing class metrics of YOLO model."""
  3. 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

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号