您最多选择25个标签 标签必须以中文、字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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