-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Complete tests for create_attack and others
- Loading branch information
1 parent
b836831
commit f5db6b0
Showing
4 changed files
with
178 additions
and
2 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,41 @@ | ||
import pytest | ||
import torch | ||
|
||
from torchattack.eval import FoolingRateMetric | ||
|
||
|
||
@pytest.fixture() | ||
def metric(): | ||
return FoolingRateMetric() | ||
|
||
|
||
def test_initial_state(metric): | ||
assert metric.total_count.item() == 0 | ||
assert metric.clean_count.item() == 0 | ||
assert metric.adv_count.item() == 0 | ||
|
||
|
||
def test_update(metric): | ||
labels = torch.tensor([0, 1, 2]) | ||
clean_logits = torch.tensor([[0.9, 0.1, 0.0], [0.1, 0.8, 0.1], [0.2, 0.2, 0.6]]) | ||
adv_logits = torch.tensor([[0.1, 0.8, 0.1], [0.2, 0.6, 0.2], [0.9, 0.1, 0.0]]) | ||
|
||
metric.update(labels, clean_logits, adv_logits) | ||
|
||
assert metric.total_count.item() == 3 | ||
assert metric.clean_count.item() == 3 # all clean samples are correctly classified | ||
assert metric.adv_count.item() == 1 # only the 2nd sample is correctly classified | ||
|
||
|
||
def test_compute(metric): | ||
labels = torch.tensor([0, 1, 2]) | ||
clean_logits = torch.tensor([[0.9, 0.1, 0.0], [0.1, 0.8, 0.1], [0.2, 0.2, 0.6]]) | ||
adv_logits = torch.tensor([[0.1, 0.8, 0.1], [0.2, 0.6, 0.2], [0.9, 0.1, 0.0]]) | ||
|
||
metric.update(labels, clean_logits, adv_logits) | ||
clean_acc, adv_acc, fooling_rate = metric.compute() | ||
|
||
assert clean_acc.item() == pytest.approx(3 / 3) | ||
assert adv_acc.item() == pytest.approx(1 / 3) | ||
# fooling_rate = (clean_count - adv_count) / clean_count | ||
assert fooling_rate.item() == pytest.approx((3 - 1) / 3) |
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