test_seg_models.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 paddlers
  15. from rs_models.test_model import TestModel
  16. __all__ = ['TestFarSegModel', 'TestFactSegModel']
  17. class TestSegModel(TestModel):
  18. DEFAULT_HW = (512, 512)
  19. def check_output(self, output, target):
  20. self.assertIsInstance(output, list)
  21. self.check_output_equal(len(output), len(target))
  22. for o, t in zip(output, target):
  23. if isinstance(o, list):
  24. self.check_output(o, t)
  25. else:
  26. o = o.numpy()
  27. self.check_output_equal(o.shape, t.shape)
  28. def set_inputs(self):
  29. def _gen_data(specs):
  30. for spec in specs:
  31. c = spec.get('in_channels', 3)
  32. yield self.get_randn_tensor(c)
  33. self.inputs = _gen_data(self.specs)
  34. def set_targets(self):
  35. def _gen_data(specs):
  36. for spec in specs:
  37. c = spec.get('num_classes', 2)
  38. yield [self.get_zeros_array(c)]
  39. self.targets = _gen_data(self.specs)
  40. class TestFarSegModel(TestSegModel):
  41. MODEL_CLASS = paddlers.rs_models.seg.FarSeg
  42. def set_specs(self):
  43. base_spec = dict(in_channels=3, num_classes=2)
  44. self.specs = [
  45. base_spec,
  46. dict(in_channels=6, num_classes=10),
  47. dict(**base_spec,
  48. backbone='resnet18',
  49. backbone_pretrained=False),
  50. dict(**base_spec,
  51. fpn_out_channels=128,
  52. fsr_out_channels=64,
  53. decoder_out_channels=32),
  54. dict(**base_spec, scale_aware_proj=False)
  55. ] # yapf: disable
  56. def set_targets(self):
  57. self.targets = [[self.get_zeros_array(2)], [self.get_zeros_array(10)],
  58. [self.get_zeros_array(2)], [self.get_zeros_array(2)],
  59. [self.get_zeros_array(2)]]
  60. class TestFactSegModel(TestSegModel):
  61. MODEL_CLASS = paddlers.rs_models.seg.FactSeg
  62. def set_specs(self):
  63. base_spec = dict(in_channels=3, num_classes=2)
  64. self.specs = [
  65. base_spec,
  66. dict(in_channels=6, num_classes=10),
  67. dict(**base_spec,
  68. backbone='resnet50',
  69. backbone_pretrained=False)
  70. ] # yapf: disable
  71. def set_targets(self):
  72. self.targets = [[self.get_zeros_array(2)], [self.get_zeros_array(10)],
  73. [self.get_zeros_array(2)]]