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.py 2.5 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # Copyright 2022 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. """test"""
  16. import numpy as np
  17. from mindspore import context, Tensor
  18. import mindspore
  19. from mindspore.dataset.vision.py_transforms import ToTensor
  20. import mindspore.dataset.vision.py_transforms as P
  21. from FaceRecognition.eval import get_model
  22. import adversarial_attack
  23. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  24. if __name__ == '__main__':
  25. image = adversarial_attack.load_data('photos/adv_input/')
  26. inputs = adversarial_attack.load_data('photos/input/')
  27. targets = adversarial_attack.load_data('photos/target/')
  28. tensorize = ToTensor()
  29. normalize = P.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
  30. expand_dims = mindspore.ops.ExpandDims()
  31. mean = Tensor([0.485, 0.456, 0.406])
  32. std = Tensor([0.229, 0.224, 0.225])
  33. resnet = get_model()
  34. adv = Tensor(normalize(tensorize(image[0])))
  35. input_tensor = Tensor(normalize(tensorize(inputs[0])))
  36. target_tensor = Tensor(normalize(tensorize(targets[0])))
  37. adversarial_emb = resnet(expand_dims(adv, 0))
  38. input_emb = resnet(expand_dims(input_tensor, 0))
  39. target_emb = resnet(expand_dims(target_tensor, 0))
  40. adversarial_index = np.argmax(adversarial_emb.asnumpy())
  41. target_index = np.argmax(target_emb.asnumpy())
  42. input_index = np.argmax(input_emb.asnumpy())
  43. print("input_label:", input_index)
  44. print("The confidence of the input image on the input label:", input_emb.asnumpy()[0][input_index])
  45. print("================================")
  46. print("adversarial_label:", adversarial_index)
  47. print("The confidence of the adversarial sample on the correct label:", adversarial_emb.asnumpy()[0][input_index])
  48. print("The confidence of the adversarial sample on the adversarial label:",
  49. adversarial_emb.asnumpy()[0][adversarial_index])
  50. print("input_label:%d, adversarial_label:%d" % (input_index, adversarial_index))

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