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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] add ignore keys if classes differ - KIE training #1271

Merged
merged 1 commit into from
Jul 27, 2023

Conversation

felixdittrich92
Copy link
Contributor

This PR:

  • make it possible to use --pretrained if you use a pretrained det model and train on a KIE dataset
  • does nothing change for "normal" det training and inference
  • same way we have handled it for recognition on differing vocabs

Any feedback is welcome 馃

@felixdittrich92 felixdittrich92 self-assigned this Jul 27, 2023
@felixdittrich92 felixdittrich92 added type: bug Something isn't working module: models Related to doctr.models framework: pytorch Related to PyTorch backend topic: text detection Related to the task of text detection labels Jul 27, 2023
@felixdittrich92 felixdittrich92 added this to the 0.7.0 milestone Jul 27, 2023
@codecov
Copy link

codecov bot commented Jul 27, 2023

Codecov Report

Merging #1271 (d2ef624) into main (f113530) will increase coverage by 0.00%.
Report is 1 commits behind head on main.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main    #1271   +/-   ##
=======================================
  Coverage   95.75%   95.75%           
=======================================
  Files         154      154           
  Lines        6901     6903    +2     
=======================================
+ Hits         6608     6610    +2     
  Misses        293      293           
Flag Coverage 螖
unittests 95.75% <100.00%> (+<0.01%) 猬嗭笍

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

Files Changed Coverage 螖
...s/detection/differentiable_binarization/pytorch.py 97.79% <100.00%> (+0.01%) 猬嗭笍
doctr/models/detection/linknet/pytorch.py 98.19% <100.00%> (+0.01%) 猬嗭笍

... and 1 file with indirect coverage changes

Comment on lines +316 to +317
# The number of class_names is not the same as the number of classes in the pretrained model =>
# remove the layer weights
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you elaborate ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, just like with the recognition models, if we want to fine-tune a pre-trained model on another vocab we have to reset (resp. resize embeddings) the classifier head as well as the embeddings, otherwise it leads to a shape mismatch.
We have the same thing now when training on a KIE data set, since the number of classes varies, before that in normal detection training it is always one class (text) so it didn't matter.

Copy link
Contributor

Choose a reason for hiding this comment

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

This will reset the Conv"head" which makes it also possible to use '--pretrained' for KIE detection training without an impact on the normal detection training and inference otherwise '--pretrained' raises a shape mismatch while init the model with the pretrained checkpoint

Copy link
Contributor

Choose a reason for hiding this comment

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

as example for crnn:

if pretrained:
        # The number of classes is not the same as the number of classes in the pretrained model =>
        # remove the last layer weights
        _ignore_keys = ignore_keys if _cfg["vocab"] != default_cfgs[arch]["vocab"] else None
        load_pretrained_params(model, _cfg["url"], ignore_keys=_ignore_keys)
        
 ignore_keys=["linear.weight", "linear.bias"],

Copy link
Collaborator

Choose a reason for hiding this comment

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

Super clear, thanks

@felixdittrich92 felixdittrich92 merged commit 416c639 into mindee:main Jul 27, 2023
58 checks passed
@felixdittrich92 felixdittrich92 deleted the fix-kie-pretrained branch July 27, 2023 15:31
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 module: models Related to doctr.models topic: text detection Related to the task of text detection type: bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants