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_rotate.py 4.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # Copyright 2021 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 Rotate Python API
  17. """
  18. import cv2
  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 mindspore.dataset.vision.utils import Inter
  23. from util import visualize_image, diff_mse
  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. IMAGE_FILE = "../data/dataset/apple.jpg"
  27. def test_rotate_pipeline_with_expanding(plot=False):
  28. """
  29. Test Rotate of c_transforms with expanding
  30. """
  31. logger.info("test_rotate_pipeline_with_expanding")
  32. # First dataset
  33. dataset1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, shuffle=False)
  34. decode_op = c_vision.Decode()
  35. rotate_op = c_vision.Rotate(90, expand=True)
  36. dataset1 = dataset1.map(operations=decode_op, input_columns=["image"])
  37. dataset1 = dataset1.map(operations=rotate_op, input_columns=["image"])
  38. # Second dataset
  39. dataset2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  40. dataset2 = dataset2.map(operations=decode_op, input_columns=["image"])
  41. num_iter = 0
  42. for data1, data2 in zip(dataset1.create_dict_iterator(num_epochs=1, output_numpy=True),
  43. dataset2.create_dict_iterator(num_epochs=1, output_numpy=True)):
  44. if num_iter > 0:
  45. break
  46. rotate_ms = data1["image"]
  47. original = data2["image"]
  48. rotate_cv = cv2.rotate(original, cv2.ROTATE_90_COUNTERCLOCKWISE)
  49. mse = diff_mse(rotate_ms, rotate_cv)
  50. logger.info("rotate_{}, mse: {}".format(num_iter + 1, mse))
  51. assert mse == 0
  52. num_iter += 1
  53. if plot:
  54. visualize_image(original, rotate_ms, mse, rotate_cv)
  55. def test_rotate_pipeline_without_expanding():
  56. """
  57. Test Rotate of c_transforms without expanding
  58. """
  59. logger.info("test_rotate_pipeline_without_expanding")
  60. # Create a Dataset then decode and rotate the image
  61. dataset = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, shuffle=False)
  62. decode_op = c_vision.Decode()
  63. resize_op = c_vision.Resize((64, 128))
  64. rotate_op = c_vision.Rotate(30)
  65. dataset = dataset.map(operations=decode_op, input_columns=["image"])
  66. dataset = dataset.map(operations=resize_op, input_columns=["image"])
  67. dataset = dataset.map(operations=rotate_op, input_columns=["image"])
  68. for data in dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
  69. rotate_img = data["image"]
  70. assert rotate_img.shape == (64, 128, 3)
  71. def test_rotate_eager():
  72. """
  73. Test Rotate with eager mode
  74. """
  75. logger.info("test_rotate_eager")
  76. img = cv2.imread(IMAGE_FILE)
  77. resize_img = c_vision.Resize((32, 64))(img)
  78. rotate_img = c_vision.Rotate(-90, expand=True)(resize_img)
  79. assert rotate_img.shape == (64, 32, 3)
  80. def test_rotate_exception():
  81. """
  82. Test Rotate with invalid parameters
  83. """
  84. logger.info("test_rotate_exception")
  85. try:
  86. _ = c_vision.Rotate("60")
  87. except TypeError as e:
  88. logger.info("Got an exception in Rotate: {}".format(str(e)))
  89. assert "not of type [<class 'float'>, <class 'int'>]" in str(e)
  90. try:
  91. _ = c_vision.Rotate(30, Inter.BICUBIC, False, (0, 0, 0))
  92. except ValueError as e:
  93. logger.info("Got an exception in Rotate: {}".format(str(e)))
  94. assert "Value center needs to be a 2-tuple." in str(e)
  95. try:
  96. _ = c_vision.Rotate(-120, Inter.NEAREST, False, (-1, -1), (255, 255))
  97. except TypeError as e:
  98. logger.info("Got an exception in Rotate: {}".format(str(e)))
  99. assert "fill_value should be a single integer or a 3-tuple." in str(e)
  100. if __name__ == "__main__":
  101. test_rotate_pipeline_with_expanding(False)
  102. test_rotate_pipeline_without_expanding()
  103. test_rotate_eager()
  104. test_rotate_exception()