forked from Surrey-EEEM071-CVDL/CourseWork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
320 lines (266 loc) · 9.86 KB
/
main.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
# Copyright (c) EEEM071, University of Surrey
import datetime
import os
import os.path as osp
import sys
import time
import warnings
import numpy as np
import torch
import torch.backends.cudnn as cudnn
import torch.nn as nn
from args import argument_parser, dataset_kwargs, optimizer_kwargs, lr_scheduler_kwargs
from src import models
from src.data_manager import ImageDataManager
from src.eval_metrics import evaluate
from src.losses import CrossEntropyLoss, TripletLoss, DeepSupervision
from src.lr_schedulers import init_lr_scheduler
from src.optimizers import init_optimizer
from src.utils.avgmeter import AverageMeter
from src.utils.generaltools import set_random_seed
from src.utils.iotools import check_isfile
from src.utils.loggers import Logger, RankLogger
from src.utils.torchtools import (
count_num_param,
accuracy,
load_pretrained_weights,
save_checkpoint,
resume_from_checkpoint,
)
from src.utils.visualtools import visualize_ranked_results
# global variables
parser = argument_parser()
args = parser.parse_args()
def main():
global args
set_random_seed(args.seed)
if not args.use_avai_gpus:
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_devices
use_gpu = torch.cuda.is_available()
if args.use_cpu:
use_gpu = False
log_name = "log_test.txt" if args.evaluate else "log_train.txt"
sys.stdout = Logger(osp.join(args.save_dir, log_name))
print(f"==========\nArgs:{args}\n==========")
if use_gpu:
print(f"Currently using GPU {args.gpu_devices}")
cudnn.benchmark = True
else:
warnings.warn("Currently using CPU, however, GPU is highly recommended")
print("Initializing image data manager")
dm = ImageDataManager(use_gpu, **dataset_kwargs(args))
trainloader, testloader_dict = dm.return_dataloaders()
print(f"Initializing model: {args.arch}")
model = models.init_model(
name=args.arch,
num_classes=dm.num_train_pids,
loss={"xent", "htri"},
pretrained=not args.no_pretrained,
use_gpu=use_gpu,
)
print("Model size: {:.3f} M".format(count_num_param(model)))
if args.load_weights and check_isfile(args.load_weights):
load_pretrained_weights(model, args.load_weights)
model = nn.DataParallel(model).cuda() if use_gpu else model
criterion_xent = CrossEntropyLoss(
num_classes=dm.num_train_pids, use_gpu=use_gpu, label_smooth=args.label_smooth
)
criterion_htri = TripletLoss(margin=args.margin)
optimizer = init_optimizer(model, **optimizer_kwargs(args))
scheduler = init_lr_scheduler(optimizer, **lr_scheduler_kwargs(args))
if args.resume and check_isfile(args.resume):
args.start_epoch = resume_from_checkpoint(
args.resume, model, optimizer=optimizer
)
if args.evaluate:
print("Evaluate only")
for name in args.target_names:
print(f"Evaluating {name} ...")
queryloader = testloader_dict[name]["query"]
galleryloader = testloader_dict[name]["gallery"]
distmat = test(
model, queryloader, galleryloader, use_gpu, return_distmat=True
)
if args.visualize_ranks:
visualize_ranked_results(
distmat,
dm.return_testdataset_by_name(name),
save_dir=osp.join(args.save_dir, "ranked_results", name),
topk=20,
)
return
time_start = time.time()
ranklogger = RankLogger(args.source_names, args.target_names)
print("=> Start training")
"""
if args.fixbase_epoch > 0:
print('Train {} for {} epochs while keeping other layers frozen'.format(args.open_layers, args.fixbase_epoch))
initial_optim_state = optimizer.state_dict()
for epoch in range(args.fixbase_epoch):
train(epoch, model, criterion_xent, criterion_htri, optimizer, trainloader, use_gpu, fixbase=True)
print('Done. All layers are open to train for {} epochs'.format(args.max_epoch))
optimizer.load_state_dict(initial_optim_state)
"""
for epoch in range(args.start_epoch, args.max_epoch):
train(
epoch,
model,
criterion_xent,
criterion_htri,
optimizer,
trainloader,
use_gpu,
)
scheduler.step()
if (
(epoch + 1) > args.start_eval
#and args.eval_freq > 0
and (epoch + 1) % args.eval_freq == 0
or (epoch + 1) == args.max_epoch
):
print("=> Test")
for name in args.target_names:
print(f"Evaluating {name} ...")
queryloader = testloader_dict[name]["query"]
galleryloader = testloader_dict[name]["gallery"]
rank1 = test(model, queryloader, galleryloader, use_gpu)
ranklogger.write(name, epoch + 1, rank1)
save_checkpoint(
{
"state_dict": model.state_dict(),
"rank1": rank1,
"epoch": epoch + 1,
"arch": args.arch,
"optimizer": optimizer.state_dict(),
},
args.save_dir,
)
elapsed = round(time.time() - time_start)
elapsed = str(datetime.timedelta(seconds=elapsed))
print(f"Elapsed {elapsed}")
ranklogger.show_summary()
def train(
epoch, model, criterion_xent, criterion_htri, optimizer, trainloader, use_gpu
):
xent_losses = AverageMeter()
htri_losses = AverageMeter()
accs = AverageMeter()
batch_time = AverageMeter()
data_time = AverageMeter()
model.train()
for p in model.parameters():
p.requires_grad = True # open all layers
end = time.time()
for batch_idx, (imgs, pids, _, _) in enumerate(trainloader):
data_time.update(time.time() - end)
if use_gpu:
imgs, pids = imgs.cuda(), pids.cuda()
outputs, features = model(imgs)
if isinstance(outputs, (tuple, list)):
xent_loss = DeepSupervision(criterion_xent, outputs, pids)
else:
xent_loss = criterion_xent(outputs, pids)
if isinstance(features, (tuple, list)):
htri_loss = DeepSupervision(criterion_htri, features, pids)
else:
htri_loss = criterion_htri(features, pids)
loss = args.lambda_xent * xent_loss + args.lambda_htri * htri_loss
optimizer.zero_grad()
loss.backward()
optimizer.step()
batch_time.update(time.time() - end)
xent_losses.update(xent_loss.item(), pids.size(0))
htri_losses.update(htri_loss.item(), pids.size(0))
accs.update(accuracy(outputs, pids)[0])
if (batch_idx + 1) % args.print_freq == 0:
print(
"Epoch: [{0}][{1}/{2}]\t"
"Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t"
"Data {data_time.val:.4f} ({data_time.avg:.4f})\t"
"Xent {xent.val:.4f} ({xent.avg:.4f})\t"
"Htri {htri.val:.4f} ({htri.avg:.4f})\t"
"Acc {acc.val:.2f} ({acc.avg:.2f})\t".format(
epoch + 1,
batch_idx + 1,
len(trainloader),
batch_time=batch_time,
data_time=data_time,
xent=xent_losses,
htri=htri_losses,
acc=accs,
)
)
end = time.time()
def test(
model,
queryloader,
galleryloader,
use_gpu,
ranks=[1, 5, 10, 20],
return_distmat=False,
):
batch_time = AverageMeter()
model.eval()
with torch.no_grad():
qf, q_pids, q_camids = [], [], []
for batch_idx, (imgs, pids, camids, _) in enumerate(queryloader):
if use_gpu:
imgs = imgs.cuda()
end = time.time()
features = model(imgs)
batch_time.update(time.time() - end)
features = features.data.cpu()
qf.append(features)
q_pids.extend(pids)
q_camids.extend(camids)
qf = torch.cat(qf, 0)
q_pids = np.asarray(q_pids)
q_camids = np.asarray(q_camids)
print(
"Extracted features for query set, obtained {}-by-{} matrix".format(
qf.size(0), qf.size(1)
)
)
gf, g_pids, g_camids = [], [], []
for batch_idx, (imgs, pids, camids, _) in enumerate(galleryloader):
if use_gpu:
imgs = imgs.cuda()
end = time.time()
features = model(imgs)
batch_time.update(time.time() - end)
features = features.data.cpu()
gf.append(features)
g_pids.extend(pids)
g_camids.extend(camids)
gf = torch.cat(gf, 0)
g_pids = np.asarray(g_pids)
g_camids = np.asarray(g_camids)
print(
"Extracted features for gallery set, obtained {}-by-{} matrix".format(
gf.size(0), gf.size(1)
)
)
print(
f"=> BatchTime(s)/BatchSize(img): {batch_time.avg:.3f}/{args.test_batch_size}"
)
m, n = qf.size(0), gf.size(0)
distmat = (
torch.pow(qf, 2).sum(dim=1, keepdim=True).expand(m, n)
+ torch.pow(gf, 2).sum(dim=1, keepdim=True).expand(n, m).t()
)
distmat.addmm_(qf, gf.t(), beta=1, alpha=-2)
distmat = distmat.numpy()
print("Computing CMC and mAP")
# cmc, mAP = evaluate(distmat, q_pids, g_pids, q_camids, g_camids, args.target_names)
cmc, mAP = evaluate(distmat, q_pids, g_pids, q_camids, g_camids)
print("Results ----------")
print(f"mAP: {mAP:.1%}")
print("CMC curve")
for r in ranks:
print("Rank-{:<3}: {:.1%}".format(r, cmc[r - 1]))
print("------------------")
if return_distmat:
return distmat
return cmc[0]
if __name__ == "__main__":
main()