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