test_cd_models.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. from itertools import cycle
  15. import paddlers
  16. from rs_models.test_model import TestModel
  17. class _CDModelAdapter(object):
  18. def __init__(self, cd_model):
  19. super().__init__()
  20. self.cd_model = cd_model
  21. def __call__(self, input):
  22. return self.cd_model(input[0], input[1])
  23. class TestCDModel(TestModel):
  24. EF_MODE = 'None' # Early-fusion strategy
  25. def check_output(self, output, target):
  26. self.assertIsInstance(output, list)
  27. self.check_output_equal(len(output), len(target))
  28. for o, t in zip(output, target):
  29. o = o.numpy()
  30. self.check_output_equal(o.shape[0], t.shape[0])
  31. self.check_output_equal(len(o.shape), 4)
  32. self.check_output_equal(o.shape[2:], t.shape[2:])
  33. def set_inputs(self):
  34. if self.EF_MODE == 'Concat':
  35. # Early-fusion
  36. def _gen_data(specs):
  37. for spec in specs:
  38. c = spec['in_channels'] // 2
  39. assert c * 2 == spec['in_channels']
  40. yield [self.get_randn_tensor(c), self.get_randn_tensor(c)]
  41. elif self.EF_MODE == 'None':
  42. # Late-fusion
  43. def _gen_data(specs):
  44. for spec in specs:
  45. c = spec.get('in_channels', 3)
  46. yield [self.get_randn_tensor(c), self.get_randn_tensor(c)]
  47. else:
  48. raise ValueError
  49. self.inputs = _gen_data(self.specs)
  50. def set_targets(self):
  51. def _gen_data(specs):
  52. for spec in specs:
  53. c = spec.get('num_classes', 2)
  54. yield [self.get_zeros_array(c)]
  55. self.targets = _gen_data(self.specs)
  56. def build_model(self, spec):
  57. model = super().build_model(spec)
  58. return _CDModelAdapter(model)
  59. class TestBITModel(TestCDModel):
  60. MODEL_CLASS = paddlers.custom_models.cd.BIT
  61. def set_specs(self):
  62. base_spec = dict(in_channels=3, num_classes=2)
  63. self.specs = [
  64. base_spec,
  65. dict(**base_spec, backbone='resnet34'),
  66. dict(**base_spec, n_stages=3),
  67. dict(**base_spec, enc_depth=4, dec_head_dim=16),
  68. dict(in_channels=4, num_classes=2),
  69. dict(in_channels=3, num_classes=8)
  70. ]
  71. class TestCDNetModel(TestCDModel):
  72. MODEL_CLASS = paddlers.custom_models.cd.CDNet
  73. EF_MODE = 'Concat'
  74. def set_specs(self):
  75. self.specs = [
  76. dict(in_channels=6, num_classes=2),
  77. dict(in_channels=8, num_classes=2),
  78. dict(in_channels=6, num_classes=8)
  79. ]
  80. class TestChangeStarModel(TestCDModel):
  81. MODEL_CLASS = paddlers.custom_models.cd.ChangeStar
  82. def set_specs(self):
  83. self.specs = [
  84. dict(num_classes=2), dict(num_classes=10),
  85. dict(num_classes=2, mid_channels=128, num_convs=2),
  86. dict(num_classes=2, _phase='eval', _stop_grad=True)
  87. ]
  88. def set_targets(self):
  89. # Avoid allocation of large memories
  90. tar_c2 = [self.get_zeros_array(2)] * 4
  91. self.targets = [
  92. tar_c2,
  93. [self.get_zeros_array(10)] * 2 + [self.get_zeros_array(2)] * 2,
  94. tar_c2, [self.get_zeros_array(2)]
  95. ]
  96. class TestDSAMNetModel(TestCDModel):
  97. MODEL_CLASS = paddlers.custom_models.cd.DSAMNet
  98. def set_specs(self):
  99. base_spec = dict(in_channels=3, num_classes=2)
  100. self.specs = [
  101. base_spec,
  102. dict(in_channels=8, num_classes=2),
  103. dict(in_channels=3, num_classes=8),
  104. dict(**base_spec, ca_ratio=4, sa_kernel=5),
  105. dict(*base_spec, _phase='eval', _stop_grad=True)
  106. ]
  107. def set_targets(self):
  108. # Avoid allocation of large memories
  109. tar_c2 = [self.get_zeros_array(2)] * 3
  110. self.targets = [
  111. tar_c2, tar_c2, [self.get_zeros_array(8)] * 3, tar_c2,
  112. [self.get_zeros_array(2)]
  113. ]
  114. class TestDSIFNModel(TestCDModel):
  115. MODEL_CLASS = paddlers.custom_models.cd.DSIFN
  116. def set_specs(self):
  117. self.specs = [
  118. dict(num_classes=2), dict(num_classes=10),
  119. dict(num_classes=2, use_dropout=True),
  120. dict(num_classes=2, _phase='eval', _stop_grad=True)
  121. ]
  122. def set_targets(self):
  123. # Avoid allocation of large memories
  124. tar_c2 = [self.get_zeros_array(2)] * 5
  125. self.targets = [
  126. tar_c2, [self.get_zeros_array(10)] * 5, tar_c2,
  127. [self.get_zeros_array(2)]
  128. ]
  129. class TestFCEarlyFusionModel(TestCDModel):
  130. MODEL_CLASS = paddlers.custom_models.cd.FCEarlyFusion
  131. EF_MODE = 'Concat'
  132. def set_specs(self):
  133. self.specs = [
  134. dict(in_channels=6, num_classes=2),
  135. dict(in_channels=8, num_classes=2),
  136. dict(in_channels=6, num_classes=8),
  137. dict(in_channels=6, num_classes=2, use_dropout=True)
  138. ]
  139. class TestFCSiamConcModel(TestCDModel):
  140. MODEL_CLASS = paddlers.custom_models.cd.FCSiamConc
  141. def set_specs(self):
  142. self.specs = [
  143. dict(in_channels=3, num_classes=2),
  144. dict(in_channels=8, num_classes=2),
  145. dict(in_channels=3, num_classes=8),
  146. dict(in_channels=3, num_classes=2, use_dropout=True)
  147. ]
  148. class TestFCSiamDiffModel(TestCDModel):
  149. MODEL_CLASS = paddlers.custom_models.cd.FCSiamDiff
  150. def set_specs(self):
  151. self.specs = [
  152. dict(in_channels=3, num_classes=2),
  153. dict(in_channels=8, num_classes=2),
  154. dict(in_channels=3, num_classes=8),
  155. dict(in_channels=3, num_classes=2, use_dropout=True)
  156. ]
  157. class TestSNUNetModel(TestCDModel):
  158. MODEL_CLASS = paddlers.custom_models.cd.SNUNet
  159. def set_specs(self):
  160. self.specs = [
  161. dict(in_channels=3, num_classes=2),
  162. dict(in_channels=8, num_classes=2),
  163. dict(in_channels=3, num_classes=8),
  164. dict(in_channels=3, num_classes=2, width=64)
  165. ]
  166. class TestSTANetModel(TestCDModel):
  167. MODEL_CLASS = paddlers.custom_models.cd.STANet
  168. def set_specs(self):
  169. base_spec = dict(in_channels=3, num_classes=2)
  170. self.specs = [
  171. base_spec,
  172. dict(in_channels=8, num_classes=2),
  173. dict(in_channels=3, num_classes=8),
  174. dict(**base_spec, att_type='PAM'),
  175. dict(**base_spec, ds_factor=4)
  176. ]