Skip to content

Commit 4c70c22

Browse files
authored
Merge pull request #2636 from Trusted-AI/lizaitang-main_great_score
Lizaitang main great score
2 parents f9600a4 + 84f5819 commit 4c70c22

File tree

9 files changed

+132
-38
lines changed

9 files changed

+132
-38
lines changed

.github/actions/deepspeech-v3/Dockerfile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Get base image
2-
FROM python:3.10.13-slim-bookworm
2+
FROM python:3.10.17-slim-bookworm
33

44
# Set to install things in non-interactive mode
55
ENV DEBIAN_FRONTEND noninteractive
@@ -11,7 +11,6 @@ RUN apt-get update \
1111
libx11-xcb1 \
1212
git \
1313
gcc \
14-
mono-mcs \
1514
libavcodec-extra \
1615
ffmpeg \
1716
curl \
@@ -25,10 +24,12 @@ RUN apt-get update \
2524
&& rm -r /var/lib/apt/lists/*
2625

2726
# Install necessary libraries for deepspeech v3
28-
RUN pip install --ignore-installed PyYAML torch==2.1.1 tensorflow==2.14.1 torchaudio==2.1.1 pytorch-lightning==2.1.2 scikit-learn==1.3.2
27+
RUN pip install --ignore-installed torch==2.1.2 torchaudio==2.1.2 --index-url https://download.pytorch.org/whl/cpu
28+
RUN pip install --ignore-installed --no-deps pytorch-lightning==2.1.2
29+
RUN pip install --ignore-installed PyYAML tensorflow==2.14.1 scikit-learn==1.3.2
2930
RUN pip install --no-build-isolation fairscale==0.4.13
3031

31-
RUN git clone https://github.com/SeanNaren/deepspeech.pytorch.git && cd deepspeech.pytorch && sed -i '/^sklearn/d' requirements.txt && pip install -r requirements.txt && pip install -e .
32+
RUN git clone https://github.com/SeanNaren/deepspeech.pytorch.git && cd deepspeech.pytorch && sed -i '/^sklearn/d;/^torch/d;/^torchaudio/d' requirements.txt && pip install -r requirements.txt && pip install -e .
3233

3334
RUN pip install numba==0.56.4 pytest-cov==4.1.0 pydub==0.25.1
3435

.github/actions/espresso/Dockerfile

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# Get base image
2-
FROM python:3.7.17-slim-bookworm
3-
4-
# Set to install things in non-interactive mode
5-
ENV DEBIAN_FRONTEND noninteractive
2+
FROM python:3.10.17-slim-bookworm
63

74
# Install system wide software
85
RUN apt-get update \
96
&& apt-get install -y \
107
build-essential \
8+
gfortran \
9+
libblas-dev \
10+
liblapack-dev \
11+
libatlas-base-dev \
1112
libgl1-mesa-glx \
1213
libx11-xcb1 \
1314
git \
1415
gcc \
15-
mono-mcs \
1616
libavcodec-extra \
1717
ffmpeg \
1818
curl \
@@ -24,7 +24,10 @@ RUN apt-get update \
2424
&& rm -r /var/lib/apt/lists/*
2525

2626
# Install necessary libraries for espresso
27-
RUN pip install torch==1.6.0 torchaudio==0.6.0 pytest-cov~=4.1.0 scipy==1.2 scikit-learn==0.24 protobuf==3.20 kaldiio==2.18.0 tensorflow==2.9
27+
RUN pip install --upgrade pip setuptools wheel
28+
RUN pip install --ignore-installed torch==1.11.0 torchaudio==0.11.0 --index-url https://download.pytorch.org/whl/cpu
29+
RUN pip install numpy==1.21.6 scipy==1.9.3
30+
RUN pip install pytest-cov~=4.1.0 scikit-learn==1.6.1 protobuf==3.20 kaldiio==2.18.0 tensorflow==2.9
2831
RUN pip install --no-build-isolation fairscale==0.3.6
2932

3033
RUN git clone https://github.com/beat-buesser/espresso

.github/actions/goturn/Dockerfile

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Get base image
2-
FROM python:3.8.19-slim-bookworm
2+
FROM python:3.10.17-slim-bookworm
33

44
ENV MONO_TLS_PROVIDER=legacy
55

@@ -11,7 +11,6 @@ RUN apt-get update \
1111
libx11-xcb1 \
1212
git \
1313
gcc \
14-
mono-mcs \
1514
libavcodec-extra \
1615
ffmpeg \
1716
curl \
@@ -26,12 +25,14 @@ RUN apt-get update \
2625
&& rm -r /var/lib/apt/lists/*
2726

2827
# Install necessary libraries for goturn
29-
RUN pip install torch==1.9.1 torchvision==0.10.1 tensorflow==2.9 scikit-learn==0.24 pytest~=6.2.5 pytest-cov~=4.1.0
28+
RUN pip install --ignore-installed torch==1.11.0 torchaudio==0.11.0 torchvision==0.12.0 --index-url https://download.pytorch.org/whl/cpu
29+
RUN pip install --ignore-installed --no-deps pytorch_lightning==0.7.1 torch_lr_finder==0.2.1 torchsummary==1.5.1
30+
RUN pip install tensorflow==2.9 scikit-learn==1.6.1 pytest~=6.2.5 pytest-cov~=4.1.0 matplotlib==3.9.4
3031

3132
RUN git clone https://github.com/nrupatunga/goturn-pytorch.git /tmp/goturn-pytorch
3233
#RUN cd /tmp/goturn-pytorch && pip install -r requirements.txt
33-
RUN pip install loguru==0.5.3 torchsummary==1.5.1 tqdm==4.62.3 pytorch_lightning==0.7.1 imutils==0.5.3 torch_lr_finder==0.2.1 numpy==1.21.6 Pillow==8.0.1 visdom==0.1.8.9 gdown==5.1.0
34-
RUN pip3 install opencv_python==4.9.0.80 --no-build-isolation
34+
RUN pip install loguru==0.5.3 tqdm==4.62.3 imutils==0.5.3 numpy==1.23.5 Pillow==8.0.1 visdom==0.1.8.9 gdown==5.1.0
35+
RUN pip install opencv_python==4.9.0.80 --no-build-isolation
3536

3637
ENV PYTHONPATH "${PYTHONPATH}:/tmp/goturn-pytorch/src"
3738
ENV PYTHONPATH "${PYTHONPATH}:/tmp/goturn-pytorch/src/scripts"
Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
# Get base from a tensorflow image
2-
FROM tensorflow/tensorflow:2.13.0
3-
4-
# Set to install things in non-interactive mode
5-
ENV DEBIAN_FRONTEND noninteractive
1+
# Get base image
2+
FROM python:3.8.20-slim-bookworm
63

74
# Install system wide softwares
85
RUN apt-get update \
@@ -11,36 +8,28 @@ RUN apt-get update \
118
libx11-xcb1 \
129
git \
1310
gcc \
14-
mono-mcs \
1511
libavcodec-extra \
1612
ffmpeg \
1713
curl \
1814
libsndfile-dev \
1915
libsndfile1 \
2016
wget \
2117
unzip \
18+
software-properties-common \
19+
gnupg2 \
20+
lsb-release \
2221
&& apt-get clean all \
2322
&& rm -r /var/lib/apt/lists/*
2423

25-
RUN wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
26-
RUN bash Miniconda3-latest-Linux-x86_64.sh -b -p /miniconda
27-
RUN /miniconda/bin/conda install --yes \
28-
astropy \
29-
matplotlib \
30-
pandas \
31-
scikit-learn \
32-
scikit-image
24+
RUN pip install --upgrade pip setuptools wheel
3325

3426
# Install necessary libraries for tensorflow faster rcnn
3527
RUN mkdir TensorFlow
3628
RUN cd TensorFlow && git clone https://github.com/tensorflow/models
3729
RUN cd TensorFlow && wget https://github.com/protocolbuffers/protobuf/releases/download/v3.17.3/protoc-3.17.3-linux-x86_64.zip
3830
RUN cd TensorFlow && unzip protoc-3.17.3-linux-x86_64.zip -d protobuf
3931
RUN cd TensorFlow/models/research && /TensorFlow/protobuf/bin/protoc object_detection/protos/*.proto --python_out=.
40-
RUN cd TensorFlow/models/research && cp object_detection/packages/tf2/setup.py .
32+
RUN cd TensorFlow/models/research && cp object_detection/packages/tf2/setup.py . && sed -i "/^'tensorflow_io',$/d; /^'keras',$/d" setup.py
4133
RUN cd TensorFlow/models/research && python -m pip install .
4234

43-
RUN pip install tqdm
44-
RUN pip install requests
45-
RUN pip install numba==0.50.0
46-
RUN pip install pytest-cov
35+
RUN pip install numpy matplotlib tqdm requests llvmlite numba==0.56.4 pytest-cov "pytest<8.0.0" "exceptiongroup<1.1.0" tensorflow==2.6.0 keras==2.6.0

.github/actions/yolo/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Get base image
2-
FROM python:3.9.19-slim
2+
FROM python:3.10.17-slim-bookworm
33

44
# Install system wide software
55
RUN apt-get update \
@@ -8,7 +8,6 @@ RUN apt-get update \
88
libx11-xcb1 \
99
git \
1010
gcc \
11-
mono-mcs \
1211
cmake \
1312
libavcodec-extra \
1413
ffmpeg \
@@ -20,7 +19,8 @@ RUN apt-get update \
2019
&& apt-get clean all \
2120
&& rm -r /var/lib/apt/lists/*
2221

23-
RUN pip install pytorchyolo==1.8.0 tensorflow==2.14.1 scikit-learn==1.4.2 pytest-cov~=4.1.0 torch==1.12.1
22+
RUN pip install --ignore-installed torch==1.12.1 --index-url https://download.pytorch.org/whl/cpu
23+
RUN pip install pytorchyolo==1.8.0 tensorflow==2.14.1 scikit-learn==1.4.2 pytest-cov~=4.1.0
2424

2525
RUN cd /tmp/ && git clone https://github.com/eriklindernoren/PyTorch-YOLOv3.git && cd ./PyTorch-YOLOv3/weights && ./download_weights.sh
2626

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""
2+
This module implements the evaluation of GREAT Score.
3+
"""
4+
5+
from art.evaluations.great_score.great_score import GreatScorePyTorch
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# MIT License
2+
#
3+
# Copyright (C) The Adversarial Robustness Toolbox (ART) Authors 2024
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+
import numpy as np
19+
20+
from art.evaluations.evaluation import Evaluation
21+
22+
23+
class GreatScorePyTorch(Evaluation):
24+
25+
def __init__(self, classifier):
26+
"""
27+
Calculate the GREAT score and accuracy for given samples using the specified classifier.
28+
29+
:param classifier: ART Classifier of the model to evaluate.
30+
"""
31+
self.classifier = classifier
32+
super().__init__()
33+
34+
def evaluate(self, x: np.ndarray, y: np.ndarray) -> tuple[list[float], float]:
35+
"""
36+
Calculate the GREAT score and accuracy for given samples using the specified model.
37+
38+
:param x: Input samples (images) as a numpy array.
39+
:param y: True labels for the samples.
40+
41+
:return: great_score, accuracy
42+
"""
43+
# Calculate accuracy
44+
y_pred: np.ndarray = self.classifier.predict(x=x)
45+
correct_predictions: np.ndarray = np.argmax(y_pred, axis=1) == np.argmax(y, axis=1)
46+
accuracy = np.mean(correct_predictions)
47+
48+
# Calculate the GREAT score
49+
k = 2
50+
top2_indices = np.argpartition(y_pred, -k, axis=1)[:, -k:] # (batch_size, 2), unsorted
51+
top2_values = np.take_along_axis(y_pred, top2_indices, axis=1) # corresponding values
52+
# Now sort these top-2 values and indices in descending order
53+
sorted_order = np.argsort(top2_values, axis=1)[:, ::-1]
54+
top2_values_sorted = np.take_along_axis(top2_values, sorted_order, axis=1)
55+
great_scores = np.where(correct_predictions, top2_values_sorted[:, 0] - top2_values_sorted[:, 1], 0)
56+
average_great_score = np.mean(great_scores)
57+
58+
return average_great_score, accuracy

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ def get_version(rel_path):
119119
"Intended Audience :: Education",
120120
"Intended Audience :: Science/Research",
121121
"License :: OSI Approved :: MIT License",
122-
"Programming Language :: Python :: 3.9",
123122
"Programming Language :: Python :: 3.10",
124123
"Programming Language :: Python :: 3.11",
124+
"Programming Language :: Python :: 3.12",
125125
"Topic :: Software Development :: Libraries",
126126
"Topic :: Software Development :: Libraries :: Python Modules",
127127
"Topic :: Scientific/Engineering :: Artificial Intelligence",

tests/evaluations/test_great_score.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# MIT License
2+
#
3+
# Copyright (C) The Adversarial Robustness Toolbox (ART) Authors 2024
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+
import pytest
19+
20+
from art.evaluations.great_score import GreatScorePyTorch
21+
22+
from tests.utils import ARTTestException
23+
24+
25+
@pytest.mark.only_with_platform("pytorch")
26+
def test_great_score(art_warning, image_dl_estimator, get_mnist_dataset):
27+
try:
28+
classifier, _ = image_dl_estimator(from_logits=False)
29+
(_, _), (x_test, y_test) = get_mnist_dataset
30+
31+
great_score = GreatScorePyTorch(classifier=classifier)
32+
score, accuracy = great_score.evaluate(x=x_test, y=y_test)
33+
assert score == pytest.approx(0.03501499, rel=1e-6)
34+
assert accuracy == pytest.approx(0.2487, rel=1e-4)
35+
36+
except ARTTestException as e:
37+
art_warning(e)

0 commit comments

Comments
 (0)