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_posterize.py 6.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 RandomPosterize op in DE
  17. """
  18. import mindspore.dataset as ds
  19. import mindspore.dataset.transforms.vision.c_transforms as c_vision
  20. from mindspore import log as logger
  21. from util import visualize_list, save_and_check_md5, \
  22. config_get_set_seed, config_get_set_num_parallel_workers
  23. GENERATE_GOLDEN = False
  24. DATA_DIR = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"]
  25. SCHEMA_DIR = "../data/dataset/test_tf_file_3_images/datasetSchema.json"
  26. def skip_test_random_posterize_op_c(plot=False, run_golden=True):
  27. """
  28. Test RandomPosterize in C transformations
  29. """
  30. logger.info("test_random_posterize_op_c")
  31. original_seed = config_get_set_seed(55)
  32. original_num_parallel_workers = config_get_set_num_parallel_workers(1)
  33. # define map operations
  34. transforms1 = [
  35. c_vision.Decode(),
  36. c_vision.RandomPosterize((1, 8))
  37. ]
  38. # First dataset
  39. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  40. data1 = data1.map(input_columns=["image"], operations=transforms1)
  41. # Second dataset
  42. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  43. data2 = data2.map(input_columns=["image"], operations=[c_vision.Decode()])
  44. image_posterize = []
  45. image_original = []
  46. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  47. image1 = item1["image"]
  48. image2 = item2["image"]
  49. image_posterize.append(image1)
  50. image_original.append(image2)
  51. if run_golden:
  52. # check results with md5 comparison
  53. filename = "random_posterize_01_result_c.npz"
  54. save_and_check_md5(data1, filename, generate_golden=GENERATE_GOLDEN)
  55. if plot:
  56. visualize_list(image_original, image_posterize)
  57. # Restore configuration
  58. ds.config.set_seed(original_seed)
  59. ds.config.set_num_parallel_workers(original_num_parallel_workers)
  60. def skip_test_random_posterize_op_fixed_point_c(plot=False, run_golden=True):
  61. """
  62. Test RandomPosterize in C transformations with fixed point
  63. """
  64. logger.info("test_random_posterize_op_c")
  65. # define map operations
  66. transforms1 = [
  67. c_vision.Decode(),
  68. c_vision.RandomPosterize(1)
  69. ]
  70. # First dataset
  71. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  72. data1 = data1.map(input_columns=["image"], operations=transforms1)
  73. # Second dataset
  74. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  75. data2 = data2.map(input_columns=["image"], operations=[c_vision.Decode()])
  76. image_posterize = []
  77. image_original = []
  78. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  79. image1 = item1["image"]
  80. image2 = item2["image"]
  81. image_posterize.append(image1)
  82. image_original.append(image2)
  83. if run_golden:
  84. # check results with md5 comparison
  85. filename = "random_posterize_fixed_point_01_result_c.npz"
  86. save_and_check_md5(data1, filename, generate_golden=GENERATE_GOLDEN)
  87. if plot:
  88. visualize_list(image_original, image_posterize)
  89. def test_random_posterize_exception_bit():
  90. """
  91. Test RandomPosterize: out of range input bits and invalid type
  92. """
  93. logger.info("test_random_posterize_exception_bit")
  94. # Test max > 8
  95. try:
  96. _ = c_vision.RandomPosterize((1, 9))
  97. except ValueError as e:
  98. logger.info("Got an exception in DE: {}".format(str(e)))
  99. assert str(e) == "Input is not within the required interval of (1 to 8)."
  100. # Test min < 1
  101. try:
  102. _ = c_vision.RandomPosterize((0, 7))
  103. except ValueError as e:
  104. logger.info("Got an exception in DE: {}".format(str(e)))
  105. assert str(e) == "Input is not within the required interval of (1 to 8)."
  106. # Test max < min
  107. try:
  108. _ = c_vision.RandomPosterize((8, 1))
  109. except ValueError as e:
  110. logger.info("Got an exception in DE: {}".format(str(e)))
  111. assert str(e) == "Input is not within the required interval of (1 to 8)."
  112. # Test wrong type (not uint8)
  113. try:
  114. _ = c_vision.RandomPosterize(1.1)
  115. except TypeError as e:
  116. logger.info("Got an exception in DE: {}".format(str(e)))
  117. assert str(e) == "Argument bits with value 1.1 is not of type (<class 'list'>, <class 'tuple'>, <class 'int'>)."
  118. # Test wrong number of bits
  119. try:
  120. _ = c_vision.RandomPosterize((1, 1, 1))
  121. except TypeError as e:
  122. logger.info("Got an exception in DE: {}".format(str(e)))
  123. assert str(e) == "Size of bits should be a single integer or a list/tuple (min, max) of length 2."
  124. def test_rescale_with_random_posterize():
  125. """
  126. Test RandomPosterize: only support CV_8S/CV_8U
  127. """
  128. logger.info("test_rescale_with_random_posterize")
  129. DATA_DIR_10 = "../data/dataset/testCifar10Data"
  130. dataset = ds.Cifar10Dataset(DATA_DIR_10)
  131. rescale_op = c_vision.Rescale((1.0 / 255.0), 0.0)
  132. dataset = dataset.map(input_columns=["image"], operations=rescale_op)
  133. random_posterize_op = c_vision.RandomPosterize((4, 8))
  134. dataset = dataset.map(input_columns=["image"], operations=random_posterize_op, num_parallel_workers=1)
  135. try:
  136. _ = dataset.output_shapes()
  137. except RuntimeError as e:
  138. logger.info("Got an exception in DE: {}".format(str(e)))
  139. assert "Input image data type can not be float" in str(e)
  140. if __name__ == "__main__":
  141. skip_test_random_posterize_op_c(plot=True)
  142. skip_test_random_posterize_op_fixed_point_c(plot=True)
  143. test_random_posterize_exception_bit()
  144. test_rescale_with_random_posterize()