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.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # Copyright 2022 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.transform.image import Translate, Curve, Perspective, Scale, Shear, Rotate, SaltAndPepperNoise, \
  18. NaturalNoise, GaussianNoise, UniformNoise, MotionBlur, GaussianBlur, GradientBlur, Contrast, GradientLuminance
  19. def test_perspective(image):
  20. """Test perspective."""
  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_uniform_noise(image):
  28. """Test uniform noise."""
  29. trans = UniformNoise(factor=0.1)
  30. dst = trans(image)
  31. cv2.imshow('dst', dst)
  32. cv2.waitKey()
  33. def test_gaussian_noise(image):
  34. """Test gaussian noise."""
  35. trans = GaussianNoise(factor=0.1)
  36. dst = trans(image)
  37. cv2.imshow('dst', dst)
  38. cv2.waitKey()
  39. def test_contrast(image):
  40. """Test contrast."""
  41. trans = Contrast(alpha=2, beta=0)
  42. dst = trans(image)
  43. cv2.imshow('dst', dst)
  44. cv2.waitKey()
  45. def test_gaussian_blur(image):
  46. """Test gaussian blur."""
  47. trans = GaussianBlur(ksize=5)
  48. dst = trans(image)
  49. cv2.imshow('dst', dst)
  50. cv2.waitKey()
  51. def test_salt_and_pepper_noise(image):
  52. """Test salt and pepper noise."""
  53. trans = SaltAndPepperNoise(factor=0.01)
  54. dst = trans(image)
  55. cv2.imshow('dst', dst)
  56. cv2.waitKey()
  57. def test_translate(image):
  58. """Test translate."""
  59. trans = Translate(x_bias=0.1, y_bias=0.1)
  60. dst = trans(image)
  61. cv2.imshow('dst', dst)
  62. cv2.waitKey()
  63. def test_scale(image):
  64. """Test scale."""
  65. trans = Scale(factor_x=0.7, factor_y=0.7)
  66. dst = trans(image)
  67. cv2.imshow('dst', dst)
  68. cv2.waitKey()
  69. def test_shear(image):
  70. """Test shear."""
  71. trans = Shear(factor=0.2)
  72. dst = trans(image)
  73. cv2.imshow('dst', dst)
  74. cv2.waitKey()
  75. def test_rotate(image):
  76. """Test rotate."""
  77. trans = Rotate(angle=20)
  78. dst = trans(image)
  79. cv2.imshow('dst', dst)
  80. cv2.waitKey()
  81. def test_curve(image):
  82. """Test curve."""
  83. trans = Curve(curves=2, depth=1.5, mode='horizontal')
  84. dst = trans(image)
  85. cv2.imshow('dst', dst)
  86. cv2.waitKey()
  87. def test_natural_noise(image):
  88. """Test natural noise."""
  89. trans = NaturalNoise(ratio=0.0001, k_x_range=(1, 30), k_y_range=(1, 10), auto_param=True)
  90. dst = trans(image)
  91. cv2.imshow('dst', dst)
  92. cv2.waitKey()
  93. def test_gradient_luminance(image):
  94. """Test gradient luminance."""
  95. height, width = image.shape[:2]
  96. point = (height // 4, width // 2)
  97. start = (255, 255, 255)
  98. end = (0, 0, 0)
  99. scope = 0.3
  100. bright_rate = 0.4
  101. trans = GradientLuminance(start, end, start_point=point, scope=scope, pattern='dark', bright_rate=bright_rate,
  102. mode='horizontal')
  103. dst = trans(image)
  104. cv2.imshow('dst', dst)
  105. cv2.waitKey()
  106. def test_motion_blur(image):
  107. """Test motion blur."""
  108. angle = -10.5
  109. i = 10
  110. trans = MotionBlur(degree=i, angle=angle)
  111. dst = trans(image)
  112. cv2.imshow('dst', dst)
  113. cv2.waitKey()
  114. def test_gradient_blur(image):
  115. """Test gradient blur."""
  116. number = 10
  117. h, w = image.shape[:2]
  118. point = (int(h / 5), int(w / 5))
  119. center = False
  120. trans = GradientBlur(point, number, center)
  121. dst = trans(image)
  122. cv2.imshow('dst', dst)
  123. cv2.waitKey()
  124. if __name__ == '__main__':
  125. img = cv2.imread('1.jpeg')
  126. img = np.array(img)
  127. test_uniform_noise(img)
  128. test_gaussian_noise(img)
  129. test_motion_blur(img)
  130. test_gradient_blur(img)
  131. test_gradient_luminance(img)
  132. test_natural_noise(img)
  133. test_curve(img)
  134. test_rotate(img)
  135. test_shear(img)
  136. test_scale(img)
  137. test_translate(img)
  138. test_salt_and_pepper_noise(img)
  139. test_gaussian_blur(img)
  140. test_contrast(img)
  141. test_perspective(img)

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