-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattack_adversa.py
59 lines (50 loc) · 1.53 KB
/
attack_adversa.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import utils.adversa as adversa
from dotenv import load_dotenv
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"--threshold",
type=float,
default=0.99999,
help="samples with confidence below threshold get attacked until their confidence is higher than the threshold",
)
parser.add_argument(
"--extension_factor",
type=float,
default=0.9,
help="the size if the cutout from the attacked face",
)
parser.add_argument(
"--epochs", type=int, default=500, help="number of epochs for the attack"
)
parser.add_argument(
"--use_alternate_attack",
type=bool,
action="store_true",
default=False,
help="when True, uses exactly cut out faces instead of square cutouts",
)
args = parser.parse_args()
load_dotenv()
MLSEC_API_KEY = os.getenv("MLSEC_API_KEY")
if args.use_alternate_attack:
attack_type = adversa.AttackScheduler
else:
attack_type = adversa.AlternateAttackScheduler
scheduler = attack_type(
MLSEC_API_KEY, extension_factor=args.extension_factor
)
threshold = args.threshold
for i in range(100):
repeaters = [
(int(el["Name"][0]), int(el["Name"][2]))
for el in scheduler.db.all()
if el["confidence"] < threshold
]
print(repeaters)
if len(repeaters) < 4:
threshold = 1 - (1 - threshold) / 2.0
print(f"New threshold is {threshold}")
for source_id, target_id in repeaters:
print(f"{source_id}_{target_id}")
scheduler.attack_pair(source_id, target_id, verbose=False, epochs=args.epochs)