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

Too slow and incorrect validation metrics #133

Closed
Adamusen opened this issue Dec 9, 2024 · 0 comments · Fixed by #134
Closed

Too slow and incorrect validation metrics #133

Adamusen opened this issue Dec 9, 2024 · 0 comments · Fixed by #134
Labels
bug Something isn't working

Comments

@Adamusen
Copy link
Contributor

Adamusen commented Dec 9, 2024

Describe the bug

There are two issues within the validation step inside yolo/tools/solver.py/ValidateModel at Lines 47-62, containing the following code:

    def validation_step(self, batch, batch_idx):
        batch_size, images, targets, rev_tensor, img_paths = batch
        H, W = images.shape[2:]
        predicts = self.post_process(self.ema(images), image_size=[W, H])
        batch_metrics = self.metric(
            [to_metrics_format(predict) for predict in predicts], [to_metrics_format(target) for target in targets]
        )

        self.log_dict(
            {
                "map": batch_metrics["map"],
                "map_50": batch_metrics["map_50"],
            },
            batch_size=batch_size,
        )
        return predicts

1, The call to self.metric() (which is a torchmetrics.detection.MeanAveragePrecision instance) at every validation step calculates the results not only for the in step provided batch, but for all values currently in its accumulator (so for every example accumulated so far from all the previous steps), resulting validation becoming progressively slower after each step. If one has more than a couple hundred validation images in the dataset, it makes validation take extremely long.

2, The collate function of the data loader fills up the "targets" tensor with dummy zeroes (boxes) for the images, which have less ground truth boxes than the image with the most labels (boxes), to be able to batch all the labels in to a single tensor. These dummy values however are not filtered out and are still there, when calculating the validation metrics. This leads to incorrect (lower than should be) metric values, since torchmetrics takes these dummy values as "missed" labels (boxes).

Proposed (and tested) solution

Change the above code snippet in Validate Model to:

    def validation_step(self, batch, batch_idx):
        batch_size, images, targets, rev_tensor, img_paths = batch
        H, W = images.shape[2:]
        predicts = self.post_process(self.ema(images), image_size=[W, H])
        self.metric.update([to_metrics_format(predict) for predict in predicts],
                           [to_metrics_format(target[target.sum(1) > 0]) for target in targets])
        return predicts

1, Change the self.metric() call to self.metric.update(), which only updates the accumulator with the new values, but does not calculate the results yet (which is done in "on_validation_epoch_end")
2, Drop the logging of per batch metric values (which were incorrect anyway, as they were cumulative values)
3, Filter out the dummy boxes from the per image "target" tensors before appending them to the metrics accumulator.

@Adamusen Adamusen added the bug Something isn't working label Dec 9, 2024
Adamusen added a commit to Adamusen/YOLO that referenced this issue Dec 9, 2024
Changed the self.metric() call to self.metric.update().
Removed the logging of per batch metric values.
Filtered out the dummy boxes from the per image "target" tensors before appending them to the metrics accumulator.

fixes WongKinYiu#133
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant