channel_mapper.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. from typing import List, Tuple
  3. import torch.nn as nn
  4. from mmcv.cnn import ConvModule
  5. from mmengine.model import BaseModule
  6. from torch import Tensor
  7. from mmdet.registry import MODELS
  8. from mmdet.utils import OptConfigType, OptMultiConfig
  9. @MODELS.register_module()
  10. class ChannelMapper(BaseModule):
  11. """Channel Mapper to reduce/increase channels of backbone features.
  12. This is used to reduce/increase channels of backbone features.
  13. Args:
  14. in_channels (List[int]): Number of input channels per scale.
  15. out_channels (int): Number of output channels (used at each scale).
  16. kernel_size (int, optional): kernel_size for reducing channels (used
  17. at each scale). Default: 3.
  18. conv_cfg (:obj:`ConfigDict` or dict, optional): Config dict for
  19. convolution layer. Default: None.
  20. norm_cfg (:obj:`ConfigDict` or dict, optional): Config dict for
  21. normalization layer. Default: None.
  22. act_cfg (:obj:`ConfigDict` or dict, optional): Config dict for
  23. activation layer in ConvModule. Default: dict(type='ReLU').
  24. num_outs (int, optional): Number of output feature maps. There would
  25. be extra_convs when num_outs larger than the length of in_channels.
  26. init_cfg (:obj:`ConfigDict` or dict or list[:obj:`ConfigDict` or dict],
  27. optional): Initialization config dict.
  28. Example:
  29. >>> import torch
  30. >>> in_channels = [2, 3, 5, 7]
  31. >>> scales = [340, 170, 84, 43]
  32. >>> inputs = [torch.rand(1, c, s, s)
  33. ... for c, s in zip(in_channels, scales)]
  34. >>> self = ChannelMapper(in_channels, 11, 3).eval()
  35. >>> outputs = self.forward(inputs)
  36. >>> for i in range(len(outputs)):
  37. ... print(f'outputs[{i}].shape = {outputs[i].shape}')
  38. outputs[0].shape = torch.Size([1, 11, 340, 340])
  39. outputs[1].shape = torch.Size([1, 11, 170, 170])
  40. outputs[2].shape = torch.Size([1, 11, 84, 84])
  41. outputs[3].shape = torch.Size([1, 11, 43, 43])
  42. """
  43. def __init__(
  44. self,
  45. in_channels: List[int],
  46. out_channels: int,
  47. kernel_size: int = 3,
  48. conv_cfg: OptConfigType = None,
  49. norm_cfg: OptConfigType = None,
  50. act_cfg: OptConfigType = dict(type='ReLU'),
  51. num_outs: int = None,
  52. init_cfg: OptMultiConfig = dict(
  53. type='Xavier', layer='Conv2d', distribution='uniform')
  54. ) -> None:
  55. super().__init__(init_cfg=init_cfg)
  56. assert isinstance(in_channels, list)
  57. self.extra_convs = None
  58. if num_outs is None:
  59. num_outs = len(in_channels)
  60. self.convs = nn.ModuleList()
  61. for in_channel in in_channels:
  62. self.convs.append(
  63. ConvModule(
  64. in_channel,
  65. out_channels,
  66. kernel_size,
  67. padding=(kernel_size - 1) // 2,
  68. conv_cfg=conv_cfg,
  69. norm_cfg=norm_cfg,
  70. act_cfg=act_cfg))
  71. if num_outs > len(in_channels):
  72. self.extra_convs = nn.ModuleList()
  73. for i in range(len(in_channels), num_outs):
  74. if i == len(in_channels):
  75. in_channel = in_channels[-1]
  76. else:
  77. in_channel = out_channels
  78. self.extra_convs.append(
  79. ConvModule(
  80. in_channel,
  81. out_channels,
  82. 3,
  83. stride=2,
  84. padding=1,
  85. conv_cfg=conv_cfg,
  86. norm_cfg=norm_cfg,
  87. act_cfg=act_cfg))
  88. def forward(self, inputs: Tuple[Tensor]) -> Tuple[Tensor]:
  89. """Forward function."""
  90. assert len(inputs) == len(self.convs)
  91. outs = [self.convs[i](inputs[i]) for i in range(len(inputs))]
  92. if self.extra_convs:
  93. for i in range(len(self.extra_convs)):
  94. if i == 0:
  95. outs.append(self.extra_convs[0](inputs[-1]))
  96. else:
  97. outs.append(self.extra_convs[i](outs[-1]))
  98. return tuple(outs)