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_cut_out.py 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 CutOut 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.c_transforms as c
  22. import mindspore.dataset.transforms.vision.py_transforms as f
  23. from mindspore import log as logger
  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. def visualize(image_1, image_2):
  27. """
  28. visualizes the image using RandomErasing and Cutout
  29. """
  30. plt.subplot(141)
  31. plt.imshow(image_1)
  32. plt.title("RandomErasing")
  33. plt.subplot(142)
  34. plt.imshow(image_2)
  35. plt.title("Cutout")
  36. plt.subplot(143)
  37. plt.imshow(image_1 - image_2)
  38. plt.title("Difference image")
  39. plt.show()
  40. def test_cut_out_op():
  41. """
  42. Test Cutout
  43. """
  44. logger.info("test_cut_out")
  45. # First dataset
  46. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"])
  47. transforms_1 = [
  48. f.Decode(),
  49. f.ToTensor(),
  50. f.RandomErasing(value='random')
  51. ]
  52. transform_1 = f.ComposeOp(transforms_1)
  53. data1 = data1.map(input_columns=["image"], operations=transform_1())
  54. # Second dataset
  55. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"])
  56. decode_op = c.Decode()
  57. cut_out_op = c.CutOut(80)
  58. transforms_2 = [
  59. decode_op,
  60. cut_out_op
  61. ]
  62. data2 = data2.map(input_columns=["image"], operations=transforms_2)
  63. num_iter = 0
  64. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  65. num_iter += 1
  66. image_1 = (item1["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  67. # C image doesn't require transpose
  68. image_2 = item2["image"]
  69. logger.info("shape of image_1: {}".format(image_1.shape))
  70. logger.info("shape of image_2: {}".format(image_2.shape))
  71. logger.info("dtype of image_1: {}".format(image_1.dtype))
  72. logger.info("dtype of image_2: {}".format(image_2.dtype))
  73. # visualize(image_1, image_2)
  74. def test_cut_out_op_multicut():
  75. """
  76. Test Cutout
  77. """
  78. logger.info("test_cut_out")
  79. # First dataset
  80. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"])
  81. transforms_1 = [
  82. f.Decode(),
  83. f.ToTensor(),
  84. f.RandomErasing(value='random')
  85. ]
  86. transform_1 = f.ComposeOp(transforms_1)
  87. data1 = data1.map(input_columns=["image"], operations=transform_1())
  88. # Second dataset
  89. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"])
  90. decode_op = c.Decode()
  91. cut_out_op = c.CutOut(80, num_patches=10)
  92. transforms_2 = [
  93. decode_op,
  94. cut_out_op
  95. ]
  96. data2 = data2.map(input_columns=["image"], operations=transforms_2)
  97. num_iter = 0
  98. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  99. num_iter += 1
  100. image_1 = (item1["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  101. # C image doesn't require transpose
  102. image_2 = item2["image"]
  103. logger.info("shape of image_1: {}".format(image_1.shape))
  104. logger.info("shape of image_2: {}".format(image_2.shape))
  105. logger.info("dtype of image_1: {}".format(image_1.dtype))
  106. logger.info("dtype of image_2: {}".format(image_2.dtype))
  107. if __name__ == "__main__":
  108. test_cut_out_op()
  109. test_cut_out_op_multicut()