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 3.3 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
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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.engine as de
  20. import mindspore.dataset.transforms.vision.py_transforms as F
  21. from mindspore import log as logger
  22. from util import visualize_list
  23. DATA_DIR = "../data/dataset/testImageNetData/train/"
  24. def test_random_sharpness(degrees=(0.1, 1.9), plot=False):
  25. """
  26. Test RandomSharpness
  27. """
  28. logger.info("Test RandomSharpness")
  29. # Original Images
  30. ds = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False)
  31. transforms_original = F.ComposeOp([F.Decode(),
  32. F.Resize((224, 224)),
  33. F.ToTensor()])
  34. ds_original = ds.map(input_columns="image",
  35. operations=transforms_original())
  36. ds_original = ds_original.batch(512)
  37. for idx, (image, _) in enumerate(ds_original):
  38. if idx == 0:
  39. images_original = np.transpose(image, (0, 2, 3, 1))
  40. else:
  41. images_original = np.append(images_original,
  42. np.transpose(image, (0, 2, 3, 1)),
  43. axis=0)
  44. # Random Sharpness Adjusted Images
  45. ds = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False)
  46. transforms_random_sharpness = F.ComposeOp([F.Decode(),
  47. F.Resize((224, 224)),
  48. F.RandomSharpness(degrees=degrees),
  49. F.ToTensor()])
  50. ds_random_sharpness = ds.map(input_columns="image",
  51. operations=transforms_random_sharpness())
  52. ds_random_sharpness = ds_random_sharpness.batch(512)
  53. for idx, (image, _) in enumerate(ds_random_sharpness):
  54. if idx == 0:
  55. images_random_sharpness = np.transpose(image, (0, 2, 3, 1))
  56. else:
  57. images_random_sharpness = np.append(images_random_sharpness,
  58. np.transpose(image, (0, 2, 3, 1)),
  59. axis=0)
  60. num_samples = images_original.shape[0]
  61. mse = np.zeros(num_samples)
  62. for i in range(num_samples):
  63. mse[i] = np.mean((images_random_sharpness[i] - images_original[i]) ** 2)
  64. logger.info("MSE= {}".format(str(np.mean(mse))))
  65. if plot:
  66. visualize_list(images_original, images_random_sharpness)
  67. if __name__ == "__main__":
  68. test_random_sharpness()
  69. test_random_sharpness(plot=True)
  70. test_random_sharpness(degrees=(0.5, 1.5), plot=True)