fc_siam_diff.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. # Transferred from https://github.com/rcdaudt/fully_convolutional_change_detection/blob/master/siamunet_diff.py .
  15. import paddle
  16. import paddle.nn as nn
  17. import paddle.nn.functional as F
  18. from .layers import Conv3x3, MaxPool2x2, ConvTransposed3x3, Identity
  19. class FCSiamDiff(nn.Layer):
  20. """
  21. The FC-Siam-diff implementation based on PaddlePaddle.
  22. The original article refers to
  23. Caye Daudt, R., et al. "Fully convolutional siamese networks for change detection"
  24. (https://arxiv.org/abs/1810.08462).
  25. Args:
  26. in_channels (int): The number of bands of the input images.
  27. num_classes (int): The number of target classes.
  28. use_dropout (bool, optional): A bool value that indicates whether to use dropout layers. When the model is trained
  29. on a relatively small dataset, the dropout layers help prevent overfitting. Default: False.
  30. """
  31. def __init__(self, in_channels, num_classes, use_dropout=False):
  32. super(FCSiamDiff, self).__init__()
  33. C1, C2, C3, C4, C5 = 16, 32, 64, 128, 256
  34. self.use_dropout = use_dropout
  35. self.conv11 = Conv3x3(in_channels, C1, norm=True, act=True)
  36. self.do11 = self._make_dropout()
  37. self.conv12 = Conv3x3(C1, C1, norm=True, act=True)
  38. self.do12 = self._make_dropout()
  39. self.pool1 = MaxPool2x2()
  40. self.conv21 = Conv3x3(C1, C2, norm=True, act=True)
  41. self.do21 = self._make_dropout()
  42. self.conv22 = Conv3x3(C2, C2, norm=True, act=True)
  43. self.do22 = self._make_dropout()
  44. self.pool2 = MaxPool2x2()
  45. self.conv31 = Conv3x3(C2, C3, norm=True, act=True)
  46. self.do31 = self._make_dropout()
  47. self.conv32 = Conv3x3(C3, C3, norm=True, act=True)
  48. self.do32 = self._make_dropout()
  49. self.conv33 = Conv3x3(C3, C3, norm=True, act=True)
  50. self.do33 = self._make_dropout()
  51. self.pool3 = MaxPool2x2()
  52. self.conv41 = Conv3x3(C3, C4, norm=True, act=True)
  53. self.do41 = self._make_dropout()
  54. self.conv42 = Conv3x3(C4, C4, norm=True, act=True)
  55. self.do42 = self._make_dropout()
  56. self.conv43 = Conv3x3(C4, C4, norm=True, act=True)
  57. self.do43 = self._make_dropout()
  58. self.pool4 = MaxPool2x2()
  59. self.upconv4 = ConvTransposed3x3(C4, C4, output_padding=1)
  60. self.conv43d = Conv3x3(C5, C4, norm=True, act=True)
  61. self.do43d = self._make_dropout()
  62. self.conv42d = Conv3x3(C4, C4, norm=True, act=True)
  63. self.do42d = self._make_dropout()
  64. self.conv41d = Conv3x3(C4, C3, norm=True, act=True)
  65. self.do41d = self._make_dropout()
  66. self.upconv3 = ConvTransposed3x3(C3, C3, output_padding=1)
  67. self.conv33d = Conv3x3(C4, C3, norm=True, act=True)
  68. self.do33d = self._make_dropout()
  69. self.conv32d = Conv3x3(C3, C3, norm=True, act=True)
  70. self.do32d = self._make_dropout()
  71. self.conv31d = Conv3x3(C3, C2, norm=True, act=True)
  72. self.do31d = self._make_dropout()
  73. self.upconv2 = ConvTransposed3x3(C2, C2, output_padding=1)
  74. self.conv22d = Conv3x3(C3, C2, norm=True, act=True)
  75. self.do22d = self._make_dropout()
  76. self.conv21d = Conv3x3(C2, C1, norm=True, act=True)
  77. self.do21d = self._make_dropout()
  78. self.upconv1 = ConvTransposed3x3(C1, C1, output_padding=1)
  79. self.conv12d = Conv3x3(C2, C1, norm=True, act=True)
  80. self.do12d = self._make_dropout()
  81. self.conv11d = Conv3x3(C1, num_classes)
  82. self.init_weight()
  83. def forward(self, t1, t2):
  84. # Encode t1
  85. # Stage 1
  86. x11 = self.do11(self.conv11(t1))
  87. x12_1 = self.do12(self.conv12(x11))
  88. x1p = self.pool1(x12_1)
  89. # Stage 2
  90. x21 = self.do21(self.conv21(x1p))
  91. x22_1 = self.do22(self.conv22(x21))
  92. x2p = self.pool2(x22_1)
  93. # Stage 3
  94. x31 = self.do31(self.conv31(x2p))
  95. x32 = self.do32(self.conv32(x31))
  96. x33_1 = self.do33(self.conv33(x32))
  97. x3p = self.pool3(x33_1)
  98. # Stage 4
  99. x41 = self.do41(self.conv41(x3p))
  100. x42 = self.do42(self.conv42(x41))
  101. x43_1 = self.do43(self.conv43(x42))
  102. x4p = self.pool4(x43_1)
  103. # Encode t2
  104. # Stage 1
  105. x11 = self.do11(self.conv11(t2))
  106. x12_2 = self.do12(self.conv12(x11))
  107. x1p = self.pool1(x12_2)
  108. # Stage 2
  109. x21 = self.do21(self.conv21(x1p))
  110. x22_2 = self.do22(self.conv22(x21))
  111. x2p = self.pool2(x22_2)
  112. # Stage 3
  113. x31 = self.do31(self.conv31(x2p))
  114. x32 = self.do32(self.conv32(x31))
  115. x33_2 = self.do33(self.conv33(x32))
  116. x3p = self.pool3(x33_2)
  117. # Stage 4
  118. x41 = self.do41(self.conv41(x3p))
  119. x42 = self.do42(self.conv42(x41))
  120. x43_2 = self.do43(self.conv43(x42))
  121. x4p = self.pool4(x43_2)
  122. # Decode
  123. # Stage 4d
  124. x4d = self.upconv4(x4p)
  125. pad4 = (0, paddle.shape(x43_1)[3] - paddle.shape(x4d)[3], 0,
  126. paddle.shape(x43_1)[2] - paddle.shape(x4d)[2])
  127. x4d = F.pad(x4d, pad=pad4, mode='replicate')
  128. x4d = paddle.concat([x4d, paddle.abs(x43_1 - x43_2)], 1)
  129. x43d = self.do43d(self.conv43d(x4d))
  130. x42d = self.do42d(self.conv42d(x43d))
  131. x41d = self.do41d(self.conv41d(x42d))
  132. # Stage 3d
  133. x3d = self.upconv3(x41d)
  134. pad3 = (0, paddle.shape(x33_1)[3] - paddle.shape(x3d)[3], 0,
  135. paddle.shape(x33_1)[2] - paddle.shape(x3d)[2])
  136. x3d = F.pad(x3d, pad=pad3, mode='replicate')
  137. x3d = paddle.concat([x3d, paddle.abs(x33_1 - x33_2)], 1)
  138. x33d = self.do33d(self.conv33d(x3d))
  139. x32d = self.do32d(self.conv32d(x33d))
  140. x31d = self.do31d(self.conv31d(x32d))
  141. # Stage 2d
  142. x2d = self.upconv2(x31d)
  143. pad2 = (0, paddle.shape(x22_1)[3] - paddle.shape(x2d)[3], 0,
  144. paddle.shape(x22_1)[2] - paddle.shape(x2d)[2])
  145. x2d = F.pad(x2d, pad=pad2, mode='replicate')
  146. x2d = paddle.concat([x2d, paddle.abs(x22_1 - x22_2)], 1)
  147. x22d = self.do22d(self.conv22d(x2d))
  148. x21d = self.do21d(self.conv21d(x22d))
  149. # Stage 1d
  150. x1d = self.upconv1(x21d)
  151. pad1 = (0, paddle.shape(x12_1)[3] - paddle.shape(x1d)[3], 0,
  152. paddle.shape(x12_1)[2] - paddle.shape(x1d)[2])
  153. x1d = F.pad(x1d, pad=pad1, mode='replicate')
  154. x1d = paddle.concat([x1d, paddle.abs(x12_1 - x12_2)], 1)
  155. x12d = self.do12d(self.conv12d(x1d))
  156. x11d = self.conv11d(x12d)
  157. return [x11d]
  158. def init_weight(self):
  159. pass
  160. def _make_dropout(self):
  161. if self.use_dropout:
  162. return nn.Dropout2D(p=0.2)
  163. else:
  164. return Identity()