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.

model_fault_injection.py 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Copyright 2021 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. Fault injection example.
  16. Download checkpoint from: https://www.mindspore.cn/resources/hub or just trained your own checkpoint.
  17. Download dataset from: http://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz.
  18. File structure:
  19. --cifar10-batches-bin
  20. --train
  21. --data_batch_1.bin
  22. --data_batch_2.bin
  23. --data_batch_3.bin
  24. --data_batch_4.bin
  25. --data_batch_5.bin
  26. --test
  27. --test_batch.bin
  28. Please extract and restructure the file as shown above.
  29. """
  30. import argparse
  31. import numpy as np
  32. from mindspore import Model, context
  33. from mindspore.train.serialization import load_checkpoint
  34. from mindarmour.reliability.model_fault_injection.fault_injection import FaultInjector
  35. from examples.common.networks.lenet5.lenet5_net import LeNet5
  36. from examples.common.networks.vgg.vgg import vgg16
  37. from examples.common.networks.resnet.resnet import resnet50
  38. from examples.common.dataset.data_processing import create_dataset_cifar, generate_mnist_dataset
  39. parser = argparse.ArgumentParser(description='layer_states')
  40. parser.add_argument('--device_target', type=str, default="CPU", choices=['Ascend', 'GPU', 'CPU'])
  41. parser.add_argument('--model', type=str, default='lenet', choices=['lenet', 'resnet50', 'vgg16'])
  42. parser.add_argument('--device_id', type=int, default=0)
  43. args = parser.parse_args()
  44. context.set_context(mode=context.GRAPH_MODE, device_target=args.device_target, device_id=args.device_id)
  45. test_flag = args.model
  46. if test_flag == 'lenet':
  47. # load data
  48. DATA_FILE = '../common/dataset/MNIST_Data/test'
  49. ckpt_path = '../common/networks/lenet5/trained_ckpt_file/checkpoint_lenet-10_1875.ckpt'
  50. ds_eval = generate_mnist_dataset(DATA_FILE, batch_size=64)
  51. net = LeNet5()
  52. elif test_flag == 'vgg16':
  53. from examples.common.networks.vgg.config import cifar_cfg as cfg
  54. DATA_FILE = '../common/dataset/cifar10-batches-bin'
  55. ckpt_path = '../common/networks/vgg16_ascend_v111_cifar10_offical_cv_bs64_acc93.ckpt'
  56. ds_eval = create_dataset_cifar(DATA_FILE, 224, 224, training=False)
  57. net = vgg16(10, cfg, 'test')
  58. elif test_flag == 'resnet50':
  59. DATA_FILE = '../common/dataset/cifar10-batches-bin'
  60. ckpt_path = '../common/networks/resnet50_ascend_v111_cifar10_offical_cv_bs32_acc92.ckpt'
  61. ds_eval = create_dataset_cifar(DATA_FILE, 224, 224, training=False)
  62. net = resnet50(10)
  63. else:
  64. exit()
  65. test_images = []
  66. test_labels = []
  67. for data in ds_eval.create_tuple_iterator(output_numpy=True):
  68. images = data[0].astype(np.float32)
  69. labels = data[1]
  70. test_images.append(images)
  71. test_labels.append(labels)
  72. ds_data = np.concatenate(test_images, axis=0)
  73. ds_label = np.concatenate(test_labels, axis=0)
  74. param_dict = load_checkpoint(ckpt_path, net=net)
  75. model = Model(net)
  76. # Initialization
  77. fi_type = ['bitflips_random', 'bitflips_designated', 'random', 'zeros',
  78. 'nan', 'inf', 'anti_activation', 'precision_loss']
  79. fi_mode = ['single_layer', 'all_layer']
  80. fi_size = [1, 2, 3]
  81. # Fault injection
  82. fi = FaultInjector(model, fi_type, fi_mode, fi_size)
  83. results = fi.kick_off(ds_data, ds_label, iter_times=100)
  84. result_summary = fi.metrics()
  85. # print result
  86. for result in results:
  87. print(result)
  88. for result in result_summary:
  89. print(result)

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