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_net_infer.py 3.9 kB

6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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_net_infer """
  16. import numpy as np
  17. import mindspore.nn as nn
  18. from mindspore import Tensor, context
  19. from mindspore.common.parameter import Parameter
  20. from mindspore.common.initializer import initializer
  21. import mindspore.ops.operations as op
  22. def test_net_infer():
  23. """ test_net_infer """
  24. class Net(nn.Cell):
  25. """ Net definition """
  26. def __init__(self):
  27. super(Net, self).__init__()
  28. self.conv = nn.Conv2d(3, 64, 3, has_bias=False, weight_init='normal')
  29. self.bn = nn.BatchNorm2d(64)
  30. self.fc = nn.Dense(64, 10)
  31. self.relu = nn.ReLU()
  32. self.flatten = nn.Flatten()
  33. def construct(self, x):
  34. x = self.conv(x)
  35. x = self.relu(x)
  36. x = self.flatten(x)
  37. out = self.fc(x)
  38. return out
  39. Tensor(np.random.randint(0, 255, [1, 3, 224, 224]))
  40. Net()
  41. def test_assign_in_while():
  42. context.set_context(device_target="Ascend")
  43. context.set_context(mode=context.GRAPH_MODE)
  44. class Net(nn.Cell):
  45. def __init__(self, input_shape):
  46. super().__init__()
  47. self.assign = op.Assign()
  48. self.inputdata = Parameter(initializer(1, input_shape), name="global_step")
  49. def construct(self, x, y, z):
  50. out = z
  51. while x < y:
  52. inputdata = self.inputdata
  53. x = x + 1
  54. out = self.assign(inputdata, z)
  55. return out
  56. x = Tensor(np.array(1).astype(np.int32))
  57. y = Tensor(np.array(3).astype(np.int32))
  58. input_shape = (1024, 512)
  59. z = Tensor(np.random.randn(*input_shape).astype(np.float32))
  60. net = Net(input_shape)
  61. net(x, y, z)
  62. def test_dup_context():
  63. ''' different func_with_fv in net1 and net2 should produce 2 different FuncGraphAbstractClosure and
  64. Evaluator.
  65. '''
  66. context.set_context(mode=context.GRAPH_MODE)
  67. class Net(nn.Cell):
  68. def __init__(self):
  69. super().__init__()
  70. def construct(self, x):
  71. def identity(f):
  72. return f
  73. def func_with_fv():
  74. return x
  75. def net1():
  76. local_func = identity(func_with_fv)
  77. out = local_func() + 20.0
  78. return out
  79. def net2():
  80. local_func = identity(func_with_fv)
  81. out = local_func() + 15.0
  82. return out
  83. return net1() + net2()
  84. Net()(5.0)
  85. def test_maybe_poly_func():
  86. ''' different func_with_fv in net1 and net2 may produce poly node. '''
  87. context.set_context(mode=context.GRAPH_MODE)
  88. class Net(nn.Cell):
  89. def __init__(self):
  90. super().__init__()
  91. def construct(self, x, y, z):
  92. def identity(f, inp):
  93. return f(inp)
  94. def func_with_fv(yy):
  95. return (x, yy)
  96. def make_call():
  97. out1 = identity(func_with_fv, y)
  98. out2 = identity(func_with_fv, z)
  99. return (out1, out2)
  100. return make_call()
  101. y_input = Tensor(np.array([1, 2]).astype(np.int32))
  102. z_input = Tensor(np.array([[2, 2], [3, 3]]).astype(np.int32))
  103. Net()(1, y_input, z_input)