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

fix: Fixed load_pretrained_params in PyTorch when ignoring keys #902

Merged
merged 11 commits into from
Apr 28, 2022
Prev Previous commit
Next Next commit
refactor: Refactored recognition ignore_keys
  • Loading branch information
FG Fernandez committed Apr 28, 2022
commit de0652bd3931d062f61c1235fca5d5ddf9eaee47
29 changes: 20 additions & 9 deletions doctr/models/recognition/crnn/pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ def _crnn(
pretrained: bool,
backbone_fn: Callable[[Any], nn.Module],
pretrained_backbone: bool = True,
ignore_keys: Optional[List[str]] = None,
**kwargs: Any,
) -> CRNN:

Expand All @@ -240,12 +241,10 @@ def _crnn(
model = CRNN(feat_extractor, cfg=_cfg, **kwargs) # type: ignore[arg-type]
# Load pretrained parameters
if pretrained:
if _cfg['vocab'] != default_cfgs[arch]['vocab']:
# The number of classes is not the same as the number of classes in the pretrained model =>
# remove the last layer weights
load_pretrained_params(model, _cfg['url'], pop_entrys=['linear.weight', 'linear.bias'])
else:
load_pretrained_params(model, _cfg['url'])
# 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 kwargs['num_classes'] == len(default_cfgs[arch]['classes']) else None
load_pretrained_params(model, _cfg['url'], ignore_keys=_ignore_keys)

return model

Expand All @@ -267,7 +266,7 @@ def crnn_vgg16_bn(pretrained: bool = False, **kwargs: Any) -> CRNN:
text recognition architecture
"""

return _crnn('crnn_vgg16_bn', pretrained, vgg16_bn_r, **kwargs)
return _crnn('crnn_vgg16_bn', pretrained, vgg16_bn_r, ignore_keys=['linear.weight', 'linear.bias'], **kwargs)


def crnn_mobilenet_v3_small(pretrained: bool = False, **kwargs: Any) -> CRNN:
Expand All @@ -287,7 +286,13 @@ def crnn_mobilenet_v3_small(pretrained: bool = False, **kwargs: Any) -> CRNN:
text recognition architecture
"""

return _crnn('crnn_mobilenet_v3_small', pretrained, mobilenet_v3_small_r, **kwargs)
return _crnn(
'crnn_mobilenet_v3_small',
pretrained,
mobilenet_v3_small_r,
ignore_keys=['linear.weight', 'linear.bias'],
**kwargs,
)


def crnn_mobilenet_v3_large(pretrained: bool = False, **kwargs: Any) -> CRNN:
Expand All @@ -307,4 +312,10 @@ def crnn_mobilenet_v3_large(pretrained: bool = False, **kwargs: Any) -> CRNN:
text recognition architecture
"""

return _crnn('crnn_mobilenet_v3_large', pretrained, mobilenet_v3_large_r, **kwargs)
return _crnn(
'crnn_mobilenet_v3_large',
pretrained,
mobilenet_v3_large_r,
ignore_keys=['linear.weight', 'linear.bias'],
**kwargs,
)
2 changes: 1 addition & 1 deletion doctr/models/utils/pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def load_pretrained_params(
>>> load_pretrained_params(model, "https://yoursource.com/yourcheckpoint-yourhash.zip")

Args:
model: the keras model to be loaded
model: the PyTorch model to be loaded
url: URL of the zipped set of parameters
hash_prefix: first characters of SHA256 expected hash
overwrite: should the zip extraction be enforced if the archive has already been extracted
Expand Down