Skip to content

Commit 508a0a4

Browse files
committed
Refactor GREAT and Retention scores
Signed-off-by: Beat Buesser <[email protected]>
1 parent 207a3cb commit 508a0a4

File tree

9 files changed

+280
-293
lines changed

9 files changed

+280
-293
lines changed

art/evaluations/great_art.py

Lines changed: 0 additions & 68 deletions
This file was deleted.

art/evaluations/great_art_test.py

Lines changed: 0 additions & 108 deletions
This file was deleted.
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) -> (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

art/evaluations/retention_score/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)