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_random_select_subpolicy.py 2.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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.dataset as ds
  16. import mindspore.dataset.transforms.c_transforms as ops
  17. import mindspore.dataset.transforms.vision.c_transforms as visions
  18. def test_random_select_subpolicy():
  19. ds.config.set_seed(0)
  20. def test_config(arr, policy):
  21. try:
  22. data = ds.NumpySlicesDataset(arr, column_names="col", shuffle=False)
  23. data = data.map(input_columns=["col"], operations=visions.RandomSelectSubpolicy(policy))
  24. res = []
  25. for i in data.create_dict_iterator():
  26. res.append(i["col"].tolist())
  27. return res
  28. except (TypeError, ValueError) as e:
  29. return str(e)
  30. # 3 possible outcomes
  31. policy1 = [[(ops.PadEnd([4], 0), 0.5), (ops.Compose([ops.Duplicate(), ops.Concatenate()]), 1)],
  32. [(ops.Slice([0, 1]), 0.5), (ops.Duplicate(), 1), (ops.Concatenate(), 1)]]
  33. res1 = test_config([[1, 2, 3]], policy1)
  34. assert res1 in [[[1, 2, 1, 2]], [[1, 2, 3, 1, 2, 3]], [[1, 2, 3, 0, 1, 2, 3, 0]]]
  35. # test exceptions
  36. assert "policy can not be empty." in test_config([[1, 2, 3]], [])
  37. assert "policy[0] can not be empty." in test_config([[1, 2, 3]], [[]])
  38. assert "op of (op, prob) in policy[1][0] is not a c_transform op (TensorOp) nor a callable pyfunc" in test_config(
  39. [[1, 2, 3]], [[(ops.PadEnd([4], 0), 0.5)], [(1, 0.4)]])
  40. assert "prob of (op, prob) policy[1][0] is not within the required interval of (0 to 1)" in test_config([[1]], [
  41. [(ops.Duplicate(), 0)], [(ops.Duplicate(), -0.1)]])
  42. if __name__ == "__main__":
  43. test_random_select_subpolicy()