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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 numpy as np
  19. import mindspore.dataset as ds
  20. import mindspore.dataset.vision.c_transforms as c_vision
  21. from mindspore import log as logger
  22. from util import visualize_list, save_and_check_md5, \
  23. config_get_set_seed, config_get_set_num_parallel_workers, diff_mse
  24. GENERATE_GOLDEN = False
  25. DATA_DIR = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"]
  26. SCHEMA_DIR = "../data/dataset/test_tf_file_3_images/datasetSchema.json"
  27. def test_random_posterize_op_c(plot=False, run_golden=False):
  28. """
  29. Test RandomPosterize in C transformations (uses assertion on mse as using md5 could have jpeg decoding
  30. inconsistencies)
  31. """
  32. logger.info("test_random_posterize_op_c")
  33. original_seed = config_get_set_seed(55)
  34. original_num_parallel_workers = config_get_set_num_parallel_workers(1)
  35. # define map operations
  36. transforms1 = [
  37. c_vision.Decode(),
  38. c_vision.RandomPosterize((1, 8))
  39. ]
  40. # First dataset
  41. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  42. data1 = data1.map(operations=transforms1, input_columns=["image"])
  43. # Second dataset
  44. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  45. data2 = data2.map(operations=[c_vision.Decode()], input_columns=["image"])
  46. image_posterize = []
  47. image_original = []
  48. for item1, item2 in zip(data1.create_dict_iterator(num_epochs=1, output_numpy=True),
  49. data2.create_dict_iterator(num_epochs=1, output_numpy=True)):
  50. image1 = item1["image"]
  51. image2 = item2["image"]
  52. image_posterize.append(image1)
  53. image_original.append(image2)
  54. # check mse as md5 can be inconsistent.
  55. # mse = 2.9668956 is calculated from
  56. # a thousand runs of diff_mse(np.array(image_original), np.array(image_posterize)) that all produced the same mse.
  57. # allow for an error of 0.0000005
  58. assert abs(2.9668956 - diff_mse(np.array(image_original), np.array(image_posterize))) <= 0.0000005
  59. if run_golden:
  60. # check results with md5 comparison
  61. filename = "random_posterize_01_result_c.npz"
  62. save_and_check_md5(data1, filename, generate_golden=GENERATE_GOLDEN)
  63. if plot:
  64. visualize_list(image_original, image_posterize)
  65. # Restore configuration
  66. ds.config.set_seed(original_seed)
  67. ds.config.set_num_parallel_workers(original_num_parallel_workers)
  68. def test_random_posterize_op_fixed_point_c(plot=False, run_golden=True):
  69. """
  70. Test RandomPosterize in C transformations with fixed point
  71. """
  72. logger.info("test_random_posterize_op_c")
  73. original_seed = config_get_set_seed(55)
  74. original_num_parallel_workers = config_get_set_num_parallel_workers(1)
  75. # define map operations
  76. transforms1 = [
  77. c_vision.Decode(),
  78. c_vision.RandomPosterize(1)
  79. ]
  80. # First dataset
  81. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  82. data1 = data1.map(operations=transforms1, input_columns=["image"])
  83. # Second dataset
  84. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  85. data2 = data2.map(operations=[c_vision.Decode()], input_columns=["image"])
  86. image_posterize = []
  87. image_original = []
  88. for item1, item2 in zip(data1.create_dict_iterator(num_epochs=1, output_numpy=True),
  89. data2.create_dict_iterator(num_epochs=1, output_numpy=True)):
  90. image1 = item1["image"]
  91. image2 = item2["image"]
  92. image_posterize.append(image1)
  93. image_original.append(image2)
  94. if run_golden:
  95. # check results with md5 comparison
  96. filename = "random_posterize_fixed_point_01_result_c.npz"
  97. save_and_check_md5(data1, filename, generate_golden=GENERATE_GOLDEN)
  98. if plot:
  99. visualize_list(image_original, image_posterize)
  100. # Restore configuration
  101. ds.config.set_seed(original_seed)
  102. ds.config.set_num_parallel_workers(original_num_parallel_workers)
  103. def test_random_posterize_default_c_md5(plot=False, run_golden=True):
  104. """
  105. Test RandomPosterize C Op (default params) with md5 comparison
  106. """
  107. logger.info("test_random_posterize_default_c_md5")
  108. original_seed = config_get_set_seed(5)
  109. original_num_parallel_workers = config_get_set_num_parallel_workers(1)
  110. # define map operations
  111. transforms1 = [
  112. c_vision.Decode(),
  113. c_vision.RandomPosterize()
  114. ]
  115. # First dataset
  116. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  117. data1 = data1.map(operations=transforms1, input_columns=["image"])
  118. # Second dataset
  119. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  120. data2 = data2.map(operations=[c_vision.Decode()], input_columns=["image"])
  121. image_posterize = []
  122. image_original = []
  123. for item1, item2 in zip(data1.create_dict_iterator(output_numpy=True, num_epochs=1),
  124. data2.create_dict_iterator(output_numpy=True, num_epochs=1)):
  125. image1 = item1["image"]
  126. image2 = item2["image"]
  127. image_posterize.append(image1)
  128. image_original.append(image2)
  129. if run_golden:
  130. # check results with md5 comparison
  131. filename = "random_posterize_01_default_result_c.npz"
  132. save_and_check_md5(data1, filename, generate_golden=GENERATE_GOLDEN)
  133. if plot:
  134. visualize_list(image_original, image_posterize)
  135. # Restore configuration
  136. ds.config.set_seed(original_seed)
  137. ds.config.set_num_parallel_workers(original_num_parallel_workers)
  138. def test_random_posterize_exception_bit():
  139. """
  140. Test RandomPosterize: out of range input bits and invalid type
  141. """
  142. logger.info("test_random_posterize_exception_bit")
  143. # Test max > 8
  144. try:
  145. _ = c_vision.RandomPosterize((1, 9))
  146. except ValueError as e:
  147. logger.info("Got an exception in DE: {}".format(str(e)))
  148. assert str(e) == "Input is not within the required interval of [1, 8]."
  149. # Test min < 1
  150. try:
  151. _ = c_vision.RandomPosterize((0, 7))
  152. except ValueError as e:
  153. logger.info("Got an exception in DE: {}".format(str(e)))
  154. assert str(e) == "Input is not within the required interval of [1, 8]."
  155. # Test max < min
  156. try:
  157. _ = c_vision.RandomPosterize((8, 1))
  158. except ValueError as e:
  159. logger.info("Got an exception in DE: {}".format(str(e)))
  160. assert str(e) == "Input is not within the required interval of [1, 8]."
  161. # Test wrong type (not uint8)
  162. try:
  163. _ = c_vision.RandomPosterize(1.1)
  164. except TypeError as e:
  165. logger.info("Got an exception in DE: {}".format(str(e)))
  166. assert str(e) == ("Argument bits with value 1.1 is not of type [<class 'list'>, <class 'tuple'>, "
  167. "<class 'int'>], but got <class 'float'>.")
  168. # Test wrong number of bits
  169. try:
  170. _ = c_vision.RandomPosterize((1, 1, 1))
  171. except TypeError as e:
  172. logger.info("Got an exception in DE: {}".format(str(e)))
  173. assert str(e) == "Size of bits should be a single integer or a list/tuple (min, max) of length 2."
  174. def test_rescale_with_random_posterize():
  175. """
  176. Test RandomPosterize: only support CV_8S/CV_8U
  177. """
  178. logger.info("test_rescale_with_random_posterize")
  179. DATA_DIR_10 = "../data/dataset/testCifar10Data"
  180. dataset = ds.Cifar10Dataset(DATA_DIR_10)
  181. rescale_op = c_vision.Rescale((1.0 / 255.0), 0.0)
  182. dataset = dataset.map(operations=rescale_op, input_columns=["image"])
  183. random_posterize_op = c_vision.RandomPosterize((4, 8))
  184. dataset = dataset.map(operations=random_posterize_op, input_columns=["image"], num_parallel_workers=1)
  185. try:
  186. _ = dataset.output_shapes()
  187. except RuntimeError as e:
  188. logger.info("Got an exception in DE: {}".format(str(e)))
  189. assert "data type of input image should be int" in str(e)
  190. if __name__ == "__main__":
  191. test_random_posterize_op_c(plot=False, run_golden=False)
  192. test_random_posterize_op_fixed_point_c(plot=False)
  193. test_random_posterize_default_c_md5(plot=False)
  194. test_random_posterize_exception_bit()
  195. test_rescale_with_random_posterize()