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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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
  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. def test_pad_op():
  27. """
  28. Test Pad op
  29. """
  30. logger.info("test_random_color_jitter_op")
  31. # First dataset
  32. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  33. decode_op = c_vision.Decode()
  34. pad_op = c_vision.Pad((100, 100, 100, 100))
  35. ctrans = [decode_op,
  36. pad_op,
  37. ]
  38. data1 = data1.map(input_columns=["image"], operations=ctrans)
  39. # Second dataset
  40. transforms = [
  41. py_vision.Decode(),
  42. py_vision.Pad(100),
  43. py_vision.ToTensor(),
  44. ]
  45. transform = py_vision.ComposeOp(transforms)
  46. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  47. data2 = data2.map(input_columns=["image"], operations=transform())
  48. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  49. c_image = item1["image"]
  50. py_image = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  51. logger.info("shape of c_image: {}".format(c_image.shape))
  52. logger.info("shape of py_image: {}".format(py_image.shape))
  53. logger.info("dtype of c_image: {}".format(c_image.dtype))
  54. logger.info("dtype of py_image: {}".format(py_image.dtype))
  55. mse = diff_mse(c_image, py_image)
  56. logger.info("mse is {}".format(mse))
  57. assert mse < 0.01
  58. def test_pad_grayscale():
  59. """
  60. Tests that the pad works for grayscale images
  61. """
  62. # Note: image.transpose performs channel swap to allow py transforms to
  63. # work with c transforms
  64. transforms = [
  65. py_vision.Decode(),
  66. py_vision.Grayscale(1),
  67. py_vision.ToTensor(),
  68. (lambda image: (image.transpose(1, 2, 0) * 255).astype(np.uint8))
  69. ]
  70. transform = py_vision.ComposeOp(transforms)
  71. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  72. data1 = data1.map(input_columns=["image"], operations=transform())
  73. # if input is grayscale, the output dimensions should be single channel
  74. pad_gray = c_vision.Pad(100, fill_value=(20, 20, 20))
  75. data1 = data1.map(input_columns=["image"], operations=pad_gray)
  76. dataset_shape_1 = []
  77. for item1 in data1.create_dict_iterator():
  78. c_image = item1["image"]
  79. dataset_shape_1.append(c_image.shape)
  80. # Dataset for comparison
  81. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  82. decode_op = c_vision.Decode()
  83. # we use the same padding logic
  84. ctrans = [decode_op, pad_gray]
  85. dataset_shape_2 = []
  86. data2 = data2.map(input_columns=["image"], operations=ctrans)
  87. for item2 in data2.create_dict_iterator():
  88. c_image = item2["image"]
  89. dataset_shape_2.append(c_image.shape)
  90. for shape1, shape2 in zip(dataset_shape_1, dataset_shape_2):
  91. # validate that the first two dimensions are the same
  92. # we have a little inconsistency here because the third dimension is 1 after py_vision.Grayscale
  93. assert shape1[0:1] == shape2[0:1]
  94. if __name__ == "__main__":
  95. test_pad_op()
  96. test_pad_grayscale()