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.

resnetv1_5.py 9.8 kB

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