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

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