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.

lenet5_net.py 2.1 kB

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. import mindspore.nn as nn
  15. import mindspore.ops.operations as P
  16. from mindspore.common.initializer import TruncatedNormal
  17. def conv(in_channels, out_channels, kernel_size, stride=1, padding=0):
  18. weight = weight_variable()
  19. return nn.Conv2d(in_channels, out_channels,
  20. kernel_size=kernel_size, stride=stride, padding=padding,
  21. weight_init=weight, has_bias=False, pad_mode="valid")
  22. def fc_with_initialize(input_channels, out_channels):
  23. weight = weight_variable()
  24. bias = weight_variable()
  25. return nn.Dense(input_channels, out_channels, weight, bias)
  26. def weight_variable():
  27. return TruncatedNormal(0.2)
  28. class LeNet5(nn.Cell):
  29. """
  30. Lenet network
  31. """
  32. def __init__(self):
  33. super(LeNet5, self).__init__()
  34. self.conv1 = conv(1, 6, 5)
  35. self.conv2 = conv(6, 16, 5)
  36. self.fc1 = fc_with_initialize(16*5*5, 120)
  37. self.fc2 = fc_with_initialize(120, 84)
  38. self.fc3 = fc_with_initialize(84, 10)
  39. self.relu = nn.ReLU()
  40. self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)
  41. self.reshape = P.Reshape()
  42. def construct(self, x):
  43. x = self.conv1(x)
  44. x = self.relu(x)
  45. x = self.max_pool2d(x)
  46. x = self.conv2(x)
  47. x = self.relu(x)
  48. x = self.max_pool2d(x)
  49. x = self.reshape(x, (-1, 16*5*5))
  50. x = self.fc1(x)
  51. x = self.relu(x)
  52. x = self.fc2(x)
  53. x = self.relu(x)
  54. x = self.fc3(x)
  55. return x

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