Insertion of custom metric

Hi team

I’m working with a UAV dataset and I want to evaluate using the COCO-style size-based metrics mAP_s, mAP_m, and mAP_l in addition to the default mAP50 and mAP50-95 that Ultralytics YOLO reports.

Could someone guide me on where/how to insert these custom metrics into the evaluation pipeline? I want to ensure the model reports small/medium/large object AP on UAV imagery, rather than only the COCO defaults.

Any pointers on the correct files/functions to modify (e.g., metrics.py, coco.py, post-processing) would be highly appreciated!

Ultralytics YOLO’s built-in val() metrics (what you see as metrics/mAP50(B) and metrics/mAP50-95(B)) come from our internal AP implementation in ultralytics/utils/metrics.py, specifically DetMetrics.process()ap_per_class(), and that pipeline does not currently compute COCO area-range splits (small/medium/large).

If you want true COCO-style mAP_s, mAP_m, mAP_l on your UAV set, the cleanest approach is to evaluate with the COCO API: run validation with JSON export enabled, then use pycocotools to read the COCO stats (APs/APm/APl). Example:

from ultralytics import YOLO
from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval
import glob

model = YOLO("yolo11n.pt")

results = model.val(data="your_coco.yaml", save_json=True)

pred_json = glob.glob(str(results.save_dir / "predictions.json"))[0]  # created by save_json=True
gt_json = "path/to/instances_val.json"  # your COCO GT annotations

coco_gt = COCO(gt_json)
coco_dt = coco_gt.loadRes(pred_json)

e = COCOeval(coco_gt, coco_dt, iouType="bbox")
e.evaluate()
e.accumulate()
e.summarize()

map_s, map_m, map_l = e.stats[3], e.stats[4], e.stats[5]
print("mAP_s, mAP_m, mAP_l:", map_s, map_m, map_l)

If you specifically want these to appear inside Ultralytics’ printed table / results.results_dict, the place to hook it in is the validator JSON-eval stage (where COCO-style evaluation is performed) and then extend DetMetrics.keys/results_dict (see DetMetrics.keys in the same metrics.py reference). I can point you to the exact function/file in your installed version if you paste your ultralytics.__version__ and confirm whether your labels are already COCO JSON or YOLO TXT.