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_invert.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
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 Invert 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, save_and_check_md5
  23. DATA_DIR = "../data/dataset/testImageNetData/train/"
  24. GENERATE_GOLDEN = False
  25. def test_invert(plot=False):
  26. """
  27. Test Invert
  28. """
  29. logger.info("Test Invert")
  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 Inverted Images
  46. ds = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False)
  47. transforms_invert = F.ComposeOp([F.Decode(),
  48. F.Resize((224, 224)),
  49. F.Invert(),
  50. F.ToTensor()])
  51. ds_invert = ds.map(input_columns="image",
  52. operations=transforms_invert())
  53. ds_invert = ds_invert.batch(512)
  54. for idx, (image, _) in enumerate(ds_invert):
  55. if idx == 0:
  56. images_invert = np.transpose(image, (0, 2, 3, 1))
  57. else:
  58. images_invert = np.append(images_invert,
  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] = np.mean((images_invert[i] - images_original[i]) ** 2)
  65. logger.info("MSE= {}".format(str(np.mean(mse))))
  66. if plot:
  67. visualize_list(images_original, images_invert)
  68. def test_invert_md5():
  69. """
  70. Test Invert with md5 check
  71. """
  72. logger.info("Test Invert with md5 check")
  73. # Generate dataset
  74. ds = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False)
  75. transforms_invert = F.ComposeOp([F.Decode(),
  76. F.Invert(),
  77. F.ToTensor()])
  78. data = ds.map(input_columns="image", operations=transforms_invert())
  79. # Compare with expected md5 from images
  80. filename = "invert_01_result.npz"
  81. save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN)
  82. if __name__ == "__main__":
  83. test_invert(plot=True)
  84. test_invert_md5()