dsamnet.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. from .layers import make_norm, Conv3x3, CBAM
  18. from .stanet import Backbone, Decoder
  19. class DSAMNet(nn.Layer):
  20. """
  21. The DSAMNet implementation based on PaddlePaddle.
  22. The original article refers to
  23. Q. Shi, et al., "A Deeply Supervised Attention Metric-Based Network and an Open Aerial Image Dataset for Remote Sensing
  24. Change Detection"
  25. (https://ieeexplore.ieee.org/document/9467555).
  26. Note that this implementation differs from the original work in two aspects:
  27. 1. We do not use multiple dilation rates in layer 4 of the ResNet backbone.
  28. 2. A classification head is used in place of the original metric learning-based head to stablize the training process.
  29. Args:
  30. in_channels (int): The number of bands of the input images.
  31. num_classes (int): The number of target classes.
  32. ca_ratio (int, optional): The channel reduction ratio for the channel attention module. Default: 8.
  33. sa_kernel (int, optional): The size of the convolutional kernel used in the spatial attention module. Default: 7.
  34. """
  35. def __init__(self, in_channels, num_classes, ca_ratio=8, sa_kernel=7):
  36. super(DSAMNet, self).__init__()
  37. WIDTH = 64
  38. self.backbone = Backbone(
  39. in_ch=in_channels, arch='resnet18', strides=(1, 1, 2, 2, 1))
  40. self.decoder = Decoder(WIDTH)
  41. self.cbam1 = CBAM(64, ratio=ca_ratio, kernel_size=sa_kernel)
  42. self.cbam2 = CBAM(64, ratio=ca_ratio, kernel_size=sa_kernel)
  43. self.dsl2 = DSLayer(64, num_classes, 32, stride=2, output_padding=1)
  44. self.dsl3 = DSLayer(128, num_classes, 32, stride=4, output_padding=3)
  45. self.conv_out = nn.Sequential(
  46. Conv3x3(
  47. WIDTH, WIDTH, norm=True, act=True),
  48. Conv3x3(WIDTH, num_classes))
  49. self.init_weight()
  50. def forward(self, t1, t2):
  51. f1 = self.backbone(t1)
  52. f2 = self.backbone(t2)
  53. y1 = self.decoder(f1)
  54. y2 = self.decoder(f2)
  55. y1 = self.cbam1(y1)
  56. y2 = self.cbam2(y2)
  57. out = paddle.abs(y1 - y2)
  58. out = F.interpolate(
  59. out, size=paddle.shape(t1)[2:], mode='bilinear', align_corners=True)
  60. pred = self.conv_out(out)
  61. if not self.training:
  62. return [pred]
  63. else:
  64. ds2 = self.dsl2(paddle.abs(f1[0] - f2[0]))
  65. ds3 = self.dsl3(paddle.abs(f1[1] - f2[1]))
  66. return [pred, ds2, ds3]
  67. def init_weight(self):
  68. pass
  69. class DSLayer(nn.Sequential):
  70. def __init__(self, in_ch, out_ch, itm_ch, **convd_kwargs):
  71. super(DSLayer, self).__init__(
  72. nn.Conv2DTranspose(
  73. in_ch, itm_ch, kernel_size=3, padding=1, **convd_kwargs),
  74. make_norm(itm_ch),
  75. nn.ReLU(),
  76. nn.Dropout2D(p=0.2),
  77. nn.Conv2DTranspose(
  78. itm_ch, out_ch, kernel_size=3, padding=1))