test_postpros.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # Copyright (c) 2023 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 copy
  15. from PIL import Image
  16. import numpy as np
  17. import paddle
  18. import paddlers.utils.postprocs as P
  19. from testing_utils import CpuCommonTest
  20. __all__ = ['TestPostProgress']
  21. class TestPostProgress(CpuCommonTest):
  22. def setUp(self):
  23. self.image1 = np.asarray(Image.open("data/ssmt/optical_t2.bmp"))
  24. self.image2 = np.asarray(Image.open("data/ssmt/optical_t2.bmp"))
  25. self.b_label = np.asarray(Image.open("data/ssmt/binary_gt.bmp")).clip(0,
  26. 1)
  27. self.m_label = np.asarray(Image.open("data/ssmt/multiclass_gt2.png"))
  28. def test_prepro_mask(self):
  29. mask = copy.deepcopy(self.b_label)
  30. mask = P.prepro_mask(mask)
  31. self.check_output_equal(len(mask.shape), 2)
  32. self.assertEqual(mask.dtype, np.uint8)
  33. self.check_output_equal(np.unique(mask), np.array([0, 1]))
  34. mask_tensor = paddle.randn((1, 3, 256, 256), dtype="float32")
  35. mask_tensor = P.prepro_mask(mask_tensor)
  36. self.check_output_equal(len(mask_tensor.shape), 2)
  37. self.assertEqual(mask_tensor.dtype, np.uint8)
  38. self.check_output_equal(np.unique(mask_tensor), np.array([0, 1, 2]))
  39. def test_del_small_connection(self):
  40. mask = copy.deepcopy(self.b_label)
  41. mask = P.prepro_mask(mask)
  42. mask = P.del_small_connection(mask)
  43. self.check_output_equal(mask.shape, self.b_label.shape)
  44. self.assertEqual(mask.dtype, self.b_label.dtype)
  45. self.check_output_equal(np.unique(mask), np.unique(self.b_label))
  46. def test_fill_small_holes(self):
  47. mask = copy.deepcopy(self.b_label)
  48. mask = P.prepro_mask(mask)
  49. mask = P.fill_small_holes(mask)
  50. self.check_output_equal(mask.shape, self.b_label.shape)
  51. self.assertEqual(mask.dtype, self.b_label.dtype)
  52. self.check_output_equal(np.unique(mask), np.unique(self.b_label))
  53. def test_morphological_operation(self):
  54. mask = copy.deepcopy(self.b_label)
  55. mask = P.prepro_mask(mask)
  56. for op in ["open", "close", "erode", "dilate"]:
  57. mask = P.morphological_operation(mask, op)
  58. self.check_output_equal(mask.shape, self.b_label.shape)
  59. self.assertEqual(mask.dtype, self.b_label.dtype)
  60. self.check_output_equal(np.unique(mask), np.unique(self.b_label))
  61. def test_building_regularization(self):
  62. mask = copy.deepcopy(self.b_label)
  63. mask = P.prepro_mask(mask)
  64. mask = P.building_regularization(mask)
  65. self.check_output_equal(mask.shape, self.b_label.shape)
  66. self.assertEqual(mask.dtype, self.b_label.dtype)
  67. self.check_output_equal(np.unique(mask), np.unique(self.b_label))
  68. def test_cut_road_connection(self):
  69. mask = copy.deepcopy(self.b_label)
  70. mask = P.prepro_mask(mask)
  71. mask = P.cut_road_connection(mask)
  72. self.check_output_equal(mask.shape, self.b_label.shape)
  73. self.assertEqual(mask.dtype, self.b_label.dtype)
  74. self.check_output_equal(np.unique(mask), np.unique(self.b_label))
  75. def test_conditional_random_field(self):
  76. if "conditional_random_field" in dir(P):
  77. mask = copy.deepcopy(self.m_label)
  78. mask = P.prepro_mask(mask)
  79. mask = P.conditional_random_field(self.image2, mask)
  80. self.check_output_equal(mask.shape, self.m_label.shape)
  81. self.assertEqual(mask.dtype, self.m_label.dtype)
  82. self.check_output_equal(np.unique(mask), np.unique(self.m_label))
  83. def test_markov_random_field(self):
  84. mask = copy.deepcopy(self.m_label)
  85. mask = P.prepro_mask(mask)
  86. mask = P.markov_random_field(self.image2, mask)
  87. self.check_output_equal(mask.shape, self.m_label.shape)
  88. self.assertEqual(mask.dtype, self.m_label.dtype)
  89. self.check_output_equal(np.unique(mask), np.unique(self.m_label))
  90. def test_deal_one_class(self):
  91. mask = copy.deepcopy(self.m_label)
  92. mask = P.prepro_mask(mask)
  93. func = P.morphological_operation
  94. mask = P.deal_one_class(mask, 1, func, ops="dilate")
  95. self.check_output_equal(mask.shape, self.m_label.shape)
  96. self.assertEqual(mask.dtype, self.m_label.dtype)
  97. self.check_output_equal(np.unique(mask), np.unique(self.m_label))
  98. def test_change_(self):
  99. mask = copy.deepcopy(self.m_label)
  100. mask = P.prepro_mask(mask)
  101. mask = P.change_detection_filter(mask, self.image1, self.image2, 0.8,
  102. 0.8, "GLI", {"b": 3,
  103. "g": 2,
  104. "r": 1})
  105. self.check_output_equal(mask.shape, self.m_label.shape)
  106. self.assertEqual(mask.dtype, self.m_label.dtype)
  107. self.check_output_equal(np.unique(mask), np.unique(self.m_label))