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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 RandomRotation op in DE
  17. """
  18. import matplotlib.pyplot as plt
  19. import numpy as np
  20. import mindspore.dataset as ds
  21. import mindspore.dataset.transforms.vision.py_transforms as vision
  22. from mindspore import log as logger
  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 visualize(image_1, image_2):
  26. """
  27. visualizes the image using RandomErasing and Cutout
  28. """
  29. plt.subplot(141)
  30. plt.imshow(image_1)
  31. plt.title("RandomErasing")
  32. plt.subplot(142)
  33. plt.imshow(image_2)
  34. plt.title("Cutout")
  35. plt.subplot(143)
  36. plt.imshow(image_1 - image_2)
  37. plt.title("Difference image")
  38. plt.show()
  39. def test_random_erasing_op():
  40. """
  41. Test RandomErasing and Cutout
  42. """
  43. logger.info("test_random_erasing")
  44. # First dataset
  45. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  46. transforms_1 = [
  47. vision.Decode(),
  48. vision.ToTensor(),
  49. vision.RandomErasing(value='random')
  50. ]
  51. transform_1 = vision.ComposeOp(transforms_1)
  52. data1 = data1.map(input_columns=["image"], operations=transform_1())
  53. # Second dataset
  54. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  55. transforms_2 = [
  56. vision.Decode(),
  57. vision.ToTensor(),
  58. vision.Cutout(80)
  59. ]
  60. transform_2 = vision.ComposeOp(transforms_2)
  61. data2 = data2.map(input_columns=["image"], operations=transform_2())
  62. num_iter = 0
  63. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  64. num_iter += 1
  65. image_1 = (item1["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  66. image_2 = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  67. logger.info("shape of image_1: {}".format(image_1.shape))
  68. logger.info("shape of image_2: {}".format(image_2.shape))
  69. logger.info("dtype of image_1: {}".format(image_1.dtype))
  70. logger.info("dtype of image_2: {}".format(image_2.dtype))
  71. # visualize(image_1, image_2)
  72. if __name__ == "__main__":
  73. test_random_erasing_op()