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

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