-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
432 lines (318 loc) · 13.4 KB
/
train.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import os
# os.environ["KMP_DUPLICATE_LIB_OK"]= "TRUE"
import argparse
import time
import sys
import torch
from torch.utils.data import Dataset, DataLoader
import torch.nn as nn
import torch.optim as optim
import matplotlib.pyplot as plt
plt.switch_backend('agg')
import pickle
from torch.optim import lr_scheduler
# import pickle
import cv2 as cv
from loss import SupConLoss
from models import MyDataset
from models.ccnet import ccnet
from utils import *
import copy
def test(model):
print('Start Testing!')
print('%s' % (time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime())))
path_hard = os.path.join(path_rst, 'rank1_hard')
# train_set_file = './data/train_IITD.txt'
# test_set_file = './data/test_IITD.txt'
trainset = MyDataset(txt=train_set_file, transforms=None, train=False)
testset = MyDataset(txt=test_set_file, transforms=None, train=False)
# batch_size = 128
data_loader_train = DataLoader(dataset=trainset, batch_size=batch_size, num_workers=2)
data_loader_test = DataLoader(dataset=testset, batch_size=batch_size, num_workers=2)
fileDB_train = getFileNames(train_set_file)
fileDB_test = getFileNames(test_set_file)
# output dir
if not os.path.exists(path_rst):
os.makedirs(path_rst)
if not os.path.exists(path_hard):
os.makedirs(path_hard)
net = model
net.cuda()
net.eval()
# feature extraction:
featDB_train = []
iddb_train = []
for batch_id, (datas, target) in enumerate(data_loader_train):
data = datas[0]
data = data.cuda()
target = target.cuda()
codes = net.getFeatureCode(data)
codes = codes.cpu().detach().numpy()
y = target.cpu().detach().numpy()
if batch_id == 0:
featDB_train = codes
iddb_train = y
else:
featDB_train = np.concatenate((featDB_train, codes), axis=0)
iddb_train = np.concatenate((iddb_train, y))
print('completed feature extraction for training set.')
print('featDB_train.shape: ', featDB_train.shape)
classNumel = len(set(iddb_train))
num_training_samples = featDB_train.shape[0]
trainNum = num_training_samples // classNumel
print('[classNumel, imgs/class]: ', classNumel, trainNum)
print('\n')
featDB_test = []
iddb_test = []
print('Start Test Feature Extraction.')
for batch_id, (datas, target) in enumerate(data_loader_test):
data = datas[0]
data = data.cuda()
target = target.cuda()
codes = net.getFeatureCode(data)
codes = codes.cpu().detach().numpy()
y = target.cpu().detach().numpy()
if batch_id == 0:
featDB_test = codes
iddb_test = y
else:
featDB_test = np.concatenate((featDB_test, codes), axis=0)
iddb_test = np.concatenate((iddb_test, y))
if batch_id != 1:
print('aaaa')
print('completed feature extraction.')
print('featDB_test.shape: ', featDB_test.shape)
print('\nFeature Extraction Done!')
print('start feature matching ...\n')
print('Verification EER of the test-test set ...')
print('Start EER for Test-Test Set! \n')
# verification EER of the test set
s = [] # matching score
l = [] # intra-class or inter-class matching
ntest = featDB_test.shape[0]
ntrain = featDB_train.shape[0]
for i in range(ntest):
feat1 = featDB_test[i]
for j in range(ntrain):
feat2 = featDB_train[j]
cosdis = np.dot(feat1, feat2)
dis = np.arccos(np.clip(cosdis, -1, 1)) / np.pi
s.append(dis)
if iddb_test[i] == iddb_train[j]: # same palm
l.append(1)
else:
l.append(-1)
if not os.path.exists(path_rst+'veriEER'):
os.makedirs(path_rst+'veriEER')
if not os.path.exists(path_rst+'veriEER/rank1_hard/'):
os.makedirs(path_rst+'veriEER/rank1_hard/')
with open(path_rst+'veriEER/scores_VeriEER.txt', 'w') as f:
for i in range(len(s)):
score = str(s[i])
label = str(l[i])
f.write(score + ' ' + label + '\n')
sys.stdout.flush()
os.system('python ./getGI.py' + ' ' + path_rst + 'veriEER/scores_VeriEER.txt scores_VeriEER')
os.system('python ./getEER.py' + ' ' + path_rst + 'veriEER/scores_VeriEER.txt scores_VeriEER')
print('\n------------------')
print('Rank-1 acc of the test set...')
# rank-1 acc
cnt = 0
corr = 0
for i in range(ntest):
probeID = iddb_test[i]
dis = np.zeros((ntrain, 1))
for j in range(ntrain):
dis[j] = s[cnt]
cnt += 1
idx = np.argmin(dis[:])
galleryID = iddb_train[idx]
if probeID == galleryID:
corr += 1
else:
testname = fileDB_test[i]
trainname = fileDB_train[idx]
# store similar inter-class samples
im_test = cv.imread(testname)
im_train = cv.imread(trainname)
img = np.concatenate((im_test, im_train), axis=1)
cv.imwrite(path_rst + 'veriEER/rank1_hard/%6.4f_%s_%s.png' % (
np.min(dis[:]), testname[-13:-4], trainname[-13:-4]), img)
rankacc = corr / ntest * 100
print('rank-1 acc: %.3f%%' % rankacc)
print('-----------')
with open(path_rst + 'veriEER/rank1.txt', 'w') as f:
f.write('rank-1 acc: %.3f%%' % rankacc)
print('\n\nReal EER of the test set...')
# dataset EER of the test set (the gallery set is not used)
s = [] # matching score
l = [] # genuine / impostor matching
n = featDB_test.shape[0]
for i in range(n - 1):
feat1 = featDB_test[i]
for jj in range(n - i - 1):
j = i + jj + 1
feat2 = featDB_test[j]
cosdis = np.dot(feat1, feat2)
dis = np.arccos(np.clip(cosdis, -1, 1)) / np.pi
s.append(dis)
if iddb_test[i] == iddb_test[j]:
l.append(1)
else:
l.append(-1)
print('feature extraction about real EER done!\n')
with open(path_rst + 'veriEER/scores_EER_test.txt', 'w') as f:
for i in range(len(s)):
score = str(s[i])
label = str(l[i])
f.write(score + ' ' + label + '\n')
sys.stdout.flush()
os.system('python ./getGI.py' + ' ' + path_rst + 'veriEER/scores_EER_test.txt scores_EER_test')
os.system('python ./getEER.py' + ' ' + path_rst + 'veriEER/scores_EER_test.txt scores_EER_test')
# perform one epoch
def fit(epoch, model, data_loader, phase='training'):
if phase != 'training' and phase != 'testing':
raise TypeError('input error!')
if phase == 'training':
model.train()
if phase == 'testing':
# print('test')
model.eval()
running_loss = 0
running_correct = 0
for batch_id, (datas, target) in enumerate(data_loader):
data = datas[0]
data = data.cuda()
data_con = datas[1]
data_con = data_con.cuda()
target = target.cuda()
if phase == 'training':
optimizer.zero_grad()
output, fe1 = model(data, target)
output2, fe2 = model(data_con, target)
fe = torch.cat([fe1.unsqueeze(1), fe2.unsqueeze(1)], dim=1)
else:
with torch.no_grad():
output, fe1 = model(data, None)
output2, fe2 = model(data_con, None)
fe = torch.cat([fe1.unsqueeze(1), fe2.unsqueeze(1)], dim=1)
ce = criterion(output, target)
ce2 = con_criterion(fe, target)
loss = weight1*ce+weight2*ce2
## log
running_loss += loss.data.cpu().numpy()
preds = output.data.max(dim=1, keepdim=True)[1] # max returns (value, index)
running_correct += preds.eq(target.data.view_as(preds)).cpu().sum().numpy()
## update
if phase == 'training':
loss.backward(retain_graph=None) #
optimizer.step()
## log info of this epoch
total = len(data_loader.dataset)
loss = running_loss / total
accuracy = (100.0 * running_correct) / total
if epoch % 1 == 0:
print('epoch %d: \t%s loss is \t%7.5f ;\t%s accuracy is \t%d/%d \t%7.3f%%' % (
epoch, phase, loss, phase, running_correct, total, accuracy))
return loss, accuracy
if __name__== "__main__" :
parser = argparse.ArgumentParser(
description="CO3Net for Palmprint Recfognition"
)
parser.add_argument("--batch_size", type=int, default=64)
parser.add_argument("--epoch_num", type=int, default=50)
parser.add_argument("--temp", type=float, default=0.07)
parser.add_argument("--weight1", type=float, default=0.8)
parser.add_argument("--weight2", type=float, default=0.2)
parser.add_argument("--com_weight",type=float,default=0.8)
parser.add_argument("--id_num", type=int, default=324,
help="IITD: 460 KTU: 145 Tongji: 600 REST: 358 XJTU: 200 POLYU 378 Multi-Spec 500 IITD_Right 230 Tongji_LR 300")
parser.add_argument("--gpu_id", type=str, default='0')
parser.add_argument("--lr", type=float, default=0.0005)
parser.add_argument("--redstep", type=int, default=200)
parser.add_argument("--test_interval", type=str, default= 25)
parser.add_argument("--save_interval", type=str, default = 10) ## 200 for Multi-spec 500 for RED
##Training Path
parser.add_argument("--train_set_file", type=str, default='./data/train_Tongji.txt')
parser.add_argument("--test_set_file", type=str, default='./data/test_Tongji.txt')
parser.add_argument("--pretrained_model", type=str, default='./results/checkpoint/net_params_best.pth', help="Path to the pre-trained .pth file")
##Store Path
parser.add_argument("--des_path", type=str, default='./results/checkpoint/')
parser.add_argument("--path_rst", type=str, default='./results/rst_test/')
args = parser.parse_args()
# print(args.gpu_id)
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_id
batch_size = args.batch_size
epoch_num = args.epoch_num
num_classes = args.id_num
weight1 = args.weight1
weight2 = args.weight2
comp_weight = args.com_weight
print('weight of cross:', weight1)
print('weight of contra:', weight2)
print('weight of competition:',comp_weight)
print('tempture:', args.temp)
des_path = args.des_path
path_rst = args.path_rst
if not os.path.exists(des_path):
os.makedirs(des_path)
if not os.path.exists(path_rst):
os.makedirs(path_rst)
# path
train_set_file = args.train_set_file
test_set_file = args.test_set_file
batch_size = args.batch_size
# dataset
trainset = MyDataset(txt=train_set_file, transforms=None, train=True, imside=128, outchannels=1)
testset = MyDataset(txt=test_set_file, transforms=None, train=False, imside=128, outchannels=1)
data_loader_train = DataLoader(dataset=trainset, batch_size=batch_size, num_workers=12, shuffle=True)
data_loader_test = DataLoader(dataset=testset, batch_size=batch_size, num_workers=12, shuffle=True)
print('%s' % (time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime())))
print('------Init Model------')
net = ccnet(num_classes=num_classes,weight=comp_weight)
best_net = ccnet(num_classes=num_classes,weight=comp_weight)
if args.pretrained_model and os.path.exists(args.pretrained_model):
print(f"Loading pre-trained model from {args.pretrained_model}...")
net.load_state_dict(torch.load(args.pretrained_model,weights_only=False))
best_net.load_state_dict(torch.load(args.pretrained_model,weights_only=False))
else:
print("No pre-trained model specified or file not found. Training from scratch.")
net.cuda()
#
criterion = nn.CrossEntropyLoss()
con_criterion = SupConLoss(temperature=args.temp, base_temperature=args.temp) ######agfzgfda
optimizer = optim.Adam(net.parameters(), lr=args.lr)
scheduler = lr_scheduler.StepLR(optimizer, step_size=args.redstep, gamma=0.8)
train_losses, train_accuracy = [], []
val_losses, val_accuracy = [], []
bestacc = 0
for epoch in range(epoch_num):
epoch_loss, epoch_accuracy = fit(epoch, net, data_loader_train, phase='training')
# print('finish')
val_epoch_loss, val_epoch_accuracy = fit(epoch, net, data_loader_train, phase='testing')
scheduler.step()
# ------------------------logs----------------------
train_losses.append(epoch_loss)
train_accuracy.append(epoch_accuracy)
val_losses.append(val_epoch_loss)
val_accuracy.append(val_epoch_accuracy)
# save the best model
if epoch_accuracy >= bestacc:
bestacc = epoch_accuracy
torch.save(net.state_dict(), des_path + 'net_params_best.pth')
best_net = copy.deepcopy(net)
# save the current model and log info:
if epoch % 10 == 0 or epoch == (epoch_num - 1) and epoch != 0:
torch.save(net.state_dict(), des_path + 'net_params.pth')
saveLossACC(train_losses, val_losses, train_accuracy, val_accuracy, bestacc,path_rst)
if epoch % args.save_interval == 0:
torch.save(net.state_dict(), des_path + 'epoch_' + str(epoch) + '_net_params.pth')
if epoch % args.test_interval == 0 and epoch != 0:
print('------------\n')
test(net)
print('------------\n')
print('Last')
test(net)
print('------------\n')
print('Best')
test(best_net)