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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 cv2
  19. import matplotlib.pyplot as plt
  20. import mindspore.dataset.transforms.vision.c_transforms as c_vision
  21. import numpy as np
  22. import mindspore.dataset as ds
  23. import mindspore.dataset.transforms.vision.py_transforms as py_vision
  24. from mindspore import log as logger
  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 visualize(a, mse, original):
  28. """
  29. visualizes the image using DE op and enCV
  30. """
  31. plt.subplot(141)
  32. plt.imshow(original)
  33. plt.title("Original image")
  34. plt.subplot(142)
  35. plt.imshow(a)
  36. plt.title("DE random_crop_and_resize image")
  37. plt.subplot(143)
  38. plt.imshow(a - original)
  39. plt.title("Difference image, mse : {}".format(mse))
  40. plt.show()
  41. def diff_mse(in1, in2):
  42. mse = (np.square(in1.astype(float) / 255 - in2.astype(float) / 255)).mean()
  43. return mse * 100
  44. def test_random_rotation_op():
  45. """
  46. Test RandomRotation op
  47. """
  48. logger.info("test_random_rotation_op")
  49. # First dataset
  50. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, shuffle=False)
  51. decode_op = c_vision.Decode()
  52. # use [90, 90] to force rotate 90 degrees, expand is set to be True to match output size
  53. random_rotation_op = c_vision.RandomRotation((90, 90), expand=True)
  54. data1 = data1.map(input_columns=["image"], operations=decode_op)
  55. data1 = data1.map(input_columns=["image"], operations=random_rotation_op)
  56. # Second dataset
  57. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  58. data2 = data2.map(input_columns=["image"], operations=decode_op)
  59. num_iter = 0
  60. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  61. if num_iter > 0:
  62. break
  63. rotation = item1["image"]
  64. original = item2["image"]
  65. logger.info("shape before rotate: {}".format(original.shape))
  66. original = cv2.rotate(original, cv2.ROTATE_90_COUNTERCLOCKWISE)
  67. diff = rotation - original
  68. mse = np.sum(np.power(diff, 2))
  69. logger.info("random_rotation_op_{}, mse: {}".format(num_iter + 1, mse))
  70. assert mse == 0
  71. # Uncomment below line if you want to visualize images
  72. # visualize(rotation, mse, original)
  73. num_iter += 1
  74. def test_random_rotation_expand():
  75. """
  76. Test RandomRotation op
  77. """
  78. logger.info("test_random_rotation_op")
  79. # First dataset
  80. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  81. decode_op = c_vision.Decode()
  82. # use [90, 90] to force rotate 90 degrees, expand is set to be True to match output size
  83. random_rotation_op = c_vision.RandomRotation((0, 90), expand=True)
  84. data1 = data1.map(input_columns=["image"], operations=decode_op)
  85. data1 = data1.map(input_columns=["image"], operations=random_rotation_op)
  86. num_iter = 0
  87. for item in data1.create_dict_iterator():
  88. rotation = item["image"]
  89. logger.info("shape after rotate: {}".format(rotation.shape))
  90. num_iter += 1
  91. def test_rotation_diff():
  92. """
  93. Test Rotation op
  94. """
  95. logger.info("test_random_rotation_op")
  96. # First dataset
  97. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  98. decode_op = c_vision.Decode()
  99. rotation_op = c_vision.RandomRotation((45, 45), expand=True)
  100. ctrans = [decode_op,
  101. rotation_op
  102. ]
  103. data1 = data1.map(input_columns=["image"], operations=ctrans)
  104. # Second dataset
  105. transforms = [
  106. py_vision.Decode(),
  107. py_vision.RandomRotation((45, 45), expand=True),
  108. py_vision.ToTensor(),
  109. ]
  110. transform = py_vision.ComposeOp(transforms)
  111. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  112. data2 = data2.map(input_columns=["image"], operations=transform())
  113. num_iter = 0
  114. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  115. num_iter += 1
  116. c_image = item1["image"]
  117. py_image = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  118. logger.info("shape of c_image: {}".format(c_image.shape))
  119. logger.info("shape of py_image: {}".format(py_image.shape))
  120. logger.info("dtype of c_image: {}".format(c_image.dtype))
  121. logger.info("dtype of py_image: {}".format(py_image.dtype))
  122. if __name__ == "__main__":
  123. test_random_rotation_op()
  124. test_random_rotation_expand()
  125. test_rotation_diff()