-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add class AttackMetric and asr meric
- Loading branch information
1 parent
bc4a85c
commit 2c8bbf6
Showing
4 changed files
with
160 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from typing import Union, List, Callable | ||
|
||
import sklearn | ||
import torch | ||
|
||
|
||
def asr( | ||
y_predict_clean, | ||
y_predict_after_attack_only, | ||
**kwargs | ||
): | ||
if isinstance(y_predict_clean, torch.Tensor): | ||
if y_predict_clean.dim() > 1: | ||
y_predict_clean = y_predict_clean.argmax(dim=1) | ||
y_predict_clean.cpu() | ||
if isinstance(y_predict_after_attack_only, torch.Tensor): | ||
if y_predict_after_attack_only.dim() > 1: | ||
y_predict_after_attack_only = y_predict_after_attack_only.argmax(dim=1) | ||
y_predict_after_attack_only.cpu() | ||
print("ASR ", 1 - sklearn.metrics.accuracy_score(y_true=y_predict_clean, y_pred=y_predict_after_attack_only)) | ||
return 1 - sklearn.metrics.accuracy_score(y_true=y_predict_clean, y_pred=y_predict_after_attack_only) | ||
|
||
|
||
class AttackMetric: | ||
available_metrics = { | ||
'ASR': asr, | ||
} | ||
|
||
def __init__( | ||
self, | ||
name: str, | ||
**kwargs | ||
): | ||
self.name = name | ||
self.kwargs = kwargs | ||
|
||
def compute( | ||
self, | ||
metrics_clean_model, | ||
metrics_after_attack | ||
): | ||
if self.name in AttackMetric.available_metrics: | ||
return AttackMetric.available_metrics[self.name]( | ||
metrics_clean_model, | ||
metrics_after_attack, | ||
**self.kwargs | ||
) | ||
raise NotImplementedError() | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters