Skip to content

Commit

Permalink
Merge pull request #1291 from Trusted-AI/dev_1.7.2
Browse files Browse the repository at this point in the history
Update to ART 1.7.2
  • Loading branch information
beat-buesser authored Aug 30, 2021
2 parents 00206ee + 9aadb45 commit 85ca10f
Show file tree
Hide file tree
Showing 106 changed files with 3,463 additions and 988 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ exclude_lines =
pragma: no cover
raise NotImplementedError
if __name__ == .__main__.:
if TYPE_CHECKING:
ignore_errors = True
omit =
docs/*
Expand Down
4 changes: 2 additions & 2 deletions .github/actions/deepspeech-v2/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

exit_code=0

pytest --cov-report=xml --cov=art --cov-append -q -vv tests/estimators/speech_recognition/test_pytorch_deep_speech.py --framework=pytorch --skip_travis=True --durations=0
pytest --cov-report=xml --cov=art --cov-append -q -vv tests/estimators/speech_recognition/test_pytorch_deep_speech.py --framework=pytorch --durations=0
if [[ $? -ne 0 ]]; then exit_code=1; echo "Failed estimators/speech_recognition/test_pytorch_deep_speech tests"; fi
pytest --cov-report=xml --cov=art --cov-append -q -vv tests/attacks/evasion/test_imperceptible_asr_pytorch.py --framework=pytorch --skip_travis=True --durations=0
pytest --cov-report=xml --cov=art --cov-append -q -vv tests/attacks/evasion/test_imperceptible_asr_pytorch.py --framework=pytorch --durations=0
if [[ $? -ne 0 ]]; then exit_code=1; echo "Failed attacks/evasion/test_imperceptible_asr_pytorch tests"; fi

exit ${exit_code}
4 changes: 2 additions & 2 deletions .github/actions/deepspeech-v3/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

exit_code=0

pytest --cov-report=xml --cov=art --cov-append -q -vv tests/estimators/speech_recognition/test_pytorch_deep_speech.py --framework=pytorch --skip_travis=True --durations=0
pytest --cov-report=xml --cov=art --cov-append -q -vv tests/estimators/speech_recognition/test_pytorch_deep_speech.py --framework=pytorch --durations=0
if [[ $? -ne 0 ]]; then exit_code=1; echo "Failed estimators/speech_recognition/test_pytorch_deep_speech tests"; fi
pytest --cov-report=xml --cov=art --cov-append -q -vv tests/attacks/evasion/test_imperceptible_asr_pytorch.py --framework=pytorch --skip_travis=True --durations=0
pytest --cov-report=xml --cov=art --cov-append -q -vv tests/attacks/evasion/test_imperceptible_asr_pytorch.py --framework=pytorch --durations=0
if [[ $? -ne 0 ]]; then exit_code=1; echo "Failed attacks/evasion/test_imperceptible_asr_pytorch tests"; fi

exit ${exit_code}
47 changes: 47 additions & 0 deletions .github/actions/tf-faster-rcnn/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Get base from a tensorflow image
FROM tensorflow/tensorflow:1.15.5-py3

# Set to install things in non-interactive mode
ENV DEBIAN_FRONTEND noninteractive

# Install system wide softwares
RUN apt-get update \
&& apt-get install -y \
libgl1-mesa-glx \
libx11-xcb1 \
git \
gcc \
mono-mcs \
libavcodec-extra \
ffmpeg \
curl \
libsndfile-dev \
libsndfile1 \
wget \
unzip \
&& apt-get clean all \
&& rm -r /var/lib/apt/lists/*

RUN wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
RUN bash Miniconda3-latest-Linux-x86_64.sh -b -p /miniconda
RUN /miniconda/bin/conda install --yes \
astropy \
matplotlib \
pandas \
scikit-learn \
scikit-image

# Install necessary libraries for tensorflow faster rcnn
RUN mkdir TensorFlow
RUN cd TensorFlow && git clone https://github.com/tensorflow/models
RUN cd TensorFlow && wget https://github.com/protocolbuffers/protobuf/releases/download/v3.17.3/protoc-3.17.3-linux-x86_64.zip
RUN cd TensorFlow && unzip protoc-3.17.3-linux-x86_64.zip -d protobuf
RUN cd TensorFlow/models/research && /TensorFlow/protobuf/bin/protoc object_detection/protos/*.proto --python_out=.
RUN cd TensorFlow/models/research && cp object_detection/packages/tf1/setup.py .
RUN cd TensorFlow/models/research && python -m pip install --use-feature=2020-resolver .

RUN pip install tqdm
RUN pip install requests
RUN pip install sklearn
RUN pip install numba==0.50.0
RUN pip install pytest-cov
7 changes: 7 additions & 0 deletions .github/actions/tf-faster-rcnn/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: 'Test TensorFlowFasterRCNN'
description: 'Run tests for TensorFlowFasterRCNN'
runs:
using: 'composite'
steps:
- run: $GITHUB_ACTION_PATH/run.sh
shell: bash
11 changes: 11 additions & 0 deletions .github/actions/tf-faster-rcnn/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

exit_code=0

pytest --cov-report=xml --cov=art --cov-append -q -vv tests/estimators/object_detection/test_tensorflow_faster_rcnn.py --framework=tensorflow --durations=0
if [[ $? -ne 0 ]]; then exit_code=1; echo "Failed estimators/object_detection/test_tensorflow_faster_rcnn.py tests"; fi

pytest --cov-report=xml --cov=art --cov-append -q -vv tests/attacks/test_shapeshifter.py --framework=tensorflow --durations=0
if [[ $? -ne 0 ]]; then exit_code=1; echo "Failed attacks/test_shapeshifter.py tests"; fi

exit ${exit_code}
87 changes: 87 additions & 0 deletions .github/workflows/ci-legacy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
name: CI Legacy
on:
# Run on manual trigger
workflow_dispatch:

# Run on pull requests
pull_request:
paths-ignore:
- '*.md'

# Run when pushing to main or dev branches
push:
branches:
- main
- dev*

# Run scheduled CI flow daily
schedule:
- cron: '0 8 * * 0'

jobs:
test:
runs-on: ubuntu-16.04
strategy:
fail-fast: false
matrix:
module: [attacks, classifiers, speech_recognizers, defences, metrics, wrappers, art]
include:
- name: legacy (TensorFlow 2.3.2 Keras 2.4.3 PyTorch 1.7.1 scikit-learn 0.22.2 Python 3.7)
framework: legacy
python: 3.7
tensorflow: 2.3.2
keras: 2.4.3
scikit-learn: 0.22.2
torch: 1.7.1+cpu
torchvision: 0.8.2+cpu
torchaudio: 0.7.2
- name: legacy (TensorFlow 2.4.1 Keras 2.4.3 PyTorch 1.7.1 scikit-learn 0.23.2 Python 3.7)
framework: legacy
python: 3.7
tensorflow: 2.4.1
keras: 2.4.3
torch: 1.7.1+cpu
torchvision: 0.8.2+cpu
torchaudio: 0.7.2
scikit-learn: 0.23.2
- name: legacy (TensorFlow 2.4.1 Keras 2.4.3 PyTorch 1.7.1 scikit-learn 0.24.1 Python 3.7)
framework: legacy
python: 3.7
tensorflow: 2.4.1
keras: 2.4.3
torch: 1.7.1+cpu
torchvision: 0.8.2+cpu
torchaudio: 0.7.2
scikit-learn: 0.24.1

name: Run ${{ matrix.module }} ${{ matrix.name }} Tests
steps:
- name: Checkout Repo
uses: actions/[email protected]
- name: Setup Python
uses: actions/[email protected]
with:
python-version: ${{ matrix.python }}
- name: Install Dependencies
run: |
sudo apt-get update
sudo apt-get -y -q install ffmpeg libavcodec-extra
python -m pip install --upgrade pip setuptools wheel
pip install tensorflow==2.4.1
pip install keras==2.4.3
pip3 install -q -r requirements.txt
pip list
- name: Pre-install legacy
if: ${{ matrix.framework == 'legacy' }}
run: |
pip install tensorflow==${{ matrix.tensorflow }}
pip install keras==${{ matrix.keras }}
pip install scikit-learn==${{ matrix.scikit-learn }}
pip install torch==${{ matrix.torch }} -f https://download.pytorch.org/whl/torch_stable.html
pip install torchvision==${{ matrix.torchvision }} -f https://download.pytorch.org/whl/torch_stable.html
pip install torchaudio==${{ matrix.torchaudio }} -f https://download.pytorch.org/whl/torch_stable.html
pip list
- name: Run ${{ matrix.name }} ${{ matrix.module }} Tests
run: ./run_tests.sh ${{ matrix.framework }} ${{ matrix.module }}
- name: Upload coverage to Codecov
uses: codecov/[email protected]
43 changes: 3 additions & 40 deletions .github/workflows/ci-mxnet.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: CI MXNet + Legacy
name: CI MXNet
on:
# Run on manual trigger
workflow_dispatch:
Expand Down Expand Up @@ -28,33 +28,6 @@ jobs:
- name: mxnet (Python 3.7)
framework: mxnet
python: 3.7
- name: legacy (TensorFlow 2.3.2 Keras 2.4.3 PyTorch 1.7.1 scikit-learn 0.22.2 Python 3.7)
framework: legacy
python: 3.7
tensorflow: 2.3.2
keras: 2.4.3
scikit-learn: 0.22.2
torch: 1.7.1+cpu
torchvision: 0.8.2+cpu
torchaudio: 0.7.2
- name: legacy (TensorFlow 2.4.1 Keras 2.4.3 PyTorch 1.7.1 scikit-learn 0.23.2 Python 3.7)
framework: legacy
python: 3.7
tensorflow: 2.4.1
keras: 2.4.3
torch: 1.7.1+cpu
torchvision: 0.8.2+cpu
torchaudio: 0.7.2
scikit-learn: 0.23.2
- name: legacy (TensorFlow 2.4.1 Keras 2.4.3 PyTorch 1.7.1 scikit-learn 0.24.1 Python 3.7)
framework: legacy
python: 3.7
tensorflow: 2.4.1
keras: 2.4.3
torch: 1.7.1+cpu
torchvision: 0.8.2+cpu
torchaudio: 0.7.2
scikit-learn: 0.24.1

name: Run ${{ matrix.name }} Tests
steps:
Expand All @@ -73,17 +46,7 @@ jobs:
pip install keras==2.4.3
pip3 install -q -r requirements.txt
pip list
- name: Pre-install legacy
if: ${{ matrix.framework == 'legacy' }}
run: |
pip install tensorflow==${{ matrix.tensorflow }}
pip install keras==${{ matrix.keras }}
pip install scikit-learn==${{ matrix.scikit-learn }}
pip install torch==${{ matrix.torch }} -f https://download.pytorch.org/whl/torch_stable.html
pip install torchvision==${{ matrix.torchvision }} -f https://download.pytorch.org/whl/torch_stable.html
pip install torchaudio==${{ matrix.torchaudio }} -f https://download.pytorch.org/whl/torch_stable.html
pip list
- name: Run ${{ matrix.name }} Tests
run: ./run_tests.sh ${{ matrix.framework }}
- name: Run ${{ matrix.name }} ${{ matrix.module }} Tests
run: ./run_tests.sh ${{ matrix.framework }} ${{ matrix.module }}
- name: Upload coverage to Codecov
uses: codecov/[email protected]
2 changes: 1 addition & 1 deletion .github/workflows/ci-pytorch-fasterrcnn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ jobs:
pip install torchvision==0.7.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
pip install torchaudio==0.6.0 -f https://download.pytorch.org/whl/torch_stable.html
- name: Run Test Action
run: pytest --cov-report=xml --cov=art --cov-append -q -vv tests/estimators/object_detection/test_pytorch_faster_rcnn.py --framework=pytorch --skip_travis=True --durations=0
run: pytest --cov-report=xml --cov=art --cov-append -q -vv tests/estimators/object_detection/test_pytorch_faster_rcnn.py --framework=pytorch --durations=0
- name: Upload coverage to Codecov
uses: codecov/[email protected]
32 changes: 32 additions & 0 deletions .github/workflows/ci-tf-faster-rcnn.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: CI TensorFlowFasterRCNN
on:
# Run on manual trigger
workflow_dispatch:

# Run on pull requests
pull_request:
paths-ignore:
- '*.md'

# Run when pushing to main or dev branches
push:
branches:
- main
- dev*

# Run scheduled CI flow daily
schedule:
- cron: '0 8 * * 0'

jobs:
test_tf_faster_rcnn:
name: TensorFlowFasterRCNN
runs-on: ubuntu-latest
container: minhitbk/art_testing_envs:tf_fasterrcnn
steps:
- name: Checkout Repo
uses: actions/[email protected]
- name: Run Test Action
uses: ./.github/actions/tf-faster-rcnn
- name: Upload coverage to Codecov
uses: codecov/[email protected]
10 changes: 0 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,6 @@ jobs:
pip install keras==2.4.3
pip3 install -q -r requirements.txt
pip list
- name: Pre-install legacy
if: ${{ matrix.framework == 'legacy' }}
run: |
pip install tensorflow==${{ matrix.tensorflow }}
pip install keras==${{ matrix.keras }}
pip install scikit-learn==${{ matrix.scikit-learn }}
pip install torch==${{ matrix.torch }} -f https://download.pytorch.org/whl/torch_stable.html
pip install torchvision==${{ matrix.torchvision }} -f https://download.pytorch.org/whl/torch_stable.html
pip install torchaudio==${{ matrix.torchaudio }} -f https://download.pytorch.org/whl/torch_stable.html
pip list
- name: Pre-install tensorflow
if: ${{ matrix.framework == 'tensorflow' || matrix.framework == 'keras' || matrix.framework == 'kerastf' || matrix.framework == 'tensorflow2v1' }}
run: |
Expand Down
2 changes: 1 addition & 1 deletion art/attacks/attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def __init__(
self._estimator = estimator
self.tensor_board = tensor_board

if tensor_board:
if tensor_board: # pragma: no cover
from tensorboardX import SummaryWriter

if isinstance(tensor_board, str):
Expand Down
25 changes: 21 additions & 4 deletions art/attacks/evasion/adversarial_patch/adversarial_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@

from art.attacks.evasion.adversarial_patch.adversarial_patch_numpy import AdversarialPatchNumpy
from art.attacks.evasion.adversarial_patch.adversarial_patch_tensorflow import AdversarialPatchTensorFlowV2
from art.attacks.evasion.adversarial_patch.adversarial_patch_pytorch import AdversarialPatchPyTorch
from art.estimators.estimator import BaseEstimator, NeuralNetworkMixin
from art.estimators.classification.classifier import ClassifierMixin
from art.estimators.classification import TensorFlowV2Classifier
from art.estimators.classification import TensorFlowV2Classifier, PyTorchClassifier
from art.attacks.attack import EvasionAttack

if TYPE_CHECKING:
Expand Down Expand Up @@ -94,7 +95,7 @@ def __init__(
if self.estimator.clip_values is None:
raise ValueError("Adversarial Patch attack requires a classifier with clip_values.")

self._attack: Union[AdversarialPatchTensorFlowV2, AdversarialPatchNumpy]
self._attack: Union[AdversarialPatchTensorFlowV2, AdversarialPatchPyTorch, AdversarialPatchNumpy]
if isinstance(self.estimator, TensorFlowV2Classifier):
self._attack = AdversarialPatchTensorFlowV2(
classifier=classifier,
Expand All @@ -107,6 +108,20 @@ def __init__(
patch_shape=patch_shape,
verbose=verbose,
)
elif isinstance(self.estimator, PyTorchClassifier):
self._attack = AdversarialPatchPyTorch(
classifier=classifier,
rotation_max=rotation_max,
scale_min=scale_min,
scale_max=scale_max,
distortion_scale_max=0.0,
learning_rate=learning_rate,
max_iter=max_iter,
batch_size=batch_size,
patch_shape=patch_shape,
patch_type="circle",
verbose=verbose,
)
else:
self._attack = AdversarialPatchNumpy(
classifier=classifier,
Expand Down Expand Up @@ -149,7 +164,9 @@ def generate(self, x: np.ndarray, y: Optional[np.ndarray] = None, **kwargs) -> T

return self._attack.generate(x=x, y=y, **kwargs)

def apply_patch(self, x: np.ndarray, scale: float, patch_external: Optional[np.ndarray] = None) -> np.ndarray:
def apply_patch(
self, x: np.ndarray, scale: float, patch_external: Optional[np.ndarray] = None, **kwargs
) -> np.ndarray:
"""
A function to apply the learned adversarial patch to images or videos.
Expand All @@ -158,7 +175,7 @@ def apply_patch(self, x: np.ndarray, scale: float, patch_external: Optional[np.n
:param patch_external: External patch to apply to images `x`.
:return: The patched instances.
"""
return self._attack.apply_patch(x, scale, patch_external=patch_external)
return self._attack.apply_patch(x, scale, patch_external=patch_external, **kwargs)

def reset_patch(self, initial_patch_value: Optional[Union[float, np.ndarray]]) -> None:
"""
Expand Down
Loading

0 comments on commit 85ca10f

Please sign in to comment.