fpn_unet.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # copyright (c) 2022 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """
  15. This code is refer from:
  16. https://github.com/open-mmlab/mmocr/blob/main/mmocr/models/textdet/necks/fpn_unet.py
  17. """
  18. import paddle
  19. import paddle.nn as nn
  20. import paddle.nn.functional as F
  21. class UpBlock(nn.Layer):
  22. def __init__(self, in_channels, out_channels):
  23. super().__init__()
  24. assert isinstance(in_channels, int)
  25. assert isinstance(out_channels, int)
  26. self.conv1x1 = nn.Conv2D(
  27. in_channels, in_channels, kernel_size=1, stride=1, padding=0
  28. )
  29. self.conv3x3 = nn.Conv2D(
  30. in_channels, out_channels, kernel_size=3, stride=1, padding=1
  31. )
  32. self.deconv = nn.Conv2DTranspose(
  33. out_channels, out_channels, kernel_size=4, stride=2, padding=1
  34. )
  35. def forward(self, x):
  36. x = F.relu(self.conv1x1(x))
  37. x = F.relu(self.conv3x3(x))
  38. x = self.deconv(x)
  39. return x
  40. class FPN_UNet(nn.Layer):
  41. def __init__(self, in_channels, out_channels):
  42. super().__init__()
  43. assert len(in_channels) == 4
  44. assert isinstance(out_channels, int)
  45. self.out_channels = out_channels
  46. blocks_out_channels = [out_channels] + [
  47. min(out_channels * 2**i, 256) for i in range(4)
  48. ]
  49. blocks_in_channels = (
  50. [blocks_out_channels[1]]
  51. + [in_channels[i] + blocks_out_channels[i + 2] for i in range(3)]
  52. + [in_channels[3]]
  53. )
  54. self.up4 = nn.Conv2DTranspose(
  55. blocks_in_channels[4],
  56. blocks_out_channels[4],
  57. kernel_size=4,
  58. stride=2,
  59. padding=1,
  60. )
  61. self.up_block3 = UpBlock(blocks_in_channels[3], blocks_out_channels[3])
  62. self.up_block2 = UpBlock(blocks_in_channels[2], blocks_out_channels[2])
  63. self.up_block1 = UpBlock(blocks_in_channels[1], blocks_out_channels[1])
  64. self.up_block0 = UpBlock(blocks_in_channels[0], blocks_out_channels[0])
  65. def forward(self, x):
  66. """
  67. Args:
  68. x (list[Tensor] | tuple[Tensor]): A list of four tensors of shape
  69. :math:`(N, C_i, H_i, W_i)`, representing C2, C3, C4, C5
  70. features respectively. :math:`C_i` should matches the number in
  71. ``in_channels``.
  72. Returns:
  73. Tensor: Shape :math:`(N, C, H, W)` where :math:`H=4H_0` and
  74. :math:`W=4W_0`.
  75. """
  76. c2, c3, c4, c5 = x
  77. x = F.relu(self.up4(c5))
  78. x = paddle.concat([x, c4], axis=1)
  79. x = F.relu(self.up_block3(x))
  80. x = paddle.concat([x, c3], axis=1)
  81. x = F.relu(self.up_block2(x))
  82. x = paddle.concat([x, c2], axis=1)
  83. x = F.relu(self.up_block1(x))
  84. x = self.up_block0(x)
  85. return x