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_crop.py 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # Copyright 2021 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 Crop op in DE
  17. """
  18. import cv2
  19. import mindspore.dataset as ds
  20. import mindspore.dataset.vision.c_transforms as c_vision
  21. from mindspore import log as logger
  22. from util import visualize_image, diff_mse
  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. IMAGE_FILE = "../data/dataset/apple.jpg"
  26. def test_crop_pipeline(plot=False):
  27. """
  28. Test Crop of c_transforms
  29. """
  30. logger.info("test_crop_pipeline")
  31. # First dataset
  32. dataset1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, shuffle=False)
  33. decode_op = c_vision.Decode()
  34. crop_op = c_vision.Crop((0, 0), (20, 25))
  35. dataset1 = dataset1.map(operations=decode_op, input_columns=["image"])
  36. dataset1 = dataset1.map(operations=crop_op, input_columns=["image"])
  37. # Second dataset
  38. dataset2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  39. dataset2 = dataset2.map(operations=decode_op, input_columns=["image"])
  40. num_iter = 0
  41. for data1, data2 in zip(dataset1.create_dict_iterator(num_epochs=1, output_numpy=True),
  42. dataset2.create_dict_iterator(num_epochs=1, output_numpy=True)):
  43. if num_iter > 0:
  44. break
  45. crop_ms = data1["image"]
  46. original = data2["image"]
  47. crop_expect = original[0:20, 0:25]
  48. mse = diff_mse(crop_ms, crop_expect)
  49. logger.info("crop_{}, mse: {}".format(num_iter + 1, mse))
  50. assert mse == 0
  51. num_iter += 1
  52. if plot:
  53. visualize_image(original, crop_ms, mse, crop_expect)
  54. def test_crop_eager():
  55. """
  56. Test Crop with eager mode
  57. """
  58. logger.info("test_crop_eager")
  59. img = cv2.imread(IMAGE_FILE)
  60. img_ms = c_vision.Crop((20, 50), (30, 50))(img)
  61. img_expect = img[20:50, 50:100]
  62. mse = diff_mse(img_ms, img_expect)
  63. assert mse == 0
  64. def test_crop_exception():
  65. """
  66. Test Crop with invalid parameters
  67. """
  68. logger.info("test_crop_exception")
  69. try:
  70. _ = c_vision.Crop([-10, 0], [20])
  71. except ValueError as e:
  72. logger.info("Got an exception in Crop: {}".format(str(e)))
  73. assert "not within the required interval of [0, 2147483647]" in str(e)
  74. try:
  75. _ = c_vision.Crop([0, 5.2], [10, 10])
  76. except TypeError as e:
  77. logger.info("Got an exception in Crop: {}".format(str(e)))
  78. assert "not of type [<class 'int'>]" in str(e)
  79. try:
  80. _ = c_vision.Crop([0], [28])
  81. except TypeError as e:
  82. logger.info("Got an exception in Crop: {}".format(str(e)))
  83. assert "Coordinates should be a list/tuple (y, x) of length 2." in str(e)
  84. try:
  85. _ = c_vision.Crop((0, 0), -1)
  86. except ValueError as e:
  87. logger.info("Got an exception in Crop: {}".format(str(e)))
  88. assert "not within the required interval of [1, 16777216]" in str(e)
  89. try:
  90. _ = c_vision.Crop((0, 0), (10.5, 15))
  91. except TypeError as e:
  92. logger.info("Got an exception in Crop: {}".format(str(e)))
  93. assert "not of type [<class 'int'>]" in str(e)
  94. try:
  95. _ = c_vision.Crop((0, 0), (0, 10, 20))
  96. except TypeError as e:
  97. logger.info("Got an exception in Crop: {}".format(str(e)))
  98. assert "Size should be a single integer or a list/tuple (h, w) of length 2." in str(e)
  99. if __name__ == "__main__":
  100. test_crop_pipeline(plot=False)
  101. test_crop_eager()
  102. test_crop_exception()