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_HWC2CHW.py 4.6 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # Copyright 2020 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 HWC2CHW 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_vision
  21. import mindspore.dataset.transforms.vision.py_transforms as py_vision
  22. from mindspore import log as logger
  23. from util import diff_mse, visualize_list, save_and_check_md5
  24. GENERATE_GOLDEN = False
  25. DATA_DIR = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"]
  26. SCHEMA_DIR = "../data/dataset/test_tf_file_3_images/datasetSchema.json"
  27. def test_HWC2CHW(plot=False):
  28. """
  29. Test HWC2CHW
  30. """
  31. logger.info("Test HWC2CHW")
  32. # First dataset
  33. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  34. decode_op = c_vision.Decode()
  35. hwc2chw_op = c_vision.HWC2CHW()
  36. data1 = data1.map(input_columns=["image"], operations=decode_op)
  37. data1 = data1.map(input_columns=["image"], operations=hwc2chw_op)
  38. # Second dataset
  39. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  40. data2 = data2.map(input_columns=["image"], operations=decode_op)
  41. image_transposed = []
  42. image = []
  43. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  44. transposed_item = item1["image"].copy()
  45. original_item = item2["image"].copy()
  46. image_transposed.append(transposed_item.transpose(1, 2, 0))
  47. image.append(original_item)
  48. # check if the shape of data is transposed correctly
  49. # transpose the original image from shape (H,W,C) to (C,H,W)
  50. mse = diff_mse(transposed_item, original_item.transpose(2, 0, 1))
  51. assert mse == 0
  52. if plot:
  53. visualize_list(image, image_transposed)
  54. def test_HWC2CHW_md5():
  55. """
  56. Test HWC2CHW(md5)
  57. """
  58. logger.info("Test HWC2CHW with md5 comparison")
  59. # First dataset
  60. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  61. decode_op = c_vision.Decode()
  62. hwc2chw_op = c_vision.HWC2CHW()
  63. data1 = data1.map(input_columns=["image"], operations=decode_op)
  64. data1 = data1.map(input_columns=["image"], operations=hwc2chw_op)
  65. # Compare with expected md5 from images
  66. filename = "HWC2CHW_01_result.npz"
  67. save_and_check_md5(data1, filename, generate_golden=GENERATE_GOLDEN)
  68. def test_HWC2CHW_comp(plot=False):
  69. """
  70. Test HWC2CHW between python and c image augmentation
  71. """
  72. logger.info("Test HWC2CHW with c_transform and py_transform comparison")
  73. # First dataset
  74. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  75. decode_op = c_vision.Decode()
  76. hwc2chw_op = c_vision.HWC2CHW()
  77. data1 = data1.map(input_columns=["image"], operations=decode_op)
  78. data1 = data1.map(input_columns=["image"], operations=hwc2chw_op)
  79. # Second dataset
  80. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  81. transforms = [
  82. py_vision.Decode(),
  83. py_vision.ToTensor(),
  84. py_vision.HWC2CHW()
  85. ]
  86. transform = py_vision.ComposeOp(transforms)
  87. data2 = data2.map(input_columns=["image"], operations=transform())
  88. image_c_transposed = []
  89. image_py_transposed = []
  90. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  91. c_image = item1["image"]
  92. py_image = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  93. # Compare images between that applying c_transform and py_transform
  94. mse = diff_mse(py_image, c_image)
  95. # Note: The images aren't exactly the same due to rounding error
  96. assert mse < 0.001
  97. image_c_transposed.append(c_image.transpose(1, 2, 0))
  98. image_py_transposed.append(py_image.transpose(1, 2, 0))
  99. if plot:
  100. visualize_list(image_c_transposed, image_py_transposed, visualize_mode=2)
  101. if __name__ == '__main__':
  102. test_HWC2CHW(True)
  103. test_HWC2CHW_md5()
  104. test_HWC2CHW_comp(True)