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.7 kB

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