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_affine.py 7.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 RandomAffine 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 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
  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_affine_op(plot=False):
  28. """
  29. Test RandomAffine in python transformations
  30. """
  31. logger.info("test_random_affine_op")
  32. # define map operations
  33. transforms1 = [
  34. py_vision.Decode(),
  35. py_vision.RandomAffine(degrees=15, translate=(0.1, 0.1), scale=(0.9, 1.1)),
  36. py_vision.ToTensor()
  37. ]
  38. transform1 = py_vision.ComposeOp(transforms1)
  39. transforms2 = [
  40. py_vision.Decode(),
  41. py_vision.ToTensor()
  42. ]
  43. transform2 = py_vision.ComposeOp(transforms2)
  44. # First dataset
  45. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  46. data1 = data1.map(input_columns=["image"], operations=transform1())
  47. # Second dataset
  48. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  49. data2 = data2.map(input_columns=["image"], operations=transform2())
  50. image_affine = []
  51. image_original = []
  52. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  53. image1 = (item1["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  54. image2 = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  55. image_affine.append(image1)
  56. image_original.append(image2)
  57. if plot:
  58. visualize_list(image_original, image_affine)
  59. def test_random_affine_md5():
  60. """
  61. Test RandomAffine with md5 comparison
  62. """
  63. logger.info("test_random_affine_md5")
  64. original_seed = config_get_set_seed(55)
  65. original_num_parallel_workers = config_get_set_num_parallel_workers(1)
  66. # define map operations
  67. transforms = [
  68. py_vision.Decode(),
  69. py_vision.RandomAffine(degrees=(-5, 15), translate=(0.1, 0.3),
  70. scale=(0.9, 1.1), shear=(-10, 10, -5, 5)),
  71. py_vision.ToTensor()
  72. ]
  73. transform = py_vision.ComposeOp(transforms)
  74. # Generate dataset
  75. data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  76. data = data.map(input_columns=["image"], operations=transform())
  77. # check results with md5 comparison
  78. filename = "random_affine_01_result.npz"
  79. save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN)
  80. # Restore configuration
  81. ds.config.set_seed(original_seed)
  82. ds.config.set_num_parallel_workers((original_num_parallel_workers))
  83. def test_random_affine_exception_negative_degrees():
  84. """
  85. Test RandomAffine: input degrees in negative, expected to raise ValueError
  86. """
  87. logger.info("test_random_affine_exception_negative_degrees")
  88. try:
  89. _ = py_vision.RandomAffine(degrees=-15)
  90. except ValueError as e:
  91. logger.info("Got an exception in DE: {}".format(str(e)))
  92. assert str(e) == "Input degrees is not within the required interval of (0 to inf)."
  93. def test_random_affine_exception_translation_range():
  94. """
  95. Test RandomAffine: translation value is not in [0, 1], expected to raise ValueError
  96. """
  97. logger.info("test_random_affine_exception_translation_range")
  98. try:
  99. _ = py_vision.RandomAffine(degrees=15, translate=(0.1, 1.5))
  100. except ValueError as e:
  101. logger.info("Got an exception in DE: {}".format(str(e)))
  102. assert str(e) == "Input translate at 1 is not within the required interval of (0.0 to 1.0)."
  103. def test_random_affine_exception_scale_value():
  104. """
  105. Test RandomAffine: scale is not positive, expected to raise ValueError
  106. """
  107. logger.info("test_random_affine_exception_scale_value")
  108. try:
  109. _ = py_vision.RandomAffine(degrees=15, scale=(0.0, 1.1))
  110. except ValueError as e:
  111. logger.info("Got an exception in DE: {}".format(str(e)))
  112. assert str(e) == "Input scale[0] must be greater than 0."
  113. def test_random_affine_exception_shear_value():
  114. """
  115. Test RandomAffine: shear is a number but is not positive, expected to raise ValueError
  116. """
  117. logger.info("test_random_affine_exception_shear_value")
  118. try:
  119. _ = py_vision.RandomAffine(degrees=15, shear=-5)
  120. except ValueError as e:
  121. logger.info("Got an exception in DE: {}".format(str(e)))
  122. assert str(e) == "Input shear must be greater than 0."
  123. def test_random_affine_exception_degrees_size():
  124. """
  125. Test RandomAffine: degrees is a list or tuple and its length is not 2,
  126. expected to raise TypeError
  127. """
  128. logger.info("test_random_affine_exception_degrees_size")
  129. try:
  130. _ = py_vision.RandomAffine(degrees=[15])
  131. except TypeError as e:
  132. logger.info("Got an exception in DE: {}".format(str(e)))
  133. assert str(e) == "If degrees is a sequence, the length must be 2."
  134. def test_random_affine_exception_translate_size():
  135. """
  136. Test RandomAffine: translate is not list or a tuple of length 2,
  137. expected to raise TypeError
  138. """
  139. logger.info("test_random_affine_exception_translate_size")
  140. try:
  141. _ = py_vision.RandomAffine(degrees=15, translate=(0.1))
  142. except TypeError as e:
  143. logger.info("Got an exception in DE: {}".format(str(e)))
  144. assert str(
  145. e) == "Argument translate with value 0.1 is not of type (<class 'list'>," \
  146. " <class 'tuple'>)."
  147. def test_random_affine_exception_scale_size():
  148. """
  149. Test RandomAffine: scale is not a list or tuple of length 2,
  150. expected to raise TypeError
  151. """
  152. logger.info("test_random_affine_exception_scale_size")
  153. try:
  154. _ = py_vision.RandomAffine(degrees=15, scale=(0.5))
  155. except TypeError as e:
  156. logger.info("Got an exception in DE: {}".format(str(e)))
  157. assert str(e) == "Argument scale with value 0.5 is not of type (<class 'tuple'>," \
  158. " <class 'list'>)."
  159. def test_random_affine_exception_shear_size():
  160. """
  161. Test RandomAffine: shear is not a list or tuple of length 2 or 4,
  162. expected to raise TypeError
  163. """
  164. logger.info("test_random_affine_exception_shear_size")
  165. try:
  166. _ = py_vision.RandomAffine(degrees=15, shear=(-5, 5, 10))
  167. except TypeError as e:
  168. logger.info("Got an exception in DE: {}".format(str(e)))
  169. assert str(e) == "shear must be of length 2 or 4."
  170. if __name__ == "__main__":
  171. test_random_affine_op(plot=True)
  172. test_random_affine_md5()
  173. test_random_affine_exception_negative_degrees()
  174. test_random_affine_exception_translation_range()
  175. test_random_affine_exception_scale_value()
  176. test_random_affine_exception_shear_value()
  177. test_random_affine_exception_degrees_size()
  178. test_random_affine_exception_translate_size()
  179. test_random_affine_exception_scale_size()
  180. test_random_affine_exception_shear_size()