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_transform.py 5.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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_adapter.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, Parameter, Model
  25. from mindspore.ops import operations as P
  26. from ..ut_filter import non_graph_engine
  27. log = logging.getLogger("test")
  28. log.setLevel(level=logging.ERROR)
  29. def conv3x3(in_channels, out_channels, stride=1, padding=1):
  30. """3x3 convolution """
  31. weight = Tensor(np.ones([out_channels, in_channels, 3, 3]).astype(np.float32))
  32. return nn.Conv2d(in_channels, out_channels,
  33. kernel_size=3, stride=stride,
  34. padding=padding, weight_init=weight)
  35. def conv1x1(in_channels, out_channels, stride=1, padding=0):
  36. """1x1 convolution"""
  37. weight = Tensor(np.ones([out_channels, in_channels, 1, 1]).astype(np.float32))
  38. return nn.Conv2d(in_channels, out_channels,
  39. kernel_size=1, stride=stride,
  40. padding=padding, weight_init=weight)
  41. class ResidualBlock(nn.Cell):
  42. """
  43. residual Block
  44. """
  45. expansion = 4
  46. def __init__(self,
  47. in_channels,
  48. out_channels,
  49. stride=1,
  50. down_sample=False):
  51. super(ResidualBlock, self).__init__()
  52. out_chls = out_channels // self.expansion
  53. self.conv1 = conv1x1(in_channels, out_chls, stride=1, padding=0)
  54. self.bn1 = nn.BatchNorm2d(out_chls)
  55. self.conv2 = conv3x3(out_chls, out_chls, stride=stride, padding=1)
  56. self.bn2 = nn.BatchNorm2d(out_chls)
  57. self.conv3 = conv1x1(out_chls, out_channels, stride=1, padding=0)
  58. self.bn3 = nn.BatchNorm2d(out_channels)
  59. self.relu = nn.ReLU()
  60. self.downsample = down_sample
  61. if self.downsample:
  62. self.conv_down_sample = conv1x1(in_channels, out_channels,
  63. stride=stride, padding=0)
  64. self.bn_down_sample = nn.BatchNorm2d(out_channels)
  65. self.add = P.TensorAdd()
  66. def construct(self, x):
  67. identity = x
  68. out = self.conv1(x)
  69. out = self.bn1(out)
  70. out = self.relu(out)
  71. out = self.conv2(out)
  72. out = self.bn2(out)
  73. out = self.relu(out)
  74. out = self.conv3(out)
  75. out = self.bn3(out)
  76. if self.downsample:
  77. identity = self.conv_down_sample(identity)
  78. identity = self.bn_down_sample(identity)
  79. out = self.add(out, identity)
  80. out = self.relu(out)
  81. return out
  82. class ResNet(nn.Cell):
  83. """ ResNet definition """
  84. def __init__(self, tensor):
  85. super(ResNet, self).__init__()
  86. self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3)
  87. self.bn1 = nn.BatchNorm2d(64)
  88. self.weight = Parameter(tensor, name='w')
  89. def construct(self, x):
  90. x = self.conv1(x)
  91. x = self.bn1(x)
  92. return x
  93. class LeNet(nn.Cell):
  94. """ LeNet definition """
  95. def __init__(self):
  96. super(LeNet, self).__init__()
  97. self.relu = nn.ReLU()
  98. weight1 = Tensor(np.ones([6, 1, 5, 5]).astype(np.float32) * 0.01)
  99. weight2 = Tensor(np.ones([16, 6, 5, 5]).astype(np.float32) * 0.01)
  100. self.conv1 = nn.Conv2d(1, 6, (5, 5), weight_init=weight1, stride=1, padding=0, pad_mode='valid')
  101. self.conv2 = nn.Conv2d(6, 16, (5, 5), weight_init=weight2, pad_mode='valid')
  102. self.pool = nn.MaxPool2d(2)
  103. self.flatten = nn.Flatten()
  104. fcweight1 = Tensor(np.ones([120, 16 * 5 * 5]).astype(np.float32) * 0.01)
  105. fcweight2 = Tensor(np.ones([84, 120]).astype(np.float32) * 0.01)
  106. fcweight3 = Tensor(np.ones([10, 84]).astype(np.float32) * 0.01)
  107. self.fc1 = nn.Dense(16 * 5 * 5, 120, weight_init=fcweight1)
  108. self.fc2 = nn.Dense(120, 84, weight_init=fcweight2)
  109. self.fc3 = nn.Dense(84, 10, weight_init=fcweight3)
  110. def construct(self, input_x):
  111. output = self.conv1(input_x)
  112. output = self.relu(output)
  113. output = self.pool(output)
  114. output = self.conv2(output)
  115. output = self.relu(output)
  116. output = self.pool(output)
  117. output = self.flatten(output)
  118. output = self.fc1(output)
  119. output = self.fc2(output)
  120. output = self.fc3(output)
  121. return output
  122. def loss_func(x):
  123. return x
  124. def optimizer(x):
  125. return x
  126. class Net(nn.Cell):
  127. """ Net definition """
  128. def __init__(self, dim):
  129. super(Net, self).__init__()
  130. self.softmax = nn.Softmax(dim)
  131. def construct(self, input_x):
  132. return self.softmax(input_x)