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_map.py 5.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # Copyright 2022 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. import pytest
  16. import mindspore.dataset as ds
  17. from mindspore.dataset.transforms import c_transforms
  18. from mindspore.dataset.transforms import py_transforms
  19. import mindspore.dataset.vision.c_transforms as c_vision
  20. import mindspore.dataset.vision.py_transforms as py_vision
  21. DATA_DIR = "../data/dataset/testPK/data"
  22. def test_map_c_transform_exception():
  23. """
  24. Feature: test c error op def
  25. Description: op defined like c_vision.HWC2CHW
  26. Expectation: success
  27. """
  28. data_set = ds.ImageFolderDataset(DATA_DIR, num_parallel_workers=1, shuffle=True)
  29. train_image_size = 224
  30. mean = [0.485 * 255, 0.456 * 255, 0.406 * 255]
  31. std = [0.229 * 255, 0.224 * 255, 0.225 * 255]
  32. # define map operations
  33. random_crop_decode_resize_op = c_vision.RandomCropDecodeResize(train_image_size,
  34. scale=(0.08, 1.0),
  35. ratio=(0.75, 1.333))
  36. random_horizontal_flip_op = c_vision.RandomHorizontalFlip(prob=0.5)
  37. normalize_op = c_vision.Normalize(mean=mean, std=std)
  38. hwc2chw_op = c_vision.HWC2CHW # exception
  39. data_set = data_set.map(operations=random_crop_decode_resize_op, input_columns="image", num_parallel_workers=1)
  40. data_set = data_set.map(operations=random_horizontal_flip_op, input_columns="image", num_parallel_workers=1)
  41. data_set = data_set.map(operations=normalize_op, input_columns="image", num_parallel_workers=1)
  42. with pytest.raises(ValueError) as info:
  43. data_set = data_set.map(operations=hwc2chw_op, input_columns="image", num_parallel_workers=1)
  44. assert "Parameter operations's element of method map should be a " in str(info.value)
  45. # compose exception
  46. with pytest.raises(ValueError) as info:
  47. c_transforms.Compose([
  48. c_vision.RandomCropDecodeResize(train_image_size, scale=(0.08, 1.0), ratio=(0.75, 1.333)),
  49. c_vision.RandomHorizontalFlip,
  50. c_vision.Normalize(mean=mean, std=std),
  51. c_vision.HWC2CHW()])
  52. assert " should be a " in str(info.value)
  53. # randomapply exception
  54. with pytest.raises(ValueError) as info:
  55. c_transforms.RandomApply([
  56. c_vision.RandomCropDecodeResize,
  57. c_vision.RandomHorizontalFlip(prob=0.5),
  58. c_vision.Normalize(mean=mean, std=std),
  59. c_vision.HWC2CHW()])
  60. assert " should be a " in str(info.value)
  61. # randomchoice exception
  62. with pytest.raises(ValueError) as info:
  63. c_transforms.RandomChoice([
  64. c_vision.RandomCropDecodeResize(train_image_size, scale=(0.08, 1.0), ratio=(0.75, 1.333)),
  65. c_vision.RandomHorizontalFlip(prob=0.5),
  66. c_vision.Normalize,
  67. c_vision.HWC2CHW()])
  68. assert " should be a " in str(info.value)
  69. def test_map_py_transform_exception():
  70. """
  71. Feature: test python error op def
  72. Description: op defined like py_vision.RandomHorizontalFlip
  73. Expectation: success
  74. """
  75. data_set = ds.ImageFolderDataset(DATA_DIR, num_parallel_workers=1, shuffle=True)
  76. # define map operations
  77. decode_op = py_vision.Decode()
  78. random_horizontal_flip_op = py_vision.RandomHorizontalFlip # exception
  79. to_tensor_op = py_vision.ToTensor()
  80. trans = [decode_op, random_horizontal_flip_op, to_tensor_op]
  81. with pytest.raises(ValueError) as info:
  82. data_set = data_set.map(operations=trans, input_columns="image", num_parallel_workers=1)
  83. assert "Parameter operations's element of method map should be a " in str(info.value)
  84. # compose exception
  85. with pytest.raises(ValueError) as info:
  86. py_transforms.Compose([
  87. py_vision.Decode,
  88. py_vision.RandomHorizontalFlip(),
  89. py_vision.ToTensor()])
  90. assert " should be a " in str(info.value)
  91. # randomapply exception
  92. with pytest.raises(ValueError) as info:
  93. py_transforms.RandomApply([
  94. py_vision.Decode(),
  95. py_vision.RandomHorizontalFlip,
  96. py_vision.ToTensor()])
  97. assert " should be a " in str(info.value)
  98. # randomchoice exception
  99. with pytest.raises(ValueError) as info:
  100. py_transforms.RandomChoice([
  101. py_vision.Decode(),
  102. py_vision.RandomHorizontalFlip(),
  103. py_vision.ToTensor])
  104. assert " should be a " in str(info.value)
  105. if __name__ == '__main__':
  106. test_map_c_transform_exception()
  107. test_map_py_transform_exception()