From d083d5e54052955015f95ad69fb6907da4064b4c Mon Sep 17 00:00:00 2001 From: ZhidanLiu Date: Thu, 17 Sep 2020 16:29:46 +0800 Subject: [PATCH] fix problems in review comments --- mindarmour/fuzz_testing/fuzzing.py | 32 ++++++++----- mindarmour/fuzz_testing/image_transform.py | 56 ++++++++++++---------- .../privacy/evaluation/membership_inference.py | 8 ++-- 3 files changed, 53 insertions(+), 43 deletions(-) diff --git a/mindarmour/fuzz_testing/fuzzing.py b/mindarmour/fuzz_testing/fuzzing.py index ec1feec..ff14adf 100644 --- a/mindarmour/fuzz_testing/fuzzing.py +++ b/mindarmour/fuzz_testing/fuzzing.py @@ -254,6 +254,10 @@ class Fuzzer: raise ValueError(msg) for seed in initial_seeds: 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[1]', seed[1]) seed.append(0) @@ -321,16 +325,16 @@ class Fuzzer: """Mutate a seed using strategies random selected from mutate_config.""" mutate_samples = [] mutate_strategies = [] - only_pixel_trans = seed[2] 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 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 = {} for p in params: selected_param[p] = choice(params[p]) @@ -367,10 +371,10 @@ class Fuzzer: for config in mutate_config: check_param_type("config", config, dict) 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) - raise TypeError(msg) + raise KeyError(msg) method = config['method'] params = config['params'] @@ -380,7 +384,7 @@ class Fuzzer: msg = "Config methods must be in {}, but got {}." \ .format(self._strategies.keys(), method) LOGGER.error(TAG, msg) - raise TypeError(msg) + raise ValueError(msg) if config['method'] in self._pixel_value_trans_list: has_pixel_trans = True @@ -415,7 +419,9 @@ class Fuzzer: if param_name == 'bounds': bounds = check_param_multi_types('bounds', param_value, [tuple]) 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: _ = check_param_multi_types('bound', bound_value, [int, float]) @@ -458,7 +464,7 @@ class Fuzzer: Args: fuzz_samples ([numpy.ndarray, list]): Generated fuzz_testing samples 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_strategies ([numpy.ndarray, list]): Mutate strategies of fuzz samples. metrics (Union[list, tuple, str]): evaluation metrics. diff --git a/mindarmour/fuzz_testing/image_transform.py b/mindarmour/fuzz_testing/image_transform.py index e5fc5a5..743aecc 100644 --- a/mindarmour/fuzz_testing/image_transform.py +++ b/mindarmour/fuzz_testing/image_transform.py @@ -23,7 +23,7 @@ from mindarmour.utils._check_param import check_param_multi_types from mindarmour.utils.logger import LogUtil LOGGER = LogUtil.get_instance() -TAG = 'ModelCoverageMetrics' +TAG = 'Image Transformation' def chw_to_hwc(img): @@ -38,7 +38,7 @@ def chw_to_hwc(img): """ if is_numpy(img): 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): @@ -56,7 +56,7 @@ def is_hwc(img): if img_shape[2] == 3 and img_shape[1] > 3 and img_shape[0] > 3: return True 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): @@ -74,7 +74,7 @@ def is_chw(img): if img_shape[0] == 3 and img_shape[1] > 3 and img_shape[2] > 3: return True 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): @@ -92,7 +92,7 @@ def is_rgb(img): if len(np.shape(img)) == 3 and (img_shape[0] == 3 or img_shape[2] == 3): return True 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): @@ -167,8 +167,8 @@ class Contrast(ImageTransform): Contrast of an image. 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): @@ -180,8 +180,8 @@ class Contrast(ImageTransform): Set contrast parameters. 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. """ if auto_param: @@ -214,8 +214,8 @@ class Brightness(ImageTransform): Brightness of an image. 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): @@ -227,7 +227,7 @@ class Brightness(ImageTransform): Set brightness parameters. 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. auto_param (bool): True if auto generate parameters. Default: False. """ @@ -260,7 +260,7 @@ class Blur(ImageTransform): Blurs the image using Gaussian blur filter. 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): @@ -272,7 +272,7 @@ class Blur(ImageTransform): Set blur parameters. 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. """ if auto_param: @@ -316,8 +316,8 @@ class Noise(ImageTransform): Set noise parameters. 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. """ if auto_param: @@ -407,8 +407,10 @@ class Scale(ImageTransform): Scale an image in the middle. 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): @@ -421,9 +423,9 @@ class Scale(ImageTransform): Set scale parameters. 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. - 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. 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. 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. Args: - factor_x ([float, int]): Shear factor of horizontal direction. + factor_x (Union[float, int]): Shear factor of horizontal direction. Default: 0. - factor_y ([float, int]): Shear factor of vertical direction. + factor_y (Union[float, int]): Shear factor of vertical direction. Default: 0. 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. Args: - angle([float, int]): Degrees counter clockwise. Default: 0. + angle(Union[float, int]): Degrees counter clockwise. Default: 0. """ def __init__(self, angle=0): @@ -561,7 +565,7 @@ class Rotate(ImageTransform): Set rotate parameters. 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. """ if auto_param: diff --git a/mindarmour/privacy/evaluation/membership_inference.py b/mindarmour/privacy/evaluation/membership_inference.py index e7bc90c..4e5d00d 100644 --- a/mindarmour/privacy/evaluation/membership_inference.py +++ b/mindarmour/privacy/evaluation/membership_inference.py @@ -148,10 +148,10 @@ class MembershipInference: 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 can be found below: - `KNN`_, - `LR`_, - `RF`_, - `MLP`_. + `KNN `_, + `LR `_, + `RF `_, + `MLP `_. Raises: KeyError: If any config in attack_config doesn't have keys {"method", "params"}.