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

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 mindspore.dataset.transforms.vision.c_transforms as vision
  16. import mindspore.dataset.transforms.vision.py_transforms as py_vision
  17. import numpy as np
  18. import matplotlib.pyplot as plt
  19. import mindspore.dataset as ds
  20. from mindspore import log as logger
  21. from util import diff_mse, visualize, save_and_check_md5
  22. GENERATE_GOLDEN = False
  23. DATA_DIR = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"]
  24. SCHEMA_DIR = "../data/dataset/test_tf_file_3_images/datasetSchema.json"
  25. def test_center_crop_op(height=375, width=375, plot=False):
  26. """
  27. Test CenterCrop
  28. """
  29. logger.info("Test CenterCrop")
  30. # First dataset
  31. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"])
  32. decode_op = vision.Decode()
  33. # 3 images [375, 500] [600, 500] [512, 512]
  34. center_crop_op = vision.CenterCrop([height, width])
  35. data1 = data1.map(input_columns=["image"], operations=decode_op)
  36. data1 = data1.map(input_columns=["image"], operations=center_crop_op)
  37. # Second dataset
  38. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"])
  39. data2 = data2.map(input_columns=["image"], operations=decode_op)
  40. image_cropped = []
  41. image = []
  42. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  43. image_cropped.append(item1["image"].copy())
  44. image.append(item2["image"].copy())
  45. if plot:
  46. visualize(image, image_cropped)
  47. def test_center_crop_md5(height=375, width=375):
  48. """
  49. Test CenterCrop
  50. """
  51. logger.info("Test CenterCrop")
  52. # First dataset
  53. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle =False)
  54. decode_op = vision.Decode()
  55. # 3 images [375, 500] [600, 500] [512, 512]
  56. center_crop_op = vision.CenterCrop([height, width])
  57. data1 = data1.map(input_columns=["image"], operations=decode_op)
  58. data1 = data1.map(input_columns=["image"], operations=center_crop_op)
  59. # expected md5 from images
  60. filename = "test_center_crop_01_result.npz"
  61. save_and_check_md5(data1, filename, generate_golden=GENERATE_GOLDEN)
  62. def test_center_crop_comp(height=375, width=375, plot=False):
  63. """
  64. Test CenterCrop between python and c image augmentation
  65. """
  66. logger.info("Test CenterCrop")
  67. # First dataset
  68. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  69. decode_op = vision.Decode()
  70. center_crop_op = vision.CenterCrop([height, width])
  71. data1 = data1.map(input_columns=["image"], operations=decode_op)
  72. data1 = data1.map(input_columns=["image"], operations=center_crop_op)
  73. # Second dataset
  74. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  75. transforms = [
  76. py_vision.Decode(),
  77. py_vision.CenterCrop([height, width]),
  78. py_vision.ToTensor()
  79. ]
  80. transform = py_vision.ComposeOp(transforms)
  81. data2 = data2.map(input_columns=["image"], operations=transform())
  82. image_cropped = []
  83. image = []
  84. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  85. c_image = item1["image"]
  86. py_image = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  87. # the images aren't exactly the same due to rouding error
  88. assert (diff_mse(py_image, c_image) < 0.001)
  89. image_cropped.append(item1["image"].copy())
  90. image.append(item2["image"].copy())
  91. if plot:
  92. visualize(image, image_cropped)
  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 (len(c_image.shape) == 3 and c_image.shape[2] == 1)
  118. if __name__ == "__main__":
  119. test_center_crop_op(600, 600)
  120. test_center_crop_op(300, 600)
  121. test_center_crop_op(600, 300)
  122. test_center_crop_md5()
  123. test_center_crop_comp()
  124. test_crop_grayscale()