-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_mine.py
532 lines (441 loc) · 21.1 KB
/
train_mine.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
from __future__ import print_function
import argparse
import sys
import time
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torch.backends.cudnn as cudnn
from torch.autograd import Variable
import torch.utils.data as data
import torchvision
import torchvision.transforms as transforms
from data_loader import SYSUData, RegDBData, TestData
from data_manager import *
from eval_metrics import eval_sysu, eval_regdb
from model_mine import embed_net
from utils import *
from loss import OriTripletLoss, CenterTripletLoss, CrossEntropyLabelSmooth, TripletLoss_WRT, MMD_Loss, MarginMMD_Loss
from tensorboardX import SummaryWriter
from re_rank import random_walk, k_reciprocal
from random_aug import RandomErasing
import numpy as np
np.set_printoptions(threshold=np.inf)
parser = argparse.ArgumentParser(description='PyTorch Cross-Modality Training')
parser.add_argument('--dataset', default='sysu', help='dataset name: regdb or sysu]')
parser.add_argument('--lr', default=0.1 , type=float, help='learning rate, 0.00035 for adam')
parser.add_argument('--optim', default='sgd', type=str, help='optimizer')
parser.add_argument('--arch', default='resnet50', type=str,
help='network baseline:resnet18 or resnet50')
parser.add_argument('--resume', '-r', default='', type=str,
help='resume from checkpoint')
parser.add_argument('--test-only', action='store_true', help='test only')
parser.add_argument('--model_path', default='save_model/', type=str,
help='model save path')
parser.add_argument('--save_epoch', default=100, type=int,
metavar='s', help='save model every 10 epochs')
parser.add_argument('--log_path', default='log/', type=str,
help='log save path')
parser.add_argument('--vis_log_path', default='log/vis_log/', type=str,
help='log save path')
parser.add_argument('--workers', default=4, type=int, metavar='N',
help='number of data loading workers (default: 4)')
parser.add_argument('--img_w', default=144, type=int,
metavar='imgw', help='img width')
parser.add_argument('--img_h', default=288, type=int,
metavar='imgh', help='img height')
parser.add_argument('--batch-size', default=4, type=int,
metavar='B', help='training batch size')
parser.add_argument('--test-batch', default=64, type=int,
metavar='tb', help='testing batch size')
parser.add_argument('--method', default='base', type=str,
metavar='m', help='method type: base or agw')
parser.add_argument('--margin', default=0.3, type=float,
metavar='margin', help='triplet loss margin')
parser.add_argument('--num_pos', default=4, type=int,
help='num of pos per identity in each modality')
parser.add_argument('--trial', default=1, type=int,
metavar='t', help='trial (only for RegDB dataset)')
parser.add_argument('--seed', default=0, type=int,
metavar='t', help='random seed')
parser.add_argument('--gpu', default='0', type=str,
help='gpu device ids for CUDA_VISIBLE_DEVICES')
parser.add_argument('--mode', default='all', type=str, help='all or indoor')
parser.add_argument('--share_net', default=3, type=int,
metavar='share', help='[1,2,3,4,5]the start number of shared network in the two-stream networks')
parser.add_argument('--re_rank', default='no', type=str, help='performing reranking. [random_walk | k_reciprocal | no]')
parser.add_argument('--pcb', default='off', type=str, help='performing PCB, on or off')
parser.add_argument('--w_center', default=2.0, type=float, help='the weight for center loss')
parser.add_argument('--local_feat_dim', default=256, type=int,
help='feature dimention of each local feature in PCB')
parser.add_argument('--num_strips', default=6, type=int,
help='num of local strips in PCB')
parser.add_argument('--aug', action='store_true', help='Use Random Erasing Augmentation')
parser.add_argument('--label_smooth', default='off', type=str, help='performing label smooth or not')
parser.add_argument('--dist_disc', type=str, help='Include Distribution Discripeancy Loss', default=None)
parser.add_argument('--margin_mmd', default=0, type=float, help='Value of Margin For MMD Loss')
parser.add_argument('--dist_w', default=0.25, type=float, help='Weight of Distribution Discrepancy Loss')
parser.add_argument('--run_name', type=str,
help='Run Name for following experiment', default='test_run')
args = parser.parse_args()
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
set_seed(args.seed)
dataset = args.dataset
if dataset == 'sysu':
data_path = './SYSU-MM01'
log_path = args.log_path + 'sysu_log/'
test_mode = [1, 2] # thermal to visible
elif dataset == 'regdb':
data_path = './RegDB/'
log_path = args.log_path + 'regdb_log/'
test_mode = [2, 1] # visible to thermal
checkpoint_path = args.model_path
if not os.path.isdir(log_path):
os.makedirs(log_path)
if not os.path.isdir(checkpoint_path):
os.makedirs(checkpoint_path)
if not os.path.isdir(args.vis_log_path):
os.makedirs(args.vis_log_path)
suffix = args.run_name + '_' + dataset+'_c_tri_pcb_{}_w_tri_{}'.format(args.pcb,args.w_center)
if args.pcb=='on':
suffix = suffix + '_s{}_f{}'.format(args.num_strips, args.local_feat_dim)
suffix = suffix + '_share_net{}'.format(args.share_net)
if args.method=='agw':
suffix = suffix + '_agw_k{}_p{}_lr_{}_seed_{}'.format(args.num_pos, args.batch_size, args.lr, args.seed)
else:
suffix = suffix + '_base_gm10_k{}_p{}_lr_{}_seed_{}'.format(args.num_pos, args.batch_size, args.lr, args.seed)
if not args.optim == 'sgd':
suffix = suffix + '_' + args.optim
if dataset == 'regdb':
suffix = suffix + '_trial_{}'.format(args.trial)
sys.stdout = Logger(log_path + suffix + '_os.txt')
vis_log_dir = args.vis_log_path + suffix + '/'
if not os.path.isdir(vis_log_dir):
os.makedirs(vis_log_dir)
writer = SummaryWriter(vis_log_dir)
print("==========\nArgs:{}\n==========".format(args))
device = 'cuda' if torch.cuda.is_available() else 'cpu'
best_acc = 0 # best test accuracy
start_epoch = 0
print('==> Loading data..')
# Data loading code
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
if args.aug:
transform_train = transforms.Compose([
transforms.ToPILImage(),
transforms.Pad(10),
transforms.RandomCrop((args.img_h, args.img_w)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
RandomErasing(probability=0.5, mean=[0.485, 0.456, 0.406])
])
else:
transform_train = transforms.Compose([
transforms.ToPILImage(),
transforms.Pad(10),
transforms.RandomCrop((args.img_h, args.img_w)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
])
transform_test = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize((args.img_h, args.img_w)),
transforms.ToTensor(),
normalize,
])
end = time.time()
if dataset == 'sysu':
# training set
trainset = SYSUData(data_path, transform=transform_train)
# generate the idx of each person identity
color_pos, thermal_pos = GenIdx(trainset.train_color_label, trainset.train_thermal_label)
# testing set
query_img, query_label, query_cam = process_query_sysu(data_path, mode=args.mode)
gall_img, gall_label, gall_cam = process_gallery_sysu(data_path, mode=args.mode, trial=0)
elif dataset == 'regdb':
# training set
trainset = RegDBData(data_path, args.trial, transform=transform_train)
# generate the idx of each person identity
color_pos, thermal_pos = GenIdx(trainset.train_color_label, trainset.train_thermal_label)
# testing set
query_img, query_label = process_test_regdb(data_path, trial=args.trial, modal='visible')
gall_img, gall_label = process_test_regdb(data_path, trial=args.trial, modal='thermal')
gallset = TestData(gall_img, gall_label, transform=transform_test, img_size=(args.img_w, args.img_h))
queryset = TestData(query_img, query_label, transform=transform_test, img_size=(args.img_w, args.img_h))
# testing data loader
gall_loader = data.DataLoader(gallset, batch_size=args.test_batch, shuffle=False, num_workers=args.workers)
query_loader = data.DataLoader(queryset, batch_size=args.test_batch, shuffle=False, num_workers=args.workers)
n_class = len(np.unique(trainset.train_color_label))
nquery = len(query_label)
ngall = len(gall_label)
print('Dataset {} statistics:'.format(dataset))
print(' ------------------------------')
print(' subset | # ids | # images')
print(' ------------------------------')
print(' visible | {:5d} | {:8d}'.format(n_class, len(trainset.train_color_label)))
print(' thermal | {:5d} | {:8d}'.format(n_class, len(trainset.train_thermal_label)))
print(' ------------------------------')
print(' query | {:5d} | {:8d}'.format(len(np.unique(query_label)), nquery))
print(' gallery | {:5d} | {:8d}'.format(len(np.unique(gall_label)), ngall))
print(' ------------------------------')
print('Data Loading Time:\t {:.3f}'.format(time.time() - end))
print('==> Building model..')
if args.method =='base':
net = embed_net(n_class, no_local= 'off', gm_pool = 'on', arch=args.arch, share_net=args.share_net, pcb=args.pcb, local_feat_dim=args.local_feat_dim, num_strips=args.num_strips)
else:
net = embed_net(n_class, no_local= 'on', gm_pool = 'on', arch=args.arch, share_net=args.share_net, pcb=args.pcb)
net.to(device)
cudnn.benchmark = True
if len(args.resume) > 0:
model_path = checkpoint_path + args.resume
if os.path.isfile(model_path):
print('==> loading checkpoint {}'.format(args.resume))
checkpoint = torch.load(model_path)
start_epoch = checkpoint['epoch']
net.load_state_dict(checkpoint['net'])
print('==> loaded checkpoint {} (epoch {})'
.format(args.resume, checkpoint['epoch']))
else:
print('==> no checkpoint found at {}'.format(args.resume))
# define loss function
if args.label_smooth == 'off':
criterion_id = nn.CrossEntropyLoss()
else:
criterion_id = CrossEntropyLabelSmooth(n_class)
if args.method == 'agw':
criterion_tri = TripletLoss_WRT()
else:
loader_batch = args.batch_size * args.num_pos
#criterion_tri= OriTripletLoss(batch_size=loader_batch, margin=args.margin)
criterion_tri= CenterTripletLoss(batch_size=loader_batch, margin=args.margin)
criterion_id.to(device)
criterion_tri.to(device)
criterion_mmd = MMD_Loss().to(device)
criterion_margin_mmd = MarginMMD_Loss(margin=args.margin_mmd).to(device)
if args.optim == 'sgd':
if args.pcb == 'on':
ignored_params = list(map(id, net.local_conv_list.parameters())) \
+ list(map(id, net.fc_list.parameters()))
base_params = filter(lambda p: id(p) not in ignored_params, net.parameters())
optimizer = optim.SGD([
{'params': base_params, 'lr': 0.1 * args.lr},
{'params': net.local_conv_list.parameters(), 'lr': args.lr},
{'params': net.fc_list.parameters(), 'lr': args.lr}
],
weight_decay=5e-4, momentum=0.9, nesterov=True)
else:
ignored_params = list(map(id, net.bottleneck.parameters())) \
+ list(map(id, net.classifier.parameters()))
base_params = filter(lambda p: id(p) not in ignored_params, net.parameters())
optimizer = optim.SGD([
{'params': base_params, 'lr': 0.1 * args.lr},
{'params': net.bottleneck.parameters(), 'lr': args.lr},
{'params': net.classifier.parameters(), 'lr': args.lr}],
weight_decay=5e-4, momentum=0.9, nesterov=True)
def adjust_learning_rate(optimizer, epoch):
"""Sets the learning rate to the initial LR decayed by 10 every 30 epochs"""
if epoch < 10:
lr = args.lr * (epoch + 1) / 10
elif epoch >= 10 and epoch < 20:
lr = args.lr
elif epoch >= 20 and epoch < 50:
lr = args.lr * 0.1
elif epoch >= 50:
lr = args.lr * 0.01
optimizer.param_groups[0]['lr'] = 0.1 * lr
for i in range(len(optimizer.param_groups) - 1):
optimizer.param_groups[i + 1]['lr'] = lr
return lr
def train(epoch):
current_lr = adjust_learning_rate(optimizer, epoch)
train_loss = AverageMeter()
id_loss = AverageMeter()
tri_loss = AverageMeter()
data_time = AverageMeter()
batch_time = AverageMeter()
correct = 0
total = 0
# switch to train mode
net.train()
end = time.time()
for batch_idx, (input1, input2, label1, label2) in enumerate(trainloader):
labels = torch.cat((label1, label2), 0)
input1 = Variable(input1.cuda())
input2 = Variable(input2.cuda())
labels = Variable(labels.cuda())
data_time.update(time.time() - end)
if args.pcb == 'on':
feat, out0, feat_all = net(input1, input2)
loss_id = criterion_id(out0[0], labels)
loss_tri_l, batch_acc = criterion_tri(feat[0], labels)
for i in range(len(feat)-1):
loss_id += criterion_id(out0[i+1], labels)
loss_tri_l += criterion_tri(feat[i+1], labels)[0]
loss_tri, batch_acc = criterion_tri(feat_all, labels)
loss_tri += loss_tri_l * args.w_center #
correct += batch_acc
loss = loss_id + loss_tri
else:
feat, out0 = net(input1, input2)
loss_id = criterion_id(out0, labels)
loss_tri, batch_acc = criterion_tri(feat, labels)
correct += (batch_acc / 2)
_, predicted = out0.max(1)
correct += (predicted.eq(labels).sum().item() / 2)
loss = loss_id + loss_tri * args.w_center #
if args.dist_disc == 'mmd':
## Apply Global MMD Loss on Pooling Layer
feat_rgb, feat_ir = torch.split(feat, [label1.size(0),label2.size(0)], dim=0)
loss_dist, l2max, expec = criterion_mmd(feat_rgb, feat_ir) ## Use Global MMD
elif args.dist_disc == 'margin_mmd':
## Apply Margin MMD-ID Loss on Pooling Layer
feat_rgb, feat_ir = torch.split(feat, [label1.size(0),label2.size(0)], dim=0)
loss_dist, l2max, expec = criterion_margin_mmd(feat_rgb, feat_ir) ## Use MMD-ID
if args.dist_disc is not None:
loss = loss + loss_dist * args.dist_w ## Add Discrepancy Loss
optimizer.zero_grad()
loss.backward()
optimizer.step()
# update P
train_loss.update(loss.item(), 2 * input1.size(0))
id_loss.update(loss_id.item(), 2 * input1.size(0))
tri_loss.update(loss_tri, 2 * input1.size(0))
total += labels.size(0)
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if batch_idx % 50 == 0:
print('Epoch: [{}][{}/{}] '
'Time: {batch_time.val:.3f} ({batch_time.avg:.3f}) '
'lr:{:.3f} '
'Loss: {train_loss.val:.4f} ({train_loss.avg:.4f}) '
'iLoss: {id_loss.val:.4f} ({id_loss.avg:.4f}) '
'TLoss: {tri_loss.val:.4f} ({tri_loss.avg:.4f}) '
'Accu: {:.2f}'.format(
epoch, batch_idx, len(trainloader), current_lr,
100. * correct / total, batch_time=batch_time,
train_loss=train_loss, id_loss=id_loss,tri_loss=tri_loss))
writer.add_scalar('total_loss', train_loss.avg, epoch)
writer.add_scalar('id_loss', id_loss.avg, epoch)
writer.add_scalar('tri_loss', tri_loss.avg, epoch)
writer.add_scalar('lr', current_lr, epoch)
def test(epoch):
# switch to evaluation mode
net.eval()
print('Extracting Gallery Feature...')
start = time.time()
ptr = 0
if args.pcb == 'on':
feat_dim = args.num_strips * args.local_feat_dim
else:
feat_dim = 2048
gall_feat = np.zeros((ngall, feat_dim))
gall_feat_att = np.zeros((ngall, feat_dim))
with torch.no_grad():
for batch_idx, (input, label) in enumerate(gall_loader):
batch_num = input.size(0)
input = Variable(input.cuda())
if args.pcb == 'on':
feat = net(input, input, test_mode[0])
gall_feat[ptr:ptr + batch_num, :] = feat.detach().cpu().numpy()
else:
feat, feat_att = net(input, input, test_mode[0])
gall_feat[ptr:ptr + batch_num, :] = feat.detach().cpu().numpy()
gall_feat_att[ptr:ptr + batch_num, :] = feat_att.detach().cpu().numpy()
ptr = ptr + batch_num
print('Extracting Time:\t {:.3f}'.format(time.time() - start))
# switch to evaluation
net.eval()
print('Extracting Query Feature...')
start = time.time()
ptr = 0
query_feat = np.zeros((nquery, feat_dim))
query_feat_att = np.zeros((nquery, feat_dim))
with torch.no_grad():
for batch_idx, (input, label) in enumerate(query_loader):
batch_num = input.size(0)
input = Variable(input.cuda())
if args.pcb == 'on':
feat = net(input, input, test_mode[1])
query_feat[ptr:ptr + batch_num, :] = feat.detach().cpu().numpy()
else:
feat, feat_att = net(input, input, test_mode[1])
query_feat[ptr:ptr + batch_num, :] = feat.detach().cpu().numpy()
query_feat_att[ptr:ptr + batch_num, :] = feat_att.detach().cpu().numpy()
ptr = ptr + batch_num
print('Extracting Time:\t {:.3f}'.format(time.time() - start))
start = time.time()
if args.re_rank == 'random_walk':
distmat = random_walk(query_feat, gall_feat)
if args.pcb == 'off': distmat_att = random_walk(query_feat_att, gall_feat_att)
elif args.re_rank == 'k_reciprocal':
distmat = k_reciprocal(query_feat, gall_feat)
if args.pcb == 'off': distmat_att = k_reciprocal(query_feat_att, gall_feat_att)
elif args.re_rank == 'no':
# compute the similarity
distmat = -np.matmul(query_feat, np.transpose(gall_feat))
if args.pcb == 'off': distmat_att = -np.matmul(query_feat_att, np.transpose(gall_feat_att))
# evaluation
if dataset == 'regdb':
cmc, mAP, mINP = eval_regdb(distmat, query_label, gall_label)
if args.pcb == 'off': cmc_att, mAP_att, mINP_att = eval_regdb(distmat_att, query_label, gall_label)
elif dataset == 'sysu':
cmc, mAP, mINP = eval_sysu(distmat, query_label, gall_label, query_cam, gall_cam)
if args.pcb == 'off': cmc_att, mAP_att, mINP_att = eval_sysu(distmat_att, query_label, gall_label, query_cam, gall_cam)
print('Evaluation Time:\t {:.3f}'.format(time.time() - start))
writer.add_scalar('rank1', cmc[0], epoch)
writer.add_scalar('mAP', mAP, epoch)
writer.add_scalar('mINP', mINP, epoch)
if args.pcb == 'off':
writer.add_scalar('rank1_att', cmc_att[0], epoch)
writer.add_scalar('mAP_att', mAP_att, epoch)
writer.add_scalar('mINP_att', mINP_att, epoch)
return cmc, mAP, mINP, cmc_att, mAP_att, mINP_att
else:
return cmc, mAP, mINP
# training
print('==> Start Training...')
for epoch in range(start_epoch, 61 - start_epoch):
print('==> Preparing Data Loader...')
# identity sampler
sampler = IdentitySampler(trainset.train_color_label, \
trainset.train_thermal_label, color_pos, thermal_pos, args.num_pos, args.batch_size,
epoch)
trainset.cIndex = sampler.index1 # color index
trainset.tIndex = sampler.index2 # thermal index
print(epoch)
loader_batch = args.batch_size * args.num_pos
trainloader = data.DataLoader(trainset, batch_size=loader_batch, \
sampler=sampler, num_workers=args.workers, drop_last=True)
# training
train(epoch)
if epoch > 9 and epoch % 2 == 0:
print('Test Epoch: {}'.format(epoch))
# testing
if args.pcb == 'off':
cmc, mAP, mINP, cmc_fc, mAP_fc, mINP_fc = test(epoch)
else:
cmc_fc, mAP_fc, mINP_fc = test(epoch)
# save model
if cmc_fc[0] > best_acc: # not the real best for sysu-mm01
best_acc = cmc_fc[0]
best_epoch = epoch
best_mAP = mAP_fc
best_mINP = mINP_fc
state = {
'net': net.state_dict(),
'cmc': cmc_fc,
'mAP': mAP_fc,
'mINP': mINP_fc,
'epoch': epoch,
}
torch.save(state, checkpoint_path + suffix + '_best.t')
if args.pcb == 'off':
print('POOL: Rank-1: {:.2%} | Rank-5: {:.2%} | Rank-10: {:.2%}| Rank-20: {:.2%}| mAP: {:.2%}| mINP: {:.2%}'.format(
cmc[0], cmc[4], cmc[9], cmc[19], mAP, mINP))
print('FC: Rank-1: {:.2%} | Rank-5: {:.2%} | Rank-10: {:.2%}| Rank-20: {:.2%}| mAP: {:.2%}| mINP: {:.2%}'.format(
cmc_fc[0], cmc_fc[4], cmc_fc[9], cmc_fc[19], mAP_fc, mINP_fc))
print('Best Epoch [{}], Rank-1: {:.2%} | mAP: {:.2%}| mINP: {:.2%}'.format(best_epoch, best_acc, best_mAP, best_mINP))