Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for BEYOND [ICML-2024] #2489

Closed
wants to merge 16 commits into from

Conversation

allenhzy
Copy link
Contributor

@allenhzy allenhzy commented Sep 3, 2024

Description

This pull request adds the support of the BEYOND Detection method proposed in [1].

[1] Be Your Own Neighborhood: Detecting Adversarial Example by the Neighborhood Relations Built on Self-Supervised Learning. ICML. 2024[Paper]

Type of change

Please check all relevant options.

  • Improvement (non-breaking)
  • Bug fix (non-breaking)
  • New feature (non-breaking)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Testing

Please describe the tests that you ran to verify your changes. Consider listing any relevant details of your test configuration.

  • Unit Test

Test Configuration:

  • OS: Ubuntu 18.04
  • Python version: 3.9.19
  • ART version or commit number: 1.18.1
  • Pytorch: 2.4.0
  • cudnn version: 90100

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • My changes have been tested using both CPU and GPU devices

Copy link

codecov bot commented Sep 4, 2024

Codecov Report

Attention: Patch coverage is 20.96774% with 49 lines in your changes missing coverage. Please review.

Project coverage is 49.07%. Comparing base (6c57e03) to head (94c6ced).

Files with missing lines Patch % Lines
art/defences/detector/evasion/beyond_detector.py 19.67% 49 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@              Coverage Diff               @@
##           dev_1.19.0    #2489      +/-   ##
==============================================
- Coverage       53.27%   49.07%   -4.21%     
==============================================
  Files             666      668       +2     
  Lines           61858    61982     +124     
  Branches        10516    10523       +7     
==============================================
- Hits            32956    30416    -2540     
- Misses          27205    29999    +2794     
+ Partials         1697     1567     -130     
Files with missing lines Coverage Δ
art/defences/detector/evasion/__init__.py 100.00% <100.00%> (ø)
art/defences/detector/evasion/beyond_detector.py 19.67% <19.67%> (ø)

... and 54 files with indirect coverage changes

@beat-buesser beat-buesser changed the base branch from main to dev_1.19.0 September 4, 2024 09:00
@beat-buesser beat-buesser self-requested a review September 4, 2024 09:04
@beat-buesser beat-buesser self-assigned this Sep 4, 2024
@beat-buesser beat-buesser added the enhancement New feature or request label Sep 4, 2024
@beat-buesser beat-buesser added this to the ART 1.19.0 milestone Sep 4, 2024
@beat-buesser beat-buesser changed the base branch from dev_1.19.0 to main September 4, 2024 09:07
@beat-buesser beat-buesser changed the base branch from main to dev_1.19.0 September 4, 2024 09:07
Copy link
Collaborator

@beat-buesser beat-buesser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @allenhzy Thank you very much for your pull request! Could you please take a look at my review comments and add the proposed updates?

@@ -0,0 +1,163 @@
# MIT License
#
# Copyright (C) The Adversarial Robustness Toolbox (ART) Authors 2023
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Copyright (C) The Adversarial Robustness Toolbox (ART) Authors 2023
# Copyright (C) The Adversarial Robustness Toolbox (ART) Authors 2024

# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""
This module implements the abstract base class for all evasion detectors.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This module implements the abstract base class for all evasion detectors.
This module implements the BEYOND detector for adversarial examples detection.
| Paper link: https://openreview.net/pdf?id=S4LqI6CcJ3

Comment on lines 31 to 34
"""
BEYOND detector for adversarial samples detection.
This detector uses a combination of SSL and target model predictions to detect adversarial samples.
"""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""
BEYOND detector for adversarial samples detection.
This detector uses a combination of SSL and target model predictions to detect adversarial samples.
"""
"""
BEYOND detector for adversarial samples detection.
This detector uses a combination of SSL and target model predictions to detect adversarial examples.
| Paper link: https://openreview.net/pdf?id=S4LqI6CcJ3
"""

from __future__ import absolute_import, division, print_function, unicode_literals, annotations

import abc
from typing import Any
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
from typing import Any

from __future__ import absolute_import, division, print_function, unicode_literals, annotations

import abc
from typing import Any
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
from typing import Any

(x_train, y_train), (x_test, y_test), min_, max_ = get_cifar10

# Load models
# Download pretrained weights from https://drive.google.com/drive/folders/1ieEdd7hOj2CIl1FQfu4-3RGZmEj-mesi?usp=sharing
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How large are the downloaded files? Can we store them in the ART repo?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pre-trained model has over 100 M

Comment on lines 172 to 173
print(f"Clean Detection Accuracy: {clean_accuracy:.4f}")
print(f"Adversarial Detection Accuracy: {adv_accuracy:.4f}")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please replace print with logger.

Comment on lines 166 to 167
assert nb_true_positives > 0
assert nb_true_negatives > 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to make these assertions more accurate?

return {'z1': z1, 'z2': z2, 'p1': p1, 'p2': p2}

@pytest.fixture
def get_cifar10():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can use load_cifar10() directly and remove get_cifar10()(.

"""
Loads CIFAR10 dataset.
"""
(x_train, y_train), (x_test, y_test), min_, max_ = load_cifar10()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add import for fixture load_cifar10().

_, test_adv_detection = detector.detect(x_test_adv)

# Assert there is at least one true positive and negative
nb_true_positives = np.sum(test_adv_detection)

Check notice

Code scanning / CodeQL

Unused local variable Note test

Variable nb_true_positives is not used.

# Assert there is at least one true positive and negative
nb_true_positives = np.sum(test_adv_detection)
nb_true_negatives = len(test_detection) - np.sum(test_detection)

Check notice

Code scanning / CodeQL

Unused local variable Note test

Variable nb_true_negatives is not used.
nb_true_positives = np.sum(test_adv_detection)
nb_true_negatives = len(test_detection) - np.sum(test_detection)

clean_accuracy = 1 - np.mean(test_detection)

Check notice

Code scanning / CodeQL

Unused local variable Note test

Variable clean_accuracy is not used.
nb_true_negatives = len(test_detection) - np.sum(test_detection)

clean_accuracy = 1 - np.mean(test_detection)
adv_accuracy = np.mean(test_adv_detection)

Check notice

Code scanning / CodeQL

Unused local variable Note test

Variable adv_accuracy is not used.
Copy link
Collaborator

@beat-buesser beat-buesser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @allenhzy Thank you very much for your updates! Could you please take a look above at my two last change requests and the alerts from CodeQL about unused variables?

Comment on lines 18 to 21
"""
This module implements the BEYOND detector for adversarial examples detection.
| Paper link: https://openreview.net/pdf?id=S4LqI6CcJ3
"""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""
This module implements the BEYOND detector for adversarial examples detection.
| Paper link: https://openreview.net/pdf?id=S4LqI6CcJ3
"""
"""
This module implements the BEYOND detector for adversarial examples detection.
| Paper link: https://openreview.net/pdf?id=S4LqI6CcJ3
"""

from art.utils import CLASSIFIER_NEURALNETWORK_TYPE


logger = logging.getLogger(__name__)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line seems to cause the unittest errors because of the missing import of logging. Because the attack here is not actively using logging I suggest to remove this line.

Suggested change
logger = logging.getLogger(__name__)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have revised the related codes.

@allenhzy
Copy link
Contributor Author

allenhzy commented Dec 10, 2024 via email

Copy link
Collaborator

@beat-buesser beat-buesser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @allenhzy Thank you very much! There seems to be one more item making the test fail, the code suggestion above should fix them. Could you please add them to the PR?

art/defences/detector/evasion/beyond_detector.py Outdated Show resolved Hide resolved
art/defences/detector/evasion/beyond_detector.py Outdated Show resolved Hide resolved
tests/defences/detector/evasion/test_beyond_detector.py Outdated Show resolved Hide resolved
@beat-buesser
Copy link
Collaborator

Hi @allenhzy I have merge this PR into the release branch, but I had to make some changes to remove errors that prevented the detector from running at all. Could you please take a look at the changes here to see if they are correct and let me know?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants