forked from mangye16/Cross-Modal-Re-ID-baseline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
455 lines (384 loc) · 18.1 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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
from __future__ import print_function
import argparse
import datetime
import sys
import time
import torch
import torch.nn as nn
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 import embed_net
from utils import *
from loss import OriTripletLoss, TripletLoss_WRT
from tensorboardX import SummaryWriter
import datetime
import local_path
start_time = datetime.datetime.now()
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=20, 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('--workers', default=0, 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=8, type=int, metavar='B', help='training batch size')
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='agw', 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')
args = parser.parse_args()
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
set_seed(args.seed)
dataset = args.dataset
if dataset == 'sysu':
# data_path = '../Datasets/SYSU-MM01/ori_data/'
data_path = local_path.my_test_SYSU_MM01
log_path = args.log_path + 'sysu_log/'
test_mode = [1, 2] # thermal to visible
elif dataset == 'regdb':
# data_path = '../Datasets/RegDB/'
data_path = local_path.data_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)
data_time = str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
data_time = data_time.replace(':', '-')
suffix = dataset
if args.method == 'agw':
# suffix = suffix + '_agw_p{}_n{}_lr_{}_seed_{}'.format(args.num_pos, args.batch_size, args.lr, args.seed)
suffix = suffix + ' ' + data_time + '_agw_p{}_n{}_lr_{}_seed_{}'.format(args.num_pos, args.batch_size, args.lr,
args.seed)
else:
# suffix = suffix + '_base_p{}_n{}_lr_{}_seed_{}'.format(args.num_pos, args.batch_size, args.lr, args.seed)
suffix = suffix + ' ' + data_time + '_base_p{}_n{}_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'
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
best_acc = 0 # best test accuracy
start_epoch = 0
print('==> Loading data..')
# Data loading code
# Imagenet数据集的均值和方差为:mean=(0.485, 0.456, 0.406),std=(0.229, 0.224, 0.225),
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
# 把不同的 transform 操作 chain起来
transform_train = transforms.Compose([
transforms.ToPILImage(),
transforms.Pad(10),
# h = 288 w = 144
transforms.RandomCrop((args.img_h, args.img_w)),
transforms.RandomHorizontalFlip(),
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
# 把同一个人放到2个list中: 一个color_pos, 一个thermal_pos
# color_pos: rgb image label position
# thermal_pos: ir image label position
color_pos, thermal_pos = GenIdx(trainset.train_color_label, trainset.train_thermal_label)
# testing set
# test_id.txt 中的id在所有ir镜头下的照片
# query_img: ir image path. images in cam3 and cam6 -> list
# query_label: person id -> ndarray
# query_cam: camera id -> ndarray
query_img, query_label, query_cam = process_query_sysu(data_path, mode=args.mode)
# test_id.txt 中的每个 id 在每个 rgb camera下随机抽取一张照片
# gall_img: rgb image path. -> list
# gall_label: person id -> ndarray
# gall_cam: camera id -> ndarray
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')
transform_test = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize((args.img_h, args.img_w)),
transforms.ToTensor(),
normalize,
])
# TestData: 将测试集中的照片resize为相同分辨率,并转为RGB
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)
'''
dataset='sysu' lr=0.1 optim='sgd' arch='resnet50'
resume='' test-only=False model_path='save_model/' save_epoch=20
log_path='log/' vis_log_path='log/vis_log/' workers=0
img_w=144 img_h=288 batch_size=4 test_batch=64
method='agw' margin=0.3 num_pos=4 trial=1
seed=0 gpu='0' mode='all'
'''
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..')
print('\targs.method:', args.method)
# method='agw'
if args.method == 'base':
print('args.method:', args.method)
net = embed_net(n_class, no_local='off', gm_pool='off', arch=args.arch)
else:
net = embed_net(n_class, no_local='on', gm_pool='on', arch=args.arch)
net.to(device)
# 让内置的 cudnn 的 auto-tuner 自动寻找最适合当前配置的高效算法,来达到优化运行效率的目的
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
criterion_id = nn.CrossEntropyLoss()
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_id.to(device)
criterion_tri.to(device)
if args.optim == 'sgd':
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)
# exp_lr_scheduler = lr_scheduler.StepLR(optimizer, step_size=30, gamma=0.1)
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())
# 新加的
labels = torch.tensor(labels, dtype=torch.long)
data_time.update(time.time() - end)
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
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.item(), 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
gall_feat = np.zeros((ngall, 2048))
gall_feat_att = np.zeros((ngall, 2048))
with torch.no_grad():
for batch_idx, (input, label) in enumerate(gall_loader):
batch_num = input.size(0)
input = Variable(input.cuda())
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, 2048))
query_feat_att = np.zeros((nquery, 2048))
with torch.no_grad():
for batch_idx, (input, label) in enumerate(query_loader):
batch_num = input.size(0)
input = Variable(input.cuda())
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()
# compute the similarity
distmat = np.matmul(query_feat, np.transpose(gall_feat))
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)
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)
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)
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
# training
print('==> Start Training...')
# 设置 epoch 的值
for epoch in range(start_epoch, 81 - 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)
print(trainset.cIndex)
print(trainset.tIndex)
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 > 0 and epoch % 2 == 0:
print('Test Epoch: {}'.format(epoch))
# testing
cmc, mAP, mINP, cmc_att, mAP_att, mINP_att = test(epoch)
# save model
if cmc_att[0] > best_acc: # not the real best for sysu-mm01
best_acc = cmc_att[0]
best_epoch = epoch
state = {
'net': net.state_dict(),
'cmc': cmc_att,
'mAP': mAP_att,
'mINP': mINP_att,
'epoch': epoch,
}
torch.save(state, checkpoint_path + suffix + '_best.t')
# save model
if epoch > 10 and epoch % args.save_epoch == 0:
state = {
'net': net.state_dict(),
'cmc': cmc,
'mAP': mAP,
'epoch': epoch,
}
torch.save(state, checkpoint_path + suffix + '_epoch_{}.t'.format(epoch))
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_att[0], cmc_att[4], cmc_att[9], cmc_att[19], mAP_att, mINP_att))
print('Best Epoch [{}]'.format(best_epoch))
end_time = datetime.datetime.now()
print('total cost time:', end_time - start_time)
print('############### train done! ###############')