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 2.9 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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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
  23. DATA_DIR = "../data/dataset/testImageNetData/train/"
  24. def test_invert(plot=False):
  25. """
  26. Test Invert
  27. """
  28. logger.info("Test Invert")
  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. # Color Inverted Images
  45. ds = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False)
  46. transforms_invert = F.ComposeOp([F.Decode(),
  47. F.Resize((224, 224)),
  48. F.Invert(),
  49. F.ToTensor()])
  50. ds_invert = ds.map(input_columns="image",
  51. operations=transforms_invert())
  52. ds_invert = ds_invert.batch(512)
  53. for idx, (image, _) in enumerate(ds_invert):
  54. if idx == 0:
  55. images_invert = np.transpose(image, (0, 2, 3, 1))
  56. else:
  57. images_invert = np.append(images_invert,
  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_invert[i] - images_original[i]) ** 2)
  64. logger.info("MSE= {}".format(str(np.mean(mse))))
  65. if plot:
  66. visualize_list(images_original, images_invert)
  67. if __name__ == "__main__":
  68. test_invert(plot=True)