Chris-hughes10 / Yolov7-training

A clean, modular implementation of the Yolov7 model family, which uses the official pretrained weights, with utilities for training the model on custom (non-COCO) tasks.
GNU General Public License v3.0
116 stars 35 forks source link

pair_wise_cls_loss incorrect? #22

Closed lars-nieradzik closed 7 months ago

lars-nieradzik commented 7 months ago

Hi, thank you for rewriting YOLOv7. The original code is really a mess.

https://github.com/Chris-hughes10/Yolov7-training/blob/b1dad9942ee406f9e095d7b442e9730cb017de55/yolov7/loss.py#L626 might be incorrect. At least mixed precision does not work, when you write it this way.

The original code does: y = sqrt(sigmoid(x) sigmoid(y)), then applying log(y/(1-y)) to get back to the logit. binary_cross_entropy_with_logits is needed for mixed precision. In the end, the original pair_wise_cls_loss is simplified something like -0.5(plog(e^(x + y)/((1 + e^x) (1 + e^y))) + (1-p)log(1 - e^(x + y)/((1 + e^x) (1 + e^y)))). Might be possible to simplify this part in a different way.

pred_class_probs = (
    (pred_class_scores.sigmoid_() * pred_objectness.sigmoid_())
    .unsqueeze(dim=0)
    .repeat(num_image_targets, 1, 1)
)
y = pred_class_probs.sqrt_()
pair_wise_cls_loss = F.binary_cross_entropy_with_logits(
    torch.log(y/(1-y)), target_class_probs, reduction="none"
).sum(-1)

I was also wondering if you tested your code on bigger datasets. I have trouble with PASCAL VOC.

Chris-hughes10 commented 7 months ago

Hi, thanks for raising this, your proposed change looks good. Please create a PR and we can merge it in!

We ran this codebase on a large private dataset and didn't have any issues, what are the problems you are having?

lars-nieradzik commented 7 months ago

I have added the pull request https://github.com/Chris-hughes10/Yolov7-training/pull/23. I was able to solve my problem with PASCAL VOC, I just chose the wrong hyperparameters. In the end, I can now confirm that your implementation works fine. On another private dataset, I was even able to outperform the original YOLOv7 implementation.

Chris-hughes10 commented 7 months ago

Fixed