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_erasing.py 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # Copyright 2019 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 RandomErasing op in DE
  17. """
  18. import numpy as np
  19. import mindspore.dataset as ds
  20. import mindspore.dataset.transforms.py_transforms
  21. import mindspore.dataset.vision.py_transforms as vision
  22. from mindspore import log as logger
  23. from util import diff_mse, visualize_image, save_and_check_md5, \
  24. config_get_set_seed, config_get_set_num_parallel_workers
  25. DATA_DIR = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"]
  26. SCHEMA_DIR = "../data/dataset/test_tf_file_3_images/datasetSchema.json"
  27. GENERATE_GOLDEN = False
  28. def test_random_erasing_op(plot=False):
  29. """
  30. Test RandomErasing and Cutout
  31. """
  32. logger.info("test_random_erasing")
  33. # First dataset
  34. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  35. transforms_1 = [
  36. vision.Decode(),
  37. vision.ToTensor(),
  38. vision.RandomErasing(value='random')
  39. ]
  40. transform_1 = mindspore.dataset.transforms.py_transforms.Compose(transforms_1)
  41. data1 = data1.map(operations=transform_1, input_columns=["image"])
  42. # Second dataset
  43. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  44. transforms_2 = [
  45. vision.Decode(),
  46. vision.ToTensor(),
  47. vision.Cutout(80)
  48. ]
  49. transform_2 = mindspore.dataset.transforms.py_transforms.Compose(transforms_2)
  50. data2 = data2.map(operations=transform_2, input_columns=["image"])
  51. num_iter = 0
  52. for item1, item2 in zip(data1.create_dict_iterator(num_epochs=1), data2.create_dict_iterator(num_epochs=1)):
  53. num_iter += 1
  54. image_1 = (item1["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  55. image_2 = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  56. logger.info("shape of image_1: {}".format(image_1.shape))
  57. logger.info("shape of image_2: {}".format(image_2.shape))
  58. logger.info("dtype of image_1: {}".format(image_1.dtype))
  59. logger.info("dtype of image_2: {}".format(image_2.dtype))
  60. mse = diff_mse(image_1, image_2)
  61. if plot:
  62. visualize_image(image_1, image_2, mse)
  63. def test_random_erasing_md5():
  64. """
  65. Test RandomErasing with md5 check
  66. """
  67. logger.info("Test RandomErasing with md5 check")
  68. original_seed = config_get_set_seed(5)
  69. original_num_parallel_workers = config_get_set_num_parallel_workers(1)
  70. # Generate dataset
  71. data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  72. transforms_1 = [
  73. vision.Decode(),
  74. vision.ToTensor(),
  75. vision.RandomErasing(value='random')
  76. ]
  77. transform_1 = mindspore.dataset.transforms.py_transforms.Compose(transforms_1)
  78. data = data.map(operations=transform_1, input_columns=["image"])
  79. # Compare with expected md5 from images
  80. filename = "random_erasing_01_result.npz"
  81. save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN)
  82. # Restore configuration
  83. ds.config.set_seed(original_seed)
  84. ds.config.set_num_parallel_workers((original_num_parallel_workers))
  85. if __name__ == "__main__":
  86. test_random_erasing_op(plot=True)
  87. test_random_erasing_md5()