You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_random_perspective.py 4.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  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. # ==============================================================================
  15. """
  16. Testing RandomPerspective op in DE
  17. """
  18. import numpy as np
  19. import mindspore.dataset as ds
  20. import mindspore.dataset.transforms.vision.py_transforms as py_vision
  21. from mindspore.dataset.transforms.vision.utils import Inter
  22. from mindspore import log as logger
  23. from util import visualize_list, save_and_check_md5, \
  24. config_get_set_seed, config_get_set_num_parallel_workers
  25. GENERATE_GOLDEN = False
  26. DATA_DIR = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"]
  27. SCHEMA_DIR = "../data/dataset/test_tf_file_3_images/datasetSchema.json"
  28. def test_random_perspective_op(plot=False):
  29. """
  30. Test RandomPerspective in python transformations
  31. """
  32. logger.info("test_random_perspective_op")
  33. # define map operations
  34. transforms1 = [
  35. py_vision.Decode(),
  36. py_vision.RandomPerspective(),
  37. py_vision.ToTensor()
  38. ]
  39. transform1 = py_vision.ComposeOp(transforms1)
  40. transforms2 = [
  41. py_vision.Decode(),
  42. py_vision.ToTensor()
  43. ]
  44. transform2 = py_vision.ComposeOp(transforms2)
  45. # First dataset
  46. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  47. data1 = data1.map(input_columns=["image"], operations=transform1())
  48. # Second dataset
  49. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  50. data2 = data2.map(input_columns=["image"], operations=transform2())
  51. image_perspective = []
  52. image_original = []
  53. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  54. image1 = (item1["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  55. image2 = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  56. image_perspective.append(image1)
  57. image_original.append(image2)
  58. if plot:
  59. visualize_list(image_original, image_perspective)
  60. def skip_test_random_perspective_md5():
  61. """
  62. Test RandomPerspective with md5 comparison
  63. """
  64. logger.info("test_random_perspective_md5")
  65. original_seed = config_get_set_seed(5)
  66. original_num_parallel_workers = config_get_set_num_parallel_workers(1)
  67. # define map operations
  68. transforms = [
  69. py_vision.Decode(),
  70. py_vision.RandomPerspective(distortion_scale=0.3, prob=0.7,
  71. interpolation=Inter.BILINEAR),
  72. py_vision.Resize(1450), # resize to a smaller size to prevent round-off error
  73. py_vision.ToTensor()
  74. ]
  75. transform = py_vision.ComposeOp(transforms)
  76. # Generate dataset
  77. data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  78. data = data.map(input_columns=["image"], operations=transform())
  79. # check results with md5 comparison
  80. filename = "random_perspective_01_result.npz"
  81. save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN)
  82. # Restore configuration
  83. ds.config.set_seed(original_seed)
  84. ds.config.set_num_parallel_workers((original_num_parallel_workers))
  85. def test_random_perspective_exception_distortion_scale_range():
  86. """
  87. Test RandomPerspective: distortion_scale is not in [0, 1], expected to raise ValueError
  88. """
  89. logger.info("test_random_perspective_exception_distortion_scale_range")
  90. try:
  91. _ = py_vision.RandomPerspective(distortion_scale=1.5)
  92. except ValueError as e:
  93. logger.info("Got an exception in DE: {}".format(str(e)))
  94. assert str(e) == "Input distortion_scale is not within the required interval of (0.0 to 1.0)."
  95. def test_random_perspective_exception_prob_range():
  96. """
  97. Test RandomPerspective: prob is not in [0, 1], expected to raise ValueError
  98. """
  99. logger.info("test_random_perspective_exception_prob_range")
  100. try:
  101. _ = py_vision.RandomPerspective(prob=1.2)
  102. except ValueError as e:
  103. logger.info("Got an exception in DE: {}".format(str(e)))
  104. assert str(e) == "Input prob is not within the required interval of (0.0 to 1.0)."
  105. if __name__ == "__main__":
  106. test_random_perspective_op(plot=True)
  107. skip_test_random_perspective_md5()
  108. test_random_perspective_exception_distortion_scale_range()
  109. test_random_perspective_exception_prob_range()