custom_model.py 6.9 KB

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