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_adjust_sharpness.py 6.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 RandomAdjustSharpness in DE
  17. """
  18. import numpy as np
  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_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_adjust_sharpness_pipeline(plot=False):
  26. """
  27. Test RandomAdjustSharpness pipeline
  28. """
  29. logger.info("Test RandomAdjustSharpness pipeline")
  30. # Original Images
  31. data_set = ds.ImageFolderDataset(dataset_dir=data_dir, shuffle=False)
  32. transforms_original = [c_vision.Decode(), c_vision.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 Sharpness Adjusted Images
  43. data_set1 = ds.ImageFolderDataset(dataset_dir=data_dir, shuffle=False)
  44. transform_random_adjust_sharpness = [c_vision.Decode(),
  45. c_vision.Resize(size=[224, 224]),
  46. c_vision.RandomAdjustSharpness(2.0, 0.6)]
  47. ds_random_adjust_sharpness = data_set1.map(operations=transform_random_adjust_sharpness, input_columns="image")
  48. ds_random_adjust_sharpness = ds_random_adjust_sharpness.batch(512)
  49. for idx, (image, _) in enumerate(ds_random_adjust_sharpness):
  50. if idx == 0:
  51. images_random_adjust_sharpness = image.asnumpy()
  52. else:
  53. images_random_adjust_sharpness = np.append(images_random_adjust_sharpness,
  54. image.asnumpy(),
  55. axis=0)
  56. if plot:
  57. visualize_list(images_original, images_random_adjust_sharpness)
  58. num_samples = images_original.shape[0]
  59. mse = np.zeros(num_samples)
  60. for i in range(num_samples):
  61. mse[i] = diff_mse(images_random_adjust_sharpness[i], images_original[i])
  62. logger.info("MSE= {}".format(str(np.mean(mse))))
  63. def test_random_adjust_sharpness_eager():
  64. """
  65. Test RandomAdjustSharpness eager.
  66. """
  67. img = np.fromfile(image_file, dtype=np.uint8)
  68. logger.info("Image.type: {}, Image.shape: {}".format(type(img), img.shape))
  69. img = c_vision.Decode()(img)
  70. img_sharped = c_vision.RandomSharpness((2.0, 2.0))(img)
  71. img_random_sharped = c_vision.RandomAdjustSharpness(2.0, 1.0)(img)
  72. logger.info("Image.type: {}, Image.shape: {}".format(type(img_random_sharped), img_random_sharped.shape))
  73. assert img_random_sharped.all() == img_sharped.all()
  74. def test_random_adjust_sharpness_comp(plot=False):
  75. """
  76. Test RandomAdjustSharpness op compared with Sharpness op.
  77. """
  78. random_adjust_sharpness_op = c_vision.RandomAdjustSharpness(degree=2.0, prob=1.0)
  79. sharpness_op = c_vision.RandomSharpness((2.0, 2.0))
  80. dataset1 = ds.ImageFolderDataset(data_dir, 1, shuffle=False, decode=True)
  81. for item in dataset1.create_dict_iterator(num_epochs=1, output_numpy=True):
  82. image = item['image']
  83. dataset1.map(operations=random_adjust_sharpness_op, input_columns=['image'])
  84. dataset2 = ds.ImageFolderDataset(data_dir, 1, shuffle=False, decode=True)
  85. dataset2.map(operations=sharpness_op, input_columns=['image'])
  86. for item1, item2 in zip(dataset1.create_dict_iterator(num_epochs=1, output_numpy=True),
  87. dataset2.create_dict_iterator(num_epochs=1, output_numpy=True)):
  88. image_random_sharpness = item1['image']
  89. image_sharpness = item2['image']
  90. mse = diff_mse(image_sharpness, image_random_sharpness)
  91. assert mse == 0
  92. logger.info("mse: {}".format(mse))
  93. if plot:
  94. visualize_image(image, image_random_sharpness, mse, image_sharpness)
  95. def test_random_adjust_sharpness_invalid_prob():
  96. """
  97. Test invalid prob. prob out of range.
  98. """
  99. logger.info("test_random_adjust_sharpness_invalid_prob")
  100. dataset = ds.ImageFolderDataset(data_dir, 1, shuffle=False, decode=True)
  101. try:
  102. random_adjust_sharpness_op = c_vision.RandomAdjustSharpness(2.0, 1.5)
  103. dataset = dataset.map(operations=random_adjust_sharpness_op, input_columns=['image'])
  104. except ValueError as e:
  105. logger.info("Got an exception in DE: {}".format(str(e)))
  106. assert "Input prob is not within the required interval of [0.0, 1.0]." in str(e)
  107. def test_random_adjust_sharpness_invalid_degree():
  108. """
  109. Test invalid prob. prob out of range.
  110. """
  111. logger.info("test_random_adjust_sharpness_invalid_prob")
  112. dataset = ds.ImageFolderDataset(data_dir, 1, shuffle=False, decode=True)
  113. try:
  114. random_adjust_sharpness_op = c_vision.RandomAdjustSharpness(-1.0, 1.5)
  115. dataset = dataset.map(operations=random_adjust_sharpness_op, input_columns=['image'])
  116. except ValueError as e:
  117. logger.info("Got an exception in DE: {}".format(str(e)))
  118. assert "interval" in str(e)
  119. if __name__ == "__main__":
  120. test_random_adjust_sharpness_pipeline(plot=True)
  121. test_random_adjust_sharpness_eager()
  122. test_random_adjust_sharpness_comp(plot=True)
  123. test_random_adjust_sharpness_invalid_prob()
  124. test_random_adjust_sharpness_invalid_degree()