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