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_center_crop.py 5.8 kB

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 CenterCrop op in DE
  17. """
  18. import numpy as np
  19. import mindspore.dataset as ds
  20. import mindspore.dataset.transforms.vision.c_transforms as vision
  21. import mindspore.dataset.transforms.vision.py_transforms as py_vision
  22. from mindspore import log as logger
  23. from util import diff_mse, visualize_list, save_and_check_md5
  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_center_crop_op(height=375, width=375, plot=False):
  28. """
  29. Test CenterCrop
  30. """
  31. logger.info("Test CenterCrop")
  32. # First dataset
  33. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"])
  34. decode_op = vision.Decode()
  35. # 3 images [375, 500] [600, 500] [512, 512]
  36. center_crop_op = vision.CenterCrop([height, width])
  37. data1 = data1.map(input_columns=["image"], operations=decode_op)
  38. data1 = data1.map(input_columns=["image"], operations=center_crop_op)
  39. # Second dataset
  40. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"])
  41. data2 = data2.map(input_columns=["image"], operations=decode_op)
  42. image_cropped = []
  43. image = []
  44. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  45. image_cropped.append(item1["image"].copy())
  46. image.append(item2["image"].copy())
  47. if plot:
  48. visualize_list(image, image_cropped)
  49. def test_center_crop_md5(height=375, width=375):
  50. """
  51. Test CenterCrop
  52. """
  53. logger.info("Test CenterCrop")
  54. # First dataset
  55. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  56. decode_op = vision.Decode()
  57. # 3 images [375, 500] [600, 500] [512, 512]
  58. center_crop_op = vision.CenterCrop([height, width])
  59. data1 = data1.map(input_columns=["image"], operations=decode_op)
  60. data1 = data1.map(input_columns=["image"], operations=center_crop_op)
  61. # Compare with expected md5 from images
  62. filename = "center_crop_01_result.npz"
  63. save_and_check_md5(data1, filename, generate_golden=GENERATE_GOLDEN)
  64. def test_center_crop_comp(height=375, width=375, plot=False):
  65. """
  66. Test CenterCrop between python and c image augmentation
  67. """
  68. logger.info("Test CenterCrop")
  69. # First dataset
  70. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  71. decode_op = vision.Decode()
  72. center_crop_op = vision.CenterCrop([height, width])
  73. data1 = data1.map(input_columns=["image"], operations=decode_op)
  74. data1 = data1.map(input_columns=["image"], operations=center_crop_op)
  75. # Second dataset
  76. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  77. transforms = [
  78. py_vision.Decode(),
  79. py_vision.CenterCrop([height, width]),
  80. py_vision.ToTensor()
  81. ]
  82. transform = py_vision.ComposeOp(transforms)
  83. data2 = data2.map(input_columns=["image"], operations=transform())
  84. image_c_cropped = []
  85. image_py_cropped = []
  86. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  87. c_image = item1["image"]
  88. py_image = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  89. # Note: The images aren't exactly the same due to rounding error
  90. assert diff_mse(py_image, c_image) < 0.001
  91. image_c_cropped.append(c_image.copy())
  92. image_py_cropped.append(py_image.copy())
  93. if plot:
  94. visualize_list(image_c_cropped, image_py_cropped, visualize_mode=2)
  95. def test_crop_grayscale(height=375, width=375):
  96. """
  97. Test that centercrop works with pad and grayscale images
  98. """
  99. # Note: image.transpose performs channel swap to allow py transforms to
  100. # work with c transforms
  101. transforms = [
  102. py_vision.Decode(),
  103. py_vision.Grayscale(1),
  104. py_vision.ToTensor(),
  105. (lambda image: (image.transpose(1, 2, 0) * 255).astype(np.uint8))
  106. ]
  107. transform = py_vision.ComposeOp(transforms)
  108. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  109. data1 = data1.map(input_columns=["image"], operations=transform())
  110. # If input is grayscale, the output dimensions should be single channel
  111. crop_gray = vision.CenterCrop([height, width])
  112. data1 = data1.map(input_columns=["image"], operations=crop_gray)
  113. for item1 in data1.create_dict_iterator():
  114. c_image = item1["image"]
  115. # Check that the image is grayscale
  116. assert (c_image.ndim == 3 and c_image.shape[2] == 1)
  117. def test_center_crop_errors():
  118. """
  119. Test that CenterCropOp errors with bad input
  120. """
  121. try:
  122. test_center_crop_op(16777216, 16777216)
  123. except RuntimeError as e:
  124. assert "Unexpected error. CenterCropOp padding size is too big, it's more than 3 times the original size." in \
  125. str(e)
  126. if __name__ == "__main__":
  127. test_center_crop_op(600, 600, plot=True)
  128. test_center_crop_op(300, 600)
  129. test_center_crop_op(600, 300)
  130. test_center_crop_md5()
  131. test_center_crop_comp(plot=True)
  132. test_crop_grayscale()