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_c_compose.py 2.2 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 mindspore.common.dtype as mstype
  16. import mindspore.dataset as ds
  17. import mindspore.dataset.transforms.c_transforms as ops
  18. import mindspore.dataset.transforms.py_transforms as py_ops
  19. def test_compose():
  20. ds.config.set_seed(0)
  21. def test_config(arr, op_list):
  22. try:
  23. data = ds.NumpySlicesDataset(arr, column_names="col", shuffle=False)
  24. data = data.map(operations=ops.Compose(op_list), input_columns=["col"])
  25. res = []
  26. for i in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  27. res.append(i["col"].tolist())
  28. return res
  29. except (TypeError, ValueError) as e:
  30. return str(e)
  31. # test simple compose with only 1 op, this would generate a warning
  32. assert test_config([[1, 0], [3, 4]], [ops.Fill(2)]) == [[2, 2], [2, 2]]
  33. # test 1 column -> 2columns -> 1 -> 2 -> 1
  34. assert test_config([[1, 0]], [ops.Duplicate(), ops.Concatenate(), ops.Duplicate(), ops.Concatenate()]) == [
  35. [1, 0] * 4]
  36. # test one python transform followed by a C transform. type after oneHot is float (mixed use-case)
  37. assert test_config([1, 0], [py_ops.OneHotOp(2), ops.TypeCast(mstype.int32)]) == [[[0, 1]], [[1, 0]]]
  38. # test exceptions. compose, randomApply randomChoice use the same validator
  39. assert "op_list[0] is neither a c_transform op" in test_config([1, 0], [1, ops.TypeCast(mstype.int32)])
  40. # test empty op list
  41. assert "op_list can not be empty." in test_config([1, 0], [])
  42. if __name__ == "__main__":
  43. test_compose()