Browse Source

!121 fix problems in review comments about fuzz testting

Merge pull request !121 from ZhidanLiu/master
tags/v1.0.0
mindspore-ci-bot Gitee 4 years ago
parent
commit
5e02eeba8b
3 changed files with 53 additions and 43 deletions
  1. +19
    -13
      mindarmour/fuzz_testing/fuzzing.py
  2. +30
    -26
      mindarmour/fuzz_testing/image_transform.py
  3. +4
    -4
      mindarmour/privacy/evaluation/membership_inference.py

+ 19
- 13
mindarmour/fuzz_testing/fuzzing.py View File

@@ -254,6 +254,10 @@ class Fuzzer:
raise ValueError(msg) raise ValueError(msg)
for seed in initial_seeds: for seed in initial_seeds:
check_param_type('seed', seed, list) check_param_type('seed', seed, list)
if len(seed) != 2:
msg = 'seed in initial seeds must have two element image and ' \
'label, but got {} element.'.format(len(seed))
raise ValueError(msg)
check_numpy_param('seed[0]', seed[0]) check_numpy_param('seed[0]', seed[0])
check_numpy_param('seed[1]', seed[1]) check_numpy_param('seed[1]', seed[1])
seed.append(0) seed.append(0)
@@ -321,16 +325,16 @@ class Fuzzer:
"""Mutate a seed using strategies random selected from mutate_config.""" """Mutate a seed using strategies random selected from mutate_config."""
mutate_samples = [] mutate_samples = []
mutate_strategies = [] mutate_strategies = []
only_pixel_trans = seed[2]
for _ in range(mutate_num_per_seed): for _ in range(mutate_num_per_seed):
strage = choice(mutate_config)
only_pixel_trans = seed[2]
strategy = choice(mutate_config)
# Choose a pixel value based transform method # Choose a pixel value based transform method
if only_pixel_trans: if only_pixel_trans:
while strage['method'] not in self._pixel_value_trans_list:
strage = choice(mutate_config)
transform = mutates[strage['method']]
params = strage['params']
method = strage['method']
while strategy['method'] not in self._pixel_value_trans_list:
strategy = choice(mutate_config)
transform = mutates[strategy['method']]
params = strategy['params']
method = strategy['method']
selected_param = {} selected_param = {}
for p in params: for p in params:
selected_param[p] = choice(params[p]) selected_param[p] = choice(params[p])
@@ -367,10 +371,10 @@ class Fuzzer:
for config in mutate_config: for config in mutate_config:
check_param_type("config", config, dict) check_param_type("config", config, dict)
if set(config.keys()) != {'method', 'params'}: if set(config.keys()) != {'method', 'params'}:
msg = "Config must contain 'method' and 'params', but got {}." \
.format(set(config.keys()))
msg = "The key of each config must be in ('method', 'params'), " \
"but got {}.".format(set(config.keys()))
LOGGER.error(TAG, msg) LOGGER.error(TAG, msg)
raise TypeError(msg)
raise KeyError(msg)


method = config['method'] method = config['method']
params = config['params'] params = config['params']
@@ -380,7 +384,7 @@ class Fuzzer:
msg = "Config methods must be in {}, but got {}." \ msg = "Config methods must be in {}, but got {}." \
.format(self._strategies.keys(), method) .format(self._strategies.keys(), method)
LOGGER.error(TAG, msg) LOGGER.error(TAG, msg)
raise TypeError(msg)
raise ValueError(msg)


if config['method'] in self._pixel_value_trans_list: if config['method'] in self._pixel_value_trans_list:
has_pixel_trans = True has_pixel_trans = True
@@ -415,7 +419,9 @@ class Fuzzer:
if param_name == 'bounds': if param_name == 'bounds':
bounds = check_param_multi_types('bounds', param_value, [tuple]) bounds = check_param_multi_types('bounds', param_value, [tuple])
if len(bounds) != 2: if len(bounds) != 2:
msg = 'bounds must be format (lower_bound, upper_bound)'
msg = 'The format of bounds must be format (lower_bound, upper_bound),' \
'but got its length as{}'.format(len(bounds))
raise ValueError(msg)
for bound_value in bounds: for bound_value in bounds:
_ = check_param_multi_types('bound', bound_value, _ = check_param_multi_types('bound', bound_value,
[int, float]) [int, float])
@@ -458,7 +464,7 @@ class Fuzzer:
Args: Args:
fuzz_samples ([numpy.ndarray, list]): Generated fuzz_testing samples fuzz_samples ([numpy.ndarray, list]): Generated fuzz_testing samples
according to seeds. according to seeds.
true_labels ([numpy.ndarray, list]): Ground Truth labels of seeds.
true_labels ([numpy.ndarray, list]): Ground truth labels of seeds.
fuzz_preds ([numpy.ndarray, list]): Predictions of generated fuzz samples. fuzz_preds ([numpy.ndarray, list]): Predictions of generated fuzz samples.
fuzz_strategies ([numpy.ndarray, list]): Mutate strategies of fuzz samples. fuzz_strategies ([numpy.ndarray, list]): Mutate strategies of fuzz samples.
metrics (Union[list, tuple, str]): evaluation metrics. metrics (Union[list, tuple, str]): evaluation metrics.


+ 30
- 26
mindarmour/fuzz_testing/image_transform.py View File

@@ -23,7 +23,7 @@ from mindarmour.utils._check_param import check_param_multi_types
from mindarmour.utils.logger import LogUtil from mindarmour.utils.logger import LogUtil


LOGGER = LogUtil.get_instance() LOGGER = LogUtil.get_instance()
TAG = 'ModelCoverageMetrics'
TAG = 'Image Transformation'




def chw_to_hwc(img): def chw_to_hwc(img):
@@ -38,7 +38,7 @@ def chw_to_hwc(img):
""" """
if is_numpy(img): if is_numpy(img):
return img.transpose(1, 2, 0).copy() return img.transpose(1, 2, 0).copy()
raise TypeError('img should be Numpy array. Got {}'.format(type(img)))
raise TypeError('img should be numpy.ndarray. Got {}'.format(type(img)))




def is_hwc(img): def is_hwc(img):
@@ -56,7 +56,7 @@ def is_hwc(img):
if img_shape[2] == 3 and img_shape[1] > 3 and img_shape[0] > 3: if img_shape[2] == 3 and img_shape[1] > 3 and img_shape[0] > 3:
return True return True
return False return False
raise TypeError('img should be Numpy array. Got {}'.format(type(img)))
raise TypeError('img should be numpy.ndarray. Got {}'.format(type(img)))




def is_chw(img): def is_chw(img):
@@ -74,7 +74,7 @@ def is_chw(img):
if img_shape[0] == 3 and img_shape[1] > 3 and img_shape[2] > 3: if img_shape[0] == 3 and img_shape[1] > 3 and img_shape[2] > 3:
return True return True
return False return False
raise TypeError('img should be Numpy array. Got {}'.format(type(img)))
raise TypeError('img should be numpy.ndarray. Got {}'.format(type(img)))




def is_rgb(img): def is_rgb(img):
@@ -92,7 +92,7 @@ def is_rgb(img):
if len(np.shape(img)) == 3 and (img_shape[0] == 3 or img_shape[2] == 3): if len(np.shape(img)) == 3 and (img_shape[0] == 3 or img_shape[2] == 3):
return True return True
return False return False
raise TypeError('img should be Numpy array. Got {}'.format(type(img)))
raise TypeError('img should be numpy.ndarray. Got {}'.format(type(img)))




def is_normalized(img): def is_normalized(img):
@@ -167,8 +167,8 @@ class Contrast(ImageTransform):
Contrast of an image. Contrast of an image.


Args: Args:
factor ([float, int]): Control the contrast of an image. If 1.0 gives the
original image. If 0 gives a gray image. Default: 1.
factor (Union[float, int]): Control the contrast of an image. If 1.0,
gives the original image. If 0, gives a gray image. Default: 1.
""" """


def __init__(self, factor=1): def __init__(self, factor=1):
@@ -180,8 +180,8 @@ class Contrast(ImageTransform):
Set contrast parameters. Set contrast parameters.


Args: Args:
factor ([float, int]): Control the contrast of an image. If 1.0 gives
the original image. If 0 gives a gray image. Default: 1.
factor (Union[float, int]): Control the contrast of an image. If 1.0
gives the original image. If 0 gives a gray image. Default: 1.
auto_param (bool): True if auto generate parameters. Default: False. auto_param (bool): True if auto generate parameters. Default: False.
""" """
if auto_param: if auto_param:
@@ -214,8 +214,8 @@ class Brightness(ImageTransform):
Brightness of an image. Brightness of an image.


Args: Args:
factor ([float, int]): Control the brightness of an image. If 1.0 gives
the original image. If 0 gives a black image. Default: 1.
factor (Union[float, int]): Control the brightness of an image. If 1.0
gives the original image. If 0 gives a black image. Default: 1.
""" """


def __init__(self, factor=1): def __init__(self, factor=1):
@@ -227,7 +227,7 @@ class Brightness(ImageTransform):
Set brightness parameters. Set brightness parameters.


Args: Args:
factor ([float, int]): Control the brightness of an image. If 1
factor (Union[float, int]): Control the brightness of an image. If 1
gives the original image. If 0 gives a black image. Default: 1. gives the original image. If 0 gives a black image. Default: 1.
auto_param (bool): True if auto generate parameters. Default: False. auto_param (bool): True if auto generate parameters. Default: False.
""" """
@@ -260,7 +260,7 @@ class Blur(ImageTransform):
Blurs the image using Gaussian blur filter. Blurs the image using Gaussian blur filter.


Args: Args:
radius([float, int]): Blur radius, 0 means no blur. Default: 0.
radius(Union[float, int]): Blur radius, 0 means no blur. Default: 0.
""" """


def __init__(self, radius=0): def __init__(self, radius=0):
@@ -272,7 +272,7 @@ class Blur(ImageTransform):
Set blur parameters. Set blur parameters.


Args: Args:
radius ([float, int]): Blur radius, 0 means no blur. Default: 0.
radius (Union[float, int]): Blur radius, 0 means no blur. Default: 0.
auto_param (bool): True if auto generate parameters. Default: False. auto_param (bool): True if auto generate parameters. Default: False.
""" """
if auto_param: if auto_param:
@@ -316,8 +316,8 @@ class Noise(ImageTransform):
Set noise parameters. Set noise parameters.


Args: Args:
factor ([float, int]): 1 - factor is the ratio of pixels to add noise.
If 0 gives the original image. Default 0.
factor (Union[float, int]): 1 - factor is the ratio of pixels to
add noise. If 0 gives the original image. Default 0.
auto_param (bool): True if auto generate parameters. Default: False. auto_param (bool): True if auto generate parameters. Default: False.
""" """
if auto_param: if auto_param:
@@ -407,8 +407,10 @@ class Scale(ImageTransform):
Scale an image in the middle. Scale an image in the middle.


Args: Args:
factor_x ([float, int]): Rescale in X-direction, x=factor_x*x. Default: 1.
factor_y ([float, int]): Rescale in Y-direction, y=factor_y*y. Default: 1.
factor_x (Union[float, int]): Rescale in X-direction, x=factor_x*x.
Default: 1.
factor_y (Union[float, int]): Rescale in Y-direction, y=factor_y*y.
Default: 1.
""" """


def __init__(self, factor_x=1, factor_y=1): def __init__(self, factor_x=1, factor_y=1):
@@ -421,9 +423,9 @@ class Scale(ImageTransform):
Set scale parameters. Set scale parameters.


Args: Args:
factor_x ([float, int]): Rescale in X-direction, x=factor_x*x.
factor_x (Union[float, int]): Rescale in X-direction, x=factor_x*x.
Default: 1. Default: 1.
factor_y ([float, int]): Rescale in Y-direction, y=factor_y*y.
factor_y (Union[float, int]): Rescale in Y-direction, y=factor_y*y.
Default: 1. Default: 1.
auto_param (bool): True if auto generate parameters. Default: False. auto_param (bool): True if auto generate parameters. Default: False.
""" """
@@ -469,8 +471,10 @@ class Shear(ImageTransform):
the sheared image will be rescaled to fit original size. the sheared image will be rescaled to fit original size.


Args: Args:
factor_x ([float, int]): Shear factor of horizontal direction. Default: 0.
factor_y ([float, int]): Shear factor of vertical direction. Default: 0.
factor_x (Union[float, int]): Shear factor of horizontal direction.
Default: 0.
factor_y (Union[float, int]): Shear factor of vertical direction.
Default: 0.


""" """


@@ -483,9 +487,9 @@ class Shear(ImageTransform):
Set shear parameters. Set shear parameters.


Args: Args:
factor_x ([float, int]): Shear factor of horizontal direction.
factor_x (Union[float, int]): Shear factor of horizontal direction.
Default: 0. Default: 0.
factor_y ([float, int]): Shear factor of vertical direction.
factor_y (Union[float, int]): Shear factor of vertical direction.
Default: 0. Default: 0.
auto_param (bool): True if auto generate parameters. Default: False. auto_param (bool): True if auto generate parameters. Default: False.
""" """
@@ -549,7 +553,7 @@ class Rotate(ImageTransform):
Rotate an image of degrees counter clockwise around its center. Rotate an image of degrees counter clockwise around its center.


Args: Args:
angle([float, int]): Degrees counter clockwise. Default: 0.
angle(Union[float, int]): Degrees counter clockwise. Default: 0.
""" """


def __init__(self, angle=0): def __init__(self, angle=0):
@@ -561,7 +565,7 @@ class Rotate(ImageTransform):
Set rotate parameters. Set rotate parameters.


Args: Args:
angle([float, int]): Degrees counter clockwise. Default: 0.
angle(Union[float, int]): Degrees counter clockwise. Default: 0.
auto_param (bool): True if auto generate parameters. Default: False. auto_param (bool): True if auto generate parameters. Default: False.
""" """
if auto_param: if auto_param:


+ 4
- 4
mindarmour/privacy/evaluation/membership_inference.py View File

@@ -148,10 +148,10 @@ class MembershipInference:
The support methods are knn, lr, mlp and rf, and the params of each method The support methods are knn, lr, mlp and rf, and the params of each method
must within the range of changeable parameters. Tips of params implement must within the range of changeable parameters. Tips of params implement
can be found below: can be found below:
`KNN<https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.html>`_,
`LR<https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html>`_,
`RF<https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html>`_,
`MLP<https://scikit-learn.org/stable/modules/generated/sklearn.neural_network.MLPRegressor.html>`_.
`KNN <https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.html>`_,
`LR <https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html>`_,
`RF <https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html>`_,
`MLP <https://scikit-learn.org/stable/modules/generated/sklearn.neural_network.MLPRegressor.html>`_.


Raises: Raises:
KeyError: If any config in attack_config doesn't have keys {"method", "params"}. KeyError: If any config in attack_config doesn't have keys {"method", "params"}.


Loading…
Cancel
Save