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

Open
wants to merge 1 commit into
base: dev_1.19.0
Choose a base branch
from

Conversation

allenhzy
Copy link

@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

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

import abc

Check notice

Code scanning / CodeQL

Unused import Note

Import of 'abc' is not used.
from __future__ import absolute_import, division, print_function, unicode_literals, annotations

import abc
from typing import Any

Check notice

Code scanning / CodeQL

Unused import Note

Import of 'Any' is not used.
:param batch_size: Batch size for processing
:param nb_epochs: Number of training epochs (not used in this method)
"""
clean_similarities = self._get_metrics(x, batch_size)

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable clean_similarities is not used.
import pytest
import numpy as np

import sys

Check notice

Code scanning / CodeQL

Unused import Note test

Import of 'sys' is not used.
import numpy as np

import sys
import os

Check notice

Code scanning / CodeQL

Unused import Note test

Import of 'os' is not used.
import os

from art.attacks.evasion.fast_gradient import FastGradientMethod
from art.estimators.classification import PyTorchClassifier

Check notice

Code scanning / CodeQL

Unused import Note test

Import of 'PyTorchClassifier' is not used.
from art.attacks.evasion.fast_gradient import FastGradientMethod
from art.estimators.classification import PyTorchClassifier
from art.defences.detector.evasion import BeyondDetector
from art.utils import load_dataset, get_file

Check notice

Code scanning / CodeQL

Unused import Note test

Import of 'load_dataset' is not used.
Import of 'get_file' is not used.
def test_beyond_detector(art_warning, get_cifar10, get_ssl_model):
try:
# Load CIFAR10 data
(x_train, y_train), (x_test, y_test), min_, max_ = get_cifar10

Check notice

Code scanning / CodeQL

Unused local variable Note test

Variable min_ is not used.
def test_beyond_detector(art_warning, get_cifar10, get_ssl_model):
try:
# Load CIFAR10 data
(x_train, y_train), (x_test, y_test), min_, max_ = get_cifar10

Check notice

Code scanning / CodeQL

Unused local variable Note test

Variable max_ is not used.
Comment on lines +146 to +154
detector = BeyondDetector(
target_model=target_model,
ssl_model=ssl_model,
img_augmentation=img_augmentations,
aug_num=50,
alpha=0.8,
K=20,
percentile=5
)

Check failure

Code scanning / CodeQL

Wrong name for an argument in a class instantiation Error test

Keyword argument 'img_augmentation' is not a supported parameter name of
BeyondDetector.__init__
.
Copy link

codecov bot commented Sep 4, 2024

Codecov Report

Attention: Patch coverage is 21.53846% with 51 lines in your changes missing coverage. Please review.

Project coverage is 77.12%. Comparing base (42c6da4) to head (f5ec52c).
Report is 9 commits behind head on dev_1.19.0.

Files with missing lines Patch % Lines
art/defences/detector/evasion/beyond_detector.py 20.31% 50 Missing and 1 partial ⚠️

❗ There is a different number of reports uploaded between BASE (42c6da4) and HEAD (f5ec52c). Click for more details.

HEAD has 3 uploads less than BASE
Flag BASE (42c6da4) HEAD (f5ec52c)
27 24
Additional details and impacted files

Impacted file tree graph

@@              Coverage Diff               @@
##           dev_1.19.0    #2489      +/-   ##
==============================================
- Coverage       85.23%   77.12%   -8.11%     
==============================================
  Files             329      330       +1     
  Lines           30143    30208      +65     
  Branches         5173     5177       +4     
==============================================
- Hits            25693    23299    -2394     
- Misses           3021     5606    +2585     
+ Partials         1429     1303     -126     
Files with missing lines Coverage Δ
art/defences/detector/evasion/__init__.py 100.00% <100.00%> (ø)
art/defences/detector/evasion/beyond_detector.py 20.31% <20.31%> (ø)

... and 49 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?

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().

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
None yet
Development

Successfully merging this pull request may close these issues.

2 participants