forked from shinington/facesec
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrobust_evaluation.py
executable file
·94 lines (80 loc) · 3.44 KB
/
robust_evaluation.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# -*- coding: utf-8 -*-
"""
robust_evalution.py
~~~~~~~
Main entry point for the robust assessment phase of FaseSec.
"""
import argparse
import os
from face_recognition import FaceRecognition
from utils import str2bool
def main():
args = parse_args()
if args.target == None or (args.target != None and not os.path.exists(args.output)):
# White-box attacks
# 1. Load source face recognition model.
source = FaceRecognition(args.mode,
args.source,
args.input,
args.gallery,
args.output,
args.universal)
print("##############################################################################")
print("Load source model: {}...".format(args.source))
source.load_model()
# 2. Evaluate on non-adversarial data.
print("Evaluate source model on clean data...")
source.eval_folder()
# 3. Evaluate by white-box attacks.
print("Evaluate source model using {} attacks...".format(args.attack))
source.eval_robustness(args.attack)
if args.target != None:
# Black-box attacks
# 4. Load target face recognition model.
target = FaceRecognition(args.mode,
args.target,
args.input,
args.gallery,
None,
None)
print("##############################################################################")
print("Load target model: {}...".format(args.target))
target.load_model()
# 5. Evaluate target on non-adversarial data
print("Evaluate target model on clean data...")
target.eval_folder()
# 6. Evaluate target with adversarial examples transferred from source
print("Evaluate target model on adversarial examples transferred from source model...")
target.eval_folder(args.output)
def parse_args():
p = argparse.ArgumentParser()
p.add_argument('-s', '--source',
help='Source model name to be evaluated.')
p.add_argument('-t', '--target', default=None,
help='Target model name to be evaluated (used in black-box attacks).')
p.add_argument('-m', '--mode',
help='Mode of face recognition. Can be one of [closed, open].')
p.add_argument('-a', '--attack',
help='Attack mothod Can be one of [pgd, sticker, eyeglass, facemask].')
p.add_argument('-u', '--universal', type=str2bool, nargs='?', const=True, default=False,
help='Whether to produce universal adversarial examples.')
p.add_argument('-i', '--input',
help='Directory of input test images.')
p.add_argument('-o', '--output',
help='Directory of output adversarial examples.')
p.add_argument('-g', '--gallery', default=None,
help='Directory of gallery images (ONLY and MUST for open-set).')
args = p.parse_args()
if not any([args.source, args.mode, args.attack, args.input, args.output]):
print('[!] Either a source model must be supplied [-s], '
'or a mode [-m],'
'or an attack must be invoked [-a],'
'or an input directory [-i],'
'or an output directory [-o].')
exit()
if args.mode == 'open' and args.gallery == None:
print('This is open-set face recognition, please input the gallery path [-g]')
exit()
return args
if __name__ == '__main__':
main()