ssd_vgg.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import warnings
  3. import torch.nn as nn
  4. from mmcv.cnn import VGG
  5. from mmengine.model import BaseModule
  6. from mmdet.registry import MODELS
  7. from ..necks import ssd_neck
  8. @MODELS.register_module()
  9. class SSDVGG(VGG, BaseModule):
  10. """VGG Backbone network for single-shot-detection.
  11. Args:
  12. depth (int): Depth of vgg, from {11, 13, 16, 19}.
  13. with_last_pool (bool): Whether to add a pooling layer at the last
  14. of the model
  15. ceil_mode (bool): When True, will use `ceil` instead of `floor`
  16. to compute the output shape.
  17. out_indices (Sequence[int]): Output from which stages.
  18. out_feature_indices (Sequence[int]): Output from which feature map.
  19. pretrained (str, optional): model pretrained path. Default: None
  20. init_cfg (dict or list[dict], optional): Initialization config dict.
  21. Default: None
  22. input_size (int, optional): Deprecated argumment.
  23. Width and height of input, from {300, 512}.
  24. l2_norm_scale (float, optional) : Deprecated argumment.
  25. L2 normalization layer init scale.
  26. Example:
  27. >>> self = SSDVGG(input_size=300, depth=11)
  28. >>> self.eval()
  29. >>> inputs = torch.rand(1, 3, 300, 300)
  30. >>> level_outputs = self.forward(inputs)
  31. >>> for level_out in level_outputs:
  32. ... print(tuple(level_out.shape))
  33. (1, 1024, 19, 19)
  34. (1, 512, 10, 10)
  35. (1, 256, 5, 5)
  36. (1, 256, 3, 3)
  37. (1, 256, 1, 1)
  38. """
  39. extra_setting = {
  40. 300: (256, 'S', 512, 128, 'S', 256, 128, 256, 128, 256),
  41. 512: (256, 'S', 512, 128, 'S', 256, 128, 'S', 256, 128, 'S', 256, 128),
  42. }
  43. def __init__(self,
  44. depth,
  45. with_last_pool=False,
  46. ceil_mode=True,
  47. out_indices=(3, 4),
  48. out_feature_indices=(22, 34),
  49. pretrained=None,
  50. init_cfg=None,
  51. input_size=None,
  52. l2_norm_scale=None):
  53. # TODO: in_channels for mmcv.VGG
  54. super(SSDVGG, self).__init__(
  55. depth,
  56. with_last_pool=with_last_pool,
  57. ceil_mode=ceil_mode,
  58. out_indices=out_indices)
  59. self.features.add_module(
  60. str(len(self.features)),
  61. nn.MaxPool2d(kernel_size=3, stride=1, padding=1))
  62. self.features.add_module(
  63. str(len(self.features)),
  64. nn.Conv2d(512, 1024, kernel_size=3, padding=6, dilation=6))
  65. self.features.add_module(
  66. str(len(self.features)), nn.ReLU(inplace=True))
  67. self.features.add_module(
  68. str(len(self.features)), nn.Conv2d(1024, 1024, kernel_size=1))
  69. self.features.add_module(
  70. str(len(self.features)), nn.ReLU(inplace=True))
  71. self.out_feature_indices = out_feature_indices
  72. assert not (init_cfg and pretrained), \
  73. 'init_cfg and pretrained cannot be specified at the same time'
  74. if init_cfg is not None:
  75. self.init_cfg = init_cfg
  76. elif isinstance(pretrained, str):
  77. warnings.warn('DeprecationWarning: pretrained is deprecated, '
  78. 'please use "init_cfg" instead')
  79. self.init_cfg = dict(type='Pretrained', checkpoint=pretrained)
  80. elif pretrained is None:
  81. self.init_cfg = [
  82. dict(type='Kaiming', layer='Conv2d'),
  83. dict(type='Constant', val=1, layer='BatchNorm2d'),
  84. dict(type='Normal', std=0.01, layer='Linear'),
  85. ]
  86. else:
  87. raise TypeError('pretrained must be a str or None')
  88. if input_size is not None:
  89. warnings.warn('DeprecationWarning: input_size is deprecated')
  90. if l2_norm_scale is not None:
  91. warnings.warn('DeprecationWarning: l2_norm_scale in VGG is '
  92. 'deprecated, it has been moved to SSDNeck.')
  93. def init_weights(self, pretrained=None):
  94. super(VGG, self).init_weights()
  95. def forward(self, x):
  96. """Forward function."""
  97. outs = []
  98. for i, layer in enumerate(self.features):
  99. x = layer(x)
  100. if i in self.out_feature_indices:
  101. outs.append(x)
  102. if len(outs) == 1:
  103. return outs[0]
  104. else:
  105. return tuple(outs)
  106. class L2Norm(ssd_neck.L2Norm):
  107. def __init__(self, **kwargs):
  108. super(L2Norm, self).__init__(**kwargs)
  109. warnings.warn('DeprecationWarning: L2Norm in ssd_vgg.py '
  110. 'is deprecated, please use L2Norm in '
  111. 'mmdet/models/necks/ssd_neck.py instead')