|
| 1 | +# MIT License |
| 2 | +# |
| 3 | +# Copyright (C) The Adversarial Robustness Toolbox (ART) Authors 2020 |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 6 | +# documentation files (the "Software"), to deal in the Software without restriction, including without limitation the |
| 7 | +# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 | +# persons to whom the Software is furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the |
| 11 | +# Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 14 | +# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 15 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 16 | +# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 17 | +# SOFTWARE. |
| 18 | +""" |
| 19 | +This module implements the Label-Only Inference Attack based on Decision Boundary. |
| 20 | +
|
| 21 | +| Paper link: https://arxiv.org/abs/2007.14321 |
| 22 | +""" |
| 23 | +import logging |
| 24 | +from typing import Optional, NoReturn, TYPE_CHECKING |
| 25 | + |
| 26 | +import numpy as np |
| 27 | + |
| 28 | +from art.attacks.attack import InferenceAttack |
| 29 | +from art.estimators.estimator import BaseEstimator |
| 30 | +from art.estimators.classification.classifier import ClassifierMixin |
| 31 | + |
| 32 | +if TYPE_CHECKING: |
| 33 | + from art.utils import CLASSIFIER_TYPE |
| 34 | + |
| 35 | +logger = logging.getLogger(__name__) |
| 36 | + |
| 37 | + |
| 38 | +class LabelOnlyDecisionBoundary(InferenceAttack): |
| 39 | + """ |
| 40 | + Implementation of Label-Only Inference Attack based on Decision Boundary. |
| 41 | +
|
| 42 | + | Paper link: https://arxiv.org/abs/2007.14321 |
| 43 | + """ |
| 44 | + |
| 45 | + attack_params = InferenceAttack.attack_params + [ |
| 46 | + "distance_threshold_tau", |
| 47 | + ] |
| 48 | + _estimator_requirements = (BaseEstimator, ClassifierMixin) |
| 49 | + |
| 50 | + def __init__(self, estimator: "CLASSIFIER_TYPE", distance_threshold_tau: Optional[float] = None): |
| 51 | + """ |
| 52 | + Create a `LabelOnlyDecisionBoundary` instance for Label-Only Inference Attack based on Decision Boundary. |
| 53 | +
|
| 54 | + :param estimator: A trained classification estimator. |
| 55 | + :param distance_threshold_tau: Threshold distance for decision boundary. Samples with boundary distances larger |
| 56 | + than threshold are considered members of the training dataset. |
| 57 | + """ |
| 58 | + super().__init__(estimator=estimator) |
| 59 | + self.distance_threshold_tau = distance_threshold_tau |
| 60 | + self._check_params() |
| 61 | + |
| 62 | + def infer(self, x: np.ndarray, y: Optional[np.ndarray] = None, **kwargs) -> np.ndarray: |
| 63 | + """ |
| 64 | + Infer membership of input `x` in estimator's training data. |
| 65 | +
|
| 66 | + :param x: Input data. |
| 67 | + :param y: True labels for `x`. |
| 68 | + :param kwargs: Parameters for HopSkipJump attack except argument `estimator`. |
| 69 | + :return: An array holding the inferred membership status, 1 indicates a member and 0 indicates non-member. |
| 70 | + """ |
| 71 | + from art.attacks.evasion.hop_skip_jump import HopSkipJump |
| 72 | + |
| 73 | + hsj = HopSkipJump(classifier=self.estimator, **kwargs) |
| 74 | + x_adv = hsj.generate(x=x, y=y) |
| 75 | + |
| 76 | + distance = np.linalg.norm((x_adv - x).reshape((x.shape[0], -1)), ord=2, axis=1) |
| 77 | + |
| 78 | + y_pred = self.estimator.predict(x=x) |
| 79 | + |
| 80 | + distance[np.argmax(y_pred, axis=1) != np.argmax(y, axis=1)] = 0 |
| 81 | + |
| 82 | + is_member = np.where(distance > self.distance_threshold_tau, 1, 0) |
| 83 | + |
| 84 | + return is_member |
| 85 | + |
| 86 | + def calibrate_distance_threshold( |
| 87 | + self, |
| 88 | + classifier_train: "CLASSIFIER_TYPE", |
| 89 | + x_train: np.ndarray, |
| 90 | + y_train: np.ndarray, |
| 91 | + x_test: np.ndarray, |
| 92 | + y_test: np.ndarray, |
| 93 | + **kwargs |
| 94 | + ) -> NoReturn: |
| 95 | + """ |
| 96 | + Calibrate distance threshold maximising the membership inference accuracy on `x_train` and `x_test`. |
| 97 | +
|
| 98 | + :param classifier_train: A trained classifier |
| 99 | + :param x_train: Training data. |
| 100 | + :param y_train: Labels of training data `x_train`. |
| 101 | + :param x_test: Test data. |
| 102 | + :param y_test: Labels of test data `x_test`. |
| 103 | + """ |
| 104 | + from art.attacks.evasion.hop_skip_jump import HopSkipJump |
| 105 | + |
| 106 | + hsj = HopSkipJump(classifier=classifier_train, **kwargs) |
| 107 | + |
| 108 | + x_train_adv = hsj.generate(x=x_train, y=y_train) |
| 109 | + x_test_adv = hsj.generate(x=x_test, y=y_test) |
| 110 | + |
| 111 | + distance_train = np.linalg.norm((x_train_adv - x_train).reshape((x_train.shape[0], -1)), ord=2, axis=1) |
| 112 | + distance_test = np.linalg.norm((x_test_adv - x_test).reshape((x_test.shape[0], -1)), ord=2, axis=1) |
| 113 | + |
| 114 | + y_train_pred = self.estimator.predict(x=x_train) |
| 115 | + y_test_pred = self.estimator.predict(x=x_test) |
| 116 | + |
| 117 | + distance_train[np.argmax(y_train_pred, axis=1) != np.argmax(y_train, axis=1)] = 0 |
| 118 | + distance_test[np.argmax(y_test_pred, axis=1) != np.argmax(y_test, axis=1)] = 0 |
| 119 | + |
| 120 | + num_increments = 100 |
| 121 | + tau_increment = np.amax([np.amax(distance_train), np.amax(distance_test)]) / num_increments |
| 122 | + |
| 123 | + acc_max = 0.0 |
| 124 | + distance_threshold_tau = 0.0 |
| 125 | + |
| 126 | + for i_tau in range(1, num_increments): |
| 127 | + |
| 128 | + is_member_train = np.where(distance_train > i_tau * tau_increment, 1, 0) |
| 129 | + is_member_test = np.where(distance_test > i_tau * tau_increment, 1, 0) |
| 130 | + |
| 131 | + acc = (np.sum(is_member_train) + (is_member_test.shape[0] - np.sum(is_member_test))) / ( |
| 132 | + is_member_train.shape[0] + is_member_test.shape[0] |
| 133 | + ) |
| 134 | + |
| 135 | + if acc > acc_max: |
| 136 | + distance_threshold_tau = i_tau * tau_increment |
| 137 | + acc_max = acc |
| 138 | + |
| 139 | + self.distance_threshold_tau = distance_threshold_tau |
| 140 | + |
| 141 | + def _check_params(self) -> None: |
| 142 | + if not isinstance(self.distance_threshold_tau, (int, float)) or self.distance_threshold_tau <= 0.0: |
| 143 | + raise ValueError("The distance threshold `distance_threshold_tau` needs to be a positive float.") |
0 commit comments