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_compile.py 2.8 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. """
  16. @File : test_compile.py
  17. @Author:
  18. @Date : 2019-03-20
  19. @Desc : test mindspore compile method
  20. """
  21. import logging
  22. import numpy as np
  23. import mindspore.nn as nn
  24. from mindspore import Tensor, Model, context
  25. from mindspore.nn.optim import Momentum
  26. from mindspore.ops.composite import add_flags
  27. from ...ut_filter import non_graph_engine
  28. log = logging.getLogger("test")
  29. log.setLevel(level=logging.ERROR)
  30. class Net(nn.Cell):
  31. """ Net definition """
  32. def __init__(self):
  33. super(Net, self).__init__()
  34. self.conv = nn.Conv2d(3, 64, 3, has_bias=False, weight_init='normal')
  35. self.relu = nn.ReLU()
  36. self.flatten = nn.Flatten()
  37. def construct(self, x):
  38. x = self.conv(x)
  39. x = self.relu(x)
  40. out = self.flatten(x)
  41. return out
  42. loss = nn.MSELoss()
  43. # Test case 1 : test the new compiler interface
  44. # _build_train_graph is deprecated
  45. def test_build():
  46. """ test_build """
  47. Tensor(np.random.randint(0, 255, [1, 3, 224, 224]))
  48. Tensor(np.random.randint(0, 10, [1, 10]))
  49. net = Net()
  50. opt = Momentum(net.get_parameters(), learning_rate=0.1, momentum=0.9)
  51. Model(net, loss_fn=loss, optimizer=opt, metrics=None)
  52. # Test case 2 : test the use different args to run graph
  53. class Net2(nn.Cell):
  54. """ Net2 definition """
  55. def __init__(self):
  56. super(Net2, self).__init__()
  57. self.relu = nn.ReLU()
  58. def construct(self, x):
  59. x = self.relu(x)
  60. return x
  61. @non_graph_engine
  62. def test_different_args_run():
  63. """ test_different_args_run """
  64. np1 = np.random.randn(2, 3, 4, 5).astype(np.float32)
  65. input_me1 = Tensor(np1)
  66. np2 = np.random.randn(2, 3, 4, 5).astype(np.float32)
  67. input_me2 = Tensor(np2)
  68. net = Net2()
  69. net = add_flags(net, predit=True)
  70. context.set_context(mode=context.GRAPH_MODE)
  71. model = Model(net)
  72. me1 = model.predict(input_me1)
  73. me2 = model.predict(input_me2)
  74. out_me1 = me1.asnumpy()
  75. out_me2 = me2.asnumpy()
  76. print(np1)
  77. print(np2)
  78. print(out_me1)
  79. print(out_me2)
  80. assert not np.allclose(out_me1, out_me2, 0.01, 0.01)