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_random_vertical_flip.py 3.2 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. """
  16. Testing the random vertical flip op in DE
  17. """
  18. import matplotlib.pyplot as plt
  19. import numpy as np
  20. import mindspore.dataset as ds
  21. import mindspore.dataset.transforms.vision.c_transforms as vision
  22. from mindspore import log as logger
  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 v_flip(image):
  26. """
  27. Apply the random_vertical
  28. """
  29. # with the seed provided in this test case, it will always flip.
  30. # that's why we flip here too
  31. image = image[::-1, :, :]
  32. return image
  33. def visualize(image_de_random_vertical, image_pil_random_vertical, mse, image_original):
  34. """
  35. visualizes the image using DE op and Numpy op
  36. """
  37. plt.subplot(141)
  38. plt.imshow(image_original)
  39. plt.title("Original image")
  40. plt.subplot(142)
  41. plt.imshow(image_de_random_vertical)
  42. plt.title("DE random_vertical image")
  43. plt.subplot(143)
  44. plt.imshow(image_pil_random_vertical)
  45. plt.title("vertically flipped image")
  46. plt.subplot(144)
  47. plt.imshow(image_de_random_vertical - image_pil_random_vertical)
  48. plt.title("Difference image, mse : {}".format(mse))
  49. plt.show()
  50. def test_random_vertical_op():
  51. """
  52. Test random_vertical
  53. """
  54. logger.info("Test random_vertical")
  55. # First dataset
  56. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  57. decode_op = vision.Decode()
  58. random_vertical_op = vision.RandomVerticalFlip()
  59. data1 = data1.map(input_columns=["image"], operations=decode_op)
  60. data1 = data1.map(input_columns=["image"], operations=random_vertical_op)
  61. # Second dataset
  62. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  63. data2 = data2.map(input_columns=["image"], operations=decode_op)
  64. num_iter = 0
  65. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  66. # with the seed value, we can only guarantee the first number generated
  67. if num_iter > 0:
  68. break
  69. image_v_flipped = item1["image"]
  70. image = item2["image"]
  71. image_v_flipped_2 = v_flip(image)
  72. diff = image_v_flipped - image_v_flipped_2
  73. mse = np.sum(np.power(diff, 2))
  74. logger.info("image_{}, mse: {}".format(num_iter + 1, mse))
  75. # Uncomment below line if you want to visualize images
  76. # visualize(image_v_flipped, image_v_flipped_2, mse, image)
  77. num_iter += 1
  78. if __name__ == "__main__":
  79. test_random_vertical_op()