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

Question about calculating mAP at test time #3042

Closed
huuquan1994 opened this issue May 5, 2021 · 7 comments · Fixed by #3058 or #5141
Closed

Question about calculating mAP at test time #3042

huuquan1994 opened this issue May 5, 2021 · 7 comments · Fixed by #3058 or #5141
Labels
question Further information is requested

Comments

@huuquan1994
Copy link
Contributor

huuquan1994 commented May 5, 2021

❔Question

Thank you for your work, I really enjoyed running the codes.

I was trying to understand the way we calculate mAP by reading the test.py
At line 197, as I understood, the IoU values (ious) wasn't sorted before the further process (lines 199-211).
Therefore, I think it doesn't guarantee that we find the best IoU (or a detected box) for a ground-truth box.
For example, a detected box is considered correct if the IoU with a ground-truth box is >= 0.5. However, there are possibilities that we detected multiple boxes with different IoU values. In this case, I think we should assign the box with the highest IoU as the correctly detected box.

Will the code affect the result of calculating mAP?

Additional context

The comments in lines 191-192 (# prediction indices, # target indices) should be swapped, shouldn't they?

@huuquan1994 huuquan1994 added the question Further information is requested label May 5, 2021
@github-actions
Copy link
Contributor

github-actions bot commented May 5, 2021

👋 Hello @huuquan1994, thank you for your interest in 🚀 YOLOv5! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://www.ultralytics.com or email Glenn Jocher at glenn.jocher@ultralytics.com.

Requirements

Python 3.8 or later with all requirements.txt dependencies installed, including torch>=1.7. To install run:

$ pip install -r requirements.txt

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), testing (test.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

@glenn-jocher
Copy link
Member

@huuquan1994 yes you're right, the comments should be swapped! Can you send a quick PR for this bug you spotted? Thanks!

The predictions are sorted by confidence here:

yolov5/utils/metrics.py

Lines 32 to 33 in b18ca31

# Sort by objectness
i = np.argsort(-conf)

kepler62f pushed a commit to kepler62f/yolov5 that referenced this issue May 7, 2021
@glenn-jocher glenn-jocher linked a pull request May 7, 2021 that will close this issue
@huuquan1994
Copy link
Contributor Author

huuquan1994 commented May 7, 2021

@huuquan1994 yes you're right, the comments should be swapped! Can you send a quick PR for this bug you spotted? Thanks!

The predictions are sorted by confidence here:

yolov5/utils/metrics.py

Lines 32 to 33 in b18ca31

# Sort by objectness
i = np.argsort(-conf)

Thanks, @glenn-jocher for your comments fix.

For my first question, I understood that the predictions are sorted by confidence at the ap_per_class function.
But before that, at test.py, it was quite hard to understand the process.

Supposed that I have 1 test image with 1 class and 6 ground-truth bounding boxes.

I tried to print out the prediction results (after NMS).
The code on test.py will be modified like below:

# Search for detections
if pi.shape[0]:
    # Prediction to target ious
    ious, i = box_iou(predn[pi, :4], tbox[ti]).max(1)  # best ious, indices

    # Append detections
    detected_set = set()
    # Print out all IoU values that > 0.5 (iouv[0])
    print(ious[ious > iouv[0]])
    # Print out all (targeted) IoU indices that > 0.5 (iouv[0])
    print(ti[i[ious > iouv[0]]])
    
    for j in (ious > iouv[0]).nonzero(as_tuple=False):
        d = ti[i[j]]  # detected target
        if d.item() not in detected_set:
            detected_set.add(d.item())
            detected.append(d)
            correct[pi[j]] = ious[j] > iouv  # iou_thres is 1xn
            if len(detected) == nl:  # all targets already located in image
                break
    # Print detected for further mAP calculation
    print(detected)

My outputs are:

# IoU values that > 0.5 (iouv[0])
[0.91536, 0.72824, 0.84568, 0.64040, 0.75937, 0.55075, 0.57479, 0.51508, 0.87954, 0.51129, 0.59774, 0.67086, 0.72601, 0.53363, 0.60063]

# (targeted) IoU indices that > 0.5 (iouv[0])
[3, 2, 0, 5, 1, 4, 2, 3, 4, 0, 1, 3, 5, 1, 2]

# Detected for further mAP calculation
[3, 2, 0, 5, 1, 4]

The corresponding IoU values of the detected targets are [0.91536, 0.72824, 0.84568, 0.64040, 0.75937, 0.55075] (which were the first elements in the list (if correctly located))

But if we consider the best IoU values for the detected boxes ([3, 2, 0, 5, 1, 4]), shouldn't they be [0.91536, 0.72824, 0.84568, 0.72601, 0.75937, 0.87954]?

@glenn-jocher
Copy link
Member

@huuquan1994 there's no IoU sorting, the sorting is by prediction confidence.

@huuquan1994
Copy link
Contributor Author

@glenn-jocher thanks for your reply!

I wonder if this affects the result of the ap_per_class function since if we use better IoU values (as mentioned above), the True Positive array (correct (https://github.com/ultralytics/yolov5/blob/master/test.py#L178)) will have more True values, and the mAP will be improved (I'm sorry if my understanding is incorrect).

Anyway, I just want to understand YOLOv5 better and if there's no IoU sorting, your code is perfect 😊

@glenn-jocher
Copy link
Member

@huuquan1994 I'm not sure I understand your question correctly. Can you submit a PR with your proposed changes so that we can see and evaluate the changes against the baseline code? Thanks!

@huuquan1994
Copy link
Contributor Author

@glenn-jocher sure, I'll do it soon!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants