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.

mock_net.py 2.1 kB

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

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