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.

lenet5_dp.py 7.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. Training example of adaClip-mechanism differential 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. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  25. import mindspore.dataset as ds
  26. import mindspore.dataset.vision.c_transforms as CV
  27. import mindspore.dataset.transforms.c_transforms as C
  28. from mindspore.dataset.vision import Inter
  29. import mindspore.common.dtype as mstype
  30. from mindarmour.privacy.diff_privacy import DPModel
  31. from mindarmour.privacy.diff_privacy import PrivacyMonitorFactory
  32. from mindarmour.privacy.diff_privacy import NoiseMechanismsFactory
  33. from mindarmour.privacy.diff_privacy import ClipMechanismsFactory
  34. from mindarmour.utils.logger import LogUtil
  35. from examples.common.networks.lenet5.lenet5_net import LeNet5
  36. from lenet5_config import mnist_cfg as cfg
  37. LOGGER = LogUtil.get_instance()
  38. LOGGER.set_level('INFO')
  39. TAG = 'Lenet5_train'
  40. def generate_mnist_dataset(data_path, batch_size=32, repeat_size=1,
  41. num_parallel_workers=1, sparse=True):
  42. """
  43. create dataset for training or testing
  44. """
  45. # define dataset
  46. ds1 = ds.MnistDataset(data_path)
  47. # define operation parameters
  48. resize_height, resize_width = 32, 32
  49. rescale = 1.0 / 255.0
  50. shift = 0.0
  51. # define map operations
  52. resize_op = CV.Resize((resize_height, resize_width),
  53. interpolation=Inter.LINEAR)
  54. rescale_op = CV.Rescale(rescale, shift)
  55. hwc2chw_op = CV.HWC2CHW()
  56. type_cast_op = C.TypeCast(mstype.int32)
  57. # apply map operations on images
  58. if not sparse:
  59. one_hot_enco = C.OneHot(10)
  60. ds1 = ds1.map(input_columns="label", operations=one_hot_enco,
  61. num_parallel_workers=num_parallel_workers)
  62. type_cast_op = C.TypeCast(mstype.float32)
  63. ds1 = ds1.map(input_columns="label", operations=type_cast_op,
  64. num_parallel_workers=num_parallel_workers)
  65. ds1 = ds1.map(input_columns="image", operations=resize_op,
  66. num_parallel_workers=num_parallel_workers)
  67. ds1 = ds1.map(input_columns="image", operations=rescale_op,
  68. num_parallel_workers=num_parallel_workers)
  69. ds1 = ds1.map(input_columns="image", operations=hwc2chw_op,
  70. num_parallel_workers=num_parallel_workers)
  71. # apply DatasetOps
  72. buffer_size = 10000
  73. ds1 = ds1.shuffle(buffer_size=buffer_size)
  74. ds1 = ds1.batch(batch_size, drop_remainder=True)
  75. ds1 = ds1.repeat(repeat_size)
  76. return ds1
  77. if __name__ == "__main__":
  78. # This configure can run both in pynative mode and graph mode
  79. context.set_context(mode=context.GRAPH_MODE,
  80. device_target=cfg.device_target)
  81. network = LeNet5()
  82. net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
  83. config_ck = CheckpointConfig(
  84. save_checkpoint_steps=cfg.save_checkpoint_steps,
  85. keep_checkpoint_max=cfg.keep_checkpoint_max)
  86. ckpoint_cb = ModelCheckpoint(prefix="checkpoint_lenet",
  87. directory='./trained_ckpt_file/',
  88. config=config_ck)
  89. # get training dataset
  90. ds_train = generate_mnist_dataset(os.path.join(cfg.data_path, "train"),
  91. cfg.batch_size)
  92. if cfg.micro_batches and cfg.batch_size % cfg.micro_batches != 0:
  93. raise ValueError(
  94. "Number of micro_batches should divide evenly batch_size")
  95. # Create a factory class of DP noise mechanisms, this method is adding noise
  96. # in gradients while training. Initial_noise_multiplier is suggested to be
  97. # greater than 1.0, otherwise the privacy budget would be huge, which means
  98. # that the privacy protection effect is weak. Mechanisms can be 'Gaussian'
  99. # or 'AdaGaussian', in which noise would be decayed with 'AdaGaussian'
  100. # mechanism while be constant with 'Gaussian' mechanism.
  101. noise_mech = NoiseMechanismsFactory().create(cfg.noise_mechanisms,
  102. norm_bound=cfg.norm_bound,
  103. initial_noise_multiplier=cfg.initial_noise_multiplier,
  104. decay_policy=None)
  105. # Create a factory class of clip mechanisms, this method is to adaptive clip
  106. # gradients while training, decay_policy support 'Linear' and 'Geometric',
  107. # learning_rate is the learning rate to update clip_norm,
  108. # target_unclipped_quantile is the target quantile of norm clip,
  109. # fraction_stddev is the stddev of Gaussian normal which used in
  110. # empirical_fraction, the formula is
  111. # $empirical_fraction + N(0, fraction_stddev)$.
  112. clip_mech = ClipMechanismsFactory().create(cfg.clip_mechanisms,
  113. decay_policy=cfg.clip_decay_policy,
  114. learning_rate=cfg.clip_learning_rate,
  115. target_unclipped_quantile=cfg.target_unclipped_quantile,
  116. fraction_stddev=cfg.fraction_stddev)
  117. net_opt = nn.Momentum(params=network.trainable_params(),
  118. learning_rate=cfg.lr, momentum=cfg.momentum)
  119. # Create a monitor for DP training. The function of the monitor is to
  120. # compute and print the privacy budget(eps and delta) while training.
  121. rdp_monitor = PrivacyMonitorFactory.create('rdp',
  122. num_samples=60000,
  123. batch_size=cfg.batch_size,
  124. initial_noise_multiplier=cfg.initial_noise_multiplier,
  125. per_print_times=234,
  126. noise_decay_mode=None)
  127. # Create the DP model for training.
  128. model = DPModel(micro_batches=cfg.micro_batches,
  129. norm_bound=cfg.norm_bound,
  130. noise_mech=noise_mech,
  131. clip_mech=clip_mech,
  132. network=network,
  133. loss_fn=net_loss,
  134. optimizer=net_opt,
  135. metrics={"Accuracy": Accuracy()})
  136. LOGGER.info(TAG, "============== Starting Training ==============")
  137. model.train(cfg['epoch_size'], ds_train,
  138. callbacks=[ckpoint_cb, LossMonitor(), rdp_monitor],
  139. dataset_sink_mode=cfg.dataset_sink_mode)
  140. LOGGER.info(TAG, "============== Starting Testing ==============")
  141. ckpt_file_name = 'trained_ckpt_file/checkpoint_lenet-10_234.ckpt'
  142. param_dict = load_checkpoint(ckpt_file_name)
  143. load_param_into_net(network, param_dict)
  144. ds_eval = generate_mnist_dataset(os.path.join(cfg.data_path, 'test'),
  145. batch_size=cfg.batch_size)
  146. acc = model.eval(ds_eval, dataset_sink_mode=False)
  147. LOGGER.info(TAG, "============== Accuracy: %s ==============", acc)

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