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.5 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. import numpy as np
  16. import mindspore.dataset as ds
  17. import mindspore.dataset.transforms.vision.c_transforms as vision
  18. import mindspore.dataset.transforms.vision.py_transforms as py_vision
  19. from mindspore import log as logger
  20. from util import diff_mse, visualize, save_and_check_md5
  21. GENERATE_GOLDEN = False
  22. DATA_DIR = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"]
  23. SCHEMA_DIR = "../data/dataset/test_tf_file_3_images/datasetSchema.json"
  24. def test_center_crop_op(height=375, width=375, plot=False):
  25. """
  26. Test CenterCrop
  27. """
  28. logger.info("Test CenterCrop")
  29. # First dataset
  30. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"])
  31. decode_op = vision.Decode()
  32. # 3 images [375, 500] [600, 500] [512, 512]
  33. center_crop_op = vision.CenterCrop([height, width])
  34. data1 = data1.map(input_columns=["image"], operations=decode_op)
  35. data1 = data1.map(input_columns=["image"], operations=center_crop_op)
  36. # Second dataset
  37. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"])
  38. data2 = data2.map(input_columns=["image"], operations=decode_op)
  39. image_cropped = []
  40. image = []
  41. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  42. image_cropped.append(item1["image"].copy())
  43. image.append(item2["image"].copy())
  44. if plot:
  45. visualize(image, image_cropped)
  46. def test_center_crop_md5(height=375, width=375):
  47. """
  48. Test CenterCrop
  49. """
  50. logger.info("Test CenterCrop")
  51. # First dataset
  52. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  53. decode_op = vision.Decode()
  54. # 3 images [375, 500] [600, 500] [512, 512]
  55. center_crop_op = vision.CenterCrop([height, width])
  56. data1 = data1.map(input_columns=["image"], operations=decode_op)
  57. data1 = data1.map(input_columns=["image"], operations=center_crop_op)
  58. # Compare with expected md5 from images
  59. filename = "center_crop_01_result.npz"
  60. save_and_check_md5(data1, filename, generate_golden=GENERATE_GOLDEN)
  61. def test_center_crop_comp(height=375, width=375, plot=False):
  62. """
  63. Test CenterCrop between python and c image augmentation
  64. """
  65. logger.info("Test CenterCrop")
  66. # First dataset
  67. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  68. decode_op = vision.Decode()
  69. center_crop_op = vision.CenterCrop([height, width])
  70. data1 = data1.map(input_columns=["image"], operations=decode_op)
  71. data1 = data1.map(input_columns=["image"], operations=center_crop_op)
  72. # Second dataset
  73. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  74. transforms = [
  75. py_vision.Decode(),
  76. py_vision.CenterCrop([height, width]),
  77. py_vision.ToTensor()
  78. ]
  79. transform = py_vision.ComposeOp(transforms)
  80. data2 = data2.map(input_columns=["image"], operations=transform())
  81. image_cropped = []
  82. image = []
  83. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  84. c_image = item1["image"]
  85. py_image = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  86. # Note: The images aren't exactly the same due to rounding error
  87. assert diff_mse(py_image, c_image) < 0.001
  88. image_cropped.append(c_image.copy())
  89. image.append(py_image.copy())
  90. if plot:
  91. visualize(image, image_cropped)
  92. # pylint: disable=unnecessary-lambda
  93. def test_crop_grayscale(height=375, width=375):
  94. """
  95. Test that centercrop works with pad and grayscale images
  96. """
  97. def channel_swap(image):
  98. """
  99. Py func hack for our pytransforms to work with c transforms
  100. """
  101. return (image.transpose(1, 2, 0) * 255).astype(np.uint8)
  102. transforms = [
  103. py_vision.Decode(),
  104. py_vision.Grayscale(1),
  105. py_vision.ToTensor(),
  106. (lambda image: channel_swap(image))
  107. ]
  108. transform = py_vision.ComposeOp(transforms)
  109. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  110. data1 = data1.map(input_columns=["image"], operations=transform())
  111. # If input is grayscale, the output dimensions should be single channel
  112. crop_gray = vision.CenterCrop([height, width])
  113. data1 = data1.map(input_columns=["image"], operations=crop_gray)
  114. for item1 in data1.create_dict_iterator():
  115. c_image = item1["image"]
  116. # Check that the image is grayscale
  117. assert (c_image.ndim == 3 and c_image.shape[2] == 1)
  118. if __name__ == "__main__":
  119. test_center_crop_op(600, 600, True)
  120. test_center_crop_op(300, 600)
  121. test_center_crop_op(600, 300)
  122. test_center_crop_md5()
  123. test_center_crop_comp(True)
  124. test_crop_grayscale()