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_sharpness.py 4.4 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # Copyright 2020 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 RandomSharpness op in DE
  17. """
  18. import numpy as np
  19. import mindspore.dataset as ds
  20. import mindspore.dataset.engine as de
  21. import mindspore.dataset.transforms.vision.py_transforms as F
  22. from mindspore import log as logger
  23. from util import visualize_list, diff_mse, save_and_check_md5, \
  24. config_get_set_seed, config_get_set_num_parallel_workers
  25. DATA_DIR = "../data/dataset/testImageNetData/train/"
  26. GENERATE_GOLDEN = False
  27. def test_random_sharpness(degrees=(0.1, 1.9), plot=False):
  28. """
  29. Test RandomSharpness
  30. """
  31. logger.info("Test RandomSharpness")
  32. # Original Images
  33. data = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False)
  34. transforms_original = F.ComposeOp([F.Decode(),
  35. F.Resize((224, 224)),
  36. F.ToTensor()])
  37. ds_original = data.map(input_columns="image",
  38. operations=transforms_original())
  39. ds_original = ds_original.batch(512)
  40. for idx, (image, _) in enumerate(ds_original):
  41. if idx == 0:
  42. images_original = np.transpose(image, (0, 2, 3, 1))
  43. else:
  44. images_original = np.append(images_original,
  45. np.transpose(image, (0, 2, 3, 1)),
  46. axis=0)
  47. # Random Sharpness Adjusted Images
  48. data = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False)
  49. transforms_random_sharpness = F.ComposeOp([F.Decode(),
  50. F.Resize((224, 224)),
  51. F.RandomSharpness(degrees=degrees),
  52. F.ToTensor()])
  53. ds_random_sharpness = data.map(input_columns="image",
  54. operations=transforms_random_sharpness())
  55. ds_random_sharpness = ds_random_sharpness.batch(512)
  56. for idx, (image, _) in enumerate(ds_random_sharpness):
  57. if idx == 0:
  58. images_random_sharpness = np.transpose(image, (0, 2, 3, 1))
  59. else:
  60. images_random_sharpness = np.append(images_random_sharpness,
  61. np.transpose(image, (0, 2, 3, 1)),
  62. axis=0)
  63. num_samples = images_original.shape[0]
  64. mse = np.zeros(num_samples)
  65. for i in range(num_samples):
  66. mse[i] = diff_mse(images_random_sharpness[i], images_original[i])
  67. logger.info("MSE= {}".format(str(np.mean(mse))))
  68. if plot:
  69. visualize_list(images_original, images_random_sharpness)
  70. def test_random_sharpness_md5():
  71. """
  72. Test RandomSharpness with md5 comparison
  73. """
  74. logger.info("Test RandomSharpness with md5 comparison")
  75. original_seed = config_get_set_seed(5)
  76. original_num_parallel_workers = config_get_set_num_parallel_workers(1)
  77. # define map operations
  78. transforms = [
  79. F.Decode(),
  80. F.RandomSharpness((0.1, 1.9)),
  81. F.ToTensor()
  82. ]
  83. transform = F.ComposeOp(transforms)
  84. # Generate dataset
  85. data = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False)
  86. data = data.map(input_columns=["image"], operations=transform())
  87. # check results with md5 comparison
  88. filename = "random_sharpness_01_result.npz"
  89. save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN)
  90. # Restore configuration
  91. ds.config.set_seed(original_seed)
  92. ds.config.set_num_parallel_workers(original_num_parallel_workers)
  93. if __name__ == "__main__":
  94. test_random_sharpness()
  95. test_random_sharpness(plot=True)
  96. test_random_sharpness(degrees=(0.5, 1.5), plot=True)
  97. test_random_sharpness_md5()