dsifn.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. # Refer to
  15. # https://github.com/GeoZcx/A-deeply-supervised-image-fusion-network-for-change-detection-in-remote-sensing-images .
  16. import paddle
  17. import paddle.nn as nn
  18. import paddle.nn.functional as F
  19. from paddle.vision.models import vgg16
  20. from .layers import Conv1x1, make_norm, ChannelAttention, SpatialAttention
  21. class DSIFN(nn.Layer):
  22. """
  23. The DSIFN implementation based on PaddlePaddle.
  24. The original article refers to
  25. C. Zhang, et al., "A deeply supervised image fusion network for change detection in high resolution bi-temporal remote
  26. sensing images"
  27. (https://www.sciencedirect.com/science/article/pii/S0924271620301532).
  28. Note that in this implementation, there is a flexible number of target classes.
  29. Args:
  30. num_classes (int): The number of target classes.
  31. use_dropout (bool, optional): A bool value that indicates whether to use dropout layers. When the model is trained
  32. on a relatively small dataset, the dropout layers help prevent overfitting. Default: False.
  33. """
  34. def __init__(self, num_classes, use_dropout=False):
  35. super(DSIFN, self).__init__()
  36. self.encoder1 = self.encoder2 = VGG16FeaturePicker()
  37. self.sa1 = SpatialAttention()
  38. self.sa2 = SpatialAttention()
  39. self.sa3 = SpatialAttention()
  40. self.sa4 = SpatialAttention()
  41. self.sa5 = SpatialAttention()
  42. self.ca1 = ChannelAttention(in_ch=1024)
  43. self.bn_ca1 = make_norm(1024)
  44. self.o1_conv1 = conv2d_bn(1024, 512, use_dropout)
  45. self.o1_conv2 = conv2d_bn(512, 512, use_dropout)
  46. self.bn_sa1 = make_norm(512)
  47. self.o1_conv3 = Conv1x1(512, num_classes)
  48. self.trans_conv1 = nn.Conv2DTranspose(512, 512, kernel_size=2, stride=2)
  49. self.ca2 = ChannelAttention(in_ch=1536)
  50. self.bn_ca2 = make_norm(1536)
  51. self.o2_conv1 = conv2d_bn(1536, 512, use_dropout)
  52. self.o2_conv2 = conv2d_bn(512, 256, use_dropout)
  53. self.o2_conv3 = conv2d_bn(256, 256, use_dropout)
  54. self.bn_sa2 = make_norm(256)
  55. self.o2_conv4 = Conv1x1(256, num_classes)
  56. self.trans_conv2 = nn.Conv2DTranspose(256, 256, kernel_size=2, stride=2)
  57. self.ca3 = ChannelAttention(in_ch=768)
  58. self.o3_conv1 = conv2d_bn(768, 256, use_dropout)
  59. self.o3_conv2 = conv2d_bn(256, 128, use_dropout)
  60. self.o3_conv3 = conv2d_bn(128, 128, use_dropout)
  61. self.bn_sa3 = make_norm(128)
  62. self.o3_conv4 = Conv1x1(128, num_classes)
  63. self.trans_conv3 = nn.Conv2DTranspose(128, 128, kernel_size=2, stride=2)
  64. self.ca4 = ChannelAttention(in_ch=384)
  65. self.o4_conv1 = conv2d_bn(384, 128, use_dropout)
  66. self.o4_conv2 = conv2d_bn(128, 64, use_dropout)
  67. self.o4_conv3 = conv2d_bn(64, 64, use_dropout)
  68. self.bn_sa4 = make_norm(64)
  69. self.o4_conv4 = Conv1x1(64, num_classes)
  70. self.trans_conv4 = nn.Conv2DTranspose(64, 64, kernel_size=2, stride=2)
  71. self.ca5 = ChannelAttention(in_ch=192)
  72. self.o5_conv1 = conv2d_bn(192, 64, use_dropout)
  73. self.o5_conv2 = conv2d_bn(64, 32, use_dropout)
  74. self.o5_conv3 = conv2d_bn(32, 16, use_dropout)
  75. self.bn_sa5 = make_norm(16)
  76. self.o5_conv4 = Conv1x1(16, num_classes)
  77. self.init_weight()
  78. def forward(self, t1, t2):
  79. # Extract bi-temporal features.
  80. with paddle.no_grad():
  81. self.encoder1.eval(), self.encoder2.eval()
  82. t1_feats = self.encoder1(t1)
  83. t2_feats = self.encoder2(t2)
  84. t1_f_l3, t1_f_l8, t1_f_l15, t1_f_l22, t1_f_l29 = t1_feats
  85. t2_f_l3, t2_f_l8, t2_f_l15, t2_f_l22, t2_f_l29, = t2_feats
  86. aux_x = []
  87. # Multi-level decoding
  88. x = paddle.concat([t1_f_l29, t2_f_l29], axis=1)
  89. x = self.o1_conv1(x)
  90. x = self.o1_conv2(x)
  91. x = self.sa1(x) * x
  92. x = self.bn_sa1(x)
  93. if self.training:
  94. aux_x.append(x)
  95. x = self.trans_conv1(x)
  96. x = paddle.concat([x, t1_f_l22, t2_f_l22], axis=1)
  97. x = self.ca2(x) * x
  98. x = self.o2_conv1(x)
  99. x = self.o2_conv2(x)
  100. x = self.o2_conv3(x)
  101. x = self.sa2(x) * x
  102. x = self.bn_sa2(x)
  103. if self.training:
  104. aux_x.append(x)
  105. x = self.trans_conv2(x)
  106. x = paddle.concat([x, t1_f_l15, t2_f_l15], axis=1)
  107. x = self.ca3(x) * x
  108. x = self.o3_conv1(x)
  109. x = self.o3_conv2(x)
  110. x = self.o3_conv3(x)
  111. x = self.sa3(x) * x
  112. x = self.bn_sa3(x)
  113. if self.training:
  114. aux_x.append(x)
  115. x = self.trans_conv3(x)
  116. x = paddle.concat([x, t1_f_l8, t2_f_l8], axis=1)
  117. x = self.ca4(x) * x
  118. x = self.o4_conv1(x)
  119. x = self.o4_conv2(x)
  120. x = self.o4_conv3(x)
  121. x = self.sa4(x) * x
  122. x = self.bn_sa4(x)
  123. if self.training:
  124. aux_x.append(x)
  125. x = self.trans_conv4(x)
  126. x = paddle.concat([x, t1_f_l3, t2_f_l3], axis=1)
  127. x = self.ca5(x) * x
  128. x = self.o5_conv1(x)
  129. x = self.o5_conv2(x)
  130. x = self.o5_conv3(x)
  131. x = self.sa5(x) * x
  132. x = self.bn_sa5(x)
  133. out5 = self.o5_conv4(x)
  134. if not self.training:
  135. return [out5]
  136. else:
  137. size = paddle.shape(t1)[2:]
  138. out1 = F.interpolate(
  139. self.o1_conv3(aux_x[0]),
  140. size=size,
  141. mode='bilinear',
  142. align_corners=True)
  143. out2 = F.interpolate(
  144. self.o2_conv4(aux_x[1]),
  145. size=size,
  146. mode='bilinear',
  147. align_corners=True)
  148. out3 = F.interpolate(
  149. self.o3_conv4(aux_x[2]),
  150. size=size,
  151. mode='bilinear',
  152. align_corners=True)
  153. out4 = F.interpolate(
  154. self.o4_conv4(aux_x[3]),
  155. size=size,
  156. mode='bilinear',
  157. align_corners=True)
  158. return [out5, out4, out3, out2, out1]
  159. def init_weight(self):
  160. # Do nothing
  161. pass
  162. class VGG16FeaturePicker(nn.Layer):
  163. def __init__(self, indices=(3, 8, 15, 22, 29)):
  164. super(VGG16FeaturePicker, self).__init__()
  165. features = list(vgg16(pretrained=True).features)[:30]
  166. self.features = nn.LayerList(features)
  167. self.features.eval()
  168. self.indices = set(indices)
  169. def forward(self, x):
  170. picked_feats = []
  171. for idx, model in enumerate(self.features):
  172. x = model(x)
  173. if idx in self.indices:
  174. picked_feats.append(x)
  175. return picked_feats
  176. def conv2d_bn(in_ch, out_ch, with_dropout=True):
  177. lst = [
  178. nn.Conv2D(
  179. in_ch, out_ch, kernel_size=3, stride=1, padding=1),
  180. nn.PReLU(),
  181. make_norm(out_ch),
  182. ]
  183. if with_dropout:
  184. lst.append(nn.Dropout(p=0.6))
  185. return nn.Sequential(*lst)