Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate coco from map #2884

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

Isalia20
Copy link
Contributor

@Isalia20 Isalia20 commented Dec 25, 2024

What does this PR do?

Part of the fix of #2877. This PR decouples the coco backend from the mAP metric. This is part of the fix of 2877 issue because for me to implement functional mAP I would have to duplicate a lot of code unless this class was created. This class will help to duplicate as little code as possible while implementing functional version of mAP. It also makes mAP less heavy with coco stuff and abstracts it away as a coco backend private instance

Before submitting
  • Was this discussed/agreed via a Github issue? (no need for typos and docs improvements)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure to update the docs?
  • Did you write any new necessary tests?

Did you have fun? yes

Make sure you had fun coding 🙃


📚 Documentation preview 📚: https://torchmetrics--2884.org.readthedocs.build/en/2884/

@Isalia20
Copy link
Contributor Author

Huh weird, I thought pre commit checks covered the mypy checks as well. Will fix them

Copy link

codecov bot commented Dec 25, 2024

Codecov Report

Attention: Patch coverage is 50.00000% with 99 lines in your changes missing coverage. Please review.

Project coverage is 69%. Comparing base (5b0e9b8) to head (1c48011).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #2884    +/-   ##
=======================================
- Coverage      69%     69%    -0%     
=======================================
  Files         346     333    -13     
  Lines       19146   18988   -158     
=======================================
- Hits        13233   13106   -127     
+ Misses       5913    5882    -31     

@Isalia20 Isalia20 marked this pull request as ready for review December 25, 2024 21:25
@Isalia20
Copy link
Contributor Author

I'll wait to get some comments on this first and then proceed with functional part of mAP

@Borda
Copy link
Member

Borda commented Feb 14, 2025

I'll wait to get some comments on this first and then proceed with functional part of mAP

apology for the delay, I'll have a look at it next week :)

Copy link
Member

@SkafteNicki SkafteNicki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall looks good to me, just a few comments

>>> # Example files can be found at
>>> # https://github.com/cocodataset/cocoapi/tree/master/results
>>> from torchmetrics.detection import MeanAveragePrecision
>>> preds, target = MeanAveragePrecision.coco_to_tm(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
>>> preds, target = MeanAveragePrecision.coco_to_tm(
>>> preds, target = MeanAveragePrecision().coco_to_tm(

]

def __init__(
self,
box_format: Literal["xyxy", "xywh", "cxcywh"] = "xyxy",
iou_type: Union[Literal["bbox", "segm"], tuple[Literal["bbox", "segm"], ...]] = "bbox",
iou_type: Union[Literal["bbox", "segm"], tuple[str]] = "bbox",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did the typing change here?

Comment on lines +23 to +46
def _load_coco_backend_tools(backend: Literal["pycocotools", "faster_coco_eval"]) -> tuple[object, object, ModuleType]:
"""Load the backend tools for the given backend."""
if backend == "pycocotools":
if not _PYCOCOTOOLS_AVAILABLE:
raise ModuleNotFoundError(
"Backend `pycocotools` in metric `MeanAveragePrecision` metric requires that `pycocotools` is"
" installed. Please install with `pip install pycocotools` or `pip install torchmetrics[detection]`"
)
import pycocotools.mask as mask_utils
from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval

return COCO, COCOeval, mask_utils

if not _FASTER_COCO_EVAL_AVAILABLE:
raise ModuleNotFoundError(
"Backend `faster_coco_eval` in metric `MeanAveragePrecision` metric requires that `faster-coco-eval` is"
" installed. Please install with `pip install faster-coco-eval`."
)
from faster_coco_eval import COCO
from faster_coco_eval import COCOeval_faster as COCOeval
from faster_coco_eval.core import mask as mask_utils

return COCO, COCOeval, mask_utils
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please move to the src/torchmetrics/detection/helpers.py file. While we do not have any strict rules for what goes to the general utilities folder, this is very much something that is unique to the detection domain and therefore should be kept there

self.iou_type,
)

def coco_to_tm(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be possible to make this a classmethod so the user can convert without initializing the class?
alternatively we should get rid of iou_type and backend arguments because they are already set in the class

@mergify mergify bot removed the has conflicts label Mar 7, 2025
@Isalia20
Copy link
Contributor Author

Ah sorry, just saw this. I'll resolve the comments this week

@Borda
Copy link
Member

Borda commented Mar 10, 2025

Ah sorry, just saw this. I'll resolve the comments this week

Perfect, thank you

@SkafteNicki SkafteNicki added this to the v1.7.0 milestone Mar 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants