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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 layer switch"""
  16. import numpy as np
  17. import mindspore
  18. from mindspore import nn
  19. from mindspore import Tensor
  20. from mindspore import context
  21. from mindspore.ops import operations as P
  22. context.set_context(mode=context.GRAPH_MODE)
  23. class Layer1(nn.Cell):
  24. def __init__(self):
  25. super(Layer1, self).__init__()
  26. self.net = nn.Conv2d(3, 1, 3, pad_mode='same')
  27. self.pad = nn.Pad(
  28. paddings=((0, 0), (0, 2), (0, 0), (0, 0)), mode="CONSTANT")
  29. def construct(self, x):
  30. y = self.net(x)
  31. return self.pad(y)
  32. class Layer2(nn.Cell):
  33. def __init__(self):
  34. super(Layer2, self).__init__()
  35. self.net = nn.Conv2d(3, 1, 7, pad_mode='same')
  36. self.pad = nn.Pad(
  37. paddings=((0, 0), (0, 2), (0, 0), (0, 0)), mode="CONSTANT")
  38. def construct(self, x):
  39. y = self.net(x)
  40. return self.pad(y)
  41. class Layer3(nn.Cell):
  42. def __init__(self):
  43. super(Layer3, self).__init__()
  44. self.net = nn.Conv2d(3, 3, 3, pad_mode='same')
  45. def construct(self, x):
  46. return self.net(x)
  47. class SwitchNet(nn.Cell):
  48. def __init__(self):
  49. super(SwitchNet, self).__init__()
  50. self.layer1 = Layer1()
  51. self.layer2 = Layer2()
  52. self.layer3 = Layer3()
  53. self.layers = (self.layer1, self.layer2, self.layer3)
  54. self.fill = P.Fill()
  55. def construct(self, x, index):
  56. y = self.layers[index](x)
  57. return y
  58. class MySwitchNet(nn.Cell):
  59. def __init__(self):
  60. super(MySwitchNet, self).__init__()
  61. self.layer1 = Layer1()
  62. self.layer2 = Layer2()
  63. self.layer3 = Layer3()
  64. self.layers = (self.layer1, self.layer2, self.layer3)
  65. self.fill = P.Fill()
  66. def construct(self, x, index):
  67. y = self.layers[0](x)
  68. for i in range(len(self.layers)):
  69. if i == index:
  70. y = self.layers[i](x)
  71. return y
  72. def test_layer_switch():
  73. net = MySwitchNet()
  74. x = Tensor(np.ones((3, 3, 24, 24)), mindspore.float32)
  75. index = Tensor(0, dtype=mindspore.int32)
  76. net(x, index)