diff --git a/mindarmour/adv_robustness/attacks/attack.py b/mindarmour/adv_robustness/attacks/attack.py index e138407..f7c6a37 100644 --- a/mindarmour/adv_robustness/attacks/attack.py +++ b/mindarmour/adv_robustness/attacks/attack.py @@ -183,13 +183,12 @@ class Attack: best_position = check_numpy_param('best_position', best_position) x_ori, best_position = check_equal_shape('x_ori', x_ori, 'best_position', best_position) _, original_num = self._detection_scores((best_position,) + auxiliary_inputs, gt_boxes, gt_labels, model) - # pylint: disable=invalid-name - REDUCTION_ITERS = 6 # recover 10% difference each time and recover 60% totally. - for _ in range(REDUCTION_ITERS): - BLOCK_NUM = 30 # divide the image into 30 segments - block_width = best_position.shape[0] // BLOCK_NUM + reduction_iters = 6 # recover 10% difference each time and recover 60% totally. + for _ in range(reduction_iters): + block_num = 30 # divide the image into 30 segments + block_width = best_position.shape[0] // block_num if block_width > 0: - for i in range(BLOCK_NUM): + for i in range(block_num): diff = x_ori[i*block_width: (i+1)*block_width, :, :]\ - best_position[i*block_width:(i+1)*block_width, :, :] if np.max(np.abs(diff)) >= 0.1*(self._bounds[1] - self._bounds[0]): diff --git a/mindarmour/fuzz_testing/image_transform.py b/mindarmour/fuzz_testing/image_transform.py index 52a1136..04c4309 100644 --- a/mindarmour/fuzz_testing/image_transform.py +++ b/mindarmour/fuzz_testing/image_transform.py @@ -107,8 +107,8 @@ def is_normalized(img): """ if is_numpy(img): minimal = np.min(img) - maximun = np.max(img) - if minimal >= 0 and maximun <= 1: + maximum = np.max(img) + if minimal >= 0 and maximum <= 1: return True return False raise TypeError('img should be Numpy array. Got {}'.format(type(img))) @@ -359,9 +359,9 @@ class Translate(ImageTransform): Translate an image. Args: - x_bias (Union[int, float]): X-direction translation, x = x + x_bias*image_length. + x_bias (Union[int, float]): X-direction translation: x + x_bias*image_length. Default: 0. - y_bias (Union[int, float]): Y-direction translation, y = y + y_bias*image_wide. + y_bias (Union[int, float]): Y-direction translation: y + y_bias*image_wide. Default: 0. """ diff --git a/mindarmour/privacy/diff_privacy/monitor/monitor.py b/mindarmour/privacy/diff_privacy/monitor/monitor.py index 62449c1..ef6ef10 100644 --- a/mindarmour/privacy/diff_privacy/monitor/monitor.py +++ b/mindarmour/privacy/diff_privacy/monitor/monitor.py @@ -347,9 +347,8 @@ class RDPMonitor(Callback): Returns: float, delta budget. """ - orders = np.atleast_1d(self._orders) rdps = np.atleast_1d(rdp) - deltas = np.exp((rdps - self._target_eps)*(orders - 1)) + deltas = np.exp((rdps - self._target_eps)*(np.atleast_1d(self._orders) - 1)) min_delta = np.min(deltas) return np.min([min_delta, 1.]) @@ -363,9 +362,8 @@ class RDPMonitor(Callback): Returns: float, eps budget. """ - orders = np.atleast_1d(self._orders) rdps = np.atleast_1d(rdp) - eps = rdps - np.log(self._target_delta) / (orders - 1) + eps = rdps - np.log(self._target_delta) / (np.atleast_1d(self._orders) - 1) return np.min(eps) diff --git a/mindarmour/privacy/sup_privacy/mask_monitor/masker.py b/mindarmour/privacy/sup_privacy/mask_monitor/masker.py index 4b9e629..09926d2 100644 --- a/mindarmour/privacy/sup_privacy/mask_monitor/masker.py +++ b/mindarmour/privacy/sup_privacy/mask_monitor/masker.py @@ -26,7 +26,7 @@ TAG = 'suppress masker' class SuppressMasker(Callback): """ Args: - model (SuppressModel): SuppressModel instance. + model (SuppressModel): SuppressModel instance. suppress_ctrl (SuppressCtrl): SuppressCtrl instance. Examples: diff --git a/mindarmour/privacy/sup_privacy/train/model.py b/mindarmour/privacy/sup_privacy/train/model.py index 0e08de9..86fc041 100644 --- a/mindarmour/privacy/sup_privacy/train/model.py +++ b/mindarmour/privacy/sup_privacy/train/model.py @@ -225,7 +225,6 @@ class _TupleMul(nn.Cell): def construct(self, input1, input2): """Add two tuple of data.""" out = self.hyper_map(self.mul, input1, input2) - #print(out) return out # come from nn.cell_wrapper.TrainOneStepCell diff --git a/mindarmour/reliability/concept_drift/concept_drift_check_images.py b/mindarmour/reliability/concept_drift/concept_drift_check_images.py index ec0046b..84aa335 100644 --- a/mindarmour/reliability/concept_drift/concept_drift_check_images.py +++ b/mindarmour/reliability/concept_drift/concept_drift_check_images.py @@ -11,20 +11,22 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -# ============================================================================ - +""" +Out-of-Distribution detection for images. +""" import heapq +from abc import abstractmethod import numpy as np -from mindspore import Tensor from sklearn.cluster import KMeans -from mindarmour.utils._check_param import check_param_type, check_param_in_range -from mindspore.train.summary.summary_record import _get_summary_tensor_data +from mindspore.train.summary.summary_record import _get_summary_tensor_data +from mindspore import Tensor +from mindarmour.utils._check_param import check_param_type, check_param_in_range +from mindarmour.utils.logger import LogUtil -""" -Out-of-Distribution detection for images. -""" +LOGGER = LogUtil.get_instance() +TAG = 'concept drift detection' class OodDetector: @@ -56,6 +58,7 @@ class OodDetector: layer_out = _get_summary_tensor_data() return layer_out[layer].asnumpy() + @abstractmethod def get_optimal_threshold(self, label, ds_eval): """ Get the optimal threshold. @@ -67,8 +70,12 @@ class OodDetector: Returns: - float, the optimal threshold. """ - pass + msg = 'The function generate() is an abstract function in class ' \ + '`OodDetector` and should be implemented in child class.' + LOGGER.error(TAG, msg) + raise NotImplementedError(msg) + @abstractmethod def ood_predict(self, threshold, ds_test): """ The out-of-distribution detection. @@ -81,7 +88,10 @@ class OodDetector: Returns: - numpy.ndarray, the detection result. 0 means the data is not ood, 1 means the data is ood. """ - pass + msg = 'The function generate() is an abstract function in class ' \ + '`OodDetector` and should be implemented in child class.' + LOGGER.error(TAG, msg) + raise NotImplementedError(msg) class OodDetectorFeatureCluster(OodDetector): @@ -103,6 +113,7 @@ class OodDetectorFeatureCluster(OodDetector): """ def __init__(self, model, ds_train, n_cluster, layer): + super().__init__(model, ds_train) self.model = model self.ds_train = check_param_type('ds_train', ds_train, np.ndarray) self.n_cluster = check_param_type('n_cluster', n_cluster, int) @@ -173,7 +184,7 @@ class OodDetectorFeatureCluster(OodDetector): threshold.append(threshold_change) acc = np.array(acc) threshold = np.array(threshold) - optimal_threshold = threshold[np.where(acc==np.max(acc))[0]][0] + optimal_threshold = threshold[np.where(acc == np.max(acc))[0]][0] return optimal_threshold def ood_predict(self, threshold, ds_test): diff --git a/mindarmour/reliability/concept_drift/concept_drift_check_time_series.py b/mindarmour/reliability/concept_drift/concept_drift_check_time_series.py index 61e553f..f3be9f4 100644 --- a/mindarmour/reliability/concept_drift/concept_drift_check_time_series.py +++ b/mindarmour/reliability/concept_drift/concept_drift_check_time_series.py @@ -9,7 +9,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -# ============================================================================ """ Concpt drift module @@ -72,8 +71,7 @@ class ConceptDriftCheckTimeSeries: >>> w, x = ConceptDriftCheckTimeSeries._reservoir_model_feature(window_data) """ # Initialize weights - res_size = self._res_size - x_state = _w_generate(res_size, len(window_data), window_data) + x_state = _w_generate(self._res_size, len(window_data), window_data) x_state_t = x_state.T # Data reshape data_channel = None @@ -85,7 +83,7 @@ class ConceptDriftCheckTimeSeries: reg = 1e-8 # Calculate w_out w_out = np.dot(np.dot(y_t, x_state_t), - np.linalg.inv(np.dot(x_state, x_state_t) + reg*np.eye(res_size))) + np.linalg.inv(np.dot(x_state, x_state_t) + reg*np.eye(self._res_size))) return w_out, x_state def _concept_distance(self, data_x, data_y): @@ -391,11 +389,11 @@ def _cal_threshold(distance, threshold_index): Returns: - float, [0, 1]. """ - distance = distance[distance > 0] + pos_distance = distance[distance > 0] # Threshold calculation - if distance.size > 0: - q_1 = np.percentile(distance, 25) - q_3 = np.percentile(distance, 75) + if pos_distance.size > 0: + q_1 = np.percentile(pos_distance, 25) + q_3 = np.percentile(pos_distance, 75) q_diff = q_3 - q_1 threshold = np.clip(0.1 + threshold_index*q_diff, 0, 1) else: diff --git a/mindarmour/reliability/model_fault_injection/__init__.py b/mindarmour/reliability/model_fault_injection/__init__.py index f2c861a..655bb9a 100644 --- a/mindarmour/reliability/model_fault_injection/__init__.py +++ b/mindarmour/reliability/model_fault_injection/__init__.py @@ -8,7 +8,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -# ============================================================================ """ This module provides model fault injection to evaluate the reliability of given model. """ diff --git a/mindarmour/reliability/model_fault_injection/fault_injection.py b/mindarmour/reliability/model_fault_injection/fault_injection.py index 9f7d4ec..01a8c59 100644 --- a/mindarmour/reliability/model_fault_injection/fault_injection.py +++ b/mindarmour/reliability/model_fault_injection/fault_injection.py @@ -8,7 +8,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -# ============================================================================ """ Fault injection module diff --git a/mindarmour/reliability/model_fault_injection/fault_type.py b/mindarmour/reliability/model_fault_injection/fault_type.py index 6e40c22..5adc3ef 100644 --- a/mindarmour/reliability/model_fault_injection/fault_type.py +++ b/mindarmour/reliability/model_fault_injection/fault_type.py @@ -8,7 +8,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -# ============================================================================ """ Fault type module