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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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
  23. DATA_DIR = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"]
  24. SCHEMA_DIR = "../data/dataset/test_tf_file_3_images/datasetSchema.json"
  25. def test_random_erasing_op(plot=False):
  26. """
  27. Test RandomErasing and Cutout
  28. """
  29. logger.info("test_random_erasing")
  30. # First dataset
  31. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  32. transforms_1 = [
  33. vision.Decode(),
  34. vision.ToTensor(),
  35. vision.RandomErasing(value='random')
  36. ]
  37. transform_1 = vision.ComposeOp(transforms_1)
  38. data1 = data1.map(input_columns=["image"], operations=transform_1())
  39. # Second dataset
  40. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  41. transforms_2 = [
  42. vision.Decode(),
  43. vision.ToTensor(),
  44. vision.Cutout(80)
  45. ]
  46. transform_2 = vision.ComposeOp(transforms_2)
  47. data2 = data2.map(input_columns=["image"], operations=transform_2())
  48. num_iter = 0
  49. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  50. num_iter += 1
  51. image_1 = (item1["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  52. image_2 = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  53. logger.info("shape of image_1: {}".format(image_1.shape))
  54. logger.info("shape of image_2: {}".format(image_2.shape))
  55. logger.info("dtype of image_1: {}".format(image_1.dtype))
  56. logger.info("dtype of image_2: {}".format(image_2.dtype))
  57. mse = diff_mse(image_1, image_2)
  58. if plot:
  59. visualize_image(image_1, image_2, mse)
  60. if __name__ == "__main__":
  61. test_random_erasing_op(plot=True)