custom_model.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  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. import paddle
  15. import paddle.nn as nn
  16. import paddle.nn.functional as F
  17. import paddlers
  18. from paddlers.rs_models.cd.layers import Conv3x3, MaxPool2x2, ConvTransposed3x3, Identity
  19. from paddlers.rs_models.cd.layers import ChannelAttention
  20. from attach_tools import Attach
  21. attach = Attach.to(paddlers.rs_models.cd)
  22. @attach
  23. class CustomModel(nn.Layer):
  24. def __init__(self,
  25. in_channels,
  26. num_classes,
  27. att_types='ct',
  28. use_dropout=False):
  29. super(CustomModel, self).__init__()
  30. C1, C2, C3, C4, C5 = 16, 32, 64, 128, 256
  31. self.use_dropout = use_dropout
  32. self.conv11 = Conv3x3(in_channels, C1, norm=True, act=True)
  33. self.do11 = self._make_dropout()
  34. self.conv12 = Conv3x3(C1, C1, norm=True, act=True)
  35. self.do12 = self._make_dropout()
  36. self.pool1 = MaxPool2x2()
  37. self.conv21 = Conv3x3(C1, C2, norm=True, act=True)
  38. self.do21 = self._make_dropout()
  39. self.conv22 = Conv3x3(C2, C2, norm=True, act=True)
  40. self.do22 = self._make_dropout()
  41. self.pool2 = MaxPool2x2()
  42. self.conv31 = Conv3x3(C2, C3, norm=True, act=True)
  43. self.do31 = self._make_dropout()
  44. self.conv32 = Conv3x3(C3, C3, norm=True, act=True)
  45. self.do32 = self._make_dropout()
  46. self.conv33 = Conv3x3(C3, C3, norm=True, act=True)
  47. self.do33 = self._make_dropout()
  48. self.pool3 = MaxPool2x2()
  49. self.conv41 = Conv3x3(C3, C4, norm=True, act=True)
  50. self.do41 = self._make_dropout()
  51. self.conv42 = Conv3x3(C4, C4, norm=True, act=True)
  52. self.do42 = self._make_dropout()
  53. self.conv43 = Conv3x3(C4, C4, norm=True, act=True)
  54. self.do43 = self._make_dropout()
  55. self.pool4 = MaxPool2x2()
  56. self.upconv4 = ConvTransposed3x3(C4, C4, output_padding=1)
  57. self.conv43d = Conv3x3(C5 + C4, C4, norm=True, act=True)
  58. self.do43d = self._make_dropout()
  59. self.conv42d = Conv3x3(C4, C4, norm=True, act=True)
  60. self.do42d = self._make_dropout()
  61. self.conv41d = Conv3x3(C4, C3, norm=True, act=True)
  62. self.do41d = self._make_dropout()
  63. self.upconv3 = ConvTransposed3x3(C3, C3, output_padding=1)
  64. self.conv33d = Conv3x3(C4 + C3, C3, norm=True, act=True)
  65. self.do33d = self._make_dropout()
  66. self.conv32d = Conv3x3(C3, C3, norm=True, act=True)
  67. self.do32d = self._make_dropout()
  68. self.conv31d = Conv3x3(C3, C2, norm=True, act=True)
  69. self.do31d = self._make_dropout()
  70. self.upconv2 = ConvTransposed3x3(C2, C2, output_padding=1)
  71. self.conv22d = Conv3x3(C3 + C2, C2, norm=True, act=True)
  72. self.do22d = self._make_dropout()
  73. self.conv21d = Conv3x3(C2, C1, norm=True, act=True)
  74. self.do21d = self._make_dropout()
  75. self.upconv1 = ConvTransposed3x3(C1, C1, output_padding=1)
  76. self.conv12d = Conv3x3(C2 + C1, C1, norm=True, act=True)
  77. self.do12d = self._make_dropout()
  78. self.conv11d = Conv3x3(C1, num_classes)
  79. self.init_weight()
  80. self.att4 = MixedAttention(C4, att_types)
  81. def forward(self, t1, t2):
  82. # Encode t1
  83. # Stage 1
  84. x11 = self.do11(self.conv11(t1))
  85. x12_1 = self.do12(self.conv12(x11))
  86. x1p = self.pool1(x12_1)
  87. # Stage 2
  88. x21 = self.do21(self.conv21(x1p))
  89. x22_1 = self.do22(self.conv22(x21))
  90. x2p = self.pool2(x22_1)
  91. # Stage 3
  92. x31 = self.do31(self.conv31(x2p))
  93. x32 = self.do32(self.conv32(x31))
  94. x33_1 = self.do33(self.conv33(x32))
  95. x3p = self.pool3(x33_1)
  96. # Stage 4
  97. x41 = self.do41(self.conv41(x3p))
  98. x42 = self.do42(self.conv42(x41))
  99. x43_1 = self.do43(self.conv43(x42))
  100. x4p = self.pool4(x43_1)
  101. # Encode t2
  102. # Stage 1
  103. x11 = self.do11(self.conv11(t2))
  104. x12_2 = self.do12(self.conv12(x11))
  105. x1p = self.pool1(x12_2)
  106. # Stage 2
  107. x21 = self.do21(self.conv21(x1p))
  108. x22_2 = self.do22(self.conv22(x21))
  109. x2p = self.pool2(x22_2)
  110. # Stage 3
  111. x31 = self.do31(self.conv31(x2p))
  112. x32 = self.do32(self.conv32(x31))
  113. x33_2 = self.do33(self.conv33(x32))
  114. x3p = self.pool3(x33_2)
  115. # Stage 4
  116. x41 = self.do41(self.conv41(x3p))
  117. x42 = self.do42(self.conv42(x41))
  118. x43_2 = self.do43(self.conv43(x42))
  119. x4p = self.pool4(x43_2)
  120. # Decode
  121. # Stage 4d
  122. x4d = self.upconv4(x4p)
  123. pad4 = (0, x43_1.shape[3] - x4d.shape[3], 0,
  124. x43_1.shape[2] - x4d.shape[2])
  125. x4d = F.pad(x4d, pad=pad4, mode='replicate')
  126. x43_1, x43_2 = self.att4(x43_1, x43_2)
  127. x4d = paddle.concat([x4d, x43_1, x43_2], 1)
  128. x43d = self.do43d(self.conv43d(x4d))
  129. x42d = self.do42d(self.conv42d(x43d))
  130. x41d = self.do41d(self.conv41d(x42d))
  131. # Stage 3d
  132. x3d = self.upconv3(x41d)
  133. pad3 = (0, x33_1.shape[3] - x3d.shape[3], 0,
  134. x33_1.shape[2] - x3d.shape[2])
  135. x3d = F.pad(x3d, pad=pad3, mode='replicate')
  136. x3d = paddle.concat([x3d, x33_1, x33_2], 1)
  137. x33d = self.do33d(self.conv33d(x3d))
  138. x32d = self.do32d(self.conv32d(x33d))
  139. x31d = self.do31d(self.conv31d(x32d))
  140. # Stage 2d
  141. x2d = self.upconv2(x31d)
  142. pad2 = (0, x22_1.shape[3] - x2d.shape[3], 0,
  143. x22_1.shape[2] - x2d.shape[2])
  144. x2d = F.pad(x2d, pad=pad2, mode='replicate')
  145. x2d = paddle.concat([x2d, x22_1, x22_2], 1)
  146. x22d = self.do22d(self.conv22d(x2d))
  147. x21d = self.do21d(self.conv21d(x22d))
  148. # Stage 1d
  149. x1d = self.upconv1(x21d)
  150. pad1 = (0, x12_1.shape[3] - x1d.shape[3], 0,
  151. x12_1.shape[2] - x1d.shape[2])
  152. x1d = F.pad(x1d, pad=pad1, mode='replicate')
  153. x1d = paddle.concat([x1d, x12_1, x12_2], 1)
  154. x12d = self.do12d(self.conv12d(x1d))
  155. x11d = self.conv11d(x12d)
  156. return [x11d]
  157. def init_weight(self):
  158. pass
  159. def _make_dropout(self):
  160. if self.use_dropout:
  161. return nn.Dropout2D(p=0.2)
  162. else:
  163. return Identity()
  164. class MixedAttention(nn.Layer):
  165. def __init__(self, in_channels, att_types='ct'):
  166. super(MixedAttention, self).__init__()
  167. self.att_types = att_types
  168. if self.has_att_c:
  169. self.att_c = ChannelAttention(in_channels, ratio=1)
  170. else:
  171. self.att_c = Identity()
  172. if self.has_att_t:
  173. self.att_t = ChannelAttention(2, ratio=1)
  174. else:
  175. self.att_t = Identity()
  176. def forward(self, x1, x2):
  177. if self.has_att_c:
  178. x1 = (1 + self.att_c(x1)) * x1
  179. x2 = (1 + self.att_c(x2)) * x2
  180. if self.has_att_t:
  181. b, c = x1.shape[:2]
  182. y = paddle.stack([x1, x2], axis=2)
  183. y = paddle.flatten(y, stop_axis=1)
  184. y = (1 + self.att_t(y)) * y
  185. y = y.reshape((b, c, 2, *y.shape[2:]))
  186. y1, y2 = y[:, :, 0], y[:, :, 1]
  187. else:
  188. y1, y2 = x1, x2
  189. return y1, y2
  190. @property
  191. def has_att_c(self):
  192. return 'c' in self.att_types
  193. @property
  194. def has_att_t(self):
  195. return 't' in self.att_types