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_pad.py 5.3 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # Copyright 2019 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 Pad 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, save_and_check_md5
  24. DATA_DIR = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"]
  25. SCHEMA_DIR = "../data/dataset/test_tf_file_3_images/datasetSchema.json"
  26. GENERATE_GOLDEN = False
  27. def test_pad_op():
  28. """
  29. Test Pad op
  30. """
  31. logger.info("test_random_color_jitter_op")
  32. # First dataset
  33. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  34. decode_op = c_vision.Decode()
  35. pad_op = c_vision.Pad((100, 100, 100, 100))
  36. ctrans = [decode_op,
  37. pad_op,
  38. ]
  39. data1 = data1.map(input_columns=["image"], operations=ctrans)
  40. # Second dataset
  41. transforms = [
  42. py_vision.Decode(),
  43. py_vision.Pad(100),
  44. py_vision.ToTensor(),
  45. ]
  46. transform = py_vision.ComposeOp(transforms)
  47. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  48. data2 = data2.map(input_columns=["image"], operations=transform())
  49. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  50. c_image = item1["image"]
  51. py_image = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  52. logger.info("shape of c_image: {}".format(c_image.shape))
  53. logger.info("shape of py_image: {}".format(py_image.shape))
  54. logger.info("dtype of c_image: {}".format(c_image.dtype))
  55. logger.info("dtype of py_image: {}".format(py_image.dtype))
  56. mse = diff_mse(c_image, py_image)
  57. logger.info("mse is {}".format(mse))
  58. assert mse < 0.01
  59. def test_pad_grayscale():
  60. """
  61. Tests that the pad works for grayscale images
  62. """
  63. # Note: image.transpose performs channel swap to allow py transforms to
  64. # work with c transforms
  65. transforms = [
  66. py_vision.Decode(),
  67. py_vision.Grayscale(1),
  68. py_vision.ToTensor(),
  69. (lambda image: (image.transpose(1, 2, 0) * 255).astype(np.uint8))
  70. ]
  71. transform = py_vision.ComposeOp(transforms)
  72. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  73. data1 = data1.map(input_columns=["image"], operations=transform())
  74. # if input is grayscale, the output dimensions should be single channel
  75. pad_gray = c_vision.Pad(100, fill_value=(20, 20, 20))
  76. data1 = data1.map(input_columns=["image"], operations=pad_gray)
  77. dataset_shape_1 = []
  78. for item1 in data1.create_dict_iterator():
  79. c_image = item1["image"]
  80. dataset_shape_1.append(c_image.shape)
  81. # Dataset for comparison
  82. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  83. decode_op = c_vision.Decode()
  84. # we use the same padding logic
  85. ctrans = [decode_op, pad_gray]
  86. dataset_shape_2 = []
  87. data2 = data2.map(input_columns=["image"], operations=ctrans)
  88. for item2 in data2.create_dict_iterator():
  89. c_image = item2["image"]
  90. dataset_shape_2.append(c_image.shape)
  91. for shape1, shape2 in zip(dataset_shape_1, dataset_shape_2):
  92. # validate that the first two dimensions are the same
  93. # we have a little inconsistency here because the third dimension is 1 after py_vision.Grayscale
  94. assert shape1[0:1] == shape2[0:1]
  95. def test_pad_md5():
  96. """
  97. Test Pad with md5 check
  98. """
  99. logger.info("test_pad_md5")
  100. # First dataset
  101. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  102. decode_op = c_vision.Decode()
  103. pad_op = c_vision.Pad(150)
  104. ctrans = [decode_op,
  105. pad_op,
  106. ]
  107. data1 = data1.map(input_columns=["image"], operations=ctrans)
  108. # Second dataset
  109. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  110. pytrans = [
  111. py_vision.Decode(),
  112. py_vision.Pad(150),
  113. py_vision.ToTensor(),
  114. ]
  115. transform = py_vision.ComposeOp(pytrans)
  116. data2 = data2.map(input_columns=["image"], operations=transform())
  117. # Compare with expected md5 from images
  118. filename1 = "pad_01_c_result.npz"
  119. save_and_check_md5(data1, filename1, generate_golden=GENERATE_GOLDEN)
  120. filename2 = "pad_01_py_result.npz"
  121. save_and_check_md5(data2, filename2, generate_golden=GENERATE_GOLDEN)
  122. if __name__ == "__main__":
  123. test_pad_op()
  124. test_pad_grayscale()
  125. test_pad_md5()