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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 matplotlib.pyplot as plt
  19. import mindspore.dataset.transforms.vision.c_transforms as c_vision
  20. import numpy as np
  21. import mindspore.dataset as ds
  22. import mindspore.dataset.transforms.vision.py_transforms as py_vision
  23. from mindspore import log as logger
  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 visualize(first, mse, second):
  27. """
  28. visualizes the image using DE op and enCV
  29. """
  30. plt.subplot(141)
  31. plt.imshow(first)
  32. plt.title("c transformed image")
  33. plt.subplot(142)
  34. plt.imshow(second)
  35. plt.title("py random_color_jitter image")
  36. plt.subplot(143)
  37. plt.imshow(first - second)
  38. plt.title("Difference image, mse : {}".format(mse))
  39. plt.show()
  40. def diff_mse(in1, in2):
  41. mse = (np.square(in1.astype(float) / 255 - in2.astype(float) / 255)).mean()
  42. return mse * 100
  43. def test_pad_op():
  44. """
  45. Test Pad op
  46. """
  47. logger.info("test_random_color_jitter_op")
  48. # First dataset
  49. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  50. decode_op = c_vision.Decode()
  51. pad_op = c_vision.Pad((100, 100, 100, 100))
  52. ctrans = [decode_op,
  53. pad_op,
  54. ]
  55. data1 = data1.map(input_columns=["image"], operations=ctrans)
  56. # Second dataset
  57. transforms = [
  58. py_vision.Decode(),
  59. py_vision.Pad(100),
  60. py_vision.ToTensor(),
  61. ]
  62. transform = py_vision.ComposeOp(transforms)
  63. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  64. data2 = data2.map(input_columns=["image"], operations=transform())
  65. num_iter = 0
  66. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  67. num_iter += 1
  68. c_image = item1["image"]
  69. py_image = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  70. logger.info("shape of c_image: {}".format(c_image.shape))
  71. logger.info("shape of py_image: {}".format(py_image.shape))
  72. logger.info("dtype of c_image: {}".format(c_image.dtype))
  73. logger.info("dtype of py_image: {}".format(py_image.dtype))
  74. diff = c_image - py_image
  75. mse = diff_mse(c_image, py_image)
  76. logger.info("mse is {}".format(mse))
  77. assert mse < 0.01
  78. if __name__ == "__main__":
  79. test_pad_op()