-
Notifications
You must be signed in to change notification settings - Fork 423
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
Add Mean Average Precision (mAP) metric #53
Comments
Hello! First time contributor here. I'd like to take a shot at this! |
would be great to have that metric! |
Not yet sure yet, but it looks like |
I recently managed to implement a minimal version of Kaggle's Mean Average Precision metric. The difference is in the calculation itself. You can find the details here: https://www.kaggle.com/c/global-wheat-detection/overview/evaluation My kernel: pytorch-mean-absolute-precision-calculation With slight modifications in the formula of mAP, I suppose we would be able to integrate this metric in pytorch-lightning (since we already have the average-precision implemented) I've already written a logic to map predicted boxes to ground truth ones (taking their respective scores into consideration) so have a look at the kernel and let me know if you found any issues. |
Hi, I would love to take this on. @SkafteNicki could you assign it to me? |
@briankosw thanks you for wanting to contribute! |
We need this! 🤩 For PyTorch lightning modules focused on multi-class classification, would be useful to obtain mAP metric over the Most classifiers have a pair of Fully-connected layers that do a classwise likelihood prediction. The output of the last layer before the FC layers could be used for a retrieval task over the test dataset and mAP could be calculated over this. |
@Borda I can give this a go. But I am wondering what input format would be the most consistent with the rest of the package ... So directly ...
On the other hand ... the COCO implementation has for each image a set of positive and negative labels in each image and ignores the rest ... should that be supported? lmk what you think edit: leaving here for reference |
mAP was added to PL some months ago and is now part of the |
added with 60c0eec |
reopening as the current implementation in torchmetrics is for information retrieval and here is being asked for an implementation for object detection. |
Just FYI, I have been working in a implementation over here https://github.com/jspaezp/metrics/tree/feature-spatial-average-precision but it is not quite ready for a PR. Right now it needs ... (this is more a personal checklist before I send the PR)
If you have any suggestion before I send the PR it would be great but not expected. |
@jspaezp if you are going to send a PR, please also take a look at this PR Lightning-AI/pytorch-lightning#4564 (it got closed after we moved from lightning to torchmetrics) |
@SkafteNicki thanks for letting me know! I feel like that implementation would be really hard to convert to a class metric due to how "monolitic" it is but I can definitely work on it. Best, |
@jspaezp I am fine with only having a functional interface (atleast to begin with). |
hello everyone! What is the status on this PR, is someone actively working on this? From the prior PR in Lightning-AI/pytorch-lightning#4564 it doesn't seem that would work for DDP, which seems to be the main challenge when implementing this as a metric. Has anyone tried first implementing this metric as a subclass of |
Hi, as discussed in #190 , Perhaps adding a wrapper of |
Sorry, not yet. Will have a look at it in my vacation in two weeks. |
Hello, I was also looking for a way to evaluate my object detector in PyTorch Lightning. In the end I implemented some simple logic to make predictions in I tried implementing this as a Just a note on technical details of Also, is it safe to move tensor to CPU inside the |
Hi @gau-nernst , I've also written a |
The main problem with your approach (moving to CPU, writing to file) will be performance and the lack of multi GPU training. |
Hey @zhiqwang, Awesome work. I see it's based on torchvision/detectron2 evaluation code. From what I understand from your code, it requires a COCO ground truth annotations file. I think if we add object detection mAP to torchmetrics, it should be agnostic to dataset format, and can be computed based on a set of ground truth and predicted boxes only. @tkupek Yes these are the obvious problems with my approach. If we want performance, the logic should be implemented in torch and not used pycocotools. Some people have done this I believe. But the problem is whether the torch implementation will be accurate and consistent with pycocotools. For multi-GPU training, if torchmetrics synchronizes target and predicted boxes accross GPUs, it should be okay to transfer the results to CPU to calculate mAP using pycocotools? |
@gau-nernst Yep, I agree with you, the design and functionality of this module still requires some deliberation here.
I guess it is possiable, and seems that pytorch 1.9 provides a better interface to support the arbitrary picklable data synchronizes for multi-GPUs mode with |
It is clearly that you all are more experts than me on this specific metric, but let me get this straight: |
@gau-nernst I have an approach where I bypassed the need for a temp file and initialize the CocoEval class with the detections and targets directly. |
So this is my approach: It takes the Incomplete TODO list:
@zhiqwang @gau-nernst @SkafteNicki if you agree with this approach I can try to finalize the draft in the next week (or we can work on this together). |
@tkupek Looks great to me. Please send a PR to us with what you have, then we can begin to review/comment/add to the your code. |
If anyone has any opinion on the input format that |
I had a look how tensorflow is handling this and it seems like a They use this for the groundtruth (target):
and this for predictions (preds):
I find this intuitive and would go the same way. One could discuss if we want different dict keys for groundtruth and detections (but I think we should do things more standardized). The Interesting sidenote: They also use a |
I'd like to add, that this would be beneficial for object detection and segmentation as well |
@twsl we are implementing this for object detection (bounding boxes). You can have a look at the PR. |
Yes, i do think so. |
@SkafteNicki do you think this needs to be a part of this PR or can we make this an additional improvement? |
PyTorch Lightning Bolts includes two object detection models. They take a list of dictionaries containing:
The detections contain:
Torchvision ops, for example NMS, take "boxes" and "scores" consistently in the same format: It would be convenient to standardize the keys and use the same keys as what the models use. (We can also change the models to use "classes" instead of "labels" if that's better.) Most importantly, I would prefer to use the same format for the boxes (x1, y1, x2, y2). @tkupek why do you think (y1, x1, y2, x2) is intuitive? I haven't seen this format being used before? Also note that |
@senarvi Thank you for your comment. The @SkafteNicki @Borda where are we on the issues with DDP and adjusting the test. Any time to look at this yet? I'm happy to help but I need some hints. |
I'm considering converting the COCOeval class to pytorch in order to make the mAP, mAR calculation a bit faster and to enable calculation on GPU. Would love to get some feedback. |
Using the COCOeval based metric is slow. I haven't checked if it's the the GPU-CPU transfer or if the operation is just so slow. If you can make it faster, that would be great, but I have a feeling that it's going take some effort to utilize the GPU efficiently and still keep the implementation correct. |
I agree with @senarvi, it would be great but is probably not trivial. Feel free to give it a try based on the existing PR and let us know what you learned 🙂 |
The PR looks quite complete to me. Can we finalize it? |
The main metric for object detection tasks is the Mean Average Precision, implemented in PyTorch, and computed on GPU.
It would be nice to add it to the collection of the metrics.
The example implementation using
numpy
:https://github.com/ternaus/iglovikov_helper_functions/blob/master/iglovikov_helper_functions/metrics/map.py
The text was updated successfully, but these errors were encountered: