unet_ef.py 6.7 KB

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