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.

resnet.py 9.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. # Copyright 2019 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 numpy as np
  16. import mindspore.nn as nn
  17. from mindspore import Tensor
  18. from mindspore.ops import operations as P
  19. from mindspore.common.initializer import initializer
  20. from mindspore.common import dtype as mstype
  21. def weight_variable(shape):
  22. return initializer('XavierUniform', shape=shape, dtype=mstype.float32)
  23. def weight_variable_uniform(shape):
  24. return initializer('Uniform', shape=shape, dtype=mstype.float32)
  25. def weight_variable_0(shape):
  26. zeros = np.zeros(shape).astype(np.float32)
  27. return Tensor(zeros)
  28. def weight_variable_1(shape):
  29. ones = np.ones(shape).astype(np.float32)
  30. return Tensor(ones)
  31. def conv3x3(in_channels, out_channels, stride=1, padding=0):
  32. """3x3 convolution """
  33. weight_shape = (out_channels, in_channels, 3, 3)
  34. weight = weight_variable(weight_shape)
  35. return nn.Conv2d(in_channels, out_channels,
  36. kernel_size=3, stride=stride, padding=padding, weight_init=weight, has_bias=False, pad_mode="same")
  37. def conv1x1(in_channels, out_channels, stride=1, padding=0):
  38. """1x1 convolution"""
  39. weight_shape = (out_channels, in_channels, 1, 1)
  40. weight = weight_variable(weight_shape)
  41. return nn.Conv2d(in_channels, out_channels,
  42. kernel_size=1, stride=stride, padding=padding, weight_init=weight, has_bias=False, pad_mode="same")
  43. def conv7x7(in_channels, out_channels, stride=1, padding=0):
  44. """1x1 convolution"""
  45. weight_shape = (out_channels, in_channels, 7, 7)
  46. weight = weight_variable(weight_shape)
  47. return nn.Conv2d(in_channels, out_channels,
  48. kernel_size=7, stride=stride, padding=padding, weight_init=weight, has_bias=False, pad_mode="same")
  49. def bn_with_initialize(out_channels):
  50. shape = (out_channels)
  51. mean = weight_variable_0(shape)
  52. var = weight_variable_1(shape)
  53. beta = weight_variable_0(shape)
  54. gamma = weight_variable_uniform(shape)
  55. bn = nn.BatchNorm2d(out_channels, momentum=0.99, eps=0.00001, gamma_init=gamma,
  56. beta_init=beta, moving_mean_init=mean, moving_var_init=var)
  57. return bn
  58. def bn_with_initialize_last(out_channels):
  59. shape = (out_channels)
  60. mean = weight_variable_0(shape)
  61. var = weight_variable_1(shape)
  62. beta = weight_variable_0(shape)
  63. gamma = weight_variable_uniform(shape)
  64. bn = nn.BatchNorm2d(out_channels, momentum=0.99, eps=0.00001, gamma_init=gamma,
  65. beta_init=beta, moving_mean_init=mean, moving_var_init=var)
  66. return bn
  67. def fc_with_initialize(input_channels, out_channels):
  68. weight_shape = (out_channels, input_channels)
  69. weight = weight_variable(weight_shape)
  70. bias_shape = (out_channels)
  71. bias = weight_variable_uniform(bias_shape)
  72. return nn.Dense(input_channels, out_channels, weight, bias)
  73. class ResidualBlock(nn.Cell):
  74. expansion = 4
  75. def __init__(self,
  76. in_channels,
  77. out_channels,
  78. stride=1,
  79. down_sample=False):
  80. super(ResidualBlock, self).__init__()
  81. out_chls = out_channels // self.expansion
  82. self.conv1 = conv1x1(in_channels, out_chls, stride=stride, padding=0)
  83. self.bn1 = bn_with_initialize(out_chls)
  84. self.conv2 = conv3x3(out_chls, out_chls, stride=1, padding=0)
  85. self.bn2 = bn_with_initialize(out_chls)
  86. self.conv3 = conv1x1(out_chls, out_channels, stride=1, padding=0)
  87. self.bn3 = bn_with_initialize_last(out_channels)
  88. self.relu = P.ReLU()
  89. self.add = P.TensorAdd()
  90. def construct(self, x):
  91. identity = x
  92. out = self.conv1(x)
  93. out = self.bn1(out)
  94. out = self.relu(out)
  95. out = self.conv2(out)
  96. out = self.bn2(out)
  97. out = self.relu(out)
  98. out = self.conv3(out)
  99. out = self.bn3(out)
  100. out = self.add(out, identity)
  101. out = self.relu(out)
  102. return out
  103. class ResidualBlockWithDown(nn.Cell):
  104. expansion = 4
  105. def __init__(self,
  106. in_channels,
  107. out_channels,
  108. stride=1,
  109. down_sample=False):
  110. super(ResidualBlockWithDown, self).__init__()
  111. out_chls = out_channels // self.expansion
  112. self.conv1 = conv1x1(in_channels, out_chls, stride=stride, padding=0)
  113. self.bn1 = bn_with_initialize(out_chls)
  114. self.conv2 = conv3x3(out_chls, out_chls, stride=1, padding=0)
  115. self.bn2 = bn_with_initialize(out_chls)
  116. self.conv3 = conv1x1(out_chls, out_channels, stride=1, padding=0)
  117. self.bn3 = bn_with_initialize_last(out_channels)
  118. self.relu = P.ReLU()
  119. self.downSample = down_sample
  120. self.conv_down_sample = conv1x1(in_channels, out_channels, stride=stride, padding=0)
  121. self.bn_down_sample = bn_with_initialize(out_channels)
  122. self.add = P.TensorAdd()
  123. def construct(self, x):
  124. identity = x
  125. out = self.conv1(x)
  126. out = self.bn1(out)
  127. out = self.relu(out)
  128. out = self.conv2(out)
  129. out = self.bn2(out)
  130. out = self.relu(out)
  131. out = self.conv3(out)
  132. out = self.bn3(out)
  133. identity = self.conv_down_sample(identity)
  134. identity = self.bn_down_sample(identity)
  135. out = self.add(out, identity)
  136. out = self.relu(out)
  137. return out
  138. class MakeLayer0(nn.Cell):
  139. def __init__(self, block, layer_num, in_channels, out_channels, stride):
  140. super(MakeLayer0, self).__init__()
  141. self.a = ResidualBlockWithDown(in_channels, out_channels, stride=1, down_sample=True)
  142. self.b = block(out_channels, out_channels, stride=stride)
  143. self.c = block(out_channels, out_channels, stride=1)
  144. def construct(self, x):
  145. x = self.a(x)
  146. x = self.b(x)
  147. x = self.c(x)
  148. return x
  149. class MakeLayer1(nn.Cell):
  150. def __init__(self, block, layer_num, in_channels, out_channels, stride):
  151. super(MakeLayer1, self).__init__()
  152. self.a = ResidualBlockWithDown(in_channels, out_channels, stride=stride, down_sample=True)
  153. self.b = block(out_channels, out_channels, stride=1)
  154. self.c = block(out_channels, out_channels, stride=1)
  155. self.d = block(out_channels, out_channels, stride=1)
  156. def construct(self, x):
  157. x = self.a(x)
  158. x = self.b(x)
  159. x = self.c(x)
  160. x = self.d(x)
  161. return x
  162. class MakeLayer2(nn.Cell):
  163. def __init__(self, block, layer_num, in_channels, out_channels, stride):
  164. super(MakeLayer2, self).__init__()
  165. self.a = ResidualBlockWithDown(in_channels, out_channels, stride=stride, down_sample=True)
  166. self.b = block(out_channels, out_channels, stride=1)
  167. self.c = block(out_channels, out_channels, stride=1)
  168. self.d = block(out_channels, out_channels, stride=1)
  169. self.e = block(out_channels, out_channels, stride=1)
  170. self.f = block(out_channels, out_channels, stride=1)
  171. def construct(self, x):
  172. x = self.a(x)
  173. x = self.b(x)
  174. x = self.c(x)
  175. x = self.d(x)
  176. x = self.e(x)
  177. x = self.f(x)
  178. return x
  179. class MakeLayer3(nn.Cell):
  180. def __init__(self, block, layer_num, in_channels, out_channels, stride):
  181. super(MakeLayer3, self).__init__()
  182. self.a = ResidualBlockWithDown(in_channels, out_channels, stride=stride, down_sample=True)
  183. self.b = block(out_channels, out_channels, stride=1)
  184. self.c = block(out_channels, out_channels, stride=1)
  185. def construct(self, x):
  186. x = self.a(x)
  187. x = self.b(x)
  188. x = self.c(x)
  189. return x
  190. class ResNet(nn.Cell):
  191. def __init__(self, block, layer_num, num_classes=100, batch_size=32):
  192. super(ResNet, self).__init__()
  193. self.batch_size = batch_size
  194. self.num_classes = num_classes
  195. self.conv1 = conv7x7(3, 64, stride=2, padding=0)
  196. self.bn1 = bn_with_initialize(64)
  197. self.relu = P.ReLU()
  198. self.maxpool = P.MaxPoolWithArgmax(ksize=3, strides=2, padding="SAME")
  199. self.layer1 = MakeLayer0(block, layer_num[0], in_channels=64, out_channels=256, stride=1)
  200. self.layer2 = MakeLayer1(block, layer_num[1], in_channels=256, out_channels=512, stride=2)
  201. self.layer3 = MakeLayer2(block, layer_num[2], in_channels=512, out_channels=1024, stride=2)
  202. self.layer4 = MakeLayer3(block, layer_num[3], in_channels=1024, out_channels=2048, stride=2)
  203. self.pool = P.ReduceMean(keep_dims=True)
  204. self.squeeze = P.Squeeze(axis=(2, 3))
  205. self.fc = fc_with_initialize(512 * block.expansion, num_classes)
  206. def construct(self, x):
  207. x = self.conv1(x)
  208. x = self.bn1(x)
  209. x = self.relu(x)
  210. x = self.maxpool(x)[0]
  211. x = self.layer1(x)
  212. x = self.layer2(x)
  213. x = self.layer3(x)
  214. x = self.layer4(x)
  215. x = self.pool(x, (2, 3))
  216. x = self.squeeze(x)
  217. x = self.fc(x)
  218. return x
  219. def resnet50(batch_size, num_classes):
  220. return ResNet(ResidualBlock, [3, 4, 6, 3], num_classes, batch_size)