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_compose.py 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. import pytest
  16. import mindspore.common.dtype as mstype
  17. import mindspore.dataset as ds
  18. import mindspore.dataset.transforms.c_transforms as ops
  19. import mindspore.dataset.transforms.py_transforms as py_ops
  20. def test_compose():
  21. """
  22. Test C++ and Python Compose Op
  23. """
  24. ds.config.set_seed(0)
  25. def test_config(arr, op_list):
  26. try:
  27. data = ds.NumpySlicesDataset(arr, column_names="col", shuffle=False)
  28. data = data.map(input_columns=["col"], operations=op_list)
  29. res = []
  30. for i in data.create_dict_iterator(output_numpy=True):
  31. res.append(i["col"].tolist())
  32. return res
  33. except (TypeError, ValueError) as e:
  34. return str(e)
  35. # Test simple compose with only 1 op, this would generate a warning
  36. assert test_config([[1, 0], [3, 4]], ops.Compose([ops.Fill(2)])) == [[2, 2], [2, 2]]
  37. # Test 1 column -> 2 columns -> 1 -> 2 -> 1
  38. assert test_config([[1, 0]],
  39. ops.Compose([ops.Duplicate(), ops.Concatenate(), ops.Duplicate(), ops.Concatenate()])) \
  40. == [[1, 0] * 4]
  41. # Test one Python transform followed by a C transform. Type after OneHot is a float (mixed use-case)
  42. assert test_config([1, 0], ops.Compose([py_ops.OneHotOp(2), ops.TypeCast(mstype.int32)])) == [[[0, 1]], [[1, 0]]]
  43. # Test exceptions.
  44. with pytest.raises(TypeError) as error_info:
  45. ops.Compose([1, ops.TypeCast(mstype.int32)])
  46. assert "op_list[0] is not a c_transform op (TensorOp) nor a callable pyfunc." in str(error_info.value)
  47. # Test empty op list
  48. with pytest.raises(ValueError) as error_info:
  49. test_config([1, 0], ops.Compose([]))
  50. assert "op_list can not be empty." in str(error_info.value)
  51. # Test Python compose op
  52. assert test_config([1, 0], py_ops.Compose([py_ops.OneHotOp(2)])) == [[[0, 1]], [[1, 0]]]
  53. assert test_config([1, 0], py_ops.Compose([py_ops.OneHotOp(2), (lambda x: x + x)])) == [[[0, 2]], [[2, 0]]]
  54. # Test nested Python compose op
  55. assert test_config([1, 0],
  56. py_ops.Compose([py_ops.Compose([py_ops.OneHotOp(2)]), (lambda x: x + x)])) \
  57. == [[[0, 2]], [[2, 0]]]
  58. with pytest.raises(TypeError) as error_info:
  59. py_ops.Compose([(lambda x: x + x)])()
  60. assert "Compose was called without an image. Fix invocation (avoid it being invoked as Compose([...])())." in str(
  61. error_info.value)
  62. if __name__ == "__main__":
  63. test_compose()