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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 numpy as np
  19. import mindspore.dataset as ds
  20. import mindspore.dataset.transforms.vision.c_transforms as c
  21. import mindspore.dataset.transforms.vision.py_transforms as f
  22. from mindspore import log as logger
  23. from util import visualize_image, diff_mse
  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 test_cut_out_op(plot=False):
  27. """
  28. Test Cutout
  29. """
  30. logger.info("test_cut_out")
  31. # First dataset
  32. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"])
  33. transforms_1 = [
  34. f.Decode(),
  35. f.ToTensor(),
  36. f.RandomErasing(value='random')
  37. ]
  38. transform_1 = f.ComposeOp(transforms_1)
  39. data1 = data1.map(input_columns=["image"], operations=transform_1())
  40. # Second dataset
  41. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"])
  42. decode_op = c.Decode()
  43. cut_out_op = c.CutOut(80)
  44. transforms_2 = [
  45. decode_op,
  46. cut_out_op
  47. ]
  48. data2 = data2.map(input_columns=["image"], operations=transforms_2)
  49. num_iter = 0
  50. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  51. num_iter += 1
  52. image_1 = (item1["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  53. # C image doesn't require transpose
  54. image_2 = item2["image"]
  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_cut_out_op_multicut():
  63. """
  64. Test Cutout
  65. """
  66. logger.info("test_cut_out")
  67. # First dataset
  68. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"])
  69. transforms_1 = [
  70. f.Decode(),
  71. f.ToTensor(),
  72. f.RandomErasing(value='random')
  73. ]
  74. transform_1 = f.ComposeOp(transforms_1)
  75. data1 = data1.map(input_columns=["image"], operations=transform_1())
  76. # Second dataset
  77. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"])
  78. decode_op = c.Decode()
  79. cut_out_op = c.CutOut(80, num_patches=10)
  80. transforms_2 = [
  81. decode_op,
  82. cut_out_op
  83. ]
  84. data2 = data2.map(input_columns=["image"], operations=transforms_2)
  85. num_iter = 0
  86. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  87. num_iter += 1
  88. image_1 = (item1["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  89. # C image doesn't require transpose
  90. image_2 = item2["image"]
  91. logger.info("shape of image_1: {}".format(image_1.shape))
  92. logger.info("shape of image_2: {}".format(image_2.shape))
  93. logger.info("dtype of image_1: {}".format(image_1.dtype))
  94. logger.info("dtype of image_2: {}".format(image_2.dtype))
  95. if __name__ == "__main__":
  96. test_cut_out_op(plot=True)
  97. test_cut_out_op_multicut()