• 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""" test_checkparam """
16import numpy as np
17import pytest
18
19import mindspore
20import mindspore.nn as nn
21from mindspore import Model, context
22from mindspore.common.tensor import Tensor
23
24
25class LeNet5(nn.Cell):
26    """ LeNet5 definition """
27
28    def __init__(self):
29        super(LeNet5, self).__init__()
30        self.conv1 = nn.Conv2d(3, 6, 5, pad_mode="valid")
31        self.conv2 = nn.Conv2d(6, 16, 5, pad_mode="valid")
32        self.fc1 = nn.Dense(16 * 5 * 5, 120)
33        self.fc2 = nn.Dense(120, 84)
34        self.fc3 = nn.Dense(84, 3)
35        self.relu = nn.ReLU()
36        self.max_pool2d = nn.MaxPool2d(kernel_size=2)
37        self.flatten = nn.Flatten()
38
39    def construct(self, x):
40        x = self.max_pool2d(self.relu(self.conv1(x)))
41        x = self.max_pool2d(self.relu(self.conv2(x)))
42        x = self.flatten(x)
43        x = self.relu(self.fc1(x))
44        x = self.relu(self.fc2(x))
45        x = self.fc3(x)
46        return x
47
48
49def predict_checke_param(in_str):
50    """ predict_checke_param """
51    net = LeNet5()  # neural network
52    context.set_context(mode=context.GRAPH_MODE)
53    model = Model(net)
54
55    a1, a2, b1, b2, b3, b4 = in_str.strip().split()
56    a1 = int(a1)
57    a2 = int(a2)
58    b1 = int(b1)
59    b2 = int(b2)
60    b3 = int(b3)
61    b4 = int(b4)
62
63    nd_data = np.random.randint(a1, a2, [b1, b2, b3, b4])
64    input_data = Tensor(nd_data, mindspore.float32)
65    model.predict(input_data)
66
67
68def test_predict_checke_param_failed():
69    """ test_predict_checke_param_failed """
70    in_str = "0 255 0 3 32 32"
71    with pytest.raises(ValueError):
72        predict_checke_param(in_str)
73