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.

natural_robustness_example.py 4.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. """Example for natural robustness methods."""
  15. import numpy as np
  16. import cv2
  17. from mindarmour.natural_robustness.natural_noise import Perlin, Perspective, Scale, Shear, SaltAndPepperNoise, \
  18. BackgroundWord, BackShadow, MotionBlur, GaussianBlur, GradientBlur, Rotate, Contrast, Translate, Curve, \
  19. GradientLuminance, NaturalNoise
  20. def test_perspective(image):
  21. ori_pos = [[0, 0], [0, 800], [800, 0], [800, 800]]
  22. dst_pos = [[50, 0], [0, 800], [780, 0], [800, 800]]
  23. trans = Perspective(ori_pos, dst_pos)
  24. dst = trans(image)
  25. cv2.imshow('dst', dst)
  26. cv2.waitKey()
  27. def test_constract(image):
  28. trans = Contrast(alpha=1.5, beta=0)
  29. dst = trans(image)
  30. cv2.imshow('dst', dst)
  31. cv2.waitKey()
  32. def test_gaussian_blur(image):
  33. trans = GaussianBlur(ksize=5)
  34. dst = trans(image)
  35. cv2.imshow('dst', dst)
  36. cv2.waitKey()
  37. def test_salt_and_pepper_noise(image):
  38. trans = SaltAndPepperNoise(factor=0.01)
  39. dst = trans(image)
  40. cv2.imshow('dst', dst)
  41. cv2.waitKey()
  42. def test_translate(image):
  43. trans = Translate(x_bias=0.1, y_bias=0.1)
  44. dst = trans(image)
  45. cv2.imshow('dst', dst)
  46. cv2.waitKey()
  47. def test_scale(image):
  48. trans = Scale(factor_x=0.7, factor_y=0.7)
  49. dst = trans(image)
  50. cv2.imshow('dst', dst)
  51. cv2.waitKey()
  52. def test_shear(image):
  53. trans = Shear(factor=0.2)
  54. dst = trans(image)
  55. cv2.imshow('dst', dst)
  56. cv2.waitKey()
  57. def test_rotate(image):
  58. trans = Rotate(angle=20)
  59. dst = trans(image)
  60. cv2.imshow('dst', dst)
  61. cv2.waitKey()
  62. def test_background_word(image):
  63. trans = BackgroundWord(shade=0.1)
  64. dst = trans(image)
  65. cv2.imshow('dst', dst)
  66. cv2.waitKey()
  67. def test_curve(image):
  68. trans = Curve(curves=1.5, depth=1.5, mode='horizontal')
  69. dst = trans(image)
  70. cv2.imshow('dst', dst)
  71. cv2.waitKey()
  72. def test_natural_noise(image):
  73. trans = NaturalNoise(ratio=0.0001, k_x_range=(1, 30), k_y_range=(1, 10))
  74. dst = trans(image)
  75. cv2.imshow('dst', dst)
  76. cv2.waitKey()
  77. def test_back_shadow(image):
  78. image = np.array(image)
  79. template_path = 'test_data/template/leaf'
  80. shade = 0.2
  81. trans = BackShadow(template_path, shade=shade)
  82. dst = trans(image)
  83. cv2.imshow('dst', dst)
  84. cv2.waitKey()
  85. def test_perlin(image):
  86. image = np.array(image)
  87. shade = 0.5
  88. ratio = 0.3
  89. trans = Perlin(ratio, shade)
  90. dst = trans(image)
  91. cv2.imshow('dst', dst)
  92. cv2.waitKey()
  93. def test_gradient_luminance(image):
  94. height, width = image.shape[:2]
  95. point = (height // 4, width // 2)
  96. start = (255, 255, 255)
  97. end = (0, 0, 0)
  98. scope = 0.3
  99. bright_rate = 0.4
  100. trans = GradientLuminance(start, end, start_point=point, scope=scope, pattern='dark', bright_rate=bright_rate,
  101. mode='horizontal')
  102. dst = trans(image)
  103. cv2.imshow('dst', dst)
  104. cv2.waitKey()
  105. def test_motion_blur(image):
  106. angle = -10.5
  107. i = 3
  108. trans = MotionBlur(degree=i, angle=angle)
  109. dst = trans(image)
  110. cv2.imshow('dst', dst)
  111. cv2.waitKey()
  112. def test_gradient_blur(image):
  113. number = 4
  114. h, w = image.shape[:2]
  115. point = (int(h / 5), int(w / 5))
  116. center = True
  117. trans = GradientBlur(point, number, center)
  118. dst = trans(image)
  119. cv2.imshow('dst', dst)
  120. cv2.waitKey()
  121. if __name__ == '__main__':
  122. img = cv2.imread('test_data/1.png')
  123. img = np.array(img)
  124. test_motion_blur(img)
  125. test_gradient_blur(img)
  126. test_gradient_luminance(img)
  127. test_perlin(img)
  128. test_back_shadow(img)
  129. test_natural_noise(img)
  130. test_curve(img)
  131. test_background_word(img)
  132. test_rotate(img)
  133. test_shear(img)
  134. test_scale(img)
  135. test_translate(img)
  136. test_salt_and_pepper_noise(img)
  137. test_gaussian_blur(img)
  138. test_constract(img)
  139. test_perspective(img)

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