-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_iom_random.py
167 lines (140 loc) · 8.29 KB
/
test_iom_random.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
164
165
from absl import app, flags, logging
from absl.flags import FLAGS
# import cv2
import os
import numpy as np
import tensorflow as tf
if tf.__version__.startswith('1'):# important is you want to run with tf1.x,
print('[*] enable eager execution')
tf.compat.v1.enable_eager_execution()
import modules
import csv
import math
from modules.evaluations import get_val_data, perform_val, perform_val_yts
from modules.utils import set_memory_growth, load_yaml, l2_norm
from modules.models import ArcFaceModel, IoMFaceModelFromArFace, build_or_load_Random_IoMmodel,IoMFaceModelFromArFace2,IoMFaceModelFromArFace3,IoMFaceModelFromArFace_T,IoMFaceModelFromArFace_T1
from modules.utils import tic,toc
flags.DEFINE_string('cfg_path', './configs/iom_res50_random.yaml', 'config file path')
flags.DEFINE_string('gpu', '0', 'which gpu to use')
flags.DEFINE_string('img_path', '', 'path to input image')
flags.DEFINE_integer('insightmodel', 0, 'using insightface pretrained model')
# modules.utils.set_memory_growth()
mycfg = {'m': 0, 'q': 0}
def callMe():
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
logger = tf.get_logger()
logger.disabled = True
logger.setLevel(logging.FATAL)
set_memory_growth()
isInsightmodel = 100
# cfg = load_yaml('./configs/iom_res50_random_xception.yaml') # cfg = load_yaml(FLAGS.cfg_path)
# cfg_arc = load_yaml('configs/config_arc/arc_Insight_res100.yaml') # cfg = load_yaml(FLAGS.cfg_path)
# cfg = cfg_iom = load_yaml('configs/config_random/iom_res100_random_insightface.yaml') # cfg = load_yaml(FLAGS.cfg_path)
# cfg = cfg_iom = load_yaml('./configs/config_random/iom_res50_random_inceptionresnet.yaml')
# cfg = cfg_iom = load_yaml('./configs/config_random/iom_res50_random_inceptionresnet.yaml')
# cfg = cfg_iom = load_yaml('./configs/config_random/iom_res50_random.yaml')
# cfg = cfg_iom = load_yaml('./configs/config_random/iom_res50_random_inceptionresnet.yaml')
cfg = cfg_iom = load_yaml('./configs/config_random/iom_res50_random_xception.yaml')
# cfg = cfg_iom = load_yaml('./configs/config_random/iom_res100_random_insightface.yaml')
m = cfg['m'] = mycfg['m']
q = cfg['q'] = mycfg['q']
cfg['T'] = 10# whateever
model = build_or_load_Random_IoMmodel(cfg)
model.summary(line_length=80)
model.layers[0].trainable = False
# for layer in model.layers:
# print(layer.name)
# layer.trainable = False
cfg['embd_shape'] = m * q
def evl(isLUT, measure):
# if measure == 'Jaccard':
# isLUT = q
#
# print("[*] Perform Retrieval Evaluation on Y.T.F and F.S...")
# mAp_ytf, rr_ytf = perform_val_yts(cfg['eval_batch_size'], model, cfg['test_dataset_ytf'], img_ext='jpg',
# isLUT=isLUT, cfg=cfg)
# mAp_fs, rr_fs = perform_val_yts(cfg['eval_batch_size'], model, cfg['test_dataset_fs'], img_ext='png',
# isLUT=isLUT, cfg=cfg)
# print(" Y.T.F mAP {:.4f}, F.S mAP: {:.2f}".format(mAp_ytf, mAp_fs))
# print(" Y.T.F CMC-1 {:.4f}, F.S CMC-1: {:.2f}".format(rr_ytf[0], rr_fs[0]))
mAp_fs = mAp_ytf = 0
rr_ytf = rr_fs = [0]
is_flip = False
print('[*] is_flip : {}'.format(is_flip))
print("[*] Loading LFW, AgeDB30 and CFP-FP...")
lfw, agedb_30, cfp_fp, lfw_issame, agedb_30_issame, cfp_fp_issame = \
get_val_data(cfg['test_dataset'])
print("[*] Perform Evaluation on LFW...")
acc_lfw, best_th_lfw, auc_lfw, eer_lfw, embeddings_lfw = perform_val(
cfg['embd_shape'], cfg['eval_batch_size'], model, lfw, lfw_issame,
is_ccrop=cfg['is_ccrop'], cfg=cfg, isLUT=isLUT, measure=measure,is_flip=is_flip)
print(" acc {:.4f}, th: {:.2f}, auc {:.4f}, EER {:.4f}".format(acc_lfw, best_th_lfw, auc_lfw, eer_lfw))
# with open('embeddings/' + cfg['sub_name'] + measure + '_' + str(isLUT) + str(m) + '_' + str(
# q) + '_embeddings_lfw.csv', 'w', newline='') as file:
# writer = csv.writer(file, escapechar='/', quoting=csv.QUOTE_NONE)
# writer.writerows(embeddings_lfw)
print("[*] Perform Evaluation on AgeDB30...")
acc_agedb30, best_th_agedb30, auc_agedb30, eer_agedb30, embeddings_agedb30 = perform_val(
cfg['embd_shape'], cfg['eval_batch_size'], model, agedb_30,
agedb_30_issame, is_ccrop=cfg['is_ccrop'], cfg=cfg, isLUT=isLUT, measure=measure,is_flip=is_flip)
print(" acc {:.4f}, th: {:.2f}, auc {:.4f}, EER {:.4f}".format(acc_agedb30, best_th_agedb30, auc_agedb30,
eer_agedb30))
print("[*] Perform Evaluation on CFP-FP...")
acc_cfp_fp, best_th_cfp_fp, auc_cfp_fp, eer_cfp_fp, embeddings_cfp_fp = perform_val(
cfg['embd_shape'], cfg['eval_batch_size'], model, cfp_fp, cfp_fp_issame,
is_ccrop=cfg['is_ccrop'], cfg=cfg, isLUT=isLUT, measure=measure,is_flip=is_flip)
print(" acc {:.4f}, th: {:.2f}, auc {:.4f}, EER {:.4f}".format(acc_cfp_fp, best_th_cfp_fp, auc_cfp_fp,
eer_cfp_fp))
log_str = '''| q = {:.2f}, m = {:.2f},LUT={} | LFW | AgeDB30 | CFP - FP |
|------------------------|--------|---------|----------|
| Accuracy | {:.4f} | {:.4f} | {:.4f} |
| EER | {:.4f} | {:.4f} | {:.4f} |
| AUC | {:.4f} | {:.4f} | {:.4f} |
| Threshold | {:.4f} | {:.4f} | {:.4f} |
| | mAP | CMC-1 | |
| Y.T.F | {:.4f} | {:.4f} | |
| F.S | {:.4f} | {:.4f} | | \n\n '''.format(q, m, isLUT,
acc_lfw, acc_agedb30,
acc_cfp_fp,
eer_lfw, eer_agedb30,
eer_cfp_fp,
auc_lfw, auc_agedb30,
auc_cfp_fp,
best_th_lfw, best_th_agedb30,
best_th_cfp_fp,
mAp_ytf, rr_ytf[0],
mAp_fs, rr_fs[0])
# with open('./logs/' + cfg['sub_name'] + "_OutputHamming.md", "a") as text_file:
# text_file.write(log_str)
print(log_str)
log_str2 = '''|{}, m={},q={}, LUT={},dist={}\t {:.4f} \t {:.4f} \t {:.4f} \t {:.4f} \t {:.4f} \t {:.4f} \t {:.4f} \t {:.4f} \t {:.4f} \t {:.4f} \t {:.4f} \t {:.4f} \t {:.4f}\n'''.format(
cfg['sub_name'], m,q,isLUT, measure, mAp_ytf, mAp_fs, rr_ytf[0], rr_fs[0], eer_lfw, eer_agedb30, eer_cfp_fp, acc_lfw,
acc_agedb30, acc_cfp_fp, auc_lfw, auc_agedb30, auc_cfp_fp)
with open('./logs/1217_Output_line_.md', "a") as text_file:
text_file.write(log_str2)
# evl(0,measure='Euclidean') # no LUT
# evl(0,measure='Jaccard') # no LUT
# evl(0,measure='Cosine') # no LUT
# evl(int(math.log2(q)), measure='Euclidean') # no LUT
# evl(q, measure='Jaccard') # no LUT
# evl(q, measure='Cosine') # no LUT
evl(0, measure='Hamming') # no LUT
evl(int(math.log2(q)), measure='Hamming') # no LUT
# evl(4)
# evl(8)
# evl(16)
for aaaa in [1]:
for m in [32, 64, 128, 256, 512]:
for q in [8]:
print(m, q, '****')
mycfg['m'] = m
mycfg['q'] = q
callMe()
for aaaa in [1]:
for m in [512]:
for q in [2, 4, 8, 16, 32]: # 2,4,8,16,32,64
print(m, q, '****')
mycfg['m'] = m
mycfg['q'] = q
callMe()