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

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