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

feat: add dice loss in linknet #816

Merged
merged 3 commits into from
Feb 14, 2022
Merged

feat: add dice loss in linknet #816

merged 3 commits into from
Feb 14, 2022

Conversation

charlesmindee
Copy link
Collaborator

This PR adds dice loss in the linknet loss (which is now bce_loss + dice_loss) and remove the boosted edges part of the loss since it hasn't led to successful trainings. Any feedback is welcome!

@charlesmindee charlesmindee added type: enhancement Improvement module: models Related to doctr.models framework: pytorch Related to PyTorch backend framework: tensorflow Related to TensorFlow backend topic: text detection Related to the task of text detection labels Feb 9, 2022
@charlesmindee charlesmindee self-assigned this Feb 9, 2022
@charlesmindee charlesmindee added this to the 0.5.1 milestone Feb 9, 2022
@codecov
Copy link

codecov bot commented Feb 14, 2022

Codecov Report

Merging #816 (7fb3c2b) into main (bf2d4ca) will increase coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #816      +/-   ##
==========================================
+ Coverage   96.01%   96.02%   +0.01%     
==========================================
  Files         131      131              
  Lines        4941     4935       -6     
==========================================
- Hits         4744     4739       -5     
+ Misses        197      196       -1     
Flag Coverage Δ
unittests 96.02% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
doctr/models/detection/linknet/base.py 93.50% <100.00%> (-0.47%) ⬇️
doctr/models/detection/linknet/pytorch.py 98.86% <100.00%> (ø)
doctr/models/detection/linknet/tensorflow.py 98.66% <100.00%> (ø)
doctr/models/builder.py 97.05% <0.00%> (+0.98%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bf2d4ca...7fb3c2b. Read the comment docs.

fg-mindee
fg-mindee previously approved these changes Feb 14, 2022
Copy link
Contributor

@fg-mindee fg-mindee left a comment

Choose a reason for hiding this comment

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

Thanks! Added a few optional suggestions

Comment on lines +182 to +184
inter = (prob_map[seg_mask] * seg_target[seg_mask]).sum()
cardinality = (prob_map[seg_mask] + seg_target[seg_mask]).sum()
dice_loss = 1 - 2 * inter / (cardinality + 1e-8)
Copy link
Contributor

Choose a reason for hiding this comment

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

we'll have to update this in another PR because it highly advantages classes that occupy the biggest area

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, I will do it in the next PR !

Copy link
Contributor

Choose a reason for hiding this comment

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

To curb this, may be FL + Dice Loss could be a better option? Because, even if the area was same for the classes, since total area of combined bboxes / total area of image could be really less, it may tend to learn more negatives.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yup, I meant that the baseline option would be to average the loss over classes (not mixing the contribution of each class to the loss before the last step). But your suggestion @SiddhantBahuguna is worth exploring as a follow-up and could improve results 👍

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for your suggestions, I will add focal loss in the next PR !

doctr/models/detection/linknet/pytorch.py Outdated Show resolved Hide resolved
doctr/models/detection/linknet/tensorflow.py Outdated Show resolved Hide resolved
Copy link
Contributor

@fg-mindee fg-mindee left a comment

Choose a reason for hiding this comment

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

Looks good!

@charlesmindee charlesmindee merged commit 612e4f8 into main Feb 14, 2022
@charlesmindee charlesmindee deleted the linknet_dice branch February 14, 2022 17:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
framework: pytorch Related to PyTorch backend framework: tensorflow Related to TensorFlow backend module: models Related to doctr.models topic: text detection Related to the task of text detection type: enhancement Improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants