# Insertion of custom metric

**URL:** <https://community.ultralytics.com/t/insertion-of-custom-metric/1706>\
**Category:** Discussion\
**Tags:** support\
**Created:** [December 22, 2025, 6:41am UTC](https://community.ultralytics.com/t/insertion-of-custom-metric/1706 "2025-12-22T06:41:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ch\_V\_Swarna\_Kumari](https://sea1.discourse-cdn.com/flex001/user_avatar/community.ultralytics.com/ch_v_swarna_kumari/32/1195_2.png) [@Ch\_V\_Swarna\_Kumari](https://community.ultralytics.com/u/Ch_V_Swarna_Kumari)\
**Post date:** [December 22, 2025, 6:41am UTC](https://community.ultralytics.com/t/insertion-of-custom-metric/1706/1 "2025-12-22T06:41:23Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![pderrenger](https://sea1.discourse-cdn.com/flex001/user_avatar/community.ultralytics.com/pderrenger/32/73_2.png) [@pderrenger](https://community.ultralytics.com/u/pderrenger)\
**Post date:** [December 23, 2025, 12:42am UTC](https://community.ultralytics.com/t/insertion-of-custom-metric/1706/2 "2025-12-23T00:42:23Z")

</div>

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`](https://docs.ultralytics.com/reference/utils/metrics/), 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:

```python
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](https://docs.ultralytics.com/reference/utils/metrics/)). 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.

---

<div class="post-metadata">

**Author:** ![Ch\_V\_Swarna\_Kumari](https://sea1.discourse-cdn.com/flex001/user_avatar/community.ultralytics.com/ch_v_swarna_kumari/32/1195_2.png) [@Ch\_V\_Swarna\_Kumari](https://community.ultralytics.com/u/Ch_V_Swarna_Kumari)\
**Post date:** [December 23, 2025, 11:32am UTC](https://community.ultralytics.com/t/insertion-of-custom-metric/1706/3 "2025-12-23T11:32:58Z")

</div>

Thanks for the explanation — that clarifies it.

I’m using **Ultralytics v8.3.233** (checked via `ultralytics. __version__ `). and the YOLO version is YOLO11.

My dataset annotations are currently in **YOLO TXT format** (not native COCO JSON).

For now, I’m exporting predictions with `save_json=True` and running COCOeval externally to obtain **AP\_s / AP\_m / AP\_l** on the validation set. I mainly wanted to confirm whether there is a clean hook in this version to surface those COCO size-based metrics inside `results.results_dict` during validation, or if keeping them external is the recommended approach.
