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_rescale_op.py 3.5 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 the rescale op in DE
  17. """
  18. import mindspore.dataset as ds
  19. import mindspore.dataset.transforms.vision.c_transforms as vision
  20. from mindspore import log as logger
  21. from util import visualize_image, diff_mse, save_and_check_md5
  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. GENERATE_GOLDEN = False
  25. def rescale_np(image):
  26. """
  27. Apply the rescale
  28. """
  29. image = image / 255.0
  30. image = image - 1.0
  31. return image
  32. def get_rescaled(image_id):
  33. """
  34. Reads the image using DE ops and then rescales using Numpy
  35. """
  36. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  37. decode_op = vision.Decode()
  38. data1 = data1.map(input_columns=["image"], operations=decode_op)
  39. num_iter = 0
  40. for item in data1.create_dict_iterator():
  41. image = item["image"]
  42. if num_iter == image_id:
  43. return rescale_np(image)
  44. num_iter += 1
  45. return None
  46. def test_rescale_op(plot=False):
  47. """
  48. Test rescale
  49. """
  50. logger.info("Test rescale")
  51. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  52. # define map operations
  53. decode_op = vision.Decode()
  54. rescale_op = vision.Rescale(1.0 / 255.0, -1.0)
  55. # apply map operations on images
  56. data1 = data1.map(input_columns=["image"], operations=decode_op)
  57. data2 = data1.map(input_columns=["image"], operations=rescale_op)
  58. num_iter = 0
  59. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  60. image_original = item1["image"]
  61. image_de_rescaled = item2["image"]
  62. image_np_rescaled = get_rescaled(num_iter)
  63. mse = diff_mse(image_de_rescaled, image_np_rescaled)
  64. assert mse < 0.001 # rounding error
  65. logger.info("image_{}, mse: {}".format(num_iter + 1, mse))
  66. num_iter += 1
  67. if plot:
  68. visualize_image(image_original, image_de_rescaled, mse, image_np_rescaled)
  69. def test_rescale_md5():
  70. """
  71. Test Rescale with md5 comparison
  72. """
  73. logger.info("Test Rescale with md5 comparison")
  74. # generate dataset
  75. data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  76. decode_op = vision.Decode()
  77. rescale_op = vision.Rescale(1.0 / 255.0, -1.0)
  78. # apply map operations on images
  79. data = data.map(input_columns=["image"], operations=decode_op)
  80. data = data.map(input_columns=["image"], operations=rescale_op)
  81. # check results with md5 comparison
  82. filename = "rescale_01_result.npz"
  83. save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN)
  84. if __name__ == "__main__":
  85. test_rescale_op(plot=True)
  86. test_rescale_md5()