Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MindSpore
mindarmour
提交
a4a2f5d2
M
mindarmour
项目概览
MindSpore
/
mindarmour
通知
4
Star
2
Fork
3
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
mindarmour
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
a4a2f5d2
编写于
8月 22, 2020
作者:
J
jin-xiulang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix Three issues.
Fix Three issues.
上级
3f35041b
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
30 addition
and
8 deletion
+30
-8
mindarmour/fuzzing/fuzzing.py
mindarmour/fuzzing/fuzzing.py
+24
-5
mindarmour/fuzzing/model_coverage_metrics.py
mindarmour/fuzzing/model_coverage_metrics.py
+6
-3
未找到文件。
mindarmour/fuzzing/fuzzing.py
浏览文件 @
a4a2f5d2
...
@@ -22,7 +22,8 @@ from mindspore import Tensor
...
@@ -22,7 +22,8 @@ from mindspore import Tensor
from
mindarmour.fuzzing.model_coverage_metrics
import
ModelCoverageMetrics
from
mindarmour.fuzzing.model_coverage_metrics
import
ModelCoverageMetrics
from
mindarmour.utils._check_param
import
check_model
,
check_numpy_param
,
\
from
mindarmour.utils._check_param
import
check_model
,
check_numpy_param
,
\
check_param_multi_types
,
check_norm_level
,
check_param_in_range
check_param_multi_types
,
check_norm_level
,
check_param_in_range
,
\
check_param_type
,
check_int_positive
from
mindarmour.fuzzing.image_transform
import
Contrast
,
Brightness
,
Blur
,
\
from
mindarmour.fuzzing.image_transform
import
Contrast
,
Brightness
,
Blur
,
\
Noise
,
Translate
,
Scale
,
Shear
,
Rotate
Noise
,
Translate
,
Scale
,
Shear
,
Rotate
from
mindarmour.attacks
import
FastGradientSignMethod
,
\
from
mindarmour.attacks
import
FastGradientSignMethod
,
\
...
@@ -185,7 +186,6 @@ class Fuzzer:
...
@@ -185,7 +186,6 @@ class Fuzzer:
ValueError: If metric in list `eval_metrics` is not in ['accuracy', 'attack_success_rate',
ValueError: If metric in list `eval_metrics` is not in ['accuracy', 'attack_success_rate',
'kmnc', 'nbc', 'snac'].
'kmnc', 'nbc', 'snac'].
"""
"""
eval_metrics_
=
None
if
isinstance
(
eval_metrics
,
(
list
,
tuple
)):
if
isinstance
(
eval_metrics
,
(
list
,
tuple
)):
eval_metrics_
=
[]
eval_metrics_
=
[]
avaliable_metrics
=
[
'accuracy'
,
'attack_success_rate'
,
'kmnc'
,
'nbc'
,
'snac'
]
avaliable_metrics
=
[
'accuracy'
,
'attack_success_rate'
,
'kmnc'
,
'nbc'
,
'snac'
]
...
@@ -215,7 +215,26 @@ class Fuzzer:
...
@@ -215,7 +215,26 @@ class Fuzzer:
raise
TypeError
(
msg
)
raise
TypeError
(
msg
)
# Check whether the mutate_config meet the specification.
# Check whether the mutate_config meet the specification.
mutate_config
=
check_param_type
(
'mutate_config'
,
mutate_config
,
list
)
for
method
in
mutate_config
:
check_param_type
(
"method['params']"
,
method
[
'params'
],
dict
)
if
coverage_metric
not
in
[
'KMNC'
,
'NBC'
,
'SNAC'
]:
msg
=
"coverage_metric must be in ['KMNC', 'NBC', 'SNAC'], but got {}."
\
.
format
(
coverage_metric
)
LOGGER
.
error
(
TAG
,
msg
)
raise
ValueError
(
msg
)
max_iters
=
check_int_positive
(
'max_iters'
,
max_iters
)
mutate_num_per_seed
=
check_int_positive
(
'mutate_num_per_seed'
,
mutate_num_per_seed
)
mutates
=
self
.
_init_mutates
(
mutate_config
)
mutates
=
self
.
_init_mutates
(
mutate_config
)
initial_seeds
=
check_param_type
(
'initial_seeds'
,
initial_seeds
,
list
)
for
seed
in
initial_seeds
:
check_param_type
(
'seed'
,
seed
,
list
)
check_numpy_param
(
'seed[0]'
,
seed
[
0
])
check_numpy_param
(
'seed[1]'
,
seed
[
1
])
if
seed
[
2
]
!=
0
:
msg
=
"initial seed[2] must be 0, but got {}."
.
format
(
seed
[
2
])
LOGGER
.
error
(
TAG
,
msg
)
raise
ValueError
(
msg
)
seed
,
initial_seeds
=
_select_next
(
initial_seeds
)
seed
,
initial_seeds
=
_select_next
(
initial_seeds
)
fuzz_samples
=
[]
fuzz_samples
=
[]
gt_labels
=
[]
gt_labels
=
[]
...
@@ -260,7 +279,7 @@ class Fuzzer:
...
@@ -260,7 +279,7 @@ class Fuzzer:
for
index
in
range
(
len
(
samples
)):
for
index
in
range
(
len
(
samples
)):
mutate
=
samples
[:
index
+
1
]
mutate
=
samples
[:
index
+
1
]
self
.
_coverage_metrics
.
calculate_coverage
(
mutate
.
astype
(
np
.
float32
))
self
.
_coverage_metrics
.
calculate_coverage
(
mutate
.
astype
(
np
.
float32
))
if
coverage_metric
==
"KMNC"
:
if
coverage_metric
==
'KMNC'
:
coverages
.
append
(
self
.
_coverage_metrics
.
get_kmnc
())
coverages
.
append
(
self
.
_coverage_metrics
.
get_kmnc
())
if
coverage_metric
==
'NBC'
:
if
coverage_metric
==
'NBC'
:
coverages
.
append
(
self
.
_coverage_metrics
.
get_nbc
())
coverages
.
append
(
self
.
_coverage_metrics
.
get_nbc
())
...
@@ -369,11 +388,11 @@ class Fuzzer:
...
@@ -369,11 +388,11 @@ class Fuzzer:
dict, evaluate metrics include accuarcy, attack success rate
dict, evaluate metrics include accuarcy, attack success rate
and neural coverage.
and neural coverage.
"""
"""
gt_labels
=
np
.
asarray
(
gt_labels
)
fuzz_preds
=
np
.
asarray
(
fuzz_preds
)
temp
=
np
.
argmax
(
gt_labels
,
axis
=
1
)
==
np
.
argmax
(
fuzz_preds
,
axis
=
1
)
temp
=
np
.
argmax
(
gt_labels
,
axis
=
1
)
==
np
.
argmax
(
fuzz_preds
,
axis
=
1
)
metrics_report
=
{}
metrics_report
=
{}
if
metrics
==
'auto'
or
'accuracy'
in
metrics
:
if
metrics
==
'auto'
or
'accuracy'
in
metrics
:
gt_labels
=
np
.
asarray
(
gt_labels
)
fuzz_preds
=
np
.
asarray
(
fuzz_preds
)
acc
=
np
.
sum
(
temp
)
/
np
.
size
(
temp
)
acc
=
np
.
sum
(
temp
)
/
np
.
size
(
temp
)
metrics_report
[
'Accuracy'
]
=
acc
metrics_report
[
'Accuracy'
]
=
acc
...
...
mindarmour/fuzzing/model_coverage_metrics.py
浏览文件 @
a4a2f5d2
...
@@ -21,7 +21,7 @@ from mindspore import Tensor
...
@@ -21,7 +21,7 @@ from mindspore import Tensor
from
mindspore
import
Model
from
mindspore
import
Model
from
mindarmour.utils._check_param
import
check_model
,
check_numpy_param
,
\
from
mindarmour.utils._check_param
import
check_model
,
check_numpy_param
,
\
check_int_positive
check_int_positive
,
check_param_multi_types
from
mindarmour.utils.logger
import
LogUtil
from
mindarmour.utils.logger
import
LogUtil
LOGGER
=
LogUtil
.
get_instance
()
LOGGER
=
LogUtil
.
get_instance
()
...
@@ -52,8 +52,9 @@ class ModelCoverageMetrics:
...
@@ -52,8 +52,9 @@ class ModelCoverageMetrics:
ValueError: If neuron_num is too big (for example, bigger than 1e+9).
ValueError: If neuron_num is too big (for example, bigger than 1e+9).
Examples:
Examples:
>>> train_images = np.random.random((10000, 128)).astype(np.float32)
>>> net = LeNet5()
>>> test_images = np.random.random((5000, 128)).astype(np.float32)
>>> train_images = np.random.random((10000, 1, 32, 32)).astype(np.float32)
>>> test_images = np.random.random((5000, 1, 32, 32)).astype(np.float32)
>>> model = Model(net)
>>> model = Model(net)
>>> model_fuzz_test = ModelCoverageMetrics(model, 10000, 10, train_images)
>>> model_fuzz_test = ModelCoverageMetrics(model, 10000, 10, train_images)
>>> model_fuzz_test.calculate_coverage(test_images)
>>> model_fuzz_test.calculate_coverage(test_images)
...
@@ -148,8 +149,10 @@ class ModelCoverageMetrics:
...
@@ -148,8 +149,10 @@ class ModelCoverageMetrics:
>>> model_fuzz_test = ModelCoverageMetrics(model, 10000, 10, train_images)
>>> model_fuzz_test = ModelCoverageMetrics(model, 10000, 10, train_images)
>>> model_fuzz_test.calculate_coverage(test_images)
>>> model_fuzz_test.calculate_coverage(test_images)
"""
"""
dataset
=
check_numpy_param
(
'dataset'
,
dataset
)
dataset
=
check_numpy_param
(
'dataset'
,
dataset
)
batch_size
=
check_int_positive
(
'batch_size'
,
batch_size
)
batch_size
=
check_int_positive
(
'batch_size'
,
batch_size
)
bias_coefficient
=
check_param_multi_types
(
'bias_coefficient'
,
bias_coefficient
,
[
int
,
float
])
self
.
_lower_bounds
-=
bias_coefficient
*
self
.
_var
self
.
_lower_bounds
-=
bias_coefficient
*
self
.
_var
self
.
_upper_bounds
+=
bias_coefficient
*
self
.
_var
self
.
_upper_bounds
+=
bias_coefficient
*
self
.
_var
intervals
=
(
self
.
_upper_bounds
-
self
.
_lower_bounds
)
/
self
.
_segmented_num
intervals
=
(
self
.
_upper_bounds
-
self
.
_lower_bounds
)
/
self
.
_segmented_num
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录