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.

data_processing.py 2.4 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Copyright 2019 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. import mindspore.dataset as ds
  15. import mindspore.dataset.transforms.vision.c_transforms as CV
  16. import mindspore.dataset.transforms.c_transforms as C
  17. from mindspore.dataset.transforms.vision import Inter
  18. import mindspore.common.dtype as mstype
  19. def generate_mnist_dataset(data_path, batch_size=32, repeat_size=1,
  20. num_parallel_workers=1, sparse=True):
  21. """
  22. create dataset for training or testing
  23. """
  24. # define dataset
  25. ds1 = ds.MnistDataset(data_path)
  26. # define operation parameters
  27. resize_height, resize_width = 32, 32
  28. rescale = 1.0 / 255.0
  29. shift = 0.0
  30. # define map operations
  31. resize_op = CV.Resize((resize_height, resize_width),
  32. interpolation=Inter.LINEAR)
  33. rescale_op = CV.Rescale(rescale, shift)
  34. hwc2chw_op = CV.HWC2CHW()
  35. type_cast_op = C.TypeCast(mstype.int32)
  36. one_hot_enco = C.OneHot(10)
  37. # apply map operations on images
  38. if not sparse:
  39. ds1 = ds1.map(input_columns="label", operations=one_hot_enco,
  40. num_parallel_workers=num_parallel_workers)
  41. type_cast_op = C.TypeCast(mstype.float32)
  42. ds1 = ds1.map(input_columns="label", operations=type_cast_op,
  43. num_parallel_workers=num_parallel_workers)
  44. ds1 = ds1.map(input_columns="image", operations=resize_op,
  45. num_parallel_workers=num_parallel_workers)
  46. ds1 = ds1.map(input_columns="image", operations=rescale_op,
  47. num_parallel_workers=num_parallel_workers)
  48. ds1 = ds1.map(input_columns="image", operations=hwc2chw_op,
  49. num_parallel_workers=num_parallel_workers)
  50. # apply DatasetOps
  51. buffer_size = 10000
  52. ds1 = ds1.shuffle(buffer_size=buffer_size)
  53. ds1 = ds1.batch(batch_size, drop_remainder=True)
  54. ds1 = ds1.repeat(repeat_size)
  55. return ds1

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