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.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.ToTensor()
  73. ]
  74. transform = py_vision.ComposeOp(transforms)
  75. # Generate dataset
  76. data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  77. data = data.map(input_columns=["image"], operations=transform())
  78. # check results with md5 comparison
  79. filename = "random_perspective_01_result.npz"
  80. save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN)
  81. # Restore configuration
  82. ds.config.set_seed(original_seed)
  83. ds.config.set_num_parallel_workers((original_num_parallel_workers))
  84. def test_random_perspective_exception_distortion_scale_range():
  85. """
  86. Test RandomPerspective: distortion_scale is not in [0, 1], expected to raise ValueError
  87. """
  88. logger.info("test_random_perspective_exception_distortion_scale_range")
  89. try:
  90. _ = py_vision.RandomPerspective(distortion_scale=1.5)
  91. except ValueError as e:
  92. logger.info("Got an exception in DE: {}".format(str(e)))
  93. assert str(e) == "Input is not within the required range"
  94. def test_random_perspective_exception_prob_range():
  95. """
  96. Test RandomPerspective: prob is not in [0, 1], expected to raise ValueError
  97. """
  98. logger.info("test_random_perspective_exception_prob_range")
  99. try:
  100. _ = py_vision.RandomPerspective(prob=1.2)
  101. except ValueError as e:
  102. logger.info("Got an exception in DE: {}".format(str(e)))
  103. assert str(e) == "Input is not within the required range"
  104. if __name__ == "__main__":
  105. test_random_perspective_op(plot=True)
  106. skip_test_random_perspective_md5()
  107. test_random_perspective_exception_distortion_scale_range()
  108. test_random_perspective_exception_prob_range()