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.

test_natural_robustness.py 16 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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 pytest
  16. import numpy as np
  17. from mindspore import context
  18. from mindarmour.natural_robustness.transform.image import Translate, Curve, Perspective, Scale, Shear, Rotate, \
  19. SaltAndPepperNoise, NaturalNoise, GaussianNoise, UniformNoise, MotionBlur, GaussianBlur, GradientBlur, Contrast,\
  20. GradientLuminance
  21. @pytest.mark.level0
  22. @pytest.mark.platform_x86_cpu
  23. @pytest.mark.env_card
  24. @pytest.mark.component_mindarmour
  25. def test_perspective():
  26. """
  27. Feature: Test image perspective.
  28. Description: Image will be transform for given perspective projection.
  29. Expectation: success.
  30. """
  31. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  32. image = np.random.random((32, 32, 3))
  33. ori_pos = [[0, 0], [0, 800], [800, 0], [800, 800]]
  34. dst_pos = [[50, 0], [0, 800], [780, 0], [800, 800]]
  35. trans = Perspective(ori_pos, dst_pos)
  36. dst = trans(image)
  37. print(dst)
  38. @pytest.mark.level0
  39. @pytest.mark.platform_x86_cpu
  40. @pytest.mark.env_card
  41. @pytest.mark.component_mindarmour
  42. def test_uniform_noise():
  43. """
  44. Feature: Test image uniform noise.
  45. Description: Add uniform image in image.
  46. Expectation: success.
  47. """
  48. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  49. image = np.random.random((32, 32, 3))
  50. trans = UniformNoise(factor=0.1)
  51. dst = trans(image)
  52. print(dst)
  53. @pytest.mark.level0
  54. @pytest.mark.platform_x86_cpu
  55. @pytest.mark.env_card
  56. @pytest.mark.component_mindarmour
  57. def test_gaussian_noise():
  58. """
  59. Feature: Test image gaussian noise.
  60. Description: Add gaussian image in image.
  61. Expectation: success.
  62. """
  63. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  64. image = np.random.random((32, 32, 3))
  65. trans = GaussianNoise(factor=0.1)
  66. dst = trans(image)
  67. print(dst)
  68. @pytest.mark.level0
  69. @pytest.mark.platform_x86_cpu
  70. @pytest.mark.env_card
  71. @pytest.mark.component_mindarmour
  72. def test_contrast():
  73. """
  74. Feature: Test image contrast.
  75. Description: Adjust image contrast.
  76. Expectation: success.
  77. """
  78. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  79. image = np.random.random((32, 32, 3))
  80. trans = Contrast(alpha=0.3, beta=0)
  81. dst = trans(image)
  82. print(dst)
  83. @pytest.mark.level0
  84. @pytest.mark.platform_x86_cpu
  85. @pytest.mark.env_card
  86. @pytest.mark.component_mindarmour
  87. def test_gaussian_blur():
  88. """
  89. Feature: Test image gaussian blur.
  90. Description: Add gaussian blur to image.
  91. Expectation: success.
  92. """
  93. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  94. image = np.random.random((32, 32, 3))
  95. trans = GaussianBlur(ksize=5)
  96. dst = trans(image)
  97. print(dst)
  98. @pytest.mark.level0
  99. @pytest.mark.platform_x86_cpu
  100. @pytest.mark.env_card
  101. @pytest.mark.component_mindarmour
  102. def test_salt_and_pepper_noise():
  103. """
  104. Feature: Test image salt and pepper noise.
  105. Description: Add salt and pepper to image.
  106. Expectation: success.
  107. """
  108. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  109. image = np.random.random((32, 32, 3))
  110. trans = SaltAndPepperNoise(factor=0.01)
  111. dst = trans(image)
  112. print(dst)
  113. @pytest.mark.level0
  114. @pytest.mark.platform_x86_cpu
  115. @pytest.mark.env_card
  116. @pytest.mark.component_mindarmour
  117. def test_translate():
  118. """
  119. Feature: Test image translate.
  120. Description: Translate an image.
  121. Expectation: success.
  122. """
  123. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  124. image = np.random.random((32, 32, 3))
  125. trans = Translate(x_bias=0.1, y_bias=0.1)
  126. dst = trans(image)
  127. print(dst)
  128. @pytest.mark.level0
  129. @pytest.mark.platform_x86_cpu
  130. @pytest.mark.env_card
  131. @pytest.mark.component_mindarmour
  132. def test_scale():
  133. """
  134. Feature: Test image scale.
  135. Description: Scale an image.
  136. Expectation: success.
  137. """
  138. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  139. image = np.random.random((32, 32, 3))
  140. trans = Scale(factor_x=0.7, factor_y=0.7)
  141. dst = trans(image)
  142. print(dst)
  143. @pytest.mark.level0
  144. @pytest.mark.platform_x86_cpu
  145. @pytest.mark.env_card
  146. @pytest.mark.component_mindarmour
  147. def test_shear():
  148. """
  149. Feature: Test image shear.
  150. Description: Shear an image.
  151. Expectation: success.
  152. """
  153. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  154. image = np.random.random((32, 32, 3))
  155. trans = Shear(factor=0.2)
  156. dst = trans(image)
  157. print(dst)
  158. @pytest.mark.level0
  159. @pytest.mark.platform_x86_cpu
  160. @pytest.mark.env_card
  161. @pytest.mark.component_mindarmour
  162. def test_rotate():
  163. """
  164. Feature: Test image rotate.
  165. Description: Rotate an image.
  166. Expectation: success.
  167. """
  168. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  169. image = np.random.random((32, 32, 3))
  170. trans = Rotate(angle=20)
  171. dst = trans(image)
  172. print(dst)
  173. @pytest.mark.level0
  174. @pytest.mark.platform_x86_cpu
  175. @pytest.mark.env_card
  176. @pytest.mark.component_mindarmour
  177. def test_curve():
  178. """
  179. Feature: Test image curve.
  180. Description: Transform an image with curve.
  181. Expectation: success.
  182. """
  183. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  184. image = np.random.random((32, 32, 3))
  185. trans = Curve(curves=1.5, depth=1.5, mode='horizontal')
  186. dst = trans(image)
  187. print(dst)
  188. @pytest.mark.level0
  189. @pytest.mark.platform_x86_cpu
  190. @pytest.mark.env_card
  191. @pytest.mark.component_mindarmour
  192. def test_natural_noise():
  193. """
  194. Feature: Test natural noise.
  195. Description: Add natural noise to an.
  196. Expectation: success.
  197. """
  198. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  199. image = np.random.random((32, 32, 3))
  200. trans = NaturalNoise(ratio=0.0001, k_x_range=(1, 30), k_y_range=(1, 10), auto_param=True)
  201. dst = trans(image)
  202. print(dst)
  203. @pytest.mark.level0
  204. @pytest.mark.platform_x86_cpu
  205. @pytest.mark.env_card
  206. @pytest.mark.component_mindarmour
  207. def test_gradient_luminance():
  208. """
  209. Feature: Test gradient luminance.
  210. Description: Adjust image luminance.
  211. Expectation: success.
  212. """
  213. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  214. image = np.random.random((32, 32, 3))
  215. height, width = image.shape[:2]
  216. point = (height // 4, width // 2)
  217. start = (255, 255, 255)
  218. end = (0, 0, 0)
  219. scope = 0.3
  220. bright_rate = 0.4
  221. trans = GradientLuminance(start, end, start_point=point, scope=scope, pattern='dark', bright_rate=bright_rate,
  222. mode='horizontal')
  223. dst = trans(image)
  224. print(dst)
  225. @pytest.mark.level0
  226. @pytest.mark.platform_x86_cpu
  227. @pytest.mark.env_card
  228. @pytest.mark.component_mindarmour
  229. def test_motion_blur():
  230. """
  231. Feature: Test motion blur.
  232. Description: Add motion blur to an image.
  233. Expectation: success.
  234. """
  235. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  236. image = np.random.random((32, 32, 3))
  237. angle = -10.5
  238. i = 3
  239. trans = MotionBlur(degree=i, angle=angle)
  240. dst = trans(image)
  241. print(dst)
  242. @pytest.mark.level0
  243. @pytest.mark.platform_x86_cpu
  244. @pytest.mark.env_card
  245. @pytest.mark.component_mindarmour
  246. def test_gradient_blur():
  247. """
  248. Feature: Test gradient blur.
  249. Description: Add gradient blur to an image.
  250. Expectation: success.
  251. """
  252. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  253. image = np.random.random((32, 32, 3))
  254. number = 10
  255. h, w = image.shape[:2]
  256. point = (int(h / 5), int(w / 5))
  257. center = False
  258. trans = GradientBlur(point, number, center)
  259. dst = trans(image)
  260. print(dst)
  261. @pytest.mark.level0
  262. @pytest.mark.platform_arm_ascend_training
  263. @pytest.mark.platform_x86_ascend_training
  264. @pytest.mark.env_card
  265. @pytest.mark.component_mindarmour
  266. def test_perspective_ascend():
  267. """
  268. Feature: Test image perspective.
  269. Description: Image will be transform for given perspective projection.
  270. Expectation: success.
  271. """
  272. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  273. image = np.random.random((32, 32, 3))
  274. ori_pos = [[0, 0], [0, 800], [800, 0], [800, 800]]
  275. dst_pos = [[50, 0], [0, 800], [780, 0], [800, 800]]
  276. trans = Perspective(ori_pos, dst_pos)
  277. dst = trans(image)
  278. print(dst)
  279. @pytest.mark.level0
  280. @pytest.mark.platform_arm_ascend_training
  281. @pytest.mark.platform_x86_ascend_training
  282. @pytest.mark.env_card
  283. @pytest.mark.component_mindarmour
  284. def test_uniform_noise_ascend():
  285. """
  286. Feature: Test image uniform noise.
  287. Description: Add uniform image in image.
  288. Expectation: success.
  289. """
  290. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  291. image = np.random.random((32, 32, 3))
  292. trans = UniformNoise(factor=0.1)
  293. dst = trans(image)
  294. print(dst)
  295. @pytest.mark.level0
  296. @pytest.mark.platform_arm_ascend_training
  297. @pytest.mark.platform_x86_ascend_training
  298. @pytest.mark.env_card
  299. @pytest.mark.component_mindarmour
  300. def test_gaussian_noise_ascend():
  301. """
  302. Feature: Test image gaussian noise.
  303. Description: Add gaussian image in image.
  304. Expectation: success.
  305. """
  306. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  307. image = np.random.random((32, 32, 3))
  308. trans = GaussianNoise(factor=0.1)
  309. dst = trans(image)
  310. print(dst)
  311. @pytest.mark.level0
  312. @pytest.mark.platform_arm_ascend_training
  313. @pytest.mark.platform_x86_ascend_training
  314. @pytest.mark.env_card
  315. @pytest.mark.component_mindarmour
  316. def test_contrast_ascend():
  317. """
  318. Feature: Test image contrast.
  319. Description: Adjust image contrast.
  320. Expectation: success.
  321. """
  322. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  323. image = np.random.random((32, 32, 3))
  324. trans = Contrast(alpha=0.3, beta=0)
  325. dst = trans(image)
  326. print(dst)
  327. @pytest.mark.level0
  328. @pytest.mark.platform_arm_ascend_training
  329. @pytest.mark.platform_x86_ascend_training
  330. @pytest.mark.env_card
  331. @pytest.mark.component_mindarmour
  332. def test_gaussian_blur_ascend():
  333. """
  334. Feature: Test image gaussian blur.
  335. Description: Add gaussian blur to image.
  336. Expectation: success.
  337. """
  338. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  339. image = np.random.random((32, 32, 3))
  340. trans = GaussianBlur(ksize=5)
  341. dst = trans(image)
  342. print(dst)
  343. @pytest.mark.level0
  344. @pytest.mark.platform_arm_ascend_training
  345. @pytest.mark.platform_x86_ascend_training
  346. @pytest.mark.env_card
  347. @pytest.mark.component_mindarmour
  348. def test_salt_and_pepper_noise_ascend():
  349. """
  350. Feature: Test image salt and pepper noise.
  351. Description: Add salt and pepper to image.
  352. Expectation: success.
  353. """
  354. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  355. image = np.random.random((32, 32, 3))
  356. trans = SaltAndPepperNoise(factor=0.01)
  357. dst = trans(image)
  358. print(dst)
  359. @pytest.mark.level0
  360. @pytest.mark.platform_arm_ascend_training
  361. @pytest.mark.platform_x86_ascend_training
  362. @pytest.mark.env_card
  363. @pytest.mark.component_mindarmour
  364. def test_translate_ascend():
  365. """
  366. Feature: Test image translate.
  367. Description: Translate an image.
  368. Expectation: success.
  369. """
  370. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  371. image = np.random.random((32, 32, 3))
  372. trans = Translate(x_bias=0.1, y_bias=0.1)
  373. dst = trans(image)
  374. print(dst)
  375. @pytest.mark.level0
  376. @pytest.mark.platform_arm_ascend_training
  377. @pytest.mark.platform_x86_ascend_training
  378. @pytest.mark.env_card
  379. @pytest.mark.component_ascend_mindarmour
  380. def test_scale_ascend():
  381. """
  382. Feature: Test image scale.
  383. Description: Scale an image.
  384. Expectation: success.
  385. """
  386. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  387. image = np.random.random((32, 32, 3))
  388. trans = Scale(factor_x=0.7, factor_y=0.7)
  389. dst = trans(image)
  390. print(dst)
  391. @pytest.mark.level0
  392. @pytest.mark.platform_arm_ascend_training
  393. @pytest.mark.platform_x86_ascend_training
  394. @pytest.mark.env_card
  395. @pytest.mark.component_mindarmour
  396. def test_shear_ascend():
  397. """
  398. Feature: Test image shear.
  399. Description: Shear an image.
  400. Expectation: success.
  401. """
  402. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  403. image = np.random.random((32, 32, 3))
  404. trans = Shear(factor=0.2)
  405. dst = trans(image)
  406. print(dst)
  407. @pytest.mark.level0
  408. @pytest.mark.platform_arm_ascend_training
  409. @pytest.mark.platform_x86_ascend_training
  410. @pytest.mark.env_card
  411. @pytest.mark.component_mindarmour
  412. def test_rotate_ascend():
  413. """
  414. Feature: Test image rotate.
  415. Description: Rotate an image.
  416. Expectation: success.
  417. """
  418. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  419. image = np.random.random((32, 32, 3))
  420. trans = Rotate(angle=20)
  421. dst = trans(image)
  422. print(dst)
  423. @pytest.mark.level0
  424. @pytest.mark.platform_arm_ascend_training
  425. @pytest.mark.platform_x86_ascend_training
  426. @pytest.mark.env_card
  427. @pytest.mark.component_mindarmour
  428. def test_curve_ascend():
  429. """
  430. Feature: Test image curve.
  431. Description: Transform an image with curve.
  432. Expectation: success.
  433. """
  434. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  435. image = np.random.random((32, 32, 3))
  436. trans = Curve(curves=1.5, depth=1.5, mode='horizontal')
  437. dst = trans(image)
  438. print(dst)
  439. @pytest.mark.level0
  440. @pytest.mark.platform_arm_ascend_training
  441. @pytest.mark.platform_x86_ascend_training
  442. @pytest.mark.env_card
  443. @pytest.mark.component_mindarmour
  444. def test_natural_noise_ascend():
  445. """
  446. Feature: Test natural noise.
  447. Description: Add natural noise to an.
  448. Expectation: success.
  449. """
  450. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  451. image = np.random.random((32, 32, 3))
  452. trans = NaturalNoise(ratio=0.0001, k_x_range=(1, 30), k_y_range=(1, 10), auto_param=True)
  453. dst = trans(image)
  454. print(dst)
  455. @pytest.mark.level0
  456. @pytest.mark.platform_arm_ascend_training
  457. @pytest.mark.platform_x86_ascend_training
  458. @pytest.mark.env_card
  459. @pytest.mark.component_mindarmour
  460. def test_gradient_luminance_ascend():
  461. """
  462. Feature: Test gradient luminance.
  463. Description: Adjust image luminance.
  464. Expectation: success.
  465. """
  466. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  467. image = np.random.random((32, 32, 3))
  468. height, width = image.shape[:2]
  469. point = (height // 4, width // 2)
  470. start = (255, 255, 255)
  471. end = (0, 0, 0)
  472. scope = 0.3
  473. bright_rate = 0.4
  474. trans = GradientLuminance(start, end, start_point=point, scope=scope, pattern='dark', bright_rate=bright_rate,
  475. mode='horizontal')
  476. dst = trans(image)
  477. print(dst)
  478. @pytest.mark.level0
  479. @pytest.mark.platform_arm_ascend_training
  480. @pytest.mark.platform_x86_ascend_training
  481. @pytest.mark.env_card
  482. @pytest.mark.component_mindarmour
  483. def test_motion_blur_ascend():
  484. """
  485. Feature: Test motion blur.
  486. Description: Add motion blur to an image.
  487. Expectation: success.
  488. """
  489. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  490. image = np.random.random((32, 32, 3))
  491. angle = -10.5
  492. i = 3
  493. trans = MotionBlur(degree=i, angle=angle)
  494. dst = trans(image)
  495. print(dst)
  496. @pytest.mark.level0
  497. @pytest.mark.platform_arm_ascend_training
  498. @pytest.mark.platform_x86_ascend_training
  499. @pytest.mark.env_card
  500. @pytest.mark.component_mindarmour
  501. def test_gradient_blur_ascend():
  502. """
  503. Feature: Test gradient blur.
  504. Description: Add gradient blur to an image.
  505. Expectation: success.
  506. """
  507. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  508. image = np.random.random((32, 32, 3))
  509. number = 10
  510. h, w = image.shape[:2]
  511. point = (int(h / 5), int(w / 5))
  512. center = False
  513. trans = GradientBlur(point, number, center)
  514. dst = trans(image)
  515. print(dst)

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