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.

sup_privacy.py 7.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. Training example of suppress-based privacy.
  16. """
  17. import os
  18. import mindspore.nn as nn
  19. from mindspore import context
  20. from mindspore.train.callback import ModelCheckpoint
  21. from mindspore.train.callback import CheckpointConfig
  22. from mindspore.train.callback import LossMonitor
  23. from mindspore.nn.metrics import Accuracy
  24. import mindspore.dataset as ds
  25. import mindspore.dataset.vision.c_transforms as CV
  26. import mindspore.dataset.transforms.c_transforms as C
  27. from mindspore.dataset.vision.utils import Inter
  28. import mindspore.common.dtype as mstype
  29. from examples.common.networks.lenet5.lenet5_net import LeNet5
  30. from sup_privacy_config import mnist_cfg as cfg
  31. from mindarmour.privacy.sup_privacy import SuppressModel
  32. from mindarmour.privacy.sup_privacy import SuppressMasker
  33. from mindarmour.privacy.sup_privacy import SuppressPrivacyFactory
  34. from mindarmour.privacy.sup_privacy import MaskLayerDes
  35. from mindarmour.utils.logger import LogUtil
  36. LOGGER = LogUtil.get_instance()
  37. LOGGER.set_level('INFO')
  38. TAG = 'Lenet5_Suppress_train'
  39. def generate_mnist_dataset(data_path, batch_size=32, repeat_size=1, samples=None, num_parallel_workers=1, sparse=True):
  40. """
  41. create dataset for training or testing
  42. """
  43. # define dataset
  44. ds1 = ds.MnistDataset(data_path, num_samples=samples)
  45. # define operation parameters
  46. resize_height, resize_width = 32, 32
  47. rescale = 1.0 / 255.0
  48. shift = 0.0
  49. # define map operations
  50. resize_op = CV.Resize((resize_height, resize_width),
  51. interpolation=Inter.LINEAR)
  52. rescale_op = CV.Rescale(rescale, shift)
  53. hwc2chw_op = CV.HWC2CHW()
  54. type_cast_op = C.TypeCast(mstype.int32)
  55. # apply map operations on images
  56. if not sparse:
  57. one_hot_enco = C.OneHot(10)
  58. ds1 = ds1.map(input_columns="label", operations=one_hot_enco, num_parallel_workers=num_parallel_workers)
  59. type_cast_op = C.TypeCast(mstype.float32)
  60. ds1 = ds1.map(input_columns="label", operations=type_cast_op,
  61. num_parallel_workers=num_parallel_workers)
  62. ds1 = ds1.map(input_columns="image", operations=resize_op,
  63. num_parallel_workers=num_parallel_workers)
  64. ds1 = ds1.map(input_columns="image", operations=rescale_op,
  65. num_parallel_workers=num_parallel_workers)
  66. ds1 = ds1.map(input_columns="image", operations=hwc2chw_op,
  67. num_parallel_workers=num_parallel_workers)
  68. # apply DatasetOps
  69. buffer_size = 10000
  70. ds1 = ds1.shuffle(buffer_size=buffer_size)
  71. ds1 = ds1.batch(batch_size, drop_remainder=True)
  72. ds1 = ds1.repeat(repeat_size)
  73. return ds1
  74. def mnist_suppress_train(epoch_size=10, start_epoch=3, lr=0.05, samples=10000, mask_times=1000,
  75. sparse_thd=0.90, sparse_start=0.0, masklayers=None):
  76. """
  77. local train by suppress-based privacy
  78. """
  79. networks_l5 = LeNet5()
  80. suppress_ctrl_instance = SuppressPrivacyFactory().create(networks_l5,
  81. masklayers,
  82. policy="local_train",
  83. end_epoch=epoch_size,
  84. batch_num=(int)(samples/cfg.batch_size),
  85. start_epoch=start_epoch,
  86. mask_times=mask_times,
  87. lr=lr,
  88. sparse_end=sparse_thd,
  89. sparse_start=sparse_start)
  90. net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
  91. net_opt = nn.SGD(networks_l5.trainable_params(), lr)
  92. config_ck = CheckpointConfig(save_checkpoint_steps=(int)(samples/cfg.batch_size),
  93. keep_checkpoint_max=10)
  94. # Create the SuppressModel model for training.
  95. model_instance = SuppressModel(network=networks_l5,
  96. loss_fn=net_loss,
  97. optimizer=net_opt,
  98. metrics={"Accuracy": Accuracy()})
  99. model_instance.link_suppress_ctrl(suppress_ctrl_instance)
  100. # Create a Masker for Suppress training. The function of the Masker is to
  101. # enforce suppress operation while training.
  102. suppress_masker = SuppressMasker(model=model_instance, suppress_ctrl=suppress_ctrl_instance)
  103. mnist_path = "./MNIST_unzip/" #"../../MNIST_unzip/"
  104. ds_train = generate_mnist_dataset(os.path.join(mnist_path, "train"),
  105. batch_size=cfg.batch_size, repeat_size=1, samples=samples)
  106. ckpoint_cb = ModelCheckpoint(prefix="checkpoint_lenet",
  107. directory="./trained_ckpt_file/",
  108. config=config_ck)
  109. print("============== Starting SUPP Training ==============")
  110. model_instance.train(epoch_size, ds_train, callbacks=[ckpoint_cb, LossMonitor(), suppress_masker],
  111. dataset_sink_mode=False)
  112. print("============== Starting SUPP Testing ==============")
  113. ds_eval = generate_mnist_dataset(os.path.join(mnist_path, 'test'),
  114. batch_size=cfg.batch_size)
  115. acc = model_instance.eval(ds_eval, dataset_sink_mode=False)
  116. print("============== SUPP Accuracy: %s ==============", acc)
  117. suppress_ctrl_instance.print_paras()
  118. if __name__ == "__main__":
  119. # This configure can run in pynative mode
  120. context.set_context(mode=context.PYNATIVE_MODE, device_target=cfg.device_target)
  121. masklayers_lenet5 = [] # determine which layer should be masked
  122. masklayers_lenet5.append(MaskLayerDes("conv1.weight", 0, False, True, 10))
  123. masklayers_lenet5.append(MaskLayerDes("conv2.weight", 1, False, True, 150))
  124. masklayers_lenet5.append(MaskLayerDes("fc1.weight", 2, True, False, -1))
  125. masklayers_lenet5.append(MaskLayerDes("fc2.weight", 4, True, False, -1))
  126. masklayers_lenet5.append(MaskLayerDes("fc3.weight", 6, True, False, 50))
  127. # do suppreess privacy train, with stronger privacy protection and better performance than Differential Privacy
  128. mnist_suppress_train(10, 3, 0.10, 60000, 1000, 0.95, 0.0, masklayers=masklayers_lenet5) # used

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