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

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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
  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. def rescale_np(image):
  25. """
  26. Apply the rescale
  27. """
  28. image = image / 255.0
  29. image = image - 1.0
  30. return image
  31. def get_rescaled(image_id):
  32. """
  33. Reads the image using DE ops and then rescales using Numpy
  34. """
  35. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  36. decode_op = vision.Decode()
  37. data1 = data1.map(input_columns=["image"], operations=decode_op)
  38. num_iter = 0
  39. for item in data1.create_dict_iterator():
  40. image = item["image"]
  41. if num_iter == image_id:
  42. return rescale_np(image)
  43. num_iter += 1
  44. return None
  45. def test_rescale_op(plot=False):
  46. """
  47. Test rescale
  48. """
  49. logger.info("Test rescale")
  50. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  51. # define map operations
  52. decode_op = vision.Decode()
  53. rescale_op = vision.Rescale(1.0 / 255.0, -1.0)
  54. # apply map operations on images
  55. data1 = data1.map(input_columns=["image"], operations=decode_op)
  56. data2 = data1.map(input_columns=["image"], operations=rescale_op)
  57. num_iter = 0
  58. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  59. image_original = item1["image"]
  60. image_de_rescaled = item2["image"]
  61. image_np_rescaled = get_rescaled(num_iter)
  62. mse = diff_mse(image_de_rescaled, image_np_rescaled)
  63. logger.info("image_{}, mse: {}".format(num_iter + 1, mse))
  64. num_iter += 1
  65. if plot:
  66. visualize_image(image_original, image_de_rescaled, mse, image_np_rescaled)
  67. if __name__ == "__main__":
  68. test_rescale_op(plot=True)