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