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_equalize.py 3.6 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 Equalize 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, diff_mse, save_and_check_md5
  23. DATA_DIR = "../data/dataset/testImageNetData/train/"
  24. GENERATE_GOLDEN = False
  25. def test_equalize(plot=False):
  26. """
  27. Test Equalize
  28. """
  29. logger.info("Test Equalize")
  30. # Original Images
  31. ds = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False)
  32. transforms_original = F.ComposeOp([F.Decode(),
  33. F.Resize((224, 224)),
  34. F.ToTensor()])
  35. ds_original = ds.map(input_columns="image",
  36. operations=transforms_original())
  37. ds_original = ds_original.batch(512)
  38. for idx, (image, _) in enumerate(ds_original):
  39. if idx == 0:
  40. images_original = np.transpose(image, (0, 2, 3, 1))
  41. else:
  42. images_original = np.append(images_original,
  43. np.transpose(image, (0, 2, 3, 1)),
  44. axis=0)
  45. # Color Equalized Images
  46. ds = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False)
  47. transforms_equalize = F.ComposeOp([F.Decode(),
  48. F.Resize((224, 224)),
  49. F.Equalize(),
  50. F.ToTensor()])
  51. ds_equalize = ds.map(input_columns="image",
  52. operations=transforms_equalize())
  53. ds_equalize = ds_equalize.batch(512)
  54. for idx, (image, _) in enumerate(ds_equalize):
  55. if idx == 0:
  56. images_equalize = np.transpose(image, (0, 2, 3, 1))
  57. else:
  58. images_equalize = np.append(images_equalize,
  59. np.transpose(image, (0, 2, 3, 1)),
  60. axis=0)
  61. num_samples = images_original.shape[0]
  62. mse = np.zeros(num_samples)
  63. for i in range(num_samples):
  64. mse[i] = diff_mse(images_equalize[i], images_original[i])
  65. logger.info("MSE= {}".format(str(np.mean(mse))))
  66. if plot:
  67. visualize_list(images_original, images_equalize)
  68. def test_equalize_md5():
  69. """
  70. Test Equalize with md5 check
  71. """
  72. logger.info("Test Equalize")
  73. # First dataset
  74. data1 = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False)
  75. transforms = F.ComposeOp([F.Decode(),
  76. F.Equalize(),
  77. F.ToTensor()])
  78. data1 = data1.map(input_columns="image", operations=transforms())
  79. # Compare with expected md5 from images
  80. filename = "equalize_01_result.npz"
  81. save_and_check_md5(data1, filename, generate_golden=GENERATE_GOLDEN)
  82. if __name__ == "__main__":
  83. test_equalize(plot=True)
  84. test_equalize_md5()