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

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