fpn_unet.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. self.conv3x3 = nn.Conv2D(
  29. in_channels, out_channels, kernel_size=3, stride=1, padding=1)
  30. self.deconv = nn.Conv2DTranspose(
  31. out_channels, out_channels, kernel_size=4, stride=2, padding=1)
  32. def forward(self, x):
  33. x = F.relu(self.conv1x1(x))
  34. x = F.relu(self.conv3x3(x))
  35. x = self.deconv(x)
  36. return x
  37. class FPN_UNet(nn.Layer):
  38. def __init__(self, in_channels, out_channels):
  39. super().__init__()
  40. assert len(in_channels) == 4
  41. assert isinstance(out_channels, int)
  42. self.out_channels = out_channels
  43. blocks_out_channels = [out_channels] + [
  44. min(out_channels * 2**i, 256) for i in range(4)
  45. ]
  46. blocks_in_channels = [blocks_out_channels[1]] + [
  47. in_channels[i] + blocks_out_channels[i + 2] for i in range(3)
  48. ] + [in_channels[3]]
  49. self.up4 = nn.Conv2DTranspose(
  50. blocks_in_channels[4],
  51. blocks_out_channels[4],
  52. kernel_size=4,
  53. stride=2,
  54. padding=1)
  55. self.up_block3 = UpBlock(blocks_in_channels[3], blocks_out_channels[3])
  56. self.up_block2 = UpBlock(blocks_in_channels[2], blocks_out_channels[2])
  57. self.up_block1 = UpBlock(blocks_in_channels[1], blocks_out_channels[1])
  58. self.up_block0 = UpBlock(blocks_in_channels[0], blocks_out_channels[0])
  59. def forward(self, x):
  60. """
  61. Args:
  62. x (list[Tensor] | tuple[Tensor]): A list of four tensors of shape
  63. :math:`(N, C_i, H_i, W_i)`, representing C2, C3, C4, C5
  64. features respectively. :math:`C_i` should matches the number in
  65. ``in_channels``.
  66. Returns:
  67. Tensor: Shape :math:`(N, C, H, W)` where :math:`H=4H_0` and
  68. :math:`W=4W_0`.
  69. """
  70. c2, c3, c4, c5 = x
  71. x = F.relu(self.up4(c5))
  72. x = paddle.concat([x, c4], axis=1)
  73. x = F.relu(self.up_block3(x))
  74. x = paddle.concat([x, c3], axis=1)
  75. x = F.relu(self.up_block2(x))
  76. x = paddle.concat([x, c2], axis=1)
  77. x = F.relu(self.up_block1(x))
  78. x = self.up_block0(x)
  79. return x