-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode_hist.py
195 lines (173 loc) · 9.55 KB
/
code_hist.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
'''
Copyright ? 2020 by Xingbo Dong
Monash University
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'''
from absl import app, flags, logging
from absl.flags import FLAGS
import cv2
import os
import numpy as np
import tensorflow as tf
import modules
import csv
import matplotlib.pyplot as plt
import numpy as np
import collections
from modules.evaluations import get_val_data, perform_val, perform_val_yts
from modules.models import ArcFaceModel, IoMFaceModelFromArFace, IoMFaceModelFromArFaceMLossHead, \
IoMFaceModelFromArFace2, IoMFaceModelFromArFace3, IoMFaceModelFromArFace_T, IoMFaceModelFromArFace_T1
from modules.utils import set_memory_growth, load_yaml, l2_norm
# modules.utils.set_memory_growth()
flags.DEFINE_string('cfg_path', './configs/iom_res50.yaml', 'config file path')
flags.DEFINE_string('ckpt_epoch', '', 'config file path')
flags.DEFINE_string('gpu', '0', 'which gpu to use')
flags.DEFINE_string('img_path', '', 'path to input image')
def main(_argv):
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
os.environ['CUDA_VISIBLE_DEVICES'] = FLAGS.gpu
logger = tf.get_logger()
logger.disabled = True
logger.setLevel(logging.FATAL)
set_memory_growth()
cfg = load_yaml(FLAGS.cfg_path)
permKey = None
if cfg['head_type'] == 'IoMHead': #
# permKey = generatePermKey(cfg['embd_shape'])
permKey = tf.eye(cfg['embd_shape']) # for training, we don't permutate, won't influence the performance
m = cfg['m']
q = cfg['q']
arcmodel = ArcFaceModel(size=cfg['input_size'],
embd_shape=cfg['embd_shape'],
backbone_type=cfg['backbone_type'],
head_type='ArcHead',
training=False,
# here equal false, just get the model without acrHead, to load the model trained by arcface
cfg=cfg)
ckpt_path = tf.train.latest_checkpoint('./checkpoints/arc_res50')
print("[*] load ckpt from {}".format(ckpt_path))
arcmodel.load_weights(ckpt_path)
# if cfg['loss_fun'] == 'margin_loss':
# model = IoMFaceModelFromArFaceMLossHead(size=cfg['input_size'],
# arcmodel=arcmodel, training=False,
# permKey=permKey, cfg=cfg)
# else:
# here I add the extra IoM layer and head
if cfg['hidden_layer_remark'] == '1':
model = IoMFaceModelFromArFace(size=cfg['input_size'],
arcmodel=arcmodel, training=False,
permKey=permKey, cfg=cfg)
elif cfg['hidden_layer_remark'] == '2':
model = IoMFaceModelFromArFace2(size=cfg['input_size'],
arcmodel=arcmodel, training=False,
permKey=permKey, cfg=cfg)
elif cfg['hidden_layer_remark'] == '3':
model = IoMFaceModelFromArFace3(size=cfg['input_size'],
arcmodel=arcmodel, training=False,
permKey=permKey, cfg=cfg)
elif cfg['hidden_layer_remark'] == 'T': # 2 layers
model = IoMFaceModelFromArFace_T(size=cfg['input_size'],
arcmodel=arcmodel, training=False,
permKey=permKey, cfg=cfg)
elif cfg['hidden_layer_remark'] == 'T1':
model = IoMFaceModelFromArFace_T1(size=cfg['input_size'],
arcmodel=arcmodel, training=False,
permKey=permKey, cfg=cfg)
else:
model = IoMFaceModelFromArFace(size=cfg['input_size'],
arcmodel=arcmodel, training=False,
permKey=permKey, cfg=cfg)
cfg['embd_shape'] = m * q
model.summary(line_length=80)
def evl(isLUT, measure, model, logremark):
print("[*] Loading LFW, AgeDB30 and CFP-FP...",logremark)
lfw, agedb_30, cfp_fp, lfw_issame, agedb_30_issame, cfp_fp_issame = \
get_val_data(cfg['test_dataset'])
print("[*] Perform Evaluation on LFW...",logremark)
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=0, measure=measure)
print(" acc {:.4f}, th: {:.2f}, auc {:.4f}, EER {:.4f}".format(acc_lfw, best_th_lfw, auc_lfw, eer_lfw))
x = np.asarray(embeddings_lfw)
x = x.astype(int)
reshaped_array = x.reshape(x.size)
counter = collections.Counter(reshaped_array)
x = counter.keys()
frequency = counter.values()
y = [x / reshaped_array.size for x in frequency]
plt.bar(x, y)
plt.ylabel('Probability')
plt.xlabel('Code value')
# plt.show()
plt.savefig('plots/histogram_'+logremark+'_iom_' + cfg['sub_name'] +'_m'+str(cfg['m'])+'q_'+str(cfg['m']) +'.svg', format='svg')
plt.close('all')
with open('embeddings/' +logremark + cfg['sub_name'] + '_embeddings_lfw.csv', 'w', newline='') as file:
writer = csv.writer(file, escapechar='/', quoting=csv.QUOTE_NONE)
writer.writerows(embeddings_lfw)
#
# acc_lfw, best_th_lfw, auc_lfw, eer_lfw, embeddings_lfw_bin = perform_val(
# cfg['embd_shape'], cfg['eval_batch_size'], model, lfw, lfw_issame,
# is_ccrop=cfg['is_ccrop'], cfg=cfg, isLUT=q, measure=measure)
# print(" Binary acc {:.4f}, th: {:.2f}, auc {:.4f}, EER {:.4f}".format(acc_lfw, best_th_lfw, auc_lfw, eer_lfw))
#
# x = np.asarray(embeddings_lfw_bin)
# x = x.astype(int)
# reshaped_array = x.reshape(x.size)
# counter = collections.Counter(reshaped_array)
# x = counter.keys()
# frequency = counter.values()
# y = [x / reshaped_array.size for x in frequency]
# plt.bar(x, y)
# plt.ylabel('Probability')
# plt.xlabel('Code value')
# plt.savefig('histogram_'+logremark+'_iom_binary_' + cfg['sub_name'] +'.svg', format='svg')
# with open('embeddings/' + cfg['sub_name'] + '_embeddings_bin_lfw.csv', 'w', newline='') as file:
# writer = csv.writer(file, escapechar='/', quoting=csv.QUOTE_NONE)
# writer.writerows(embeddings_lfw_bin)
evl(q, 'Euclidean',model,'random')
if cfg['loss_fun']:
# here I add the extra IoM layer and head
if cfg['hidden_layer_remark'] == '1':
model = IoMFaceModelFromArFace(size=cfg['input_size'],
arcmodel=arcmodel, training=False,
permKey=permKey, cfg=cfg)
elif cfg['hidden_layer_remark'] == '2':
model = IoMFaceModelFromArFace2(size=cfg['input_size'],
arcmodel=arcmodel, training=False,
permKey=permKey, cfg=cfg)
elif cfg['hidden_layer_remark'] == '3':
model = IoMFaceModelFromArFace3(size=cfg['input_size'],
arcmodel=arcmodel, training=False,
permKey=permKey, cfg=cfg)
elif cfg['hidden_layer_remark'] == 'T': # 2 layers
model = IoMFaceModelFromArFace_T(size=cfg['input_size'],
arcmodel=arcmodel, training=False,
permKey=permKey, cfg=cfg)
elif cfg['hidden_layer_remark'] == 'T1':
model = IoMFaceModelFromArFace_T1(size=cfg['input_size'],
arcmodel=arcmodel, training=False,
permKey=permKey, cfg=cfg)
else:
model = IoMFaceModelFromArFace(size=cfg['input_size'],
arcmodel=arcmodel, training=False,
permKey=permKey, cfg=cfg)
cfg['embd_shape'] = m * q
if FLAGS.ckpt_epoch == '':
ckpt_path = tf.train.latest_checkpoint('./checkpoints/' + cfg['sub_name'])
else:
ckpt_path = './checkpoints/' + cfg['sub_name'] + '/' + FLAGS.ckpt_epoch
if ckpt_path is not None:
print("[*] load ckpt from {}".format(ckpt_path))
model.load_weights(ckpt_path)
else:
print("[*] Cannot find ckpt from {}.".format(ckpt_path))
exit()
evl(q, 'Euclidean',model,'after_train')
if __name__ == '__main__':
try:
app.run(main)
except SystemExit:
pass