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_horizontal_flip.py 2.8 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 HorizontalFlip 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_horizontal_flip_pipeline(plot=False):
  27. """
  28. Test HorizontalFlip of c_transforms
  29. """
  30. logger.info("test_horizontal_flip_pipeline")
  31. # First dataset
  32. dataset1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, shuffle=False)
  33. decode_op = c_vision.Decode()
  34. horizontal_flip_op = c_vision.HorizontalFlip()
  35. dataset1 = dataset1.map(operations=decode_op, input_columns=["image"])
  36. dataset1 = dataset1.map(operations=horizontal_flip_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. horizontal_flip_ms = data1["image"]
  46. original = data2["image"]
  47. horizontal_flip_cv = cv2.flip(original, 1)
  48. mse = diff_mse(horizontal_flip_ms, horizontal_flip_cv)
  49. logger.info("horizontal_flip_{}, mse: {}".format(num_iter + 1, mse))
  50. assert mse == 0
  51. num_iter += 1
  52. if plot:
  53. visualize_image(original, horizontal_flip_ms, mse, horizontal_flip_cv)
  54. def test_horizontal_flip_eager():
  55. """
  56. Test HorizontalFlip with eager mode
  57. """
  58. logger.info("test_horizontal_flip_eager")
  59. img = cv2.imread(IMAGE_FILE)
  60. img_ms = c_vision.HorizontalFlip()(img)
  61. img_cv = cv2.flip(img, 1)
  62. mse = diff_mse(img_ms, img_cv)
  63. assert mse == 0
  64. if __name__ == "__main__":
  65. test_horizontal_flip_pipeline(plot=False)
  66. test_horizontal_flip_eager()