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