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 8.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. """ResNet."""
  16. import numpy as np
  17. import mindspore.nn as nn
  18. from mindspore.ops import operations as P
  19. from mindspore.common.tensor import Tensor
  20. def _weight_variable(shape, factor=0.01):
  21. init_value = np.random.randn(*shape).astype(np.float32) * factor
  22. return Tensor(init_value)
  23. def _conv3x3(in_channel, out_channel, stride=1):
  24. weight_shape = (out_channel, in_channel, 3, 3)
  25. weight = _weight_variable(weight_shape)
  26. return nn.Conv2d(in_channel, out_channel,
  27. kernel_size=3, stride=stride, padding=0, pad_mode='same', weight_init=weight)
  28. def _conv1x1(in_channel, out_channel, stride=1):
  29. weight_shape = (out_channel, in_channel, 1, 1)
  30. weight = _weight_variable(weight_shape)
  31. return nn.Conv2d(in_channel, out_channel,
  32. kernel_size=1, stride=stride, padding=0, pad_mode='same', weight_init=weight)
  33. def _conv7x7(in_channel, out_channel, stride=1):
  34. weight_shape = (out_channel, in_channel, 7, 7)
  35. weight = _weight_variable(weight_shape)
  36. return nn.Conv2d(in_channel, out_channel,
  37. kernel_size=7, stride=stride, padding=0, pad_mode='same', weight_init=weight)
  38. def _bn(channel):
  39. return nn.BatchNorm2d(channel, eps=1e-4, momentum=0.9,
  40. gamma_init=1, beta_init=0, moving_mean_init=0, moving_var_init=1)
  41. def _bn_last(channel):
  42. return nn.BatchNorm2d(channel, eps=1e-4, momentum=0.9,
  43. gamma_init=0, beta_init=0, moving_mean_init=0, moving_var_init=1)
  44. def _fc(in_channel, out_channel):
  45. weight_shape = (out_channel, in_channel)
  46. weight = _weight_variable(weight_shape)
  47. return nn.Dense(in_channel, out_channel, has_bias=True, weight_init=weight, bias_init=0)
  48. class ResidualBlock(nn.Cell):
  49. """
  50. ResNet V1 residual block definition.
  51. Args:
  52. in_channel (int): Input channel.
  53. out_channel (int): Output channel.
  54. stride (int): Stride size for the first convolutional layer. Default: 1.
  55. Returns:
  56. Tensor, output tensor.
  57. Examples:
  58. >>> ResidualBlock(3, 256, stride=2)
  59. """
  60. expansion = 4
  61. def __init__(self,
  62. in_channel,
  63. out_channel,
  64. stride=1):
  65. super(ResidualBlock, self).__init__()
  66. channel = out_channel // self.expansion
  67. self.conv1 = _conv1x1(in_channel, channel, stride=1)
  68. self.bn1 = _bn(channel)
  69. self.conv2 = _conv3x3(channel, channel, stride=stride)
  70. self.bn2 = _bn(channel)
  71. self.conv3 = _conv1x1(channel, out_channel, stride=1)
  72. self.bn3 = _bn_last(out_channel)
  73. self.relu = nn.ReLU()
  74. self.down_sample = False
  75. if stride != 1 or in_channel != out_channel:
  76. self.down_sample = True
  77. self.down_sample_layer = None
  78. if self.down_sample:
  79. self.down_sample_layer = nn.SequentialCell([_conv1x1(in_channel, out_channel, stride),
  80. _bn(out_channel)])
  81. self.add = P.TensorAdd()
  82. def construct(self, x):
  83. identity = x
  84. out = self.conv1(x)
  85. out = self.bn1(out)
  86. out = self.relu(out)
  87. out = self.conv2(out)
  88. out = self.bn2(out)
  89. out = self.relu(out)
  90. out = self.conv3(out)
  91. out = self.bn3(out)
  92. if self.down_sample:
  93. identity = self.down_sample_layer(identity)
  94. out = self.add(out, identity)
  95. out = self.relu(out)
  96. return out
  97. class ResNet(nn.Cell):
  98. """
  99. ResNet architecture.
  100. Args:
  101. block (Cell): Block for network.
  102. layer_nums (list): Numbers of block in different layers.
  103. in_channels (list): Input channel in each layer.
  104. out_channels (list): Output channel in each layer.
  105. strides (list): Stride size in each layer.
  106. num_classes (int): The number of classes that the training images are belonging to.
  107. Returns:
  108. Tensor, output tensor.
  109. Examples:
  110. >>> ResNet(ResidualBlock,
  111. >>> [3, 4, 6, 3],
  112. >>> [64, 256, 512, 1024],
  113. >>> [256, 512, 1024, 2048],
  114. >>> [1, 2, 2, 2],
  115. >>> 10)
  116. """
  117. def __init__(self,
  118. block,
  119. layer_nums,
  120. in_channels,
  121. out_channels,
  122. strides,
  123. num_classes):
  124. super(ResNet, self).__init__()
  125. if not len(layer_nums) == len(in_channels) == len(out_channels) == 4:
  126. raise ValueError("the length of layer_num, in_channels, out_channels list must be 4!")
  127. self.conv1 = _conv7x7(3, 64, stride=2)
  128. self.bn1 = _bn(64)
  129. self.relu = P.ReLU()
  130. self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, pad_mode="same")
  131. self.layer1 = self._make_layer(block,
  132. layer_nums[0],
  133. in_channel=in_channels[0],
  134. out_channel=out_channels[0],
  135. stride=strides[0])
  136. self.layer2 = self._make_layer(block,
  137. layer_nums[1],
  138. in_channel=in_channels[1],
  139. out_channel=out_channels[1],
  140. stride=strides[1])
  141. self.layer3 = self._make_layer(block,
  142. layer_nums[2],
  143. in_channel=in_channels[2],
  144. out_channel=out_channels[2],
  145. stride=strides[2])
  146. self.layer4 = self._make_layer(block,
  147. layer_nums[3],
  148. in_channel=in_channels[3],
  149. out_channel=out_channels[3],
  150. stride=strides[3])
  151. self.mean = P.ReduceMean(keep_dims=True)
  152. self.flatten = nn.Flatten()
  153. self.end_point = _fc(out_channels[3], num_classes)
  154. def _make_layer(self, block, layer_num, in_channel, out_channel, stride):
  155. """
  156. Make stage network of ResNet.
  157. Args:
  158. block (Cell): Resnet block.
  159. layer_num (int): Layer number.
  160. in_channel (int): Input channel.
  161. out_channel (int): Output channel.
  162. stride (int): Stride size for the first convolutional layer.
  163. Returns:
  164. SequentialCell, the output layer.
  165. Examples:
  166. >>> _make_layer(ResidualBlock, 3, 128, 256, 2)
  167. """
  168. layers = []
  169. resnet_block = block(in_channel, out_channel, stride=stride)
  170. layers.append(resnet_block)
  171. for _ in range(1, layer_num):
  172. resnet_block = block(out_channel, out_channel, stride=1)
  173. layers.append(resnet_block)
  174. return nn.SequentialCell(layers)
  175. def construct(self, x):
  176. x = self.conv1(x)
  177. x = self.bn1(x)
  178. x = self.relu(x)
  179. c1 = self.maxpool(x)
  180. c2 = self.layer1(c1)
  181. c3 = self.layer2(c2)
  182. c4 = self.layer3(c3)
  183. c5 = self.layer4(c4)
  184. out = self.mean(c5, (2, 3))
  185. out = self.flatten(out)
  186. out = self.end_point(out)
  187. return out
  188. def resnet50(class_num=10):
  189. """
  190. Get ResNet50 neural network.
  191. Args:
  192. class_num (int): Class number.
  193. Returns:
  194. Cell, cell instance of ResNet50 neural network.
  195. Examples:
  196. >>> net = resnet50(10)
  197. """
  198. return ResNet(ResidualBlock,
  199. [3, 4, 6, 3],
  200. [64, 256, 512, 1024],
  201. [256, 512, 1024, 2048],
  202. [1, 2, 2, 2],
  203. class_num)
  204. def resnet101(class_num=1001):
  205. """
  206. Get ResNet101 neural network.
  207. Args:
  208. class_num (int): Class number.
  209. Returns:
  210. Cell, cell instance of ResNet101 neural network.
  211. Examples:
  212. >>> net = resnet101(1001)
  213. """
  214. return ResNet(ResidualBlock,
  215. [3, 4, 23, 3],
  216. [64, 256, 512, 1024],
  217. [256, 512, 1024, 2048],
  218. [1, 2, 2, 2],
  219. class_num)