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

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