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_equalize.py 5.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 RandomEqualize op in DE
  17. """
  18. import numpy as np
  19. import mindspore.dataset as ds
  20. from mindspore.dataset.vision.c_transforms import Decode, Resize, RandomEqualize, Equalize
  21. from mindspore import log as logger
  22. from util import visualize_list, visualize_image, diff_mse
  23. image_file = "../data/dataset/testImageNetData/train/class1/1_1.jpg"
  24. data_dir = "../data/dataset/testImageNetData/train/"
  25. def test_random_equalize_pipeline(plot=False):
  26. """
  27. Test RandomEqualize pipeline
  28. """
  29. logger.info("Test RandomEqualize pipeline")
  30. # Original Images
  31. data_set = ds.ImageFolderDataset(dataset_dir=data_dir, shuffle=False)
  32. transforms_original = [Decode(), Resize(size=[224, 224])]
  33. ds_original = data_set.map(operations=transforms_original, input_columns="image")
  34. ds_original = ds_original.batch(512)
  35. for idx, (image, _) in enumerate(ds_original):
  36. if idx == 0:
  37. images_original = image.asnumpy()
  38. else:
  39. images_original = np.append(images_original,
  40. image.asnumpy(),
  41. axis=0)
  42. # Randomly Equalized Images
  43. data_set1 = ds.ImageFolderDataset(dataset_dir=data_dir, shuffle=False)
  44. transform_random_equalize = [Decode(), Resize(size=[224, 224]), RandomEqualize(0.6)]
  45. ds_random_equalize = data_set1.map(operations=transform_random_equalize, input_columns="image")
  46. ds_random_equalize = ds_random_equalize.batch(512)
  47. for idx, (image, _) in enumerate(ds_random_equalize):
  48. if idx == 0:
  49. images_random_equalize = image.asnumpy()
  50. else:
  51. images_random_equalize = np.append(images_random_equalize,
  52. image.asnumpy(),
  53. axis=0)
  54. if plot:
  55. visualize_list(images_original, images_random_equalize)
  56. num_samples = images_original.shape[0]
  57. mse = np.zeros(num_samples)
  58. for i in range(num_samples):
  59. mse[i] = diff_mse(images_random_equalize[i], images_original[i])
  60. logger.info("MSE= {}".format(str(np.mean(mse))))
  61. def test_random_equalize_eager():
  62. """
  63. Test RandomEqualize eager.
  64. """
  65. img = np.fromfile(image_file, dtype=np.uint8)
  66. logger.info("Image.type: {}, Image.shape: {}".format(type(img), img.shape))
  67. img = Decode()(img)
  68. img_equalized = Equalize()(img)
  69. img_random_equalized = RandomEqualize(1.0)(img)
  70. logger.info("Image.type: {}, Image.shape: {}".format(type(img_random_equalized), img_random_equalized.shape))
  71. assert img_random_equalized.all() == img_equalized.all()
  72. def test_random_equalize_comp(plot=False):
  73. """
  74. Test RandomEqualize op compared with Equalize op.
  75. """
  76. random_equalize_op = RandomEqualize(prob=1.0)
  77. equalize_op = Equalize()
  78. dataset1 = ds.ImageFolderDataset(data_dir, 1, shuffle=False, decode=True)
  79. for item in dataset1.create_dict_iterator(num_epochs=1, output_numpy=True):
  80. image = item['image']
  81. dataset1.map(operations=random_equalize_op, input_columns=['image'])
  82. dataset2 = ds.ImageFolderDataset(data_dir, 1, shuffle=False, decode=True)
  83. dataset2.map(operations=equalize_op, input_columns=['image'])
  84. for item1, item2 in zip(dataset1.create_dict_iterator(num_epochs=1, output_numpy=True),
  85. dataset2.create_dict_iterator(num_epochs=1, output_numpy=True)):
  86. image_random_equalized = item1['image']
  87. image_equalized = item2['image']
  88. mse = diff_mse(image_equalized, image_random_equalized)
  89. assert mse == 0
  90. logger.info("mse: {}".format(mse))
  91. if plot:
  92. visualize_image(image, image_random_equalized, mse, image_equalized)
  93. def test_random_equalize_invalid_prob():
  94. """
  95. Test eager. prob out of range.
  96. """
  97. logger.info("test_random_equalize_invalid_prob")
  98. dataset = ds.ImageFolderDataset(data_dir, 1, shuffle=False, decode=True)
  99. try:
  100. random_equalize_op = RandomEqualize(1.5)
  101. dataset = dataset.map(operations=random_equalize_op, input_columns=['image'])
  102. except ValueError as e:
  103. logger.info("Got an exception in DE: {}".format(str(e)))
  104. assert "Input prob is not within the required interval of [0.0, 1.0]." in str(e)
  105. if __name__ == "__main__":
  106. test_random_equalize_pipeline(plot=True)
  107. test_random_equalize_eager()
  108. test_random_equalize_comp(plot=True)
  109. test_random_equalize_invalid_prob()