def __init__(self, network, eps=0.3, eps_iter=0.1, bounds=(0.0, 1.0), is_targeted=False, nb_iter=5, norm_level='inf', loss_fn=None): super(ProjectedGradientDescent, self).__init__(network, eps=eps, eps_iter=eps_iter, bounds=bounds, is_targeted=is_targeted, nb_iter=nb_iter, loss_fn=loss_fn) self._norm_level = check_norm_level(norm_level)
def __init__(self, network, eps=0.3, eps_iter=0.1, bounds=(0.0, 1.0), is_targeted=False, nb_iter=5, decay_factor=1.0, norm_level='inf', loss_fn=None): super(MomentumIterativeMethod, self).__init__(network, eps=eps, eps_iter=eps_iter, bounds=bounds, nb_iter=nb_iter, loss_fn=loss_fn) self._is_targeted = check_param_type('is_targeted', is_targeted, bool) self._decay_factor = check_value_positive('decay_factor', decay_factor) self._norm_level = check_norm_level(norm_level)
def __init__(self, network, num_classes, max_iters=50, overshoot=0.02, norm_level=2, bounds=None, sparse=True): super(DeepFool, self).__init__() self._network = check_model('network', network, Cell) self._network.set_grad(True) self._max_iters = check_int_positive('max_iters', max_iters) self._overshoot = check_value_positive('overshoot', overshoot) self._norm_level = check_norm_level(norm_level) self._num_classes = check_int_positive('num_classes', num_classes) self._net_grad = GradWrap(self._network) self._bounds = check_param_multi_types('bounds', bounds, [list, tuple]) self._sparse = check_param_type('sparse', sparse, bool) for b in self._bounds: _ = check_param_multi_types('bound', b, [int, float])
def __init__(self, network, eps=0.07, alpha=None, bounds=(0.0, 1.0), norm_level=2, is_targeted=False, loss_fn=None): super(FastGradientMethod, self).__init__(network, eps=eps, alpha=alpha, bounds=bounds, loss_fn=loss_fn) self._norm_level = check_norm_level(norm_level) self._is_targeted = check_param_type('is_targeted', is_targeted, bool)
def __init__(self, network, num_classes, model_type='classification', reserve_ratio=0.3, max_iters=50, overshoot=0.02, norm_level=2, bounds=None, sparse=True): super(DeepFool, self).__init__() self._network = check_model('network', network, Cell) self._max_iters = check_int_positive('max_iters', max_iters) self._overshoot = check_value_positive('overshoot', overshoot) self._norm_level = check_norm_level(norm_level) self._num_classes = check_int_positive('num_classes', num_classes) self._net_grad = GradWrap(self._network) self._bounds = bounds if self._bounds is not None: self._bounds = check_param_multi_types('bounds', bounds, [list, tuple]) for b in self._bounds: _ = check_param_multi_types('bound', b, [int, float]) self._sparse = check_param_type('sparse', sparse, bool) self._model_type = check_param_type('model_type', model_type, str) if self._model_type not in ('classification', 'detection'): msg = "Only 'classification' or 'detection' is supported now, but got {}.".format( self._model_type) LOGGER.error(TAG, msg) raise ValueError(msg) self._reserve_ratio = check_value_non_negative('reserve_ratio', reserve_ratio) if self._reserve_ratio > 1: msg = 'reserve_ratio should be less than 1.0, but got {}.'.format( self._reserve_ratio) LOGGER.error(TAG, msg) raise ValueError(TAG, msg)
def _check_attack_params(self, method, params): """Check input parameters of attack methods.""" allow_params = self._attack_param_checklists[method].keys() for param_name in params: if param_name not in allow_params: msg = "parameters of {} must in {}".format( method, allow_params) raise ValueError(msg) check_param_type(param_name, params[param_name], list) for param_value in params[param_name]: if param_name == 'bounds': bounds = check_param_multi_types('bounds', param_value, [tuple]) if len(bounds) != 2: 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]) if bounds[0] >= bounds[1]: msg = "upper bound must more than lower bound, " \ "but upper bound got {}, lower bound " \ "got {}".format(bounds[0], bounds[1]) raise ValueError(msg) elif param_name == 'norm_level': _ = check_norm_level(param_value) else: allow_type = self._attack_param_checklists[method][ param_name]['dtype'] allow_range = self._attack_param_checklists[method][ param_name]['range'] _ = check_param_multi_types(str(param_name), param_value, allow_type) _ = check_param_in_range(str(param_name), param_value, allow_range[0], allow_range[1])