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_rotation.py 4.7 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # Copyright 2019 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 RandomRotation op in DE
  17. """
  18. import numpy as np
  19. import cv2
  20. import mindspore.dataset as ds
  21. import mindspore.dataset.transforms.vision.c_transforms as c_vision
  22. import mindspore.dataset.transforms.vision.py_transforms as py_vision
  23. from mindspore import log as logger
  24. from util import visualize_image, diff_mse
  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_rotation_op(plot=False):
  28. """
  29. Test RandomRotation op
  30. """
  31. logger.info("test_random_rotation_op")
  32. # First dataset
  33. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, shuffle=False)
  34. decode_op = c_vision.Decode()
  35. # use [90, 90] to force rotate 90 degrees, expand is set to be True to match output size
  36. random_rotation_op = c_vision.RandomRotation((90, 90), expand=True)
  37. data1 = data1.map(input_columns=["image"], operations=decode_op)
  38. data1 = data1.map(input_columns=["image"], operations=random_rotation_op)
  39. # Second dataset
  40. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  41. data2 = data2.map(input_columns=["image"], operations=decode_op)
  42. num_iter = 0
  43. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  44. if num_iter > 0:
  45. break
  46. rotation_de = item1["image"]
  47. original = item2["image"]
  48. logger.info("shape before rotate: {}".format(original.shape))
  49. rotation_cv = cv2.rotate(original, cv2.ROTATE_90_COUNTERCLOCKWISE)
  50. mse = diff_mse(rotation_de, rotation_cv)
  51. logger.info("random_rotation_op_{}, mse: {}".format(num_iter + 1, mse))
  52. assert mse == 0
  53. num_iter += 1
  54. if plot:
  55. visualize_image(original, rotation_de, mse, rotation_cv)
  56. def test_random_rotation_expand():
  57. """
  58. Test RandomRotation op
  59. """
  60. logger.info("test_random_rotation_op")
  61. # First dataset
  62. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  63. decode_op = c_vision.Decode()
  64. # use [90, 90] to force rotate 90 degrees, expand is set to be True to match output size
  65. random_rotation_op = c_vision.RandomRotation((0, 90), expand=True)
  66. data1 = data1.map(input_columns=["image"], operations=decode_op)
  67. data1 = data1.map(input_columns=["image"], operations=random_rotation_op)
  68. num_iter = 0
  69. for item in data1.create_dict_iterator():
  70. rotation = item["image"]
  71. logger.info("shape after rotate: {}".format(rotation.shape))
  72. num_iter += 1
  73. def test_rotation_diff():
  74. """
  75. Test Rotation op
  76. """
  77. logger.info("test_random_rotation_op")
  78. # First dataset
  79. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  80. decode_op = c_vision.Decode()
  81. rotation_op = c_vision.RandomRotation((45, 45), expand=True)
  82. ctrans = [decode_op,
  83. rotation_op
  84. ]
  85. data1 = data1.map(input_columns=["image"], operations=ctrans)
  86. # Second dataset
  87. transforms = [
  88. py_vision.Decode(),
  89. py_vision.RandomRotation((45, 45), expand=True),
  90. py_vision.ToTensor(),
  91. ]
  92. transform = py_vision.ComposeOp(transforms)
  93. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  94. data2 = data2.map(input_columns=["image"], operations=transform())
  95. num_iter = 0
  96. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  97. num_iter += 1
  98. c_image = item1["image"]
  99. py_image = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  100. logger.info("shape of c_image: {}".format(c_image.shape))
  101. logger.info("shape of py_image: {}".format(py_image.shape))
  102. logger.info("dtype of c_image: {}".format(c_image.dtype))
  103. logger.info("dtype of py_image: {}".format(py_image.dtype))
  104. if __name__ == "__main__":
  105. test_random_rotation_op(True)
  106. test_random_rotation_expand()
  107. test_rotation_diff()