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_switch_layer_mindir.py 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. import os
  16. import numpy as np
  17. import pytest
  18. import mindspore.context as context
  19. from mindspore import Tensor, nn
  20. from mindspore.common import dtype as mstype
  21. from mindspore.train.serialization import export, load
  22. class CaseNet(nn.Cell):
  23. def __init__(self):
  24. super(CaseNet, self).__init__()
  25. self.conv = nn.Conv2d(1, 1, 3)
  26. self.relu = nn.ReLU()
  27. self.relu1 = nn.ReLU()
  28. self.softmax = nn.Softmax()
  29. self.layers1 = (self.relu, self.softmax)
  30. self.layers2 = (self.conv, self.relu1)
  31. def construct(self, x, index1, index2):
  32. x = self.layers1[index1](x)
  33. x = self.layers2[index2](x)
  34. return x
  35. @pytest.mark.level0
  36. @pytest.mark.platform_x86_ascend_training
  37. @pytest.mark.platform_arm_ascend_training
  38. @pytest.mark.env_onecard
  39. def test_mindir_switch_layer():
  40. context.set_context(mode=context.GRAPH_MODE)
  41. net = CaseNet()
  42. data = Tensor(np.ones((1, 1, 224, 224)), mstype.float32)
  43. idx = Tensor(0, mstype.int32)
  44. idx2 = Tensor(-1, mstype.int32)
  45. file_name = "switch_layer_net"
  46. mindir_name = file_name + ".mindir"
  47. export(net, data, idx, idx2, file_name=file_name, file_format='MINDIR')
  48. assert os.path.exists(mindir_name)
  49. graph = load(mindir_name)
  50. loaded_net = nn.GraphCell(graph)
  51. outputs_after_load = loaded_net(data, idx, idx2)
  52. relu = nn.ReLU()
  53. true_value = relu(data)
  54. ret = np.allclose(outputs_after_load.asnumpy(), true_value.asnumpy())
  55. assert ret
  56. @pytest.mark.skip(reason="depend on export")
  57. @pytest.mark.level0
  58. @pytest.mark.platform_x86_ascend_training
  59. @pytest.mark.platform_arm_ascend_training
  60. @pytest.mark.env_onecard
  61. def test_mindir_export():
  62. context.set_context(mode=context.GRAPH_MODE)
  63. net = CaseNet()
  64. data = Tensor(np.ones((1, 1, 224, 224)), mstype.float32)
  65. idx = Tensor(0, mstype.int32)
  66. idx2 = Tensor(-1, mstype.int32)
  67. file_name = "switch_layer_net"
  68. mindir_name = file_name + ".mindir"
  69. export(net, data, idx, idx2, file_name=file_name, file_format='MINDIR')
  70. assert os.path.exists(mindir_name)
  71. @pytest.mark.skip(reason="depend on export")
  72. @pytest.mark.level0
  73. @pytest.mark.platform_x86_ascend_training
  74. @pytest.mark.platform_arm_ascend_training
  75. @pytest.mark.env_onecard
  76. def test_mindir_load():
  77. context.set_context(mode=context.GRAPH_MODE)
  78. data = Tensor(np.ones((1, 1, 224, 224)), mstype.float32)
  79. idx = Tensor(0, mstype.int32)
  80. idx2 = Tensor(-1, mstype.int32)
  81. file_name = "switch_layer_net"
  82. mindir_name = file_name + ".mindir"
  83. graph = load(mindir_name)
  84. loaded_net = nn.GraphCell(graph)
  85. outputs_after_load = loaded_net(data, idx, idx2)
  86. relu = nn.ReLU()
  87. true_value = relu(data)
  88. ret = np.allclose(outputs_after_load.asnumpy(), true_value.asnumpy())
  89. assert ret