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.6 kB

3 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. import numpy as np
  16. import matplotlib.image as mp
  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 mindspore.dataset.vision.py_transforms import ToPIL as ToPILImage
  22. from FaceRecognition.eval import get_net
  23. import AFR
  24. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  25. imageize = ToPILImage()
  26. if __name__ == '__main__':
  27. """
  28. The input image, target image and adversarial image are tested using the FaceRecognition model.
  29. """
  30. image = AFR.load_data('opencv_photo/adv_input')
  31. inputs = AFR.load_data('opencv_photo/input/')
  32. targets = AFR.load_data('opencv_photo/target/')
  33. tensorize = ToTensor()
  34. normalize = P.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
  35. expand_dims = mindspore.ops.ExpandDims()
  36. MEAN = Tensor([0.485, 0.456, 0.406])
  37. STD = Tensor([0.229, 0.224, 0.225])
  38. resnet = get_net()
  39. image = mp.imread("./对抗图像.jpg")
  40. adv = Tensor(normalize(tensorize(image)))
  41. input_tensor = Tensor(normalize(tensorize(inputs[0])))
  42. target_tensor = Tensor(normalize(tensorize(targets[0])))
  43. adversarial_emb = resnet(expand_dims(adv, 0))
  44. input_emb = resnet(expand_dims(input_tensor, 0))
  45. target_emb = resnet(expand_dims(target_tensor, 0))
  46. adversarial = np.argmax(adversarial_emb.asnumpy())
  47. target = np.argmax(target_emb.asnumpy())
  48. input = np.argmax(input_emb.asnumpy())
  49. print("input:", input)
  50. print("input_confidence:", input_emb.asnumpy()[0][input])
  51. print("================================")
  52. print("adversarial:", adversarial)
  53. print("adversarial_confidence:", adversarial_emb.asnumpy()[0][adversarial])
  54. print("Confidence changes for input:", adversarial_emb.asnumpy()[0][input])
  55. print("================================")
  56. print("input:%d, target:%d, adversarial:%d" % (input, target, adversarial))

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