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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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.py_transforms
  21. import mindspore.dataset.vision.c_transforms as c
  22. import mindspore.dataset.vision.py_transforms as f
  23. from mindspore import log as logger
  24. from util import visualize_image, visualize_list, diff_mse, save_and_check_md5, \
  25. config_get_set_seed, config_get_set_num_parallel_workers
  26. DATA_DIR = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"]
  27. SCHEMA_DIR = "../data/dataset/test_tf_file_3_images/datasetSchema.json"
  28. GENERATE_GOLDEN = False
  29. def test_cut_out_op(plot=False):
  30. """
  31. Test Cutout
  32. """
  33. logger.info("test_cut_out")
  34. # First dataset
  35. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  36. transforms_1 = [
  37. f.Decode(),
  38. f.ToTensor(),
  39. f.RandomErasing(value='random')
  40. ]
  41. transform_1 = mindspore.dataset.transforms.py_transforms.Compose(transforms_1)
  42. data1 = data1.map(operations=transform_1, input_columns=["image"])
  43. # Second dataset
  44. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  45. decode_op = c.Decode()
  46. cut_out_op = c.CutOut(80)
  47. transforms_2 = [
  48. decode_op,
  49. cut_out_op
  50. ]
  51. data2 = data2.map(operations=transforms_2, input_columns=["image"])
  52. num_iter = 0
  53. for item1, item2 in zip(data1.create_dict_iterator(num_epochs=1), data2.create_dict_iterator(num_epochs=1)):
  54. num_iter += 1
  55. image_1 = (item1["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  56. # C image doesn't require transpose
  57. image_2 = item2["image"]
  58. logger.info("shape of image_1: {}".format(image_1.shape))
  59. logger.info("shape of image_2: {}".format(image_2.shape))
  60. logger.info("dtype of image_1: {}".format(image_1.dtype))
  61. logger.info("dtype of image_2: {}".format(image_2.dtype))
  62. mse = diff_mse(image_1, image_2)
  63. if plot:
  64. visualize_image(image_1, image_2, mse)
  65. def test_cut_out_op_multicut(plot=False):
  66. """
  67. Test Cutout
  68. """
  69. logger.info("test_cut_out")
  70. # First dataset
  71. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  72. transforms_1 = [
  73. f.Decode(),
  74. f.ToTensor(),
  75. ]
  76. transform_1 = mindspore.dataset.transforms.py_transforms.Compose(transforms_1)
  77. data1 = data1.map(operations=transform_1, input_columns=["image"])
  78. # Second dataset
  79. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  80. decode_op = c.Decode()
  81. cut_out_op = c.CutOut(80, num_patches=10)
  82. transforms_2 = [
  83. decode_op,
  84. cut_out_op
  85. ]
  86. data2 = data2.map(operations=transforms_2, input_columns=["image"])
  87. num_iter = 0
  88. image_list_1, image_list_2 = [], []
  89. for item1, item2 in zip(data1.create_dict_iterator(num_epochs=1), data2.create_dict_iterator(num_epochs=1)):
  90. num_iter += 1
  91. image_1 = (item1["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  92. # C image doesn't require transpose
  93. image_2 = item2["image"]
  94. image_list_1.append(image_1)
  95. image_list_2.append(image_2)
  96. logger.info("shape of image_1: {}".format(image_1.shape))
  97. logger.info("shape of image_2: {}".format(image_2.shape))
  98. logger.info("dtype of image_1: {}".format(image_1.dtype))
  99. logger.info("dtype of image_2: {}".format(image_2.dtype))
  100. if plot:
  101. visualize_list(image_list_1, image_list_2)
  102. def test_cut_out_md5():
  103. """
  104. Test Cutout with md5 check
  105. """
  106. logger.info("test_cut_out_md5")
  107. original_seed = config_get_set_seed(2)
  108. original_num_parallel_workers = config_get_set_num_parallel_workers(1)
  109. # First dataset
  110. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  111. decode_op = c.Decode()
  112. cut_out_op = c.CutOut(100)
  113. data1 = data1.map(operations=decode_op, input_columns=["image"])
  114. data1 = data1.map(operations=cut_out_op, input_columns=["image"])
  115. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  116. transforms = [
  117. f.Decode(),
  118. f.ToTensor(),
  119. f.Cutout(100)
  120. ]
  121. transform = mindspore.dataset.transforms.py_transforms.Compose(transforms)
  122. data2 = data2.map(operations=transform, input_columns=["image"])
  123. # Compare with expected md5 from images
  124. filename1 = "cut_out_01_c_result.npz"
  125. save_and_check_md5(data1, filename1, generate_golden=GENERATE_GOLDEN)
  126. filename2 = "cut_out_01_py_result.npz"
  127. save_and_check_md5(data2, filename2, generate_golden=GENERATE_GOLDEN)
  128. # Restore config
  129. ds.config.set_seed(original_seed)
  130. ds.config.set_num_parallel_workers(original_num_parallel_workers)
  131. def test_cut_out_comp(plot=False):
  132. """
  133. Test Cutout with c++ and python op comparison
  134. """
  135. logger.info("test_cut_out_comp")
  136. # First dataset
  137. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  138. transforms_1 = [
  139. f.Decode(),
  140. f.ToTensor(),
  141. f.Cutout(200)
  142. ]
  143. transform_1 = mindspore.dataset.transforms.py_transforms.Compose(transforms_1)
  144. data1 = data1.map(operations=transform_1, input_columns=["image"])
  145. # Second dataset
  146. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  147. transforms_2 = [
  148. c.Decode(),
  149. c.CutOut(200)
  150. ]
  151. data2 = data2.map(operations=transforms_2, input_columns=["image"])
  152. num_iter = 0
  153. image_list_1, image_list_2 = [], []
  154. for item1, item2 in zip(data1.create_dict_iterator(num_epochs=1), data2.create_dict_iterator(num_epochs=1)):
  155. num_iter += 1
  156. image_1 = (item1["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  157. # C image doesn't require transpose
  158. image_2 = item2["image"]
  159. image_list_1.append(image_1)
  160. image_list_2.append(image_2)
  161. logger.info("shape of image_1: {}".format(image_1.shape))
  162. logger.info("shape of image_2: {}".format(image_2.shape))
  163. logger.info("dtype of image_1: {}".format(image_1.dtype))
  164. logger.info("dtype of image_2: {}".format(image_2.dtype))
  165. if plot:
  166. visualize_list(image_list_1, image_list_2, visualize_mode=2)
  167. if __name__ == "__main__":
  168. test_cut_out_op(plot=True)
  169. test_cut_out_op_multicut(plot=True)
  170. test_cut_out_md5()
  171. test_cut_out_comp(plot=True)