-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_forward.py
163 lines (143 loc) · 4.14 KB
/
test_forward.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import torch
import sys
from config import get_config
from vit_pytorch import ViT_face
from vit_pytorch import ViTs_face
from vit_pytorch import EfficientNet_V2_face
from vit_pytorch import EfficientNet_V2_ViT
from vit_pytorch import EfficientNet_V1_ViT
from vit_pytorch import EfficientNet_Trim_ViT
from vit_pytorch import CrossViT
from util.utils import (
get_val_data,
test_forward,
)
import argparse
import os
def main(args):
print(args)
DEVICE = torch.device("cuda:0")
DATA_ROOT = "./Data/casia-webface/"
# ======= Hyperparameters & Data Loaders =======#
cfg = get_config(args)
# Support: ['Softmax', 'ArcFace', 'CosFace', 'SFaceLoss']
HEAD_NAME = cfg["HEAD_NAME"]
# Specify GPU ids
GPU_ID = cfg["GPU_ID"]
print("GPU_ID", GPU_ID)
with open(os.path.join(DATA_ROOT, "property"), "r") as f:
NUM_CLASS, h, w = [int(i) for i in f.read().split(",")]
# ViT
if args.network == "VIT":
model = ViT_face(
image_size=112,
patch_size=8,
loss_type=HEAD_NAME,
GPU_ID=DEVICE,
num_class=NUM_CLASS,
dim=512,
depth=20,
heads=8,
mlp_dim=2048,
dropout=0.1,
emb_dropout=0.1,
)
# ViTs
elif args.network == "VITs":
model = ViTs_face(
image_size=112,
patch_size=8,
loss_type=HEAD_NAME,
GPU_ID=DEVICE,
num_class=NUM_CLASS,
ac_patch_size=12,
pad=4,
dim=512,
depth=20,
heads=8,
mlp_dim=2048,
dropout=0.1,
emb_dropout=0.1,
)
# EfficientNet_V2_face
elif args.network == "EffNet_V2_face":
model = (
EfficientNet_V2_face(
GPU_ID=GPU_ID,
pretrained=True,
fine_tune=True,
loss_type=HEAD_NAME,
dim=512,
num_class=10572,
),
)
# EfficientNet_V2 + ViT
elif args.network == "EffNet_V2_VIT":
model = (
EfficientNet_V2_ViT(
GPU_ID=GPU_ID,
pretrained=True,
fine_tune=True,
loss_type=HEAD_NAME,
dim=512,
num_class=10572,
),
)
# EfficientNet_V1 + ViT
elif args.network == "EffNet_V1_VIT":
model = (
EfficientNet_V1_ViT(
GPU_ID=GPU_ID,
pretrained=True,
fine_tune=True,
loss_type=HEAD_NAME,
dim=512,
num_class=10572,
),
)
# Trimmed EfficientNet + ViT
elif args.network == "EffNet_trim_VIT":
model = (
EfficientNet_Trim_ViT(
GPU_ID,
pretrained=True,
fine_tune=False,
loss_type=HEAD_NAME,
dim=512,
num_class=10572,
),
)
# CrossViT
elif args.network == "CROSSVIT":
model = (
CrossViT(
GPU_ID,
loss_type=HEAD_NAME,
dim=512,
num_class=10572,
),
)
model_root = args.model
model.load_state_dict(torch.load(model_root))
TARGET = [i for i in args.target.split(",")]
vers = get_val_data("./eval/", TARGET)
for ver in vers:
name, data_set, issame = ver
time = test_forward(DEVICE, model, data_set)
def parse_arguments(argv):
parser = argparse.ArgumentParser()
parser.add_argument(
"--model",
default="",
help="pretrained model",
type=str,
)
parser.add_argument(
"--target",
help="verification targets ['agedb_30', 'calfw','cfp_ff', 'cfp_fp', 'cplfw', 'lfw', 'sllfw', 'talfw']",
default="lfw",
type=str,
)
return parser.parse_args(argv)
if __name__ == "__main__":
main(parse_arguments(sys.argv[1:]))