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.8 kB

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