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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. # Copyright 2021 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. import mindspore.common.dtype as mstype
  18. from mindspore.ops import operations as P
  19. from mindspore.ops import functional as F
  20. from mindspore.common.tensor import Tensor
  21. from scipy.stats import truncnorm
  22. from mindspore.ops import TensorSummary
  23. def _conv_variance_scaling_initializer(in_channel, out_channel, kernel_size):
  24. fan_in = in_channel * kernel_size * kernel_size
  25. scale = 1.0
  26. scale /= max(1., fan_in)
  27. stddev = (scale ** 0.5) / .87962566103423978
  28. mu, sigma = 0, stddev
  29. weight = truncnorm(-2, 2, loc=mu, scale=sigma).rvs(out_channel * in_channel * kernel_size*kernel_size)
  30. weight = np.reshape(weight, (out_channel, in_channel, kernel_size, kernel_size))
  31. return Tensor(weight, dtype=mstype.float32)
  32. def _weight_variable(shape, factor=0.01):
  33. init_value = np.random.randn(*shape).astype(np.float32) * factor
  34. return Tensor(init_value)
  35. def _conv3x3(in_channel, out_channel, stride=1, use_se=False):
  36. if use_se:
  37. weight = _conv_variance_scaling_initializer(in_channel, out_channel, kernel_size=3)
  38. else:
  39. weight_shape = (out_channel, in_channel, 3, 3)
  40. weight = _weight_variable(weight_shape)
  41. return nn.Conv2d(in_channel, out_channel,
  42. kernel_size=3, stride=stride, padding=0, pad_mode='same', weight_init=weight)
  43. def _conv1x1(in_channel, out_channel, stride=1, use_se=False):
  44. if use_se:
  45. weight = _conv_variance_scaling_initializer(in_channel, out_channel, kernel_size=1)
  46. else:
  47. weight_shape = (out_channel, in_channel, 1, 1)
  48. weight = _weight_variable(weight_shape)
  49. return nn.Conv2d(in_channel, out_channel,
  50. kernel_size=1, stride=stride, padding=0, pad_mode='same', weight_init=weight)
  51. def _conv7x7(in_channel, out_channel, stride=1, use_se=False):
  52. if use_se:
  53. weight = _conv_variance_scaling_initializer(in_channel, out_channel, kernel_size=7)
  54. else:
  55. weight_shape = (out_channel, in_channel, 7, 7)
  56. weight = _weight_variable(weight_shape)
  57. return nn.Conv2d(in_channel, out_channel,
  58. kernel_size=7, stride=stride, padding=0, pad_mode='same', weight_init=weight)
  59. def _bn(channel):
  60. return nn.BatchNorm2d(channel, eps=1e-4, momentum=0.9,
  61. gamma_init=1, beta_init=0, moving_mean_init=0, moving_var_init=1)
  62. def _bn_last(channel):
  63. return nn.BatchNorm2d(channel, eps=1e-4, momentum=0.9,
  64. gamma_init=0, beta_init=0, moving_mean_init=0, moving_var_init=1)
  65. def _fc(in_channel, out_channel, use_se=False):
  66. if use_se:
  67. weight = np.random.normal(loc=0, scale=0.01, size=out_channel*in_channel)
  68. weight = Tensor(np.reshape(weight, (out_channel, in_channel)), dtype=mstype.float32)
  69. else:
  70. weight_shape = (out_channel, in_channel)
  71. weight = _weight_variable(weight_shape)
  72. return nn.Dense(in_channel, out_channel, has_bias=True, weight_init=weight, bias_init=0)
  73. class ResidualBlock(nn.Cell):
  74. """
  75. ResNet V1 residual block definition.
  76. Args:
  77. in_channel (int): Input channel.
  78. out_channel (int): Output channel.
  79. stride (int): Stride size for the first convolutional layer. Default: 1.
  80. use_se (bool): enable SE-ResNet50 net. Default: False.
  81. se_block (bool): use se block in SE-ResNet50 net. Default: False.
  82. Returns:
  83. Tensor, output tensor.
  84. Examples:
  85. >>> ResidualBlock(3, 256, stride=2)
  86. """
  87. expansion = 4
  88. def __init__(self,
  89. in_channel,
  90. out_channel,
  91. stride=1,
  92. use_se=False, se_block=False):
  93. super(ResidualBlock, self).__init__()
  94. self.summary = TensorSummary()
  95. self.stride = stride
  96. self.use_se = use_se
  97. self.se_block = se_block
  98. channel = out_channel // self.expansion
  99. self.conv1 = _conv1x1(in_channel, channel, stride=1, use_se=self.use_se)
  100. self.bn1 = _bn(channel)
  101. if self.use_se and self.stride != 1:
  102. self.e2 = nn.SequentialCell([_conv3x3(channel, channel, stride=1, use_se=True), _bn(channel),
  103. nn.ReLU(), nn.MaxPool2d(kernel_size=2, stride=2, pad_mode='same')])
  104. else:
  105. self.conv2 = _conv3x3(channel, channel, stride=stride, use_se=self.use_se)
  106. self.bn2 = _bn(channel)
  107. self.conv3 = _conv1x1(channel, out_channel, stride=1, use_se=self.use_se)
  108. self.bn3 = _bn_last(out_channel)
  109. if self.se_block:
  110. self.se_global_pool = P.ReduceMean(keep_dims=False)
  111. self.se_dense_0 = _fc(out_channel, int(out_channel/4), use_se=self.use_se)
  112. self.se_dense_1 = _fc(int(out_channel/4), out_channel, use_se=self.use_se)
  113. self.se_sigmoid = nn.Sigmoid()
  114. self.se_mul = P.Mul()
  115. self.relu = nn.ReLU()
  116. self.down_sample = False
  117. if stride != 1 or in_channel != out_channel:
  118. self.down_sample = True
  119. self.down_sample_layer = None
  120. if self.down_sample:
  121. if self.use_se:
  122. if stride == 1:
  123. self.down_sample_layer = nn.SequentialCell([_conv1x1(in_channel, out_channel,
  124. stride, use_se=self.use_se), _bn(out_channel)])
  125. else:
  126. self.down_sample_layer = nn.SequentialCell([nn.MaxPool2d(kernel_size=2, stride=2, pad_mode='same'),
  127. _conv1x1(in_channel, out_channel, 1,
  128. use_se=self.use_se), _bn(out_channel)])
  129. else:
  130. self.down_sample_layer = nn.SequentialCell([_conv1x1(in_channel, out_channel, stride,
  131. use_se=self.use_se), _bn(out_channel)])
  132. self.add = P.TensorAdd()
  133. def construct(self, x):
  134. identity = x
  135. out = self.conv1(x)
  136. out = self.bn1(out)
  137. out = self.relu(out)
  138. if self.use_se and self.stride != 1:
  139. out = self.e2(out)
  140. else:
  141. out = self.conv2(out)
  142. out = self.bn2(out)
  143. out = self.relu(out)
  144. out = self.conv3(out)
  145. out = self.bn3(out)
  146. if self.se_block:
  147. out_se = out
  148. out = self.se_global_pool(out, (2, 3))
  149. out = self.se_dense_0(out)
  150. out = self.relu(out)
  151. out = self.se_dense_1(out)
  152. out = self.se_sigmoid(out)
  153. out = F.reshape(out, F.shape(out) + (1, 1))
  154. out = self.se_mul(out, out_se)
  155. if self.down_sample:
  156. identity = self.down_sample_layer(identity)
  157. out = self.add(out, identity)
  158. out = self.relu(out)
  159. return out
  160. class ResNet(nn.Cell):
  161. """
  162. ResNet architecture.
  163. Args:
  164. block (Cell): Block for network.
  165. layer_nums (list): Numbers of block in different layers.
  166. in_channels (list): Input channel in each layer.
  167. out_channels (list): Output channel in each layer.
  168. strides (list): Stride size in each layer.
  169. num_classes (int): The number of classes that the training images are belonging to.
  170. use_se (bool): enable SE-ResNet50 net. Default: False.
  171. se_block (bool): use se block in SE-ResNet50 net in layer 3 and layer 4. Default: False.
  172. Returns:
  173. Tensor, output tensor.
  174. Examples:
  175. >>> ResNet(ResidualBlock,
  176. >>> [3, 4, 6, 3],
  177. >>> [64, 256, 512, 1024],
  178. >>> [256, 512, 1024, 2048],
  179. >>> [1, 2, 2, 2],
  180. >>> 10)
  181. """
  182. def __init__(self,
  183. block,
  184. layer_nums,
  185. in_channels,
  186. out_channels,
  187. strides,
  188. num_classes,
  189. use_se=False):
  190. super(ResNet, self).__init__()
  191. if not len(layer_nums) == len(in_channels) == len(out_channels) == 4:
  192. raise ValueError("the length of layer_num, in_channels, out_channels list must be 4!")
  193. self.use_se = use_se
  194. self.se_block = False
  195. if self.use_se:
  196. self.se_block = True
  197. if self.use_se:
  198. self.conv1_0 = _conv3x3(3, 32, stride=2, use_se=self.use_se)
  199. self.bn1_0 = _bn(32)
  200. self.conv1_1 = _conv3x3(32, 32, stride=1, use_se=self.use_se)
  201. self.bn1_1 = _bn(32)
  202. self.conv1_2 = _conv3x3(32, 64, stride=1, use_se=self.use_se)
  203. else:
  204. self.conv1 = _conv7x7(3, 64, stride=2)
  205. self.bn1 = _bn(64)
  206. self.relu = P.ReLU()
  207. self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, pad_mode="same")
  208. self.layer1 = self._make_layer(block,
  209. layer_nums[0],
  210. in_channel=in_channels[0],
  211. out_channel=out_channels[0],
  212. stride=strides[0],
  213. use_se=self.use_se)
  214. self.layer2 = self._make_layer(block,
  215. layer_nums[1],
  216. in_channel=in_channels[1],
  217. out_channel=out_channels[1],
  218. stride=strides[1],
  219. use_se=self.use_se)
  220. self.layer3 = self._make_layer(block,
  221. layer_nums[2],
  222. in_channel=in_channels[2],
  223. out_channel=out_channels[2],
  224. stride=strides[2],
  225. use_se=self.use_se,
  226. se_block=self.se_block)
  227. self.layer4 = self._make_layer(block,
  228. layer_nums[3],
  229. in_channel=in_channels[3],
  230. out_channel=out_channels[3],
  231. stride=strides[3],
  232. use_se=self.use_se,
  233. se_block=self.se_block)
  234. self.mean = P.ReduceMean(keep_dims=True)
  235. self.flatten = nn.Flatten()
  236. self.end_point = _fc(out_channels[3], num_classes, use_se=self.use_se)
  237. self.summary = TensorSummary()
  238. def _make_layer(self, block, layer_num, in_channel, out_channel, stride, use_se=False, se_block=False):
  239. """
  240. Make stage network of ResNet.
  241. Args:
  242. block (Cell): Resnet block.
  243. layer_num (int): Layer number.
  244. in_channel (int): Input channel.
  245. out_channel (int): Output channel.
  246. stride (int): Stride size for the first convolutional layer.
  247. se_block (bool): use se block in SE-ResNet50 net. Default: False.
  248. Returns:
  249. SequentialCell, the output layer.
  250. Examples:
  251. >>> _make_layer(ResidualBlock, 3, 128, 256, 2)
  252. """
  253. layers = []
  254. resnet_block = block(in_channel, out_channel, stride=stride, use_se=use_se)
  255. layers.append(resnet_block)
  256. if se_block:
  257. for _ in range(1, layer_num - 1):
  258. resnet_block = block(out_channel, out_channel, stride=1, use_se=use_se)
  259. layers.append(resnet_block)
  260. resnet_block = block(out_channel, out_channel, stride=1, use_se=use_se, se_block=se_block)
  261. layers.append(resnet_block)
  262. else:
  263. for _ in range(1, layer_num):
  264. resnet_block = block(out_channel, out_channel, stride=1, use_se=use_se)
  265. layers.append(resnet_block)
  266. return nn.SequentialCell(layers)
  267. def construct(self, x):
  268. if self.use_se:
  269. x = self.conv1_0(x)
  270. x = self.bn1_0(x)
  271. x = self.relu(x)
  272. x = self.conv1_1(x)
  273. x = self.bn1_1(x)
  274. x = self.relu(x)
  275. x = self.conv1_2(x)
  276. else:
  277. x = self.conv1(x)
  278. x = self.bn1(x)
  279. x = self.relu(x)
  280. c1 = self.maxpool(x)
  281. c2 = self.layer1(c1)
  282. c3 = self.layer2(c2)
  283. c4 = self.layer3(c3)
  284. c5 = self.layer4(c4)
  285. out = self.mean(c5, (2, 3))
  286. out = self.flatten(out)
  287. self.summary('1', out)
  288. out = self.end_point(out)
  289. if self.training:
  290. return out
  291. self.summary('output', out)
  292. return out
  293. def resnet50(class_num=10):
  294. """
  295. Get ResNet50 neural network.
  296. Args:
  297. class_num (int): Class number.
  298. Returns:
  299. Cell, cell instance of ResNet50 neural network.
  300. Examples:
  301. >>> net = resnet50(10)
  302. """
  303. return ResNet(ResidualBlock,
  304. [3, 4, 6, 3],
  305. [64, 256, 512, 1024],
  306. [256, 512, 1024, 2048],
  307. [1, 2, 2, 2],
  308. class_num)
  309. def se_resnet50(class_num=1001):
  310. """
  311. Get SE-ResNet50 neural network.
  312. Args:
  313. class_num (int): Class number.
  314. Returns:
  315. Cell, cell instance of SE-ResNet50 neural network.
  316. Examples:
  317. >>> net = se-resnet50(1001)
  318. """
  319. return ResNet(ResidualBlock,
  320. [3, 4, 6, 3],
  321. [64, 256, 512, 1024],
  322. [256, 512, 1024, 2048],
  323. [1, 2, 2, 2],
  324. class_num,
  325. use_se=True)
  326. def resnet101(class_num=1001):
  327. """
  328. Get ResNet101 neural network.
  329. Args:
  330. class_num (int): Class number.
  331. Returns:
  332. Cell, cell instance of ResNet101 neural network.
  333. Examples:
  334. >>> net = resnet101(1001)
  335. """
  336. return ResNet(ResidualBlock,
  337. [3, 4, 23, 3],
  338. [64, 256, 512, 1024],
  339. [256, 512, 1024, 2048],
  340. [1, 2, 2, 2],
  341. class_num)

MindArmour关注AI的安全和隐私问题。致力于增强模型的安全可信、保护用户的数据隐私。主要包含3个模块:对抗样本鲁棒性模块、Fuzz Testing模块、隐私保护与评估模块。 对抗样本鲁棒性模块 对抗样本鲁棒性模块用于评估模型对于对抗样本的鲁棒性,并提供模型增强方法用于增强模型抗对抗样本攻击的能力,提升模型鲁棒性。对抗样本鲁棒性模块包含了4个子模块:对抗样本的生成、对抗样本的检测、模型防御、攻防评估。