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.

test_cifar10_attack_fgsm.py 2.4 kB

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. Fuction:
  16. Test fgsm attack about resnet50 network
  17. Usage:
  18. py.test test_cifar10_attack_fgsm.py
  19. """
  20. import os
  21. import numpy as np
  22. import pytest
  23. from mindspore import Tensor
  24. from mindspore import context
  25. from mindspore.nn import Cell
  26. from mindspore.common import dtype as mstype
  27. from mindspore.ops import operations as P
  28. from mindspore.ops import functional as F
  29. from mindarmour.attacks.gradient_method import FastGradientSignMethod
  30. from resnet_cifar10 import resnet50_cifar10
  31. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  32. class CrossEntropyLoss(Cell):
  33. def __init__(self):
  34. super(CrossEntropyLoss, self).__init__()
  35. self.cross_entropy = P.SoftmaxCrossEntropyWithLogits()
  36. self.mean = P.ReduceMean()
  37. self.one_hot = P.OneHot()
  38. self.on_value = Tensor(1.0, mstype.float32)
  39. self.off_value = Tensor(0.0, mstype.float32)
  40. def construct(self, logits, label):
  41. label = self.one_hot(label, F.shape(logits)[1], self.on_value, self.off_value)
  42. loss = self.cross_entropy(logits, label)[0]
  43. loss = self.mean(loss, (-1,))
  44. return loss
  45. @pytest.mark.level0
  46. @pytest.mark.env_single
  47. @pytest.mark.platform_x86_ascend_training
  48. @pytest.mark.platform_x86_ascend_inference
  49. def test_fast_gradient_sign_method():
  50. """
  51. FGSM-Attack test
  52. """
  53. context.set_context(mode=context.GRAPH_MODE)
  54. # get network
  55. net = resnet50_cifar10(10)
  56. # create test data
  57. test_images = np.random.rand(64, 3, 224, 224).astype(np.float32)
  58. test_labels = np.random.randint(10, size=64).astype(np.int32)
  59. # attacking
  60. loss_fn = CrossEntropyLoss()
  61. attack = FastGradientSignMethod(net, eps=0.1, loss_fn=loss_fn)
  62. adv_data = attack.batch_generate(test_images, test_labels, batch_size=32)
  63. assert np.any(adv_data != test_images)

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