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

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 math
  17. import numpy as np
  18. import mindspore.nn as nn
  19. from mindspore.common.tensor import Tensor
  20. from mindspore.ops import operations as P
  21. from model.thor_layer import Conv2d_Thor, Dense_Thor
  22. def calculate_gain(nonlinearity, param=None):
  23. """calculate_gain"""
  24. linear_fns = ['linear', 'conv1d', 'conv2d', 'conv3d', 'conv_transpose1d', 'conv_transpose2d', 'conv_transpose3d']
  25. res = 0
  26. if nonlinearity in linear_fns or nonlinearity == 'sigmoid':
  27. res = 1
  28. elif nonlinearity == 'tanh':
  29. res = 5.0 / 3
  30. elif nonlinearity == 'relu':
  31. res = math.sqrt(2.0)
  32. elif nonlinearity == 'leaky_relu':
  33. if param is None:
  34. negative_slope = 0.01
  35. elif not isinstance(param, bool) and isinstance(param, int) or isinstance(param, float):
  36. # True/False are instances of int, hence check above
  37. negative_slope = param
  38. else:
  39. raise ValueError("negative_slope {} not a valid number".format(param))
  40. res = math.sqrt(2.0 / (1 + negative_slope ** 2))
  41. else:
  42. raise ValueError("Unsupported nonlinearity {}".format(nonlinearity))
  43. return res
  44. def _calculate_fan_in_and_fan_out(tensor):
  45. """_calculate_fan_in_and_fan_out"""
  46. dimensions = len(tensor)
  47. if dimensions < 2:
  48. raise ValueError("Fan in and fan out can not be computed for tensor with fewer than 2 dimensions")
  49. if dimensions == 2: # Linear
  50. fan_in = tensor[1]
  51. fan_out = tensor[0]
  52. else:
  53. num_input_fmaps = tensor[1]
  54. num_output_fmaps = tensor[0]
  55. receptive_field_size = 1
  56. if dimensions > 2:
  57. receptive_field_size = tensor[2] * tensor[3]
  58. fan_in = num_input_fmaps * receptive_field_size
  59. fan_out = num_output_fmaps * receptive_field_size
  60. return fan_in, fan_out
  61. def _calculate_correct_fan(tensor, mode):
  62. mode = mode.lower()
  63. valid_modes = ['fan_in', 'fan_out']
  64. if mode not in valid_modes:
  65. raise ValueError("Mode {} not supported, please use one of {}".format(mode, valid_modes))
  66. fan_in, fan_out = _calculate_fan_in_and_fan_out(tensor)
  67. return fan_in if mode == 'fan_in' else fan_out
  68. def kaiming_normal(inputs_shape, a=0, mode='fan_in', nonlinearity='leaky_relu'):
  69. fan = _calculate_correct_fan(inputs_shape, mode)
  70. gain = calculate_gain(nonlinearity, a)
  71. std = gain / math.sqrt(fan)
  72. return np.random.normal(0, std, size=inputs_shape).astype(np.float32)
  73. def kaiming_uniform(inputs_shape, a=0, mode='fan_in', nonlinearity='leaky_relu'):
  74. fan = _calculate_correct_fan(inputs_shape, mode)
  75. gain = calculate_gain(nonlinearity, a)
  76. std = gain / math.sqrt(fan)
  77. bound = math.sqrt(3.0) * std # Calculate uniform bounds from standard deviation
  78. return np.random.uniform(-bound, bound, size=inputs_shape).astype(np.float32)
  79. def _conv3x3(in_channel, out_channel, stride=1, damping=0.03, loss_scale=1, frequency=278):
  80. weight_shape = (out_channel, in_channel, 3, 3)
  81. weight = Tensor(kaiming_normal(weight_shape, mode="fan_out", nonlinearity='relu'))
  82. return Conv2d_Thor(in_channel, out_channel,
  83. kernel_size=3, stride=stride, padding=0, pad_mode='same', weight_init=weight,
  84. damping=damping, loss_scale=loss_scale, frequency=frequency)
  85. def _conv1x1(in_channel, out_channel, stride=1, damping=0.03, loss_scale=1, frequency=278):
  86. weight_shape = (out_channel, in_channel, 1, 1)
  87. weight = Tensor(kaiming_normal(weight_shape, mode="fan_out", nonlinearity='relu'))
  88. return Conv2d_Thor(in_channel, out_channel,
  89. kernel_size=1, stride=stride, padding=0, pad_mode='same', weight_init=weight,
  90. damping=damping, loss_scale=loss_scale, frequency=frequency)
  91. def _conv7x7(in_channel, out_channel, stride=1, damping=0.03, loss_scale=1, frequency=278):
  92. weight_shape = (out_channel, in_channel, 7, 7)
  93. weight = Tensor(kaiming_normal(weight_shape, mode="fan_out", nonlinearity='relu'))
  94. return Conv2d_Thor(in_channel, out_channel,
  95. kernel_size=7, stride=stride, padding=0, pad_mode='same', weight_init=weight,
  96. damping=damping, loss_scale=loss_scale, frequency=frequency)
  97. def _bn(channel):
  98. return nn.BatchNorm2d(channel, eps=1e-4, momentum=0.9,
  99. gamma_init=1, beta_init=0, moving_mean_init=0, moving_var_init=1)
  100. def _bn_last(channel):
  101. return nn.BatchNorm2d(channel, eps=1e-4, momentum=0.9,
  102. gamma_init=1, beta_init=0, moving_mean_init=0, moving_var_init=1)
  103. def _fc(in_channel, out_channel, damping, loss_scale, frequency):
  104. weight_shape = (out_channel, in_channel)
  105. weight = Tensor(kaiming_uniform(weight_shape, a=math.sqrt(5)))
  106. return Dense_Thor(in_channel, out_channel, has_bias=False, weight_init=weight,
  107. bias_init=0, damping=damping, loss_scale=loss_scale, frequency=frequency)
  108. class ResidualBlock(nn.Cell):
  109. """
  110. ResNet V1 residual block definition.
  111. Args:
  112. in_channel (int): Input channel.
  113. out_channel (int): Output channel.
  114. stride (int): Stride size for the first convolutional layer. Default: 1.
  115. Returns:
  116. Tensor, output tensor.
  117. Examples:
  118. >>> ResidualBlock(3, 256, stride=2)
  119. """
  120. expansion = 4
  121. def __init__(self,
  122. in_channel,
  123. out_channel,
  124. stride=1,
  125. damping=0.03,
  126. loss_scale=1,
  127. frequency=278):
  128. super(ResidualBlock, self).__init__()
  129. channel = out_channel // self.expansion
  130. self.conv1 = _conv1x1(in_channel, channel, stride=1, damping=damping, loss_scale=loss_scale,
  131. frequency=frequency)
  132. self.bn1 = _bn(channel)
  133. self.conv2 = _conv3x3(channel, channel, stride=stride, damping=damping, loss_scale=loss_scale,
  134. frequency=frequency)
  135. self.bn2 = _bn(channel)
  136. self.conv3 = _conv1x1(channel, out_channel, stride=1, damping=damping, loss_scale=loss_scale,
  137. frequency=frequency)
  138. self.bn3 = _bn_last(out_channel)
  139. self.relu = nn.ReLU()
  140. self.down_sample = False
  141. if stride != 1 or in_channel != out_channel:
  142. self.down_sample = True
  143. self.down_sample_layer = None
  144. if self.down_sample:
  145. self.down_sample_layer = nn.SequentialCell([_conv1x1(in_channel, out_channel, stride,
  146. damping=damping, loss_scale=loss_scale,
  147. frequency=frequency),
  148. _bn(out_channel)])
  149. self.add = P.TensorAdd()
  150. def construct(self, x):
  151. identity = x
  152. out = self.conv1(x)
  153. out = self.bn1(out)
  154. out = self.relu(out)
  155. out = self.conv2(out)
  156. out = self.bn2(out)
  157. out = self.relu(out)
  158. out = self.conv3(out)
  159. out = self.bn3(out)
  160. if self.down_sample:
  161. identity = self.down_sample_layer(identity)
  162. out = self.add(out, identity)
  163. out = self.relu(out)
  164. return out
  165. class ResNet(nn.Cell):
  166. """
  167. ResNet architecture.
  168. Args:
  169. block (Cell): Block for network.
  170. layer_nums (list): Numbers of block in different layers.
  171. in_channels (list): Input channel in each layer.
  172. out_channels (list): Output channel in each layer.
  173. strides (list): Stride size in each layer.
  174. num_classes (int): The number of classes that the training images are belonging to.
  175. Returns:
  176. Tensor, output tensor.
  177. Examples:
  178. >>> ResNet(ResidualBlock,
  179. >>> [3, 4, 6, 3],
  180. >>> [64, 256, 512, 1024],
  181. >>> [256, 512, 1024, 2048],
  182. >>> [1, 2, 2, 2],
  183. >>> 10)
  184. """
  185. def __init__(self,
  186. block,
  187. layer_nums,
  188. in_channels,
  189. out_channels,
  190. strides,
  191. num_classes,
  192. damping,
  193. loss_scale,
  194. frequency):
  195. super(ResNet, self).__init__()
  196. if not len(layer_nums) == len(in_channels) == len(out_channels) == 4:
  197. raise ValueError("the length of layer_num, in_channels, out_channels list must be 4!")
  198. self.conv1 = _conv7x7(3, 64, stride=2, damping=damping, loss_scale=loss_scale, frequency=frequency)
  199. self.bn1 = _bn(64)
  200. self.relu = P.ReLU()
  201. self.maxpool = P.MaxPoolWithArgmax(padding="same", ksize=3, strides=2)
  202. self.layer1 = self._make_layer(block,
  203. layer_nums[0],
  204. in_channel=in_channels[0],
  205. out_channel=out_channels[0],
  206. stride=strides[0],
  207. damping=damping,
  208. loss_scale=loss_scale,
  209. frequency=frequency)
  210. self.layer2 = self._make_layer(block,
  211. layer_nums[1],
  212. in_channel=in_channels[1],
  213. out_channel=out_channels[1],
  214. stride=strides[1],
  215. damping=damping,
  216. loss_scale=loss_scale,
  217. frequency=frequency)
  218. self.layer3 = self._make_layer(block,
  219. layer_nums[2],
  220. in_channel=in_channels[2],
  221. out_channel=out_channels[2],
  222. stride=strides[2], damping=damping,
  223. loss_scale=loss_scale,
  224. frequency=frequency)
  225. self.layer4 = self._make_layer(block,
  226. layer_nums[3],
  227. in_channel=in_channels[3],
  228. out_channel=out_channels[3],
  229. stride=strides[3],
  230. damping=damping,
  231. loss_scale=loss_scale,
  232. frequency=frequency)
  233. self.mean = P.ReduceMean(keep_dims=True)
  234. self.flatten = nn.Flatten()
  235. self.end_point = _fc(out_channels[3], num_classes, damping=damping, loss_scale=loss_scale, frequency=frequency)
  236. def _make_layer(self, block, layer_num, in_channel, out_channel, stride,
  237. damping, loss_scale, frequency):
  238. """
  239. Make stage network of ResNet.
  240. Args:
  241. block (Cell): Resnet block.
  242. layer_num (int): Layer number.
  243. in_channel (int): Input channel.
  244. out_channel (int): Output channel.
  245. stride (int): Stride size for the first convolutional layer.
  246. Returns:
  247. SequentialCell, the output layer.
  248. Examples:
  249. >>> _make_layer(ResidualBlock, 3, 128, 256, 2)
  250. """
  251. layers = []
  252. resnet_block = block(in_channel, out_channel, stride=stride,
  253. damping=damping, loss_scale=loss_scale, frequency=frequency)
  254. layers.append(resnet_block)
  255. for _ in range(1, layer_num):
  256. resnet_block = block(out_channel, out_channel, stride=1,
  257. damping=damping, loss_scale=loss_scale, frequency=frequency)
  258. layers.append(resnet_block)
  259. return nn.SequentialCell(layers)
  260. def construct(self, x):
  261. x = self.conv1(x)
  262. x = self.bn1(x)
  263. x = self.relu(x)
  264. c1, _ = self.maxpool(x)
  265. c2 = self.layer1(c1)
  266. c3 = self.layer2(c2)
  267. c4 = self.layer3(c3)
  268. c5 = self.layer4(c4)
  269. out = self.mean(c5, (2, 3))
  270. out = self.flatten(out)
  271. out = self.end_point(out)
  272. return out
  273. def resnet50(class_num=10, damping=0.03, loss_scale=1, frequency=278):
  274. """
  275. Get ResNet50 neural network.
  276. Args:
  277. class_num (int): Class number.
  278. Returns:
  279. Cell, cell instance of ResNet50 neural network.
  280. Examples:
  281. >>> net = resnet50(10)
  282. """
  283. return ResNet(ResidualBlock,
  284. [3, 4, 6, 3],
  285. [64, 256, 512, 1024],
  286. [256, 512, 1024, 2048],
  287. [1, 2, 2, 2],
  288. class_num,
  289. damping,
  290. loss_scale,
  291. frequency)