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_to_pil.py 2.7 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # Copyright 2020 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 ToPIL op in DE
  17. """
  18. import mindspore.dataset as ds
  19. import mindspore.dataset.transforms.vision.c_transforms as c_vision
  20. import mindspore.dataset.transforms.vision.py_transforms as py_vision
  21. from mindspore import log as logger
  22. from util import save_and_check_md5
  23. GENERATE_GOLDEN = False
  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_to_pil_01():
  27. """
  28. Test ToPIL Op with md5 comparison: input is already PIL image
  29. Expected to pass
  30. """
  31. logger.info("test_to_pil_01")
  32. # Generate dataset
  33. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  34. transforms = [
  35. py_vision.Decode(),
  36. # If input is already PIL image.
  37. py_vision.ToPIL(),
  38. py_vision.CenterCrop(375),
  39. py_vision.ToTensor()
  40. ]
  41. transform = py_vision.ComposeOp(transforms)
  42. data1 = data1.map(input_columns=["image"], operations=transform())
  43. # Compare with expected md5 from images
  44. filename = "to_pil_01_result.npz"
  45. save_and_check_md5(data1, filename, generate_golden=GENERATE_GOLDEN)
  46. def test_to_pil_02():
  47. """
  48. Test ToPIL Op with md5 comparison: input is not PIL image
  49. Expected to pass
  50. """
  51. logger.info("test_to_pil_02")
  52. # Generate dataset
  53. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  54. decode_op = c_vision.Decode()
  55. transforms = [
  56. # If input type is not PIL.
  57. py_vision.ToPIL(),
  58. py_vision.CenterCrop(375),
  59. py_vision.ToTensor()
  60. ]
  61. transform = py_vision.ComposeOp(transforms)
  62. data1 = data1.map(input_columns=["image"], operations=decode_op)
  63. data1 = data1.map(input_columns=["image"], operations=transform())
  64. # Compare with expected md5 from images
  65. filename = "to_pil_02_result.npz"
  66. save_and_check_md5(data1, filename, generate_golden=GENERATE_GOLDEN)
  67. if __name__ == "__main__":
  68. test_to_pil_01()
  69. test_to_pil_02()