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_gaussian_blur.py 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 GaussianBlur Python API
  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_gaussian_blur_pipeline(plot=False):
  27. """
  28. Test GaussianBlur of c_transforms
  29. """
  30. logger.info("test_gaussian_blur_pipeline")
  31. # First dataset
  32. dataset1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, shuffle=False)
  33. decode_op = c_vision.Decode()
  34. gaussian_blur_op = c_vision.GaussianBlur(3, 3)
  35. dataset1 = dataset1.map(operations=decode_op, input_columns=["image"])
  36. dataset1 = dataset1.map(operations=gaussian_blur_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. gaussian_blur_ms = data1["image"]
  46. original = data2["image"]
  47. gaussian_blur_cv = cv2.GaussianBlur(original, (3, 3), 3)
  48. mse = diff_mse(gaussian_blur_ms, gaussian_blur_cv)
  49. logger.info("gaussian_blur_{}, mse: {}".format(num_iter + 1, mse))
  50. assert mse == 0
  51. num_iter += 1
  52. if plot:
  53. visualize_image(original, gaussian_blur_ms, mse, gaussian_blur_cv)
  54. def test_gaussian_blur_eager():
  55. """
  56. Test GaussianBlur with eager mode
  57. """
  58. logger.info("test_gaussian_blur_eager")
  59. img = cv2.imread(IMAGE_FILE)
  60. img_ms = c_vision.GaussianBlur((3, 5), (3.5, 3.5))(img)
  61. img_cv = cv2.GaussianBlur(img, (3, 5), 3.5, 3.5)
  62. mse = diff_mse(img_ms, img_cv)
  63. assert mse == 0
  64. def test_gaussian_blur_exception():
  65. """
  66. Test GaussianBlur with invalid parameters
  67. """
  68. logger.info("test_gaussian_blur_exception")
  69. try:
  70. _ = c_vision.GaussianBlur([2, 2])
  71. except ValueError as e:
  72. logger.info("Got an exception in GaussianBlur: {}".format(str(e)))
  73. assert "not an odd value" in str(e)
  74. try:
  75. _ = c_vision.GaussianBlur(3.0, [3, 3])
  76. except TypeError as e:
  77. logger.info("Got an exception in GaussianBlur: {}".format(str(e)))
  78. assert "not of type [<class 'int'>, <class 'list'>, <class 'tuple'>]" in str(e)
  79. try:
  80. _ = c_vision.GaussianBlur(3, -3)
  81. except ValueError as e:
  82. logger.info("Got an exception in GaussianBlur: {}".format(str(e)))
  83. assert "not within the required interval" in str(e)
  84. try:
  85. _ = c_vision.GaussianBlur(3, [3, 3, 3])
  86. except TypeError as e:
  87. logger.info("Got an exception in GaussianBlur: {}".format(str(e)))
  88. assert "should be a single number or a list/tuple of length 2" in str(e)
  89. if __name__ == "__main__":
  90. test_gaussian_blur_pipeline(plot=False)
  91. test_gaussian_blur_eager()
  92. test_gaussian_blur_exception()