-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_crowd_counting.py
632 lines (526 loc) · 26.6 KB
/
main_crowd_counting.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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
# --------------------------------------------------------
# Swin Transformer
# Copyright (c) 2021 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Ze Liu
# --------------------------------------------------------
import os
os.environ["CUDA_VISIBLE_DEVICES"]="1"
import time
import json
import random
import argparse
import datetime
import numpy as np
import math
import sys
import torch
import torch.backends.cudnn as cudnn
import torch.distributed as dist
from timm.loss import LabelSmoothingCrossEntropy, SoftTargetCrossEntropy
from timm.utils import accuracy, AverageMeter
from config import get_config
from models import build_model
from data import build_loader
from lr_scheduler import build_scheduler
from optimizer import build_optimizer
from logger import create_logger
from utils import load_checkpoint, load_checkpoint_crowd, load_pretrained_crowd, save_checkpoint, save_checkpoint_crowd, \
save_checkpoint_crowd_best, NativeScalerWithGradNormCount, auto_resume_helper, reduce_tensor, compute_relerr, rsquared
from crowd_datasets import build_dataset
from torch.utils.data import DataLoader
from util.misc import collate_fn_crowd
from models.swin_crowd import SetCriterion_Crowd
from models.matcher import build_matcher_crowd
import util.misc as utils
import cv2
import torchvision.transforms as standard_transforms
def parse_option():
parser = argparse.ArgumentParser('Swin Transformer training and evaluation script', add_help=False)
parser.add_argument('--cfg', type=str, required=True, metavar="FILE", help='path to config file', )
parser.add_argument(
"--opts",
help="Modify config options by adding 'KEY VALUE' pairs. ",
default=None,
nargs='+',
)
# easy config modification
parser.add_argument('--batch-size', type=int, help="batch size for single GPU")
parser.add_argument('--data-path', type=str, help='path to dataset')
parser.add_argument('--zip', action='store_true', help='use zipped dataset instead of folder dataset')
parser.add_argument('--cache-mode', type=str, default='part', choices=['no', 'full', 'part'],
help='no: no cache, '
'full: cache all data, '
'part: sharding the dataset into nonoverlapping pieces and only cache one piece')
parser.add_argument('--pretrained',
help='pretrained weight from checkpoint, could be imagenet22k pretrained weight')
parser.add_argument('--resume', help='resume from checkpoint')
parser.add_argument('--accumulation-steps', type=int, help="gradient accumulation steps")
parser.add_argument('--use-checkpoint', action='store_true',
help="whether to use gradient checkpointing to save memory")
parser.add_argument('--disable_amp', action='store_true', help='Disable pytorch amp')
parser.add_argument('--amp-opt-level', type=str, choices=['O0', 'O1', 'O2'],
help='mixed precision opt level, if O0, no amp is used (deprecated!)')
parser.add_argument('--output', default='output', type=str, metavar='PATH',
help='root of output folder, the full path is <output>/<model_name>/<tag> (default: output)')
parser.add_argument('--tag', help='tag of experiment')
parser.add_argument('--eval', action='store_true', help='Perform evaluation only')
parser.add_argument('--throughput', action='store_true', help='Test throughput only')
# distributed training
parser.add_argument("--local_rank", type=int, required=True, help='local rank for DistributedDataParallel')
# for acceleration
parser.add_argument('--fused_window_process', action='store_true',
help='Fused window shift & window partition, similar for reversed part.')
parser.add_argument('--fused_layernorm', action='store_true', help='Use fused layernorm.')
## overwrite optimizer in config (*.yaml) if specified, e.g., fused_adam/fused_lamb
parser.add_argument('--optim', type=str,
help='overwrite optimizer if provided, can be adamw/sgd/fused_adam/fused_lamb.')
parser.add_argument('--dataset_file', default='SHHA')
parser.add_argument('--num_workers', default=2, type=int)
parser.add_argument('--data-root', default='./new_public_density_data',
help='path where the dataset is')
# * Loss coefficients
parser.add_argument('--point_loss_coef', default=0.0002, type=float)
parser.add_argument('--eos_coef', default=0.5, type=float,
help="Relative classification weight of the no-object class")
# * Matcher
parser.add_argument('--set_cost_class', default=1, type=float,
help="Class coefficient in the matching cost")
parser.add_argument('--set_cost_point', default=0.05, type=float,
help="L1 point coefficient in the matching cost")
parser.add_argument('--clip_max_norm', default=0.1, type=float,
help='gradient clipping max norm')
parser.add_argument('--eval_freq', default=5, type=int,
help='frequency of evaluation, default setting is evaluating in every 5 epoch')
parser.add_argument('--weight_path', default='',
help='path where the trained weights saved')
parser.add_argument('--save_freq', default=5, type=int,
help='frequency of model saving, default setting is evaluating in every 5 epoch')
args, unparsed = parser.parse_known_args()
config = get_config(args)
return args, config
def main(config):
_, _, _, _, mixup_fn = build_loader(config)
logger.info(f"Creating model:{config.MODEL.TYPE}/{config.MODEL.NAME}")
model = build_model(config)
if config.MODEL.TYPE == 'swin_crowd':
num_classes = 1
weight_dict = {'loss_ce': 1, 'loss_points': config.POINT_LOSS_COEF}
losses = ['labels', 'points']
matcher = build_matcher_crowd(config)
criterion = SetCriterion_Crowd(num_classes, \
matcher=matcher, weight_dict=weight_dict, \
eos_coef=config.EOS_COEF, losses=losses)
# print("****#####******Model: ", model)
logger.info(str(model))
n_parameters = sum(p.numel() for p in model.parameters() if p.requires_grad)
logger.info(f"number of params: {n_parameters}")
if hasattr(model, 'flops'):
flops = model.flops()
logger.info(f"number of GFLOPs: {flops / 1e9}")
model.cuda()
model_without_ddp = model
optimizer = build_optimizer(config, model)
# model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[config.LOCAL_RANK], broadcast_buffers=False)
model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[config.LOCAL_RANK], broadcast_buffers=False, find_unused_parameters=True)
loss_scaler = NativeScalerWithGradNormCount()
# create the dataset
loading_data = build_dataset(args=args)
# create the training and valiation set
train_set, val_set = loading_data(args.data_root)
# create the sampler used during training
sampler_train = torch.utils.data.RandomSampler(train_set)
sampler_val = torch.utils.data.SequentialSampler(val_set)
batch_sampler_train = torch.utils.data.BatchSampler(
sampler_train, args.batch_size, drop_last=True)
# the dataloader for training
data_loader_train = DataLoader(train_set, batch_sampler=batch_sampler_train,
collate_fn=collate_fn_crowd, num_workers=args.num_workers)
data_loader_val = DataLoader(val_set, 1, sampler=sampler_val,
drop_last=False, collate_fn=collate_fn_crowd, num_workers=args.num_workers)
if config.TRAIN.ACCUMULATION_STEPS > 1:
lr_scheduler = build_scheduler(config, optimizer, len(data_loader_train) // config.TRAIN.ACCUMULATION_STEPS)
else:
# lr_scheduler = build_scheduler(config, optimizer, len(data_loader_train))
lr_scheduler = build_scheduler(config, optimizer, len(data_loader_val))
min_mae, mae = 1e9, 1e9
if config.TRAIN.AUTO_RESUME:
resume_file = auto_resume_helper(config.OUTPUT)
if resume_file:
if config.MODEL.RESUME:
logger.warning(f"auto-resume changing resume file from {config.MODEL.RESUME} to {resume_file}")
config.defrost()
config.MODEL.RESUME = resume_file
config.freeze()
logger.info(f'auto resuming from {resume_file}')
else:
logger.info(f'no checkpoint found in {config.OUTPUT}, ignoring auto resume')
if config.MODEL.RESUME:
min_mae = load_checkpoint_crowd(config, model_without_ddp, optimizer, lr_scheduler, loss_scaler, logger)
mae, mse, rmae, acc, r2, rmse = evaluate_crowd_no_overlap_eval(model, data_loader_val, args.output)
logger.info(f"MAE:{mae:.2f}, RMSE:{mse:.2f}, Acc:{acc:.2f}, R2:{r2:.4f}, RMAE:{rmae:.2f}, rRMSE:{rmse:.2f} of the network on the {len(data_loader_val)} test images")
if config.EVAL_MODE:
return
if config.MODEL.PRETRAINED and (not config.MODEL.RESUME):
load_pretrained_crowd(config, model_without_ddp, logger)
if config.THROUGHPUT_MODE:
throughput(data_loader_val, model, logger)
return
# save the performance during the training
logger.info("Start training")
start_time = time.time()
for epoch in range(config.TRAIN.START_EPOCH, config.TRAIN.EPOCHS):
# data_loader_train.sampler.set_epoch(epoch)
train_one_epoch(config, model, criterion, data_loader_train, optimizer, epoch, mixup_fn, lr_scheduler,
loss_scaler)
if dist.get_rank() == 0 and (epoch % args.save_freq == 0 or epoch == (config.TRAIN.EPOCHS - 1)):
save_checkpoint_crowd(config, epoch, model_without_ddp, min_mae, optimizer, lr_scheduler, loss_scaler,
logger)
# run evaluation
if epoch % args.eval_freq == 0 and epoch != 0:
mae, mse = evaluate_crowd_no_overlap(model, data_loader_val)
min_mae = min(min_mae, mae)
# print the evaluation results
print('=======================================test=======================================')
logger.info(f'MAE: {mae:.2f}\t'
f'MSE: {mse:.2f}\t'
f'Best MAE: {min_mae:.2f}')
print('=======================================test=======================================')
if abs(min_mae - mae) < 0.01:
save_checkpoint_crowd_best(config, epoch, model_without_ddp, min_mae, optimizer, lr_scheduler, loss_scaler,
logger)
total_time = time.time() - start_time
total_time_str = str(datetime.timedelta(seconds=int(total_time)))
logger.info('Training time {}'.format(total_time_str))
class DeNormalize(object):
def __init__(self, mean, std):
self.mean = mean
self.std = std
def __call__(self, tensor):
for t, m, s in zip(tensor, self.mean, self.std):
t.mul_(s).add_(m)
return tensor
def vis_pred(samples, targets, pred, vis_dir, des=None):
'''
samples -> tensor: [batch, 3, H, W]
targets -> list of dict: [{'points':[], 'image_id': str}]
pred -> list: [num_preds, 2]
'''
gts = [t['point'].tolist() for t in targets]
pil_to_tensor = standard_transforms.ToTensor()
restore_transform = standard_transforms.Compose([
DeNormalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
standard_transforms.ToPILImage()
])
# draw one by one
for idx in range(samples.shape[0]):
sample = restore_transform(samples[idx])
sample = pil_to_tensor(sample.convert('RGB')).numpy() * 255
sample_gt = sample.transpose([1, 2, 0])[:, :, ::-1].astype(np.uint8).copy()
sample_pred = sample.transpose([1, 2, 0])[:, :, ::-1].astype(np.uint8).copy()
max_len = np.max(sample_gt.shape)
size = 2
# draw gt
for t in gts[idx]:
sample_gt = cv2.circle(sample_gt, (int(t[0]), int(t[1])), size, (0, 255, 0), -1)
# draw predictions
for p in pred[idx]:
sample_pred = cv2.circle(sample_pred, (int(p[0]), int(p[1])), size, (0, 0, 255), -1)
name = targets[idx]['image_id']
# save the visualized images
if des is not None:
# cv2.imwrite(os.path.join(vis_dir, '{}_{}_gt_{}_pred_{}_gt.jpg'.format(int(name),
# des, len(gts[idx]), len(pred[idx]))), sample_gt)
cv2.imwrite(os.path.join(vis_dir, '{}_{}_pred_{}.jpg'.format(int(name),
des, len(pred[idx]))), sample_pred)
else:
# cv2.imwrite(
# os.path.join(vis_dir, '{}_gt_{}_pred_{}_gt.jpg'.format(int(name), len(gts[idx]), len(pred[idx]))),
# sample_gt)
cv2.imwrite(
# os.path.join(vis_dir, '{}_pred_{}.jpg'.format(int(name), len(pred[idx]))),
os.path.join(vis_dir, '{}_pred_{}.jpg'.format(name, len(pred[idx]))),
sample_pred)
def vis(samples, targets, pred, vis_dir, des=None):
'''
samples -> tensor: [batch, 3, H, W]
targets -> list of dict: [{'points':[], 'image_id': str}]
pred -> list: [num_preds, 2]
'''
gts = [t['point'].tolist() for t in targets]
pil_to_tensor = standard_transforms.ToTensor()
restore_transform = standard_transforms.Compose([
DeNormalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
standard_transforms.ToPILImage()
])
# draw one by one
for idx in range(samples.shape[0]):
sample = restore_transform(samples[idx])
sample = pil_to_tensor(sample.convert('RGB')).numpy() * 255
sample_gt = sample.transpose([1, 2, 0])[:, :, ::-1].astype(np.uint8).copy()
sample_pred = sample.transpose([1, 2, 0])[:, :, ::-1].astype(np.uint8).copy()
max_len = np.max(sample_gt.shape)
size = 2
# draw gt
for t in gts[idx]:
sample_gt = cv2.circle(sample_gt, (int(t[0]), int(t[1])), size, (0, 255, 0), -1)
# draw predictions
for p in pred[idx]:
sample_pred = cv2.circle(sample_pred, (int(p[0]), int(p[1])), size, (0, 0, 255), -1)
name = targets[idx]['image_id']
# save the visualized images
if des is not None:
# cv2.imwrite(os.path.join(vis_dir, '{}_{}_gt_{}_pred_{}_gt.jpg'.format(int(name),
# des, len(gts[idx]), len(pred[idx]))), sample_gt)
# cv2.imwrite(os.path.join(vis_dir, '{}_{}_gt_{}_pred_{}_pred.jpg'.format(int(name),
# des, len(gts[idx]), len(pred[idx]))), sample_pred)
cv2.imwrite(os.path.join(vis_dir, '{}_{}_gt_{}_pred_{}_gt.jpg'.format(name,
des, len(gts[idx]), len(pred[idx]))), sample_gt)
cv2.imwrite(os.path.join(vis_dir, '{}_{}_gt_{}_pred_{}_pred.jpg'.format(name,
des, len(gts[idx]), len(pred[idx]))), sample_pred)
else:
# cv2.imwrite(
# os.path.join(vis_dir, '{}_gt_{}_pred_{}_gt.jpg'.format(int(name), len(gts[idx]), len(pred[idx]))),
# sample_gt)
# cv2.imwrite(
# os.path.join(vis_dir, '{}_gt_{}_pred_{}_pred.jpg'.format(int(name), len(gts[idx]), len(pred[idx]))),
# sample_pred)
cv2.imwrite(
os.path.join(vis_dir, '{}_gt_{}_pred_{}_gt.jpg'.format(name, len(gts[idx]), len(pred[idx]))),
sample_gt)
cv2.imwrite(
os.path.join(vis_dir, '{}_gt_{}_pred_{}_pred.jpg'.format(name, len(gts[idx]), len(pred[idx]))),
sample_pred)
# def train_one_epoch(config, model, criterion, data_loader, optimizer, epoch, max_norm):
def train_one_epoch(config, model, criterion, data_loader, optimizer, epoch, mixup_fn, lr_scheduler,
loss_scaler):
model.train()
criterion.train()
# metric_logger = utils.MetricLogger(delimiter=" ")
# metric_logger.add_meter('lr', utils.SmoothedValue(window_size=1, fmt='{value:.6f}'))
optimizer.zero_grad()
num_steps = len(data_loader)
batch_time = AverageMeter()
loss_meter = AverageMeter()
norm_meter = AverageMeter()
scaler_meter = AverageMeter()
start = time.time()
end = time.time()
for idx, (samples, targets) in enumerate(data_loader):
samples = samples.cuda()
targets = [{k: v.cuda() for k, v in t.items()} for t in targets]
with torch.cuda.amp.autocast(enabled=config.AMP_ENABLE):
outputs = model(samples)
loss_dict = criterion(outputs, targets)
weight_dict = criterion.weight_dict
# losses = sum(loss_dict[k] * weight_dict[k] for k in loss_dict.keys() if k in weight_dict)
loss = sum(loss_dict[k] * weight_dict[k] for k in loss_dict.keys() if k in weight_dict)
loss = loss / config.TRAIN.ACCUMULATION_STEPS
# this attribute is added by timm on one optimizer (adahessian)
is_second_order = hasattr(optimizer, 'is_second_order') and optimizer.is_second_order
grad_norm = loss_scaler(loss, optimizer, clip_grad=config.TRAIN.CLIP_GRAD,
parameters=model.parameters(), create_graph=is_second_order,
update_grad=(idx + 1) % config.TRAIN.ACCUMULATION_STEPS == 0)
if (idx + 1) % config.TRAIN.ACCUMULATION_STEPS == 0:
optimizer.zero_grad()
lr_scheduler.step_update((epoch * num_steps + idx) // config.TRAIN.ACCUMULATION_STEPS)
loss_scale_value = loss_scaler.state_dict()["scale"]
torch.cuda.synchronize()
# loss_meter.update(loss.item(), targets.size(0))
loss_meter.update(loss.item(), len(targets))
if grad_norm is not None: # loss_scaler return None if not update
norm_meter.update(grad_norm)
scaler_meter.update(loss_scale_value)
batch_time.update(time.time() - end)
end = time.time()
if idx % config.PRINT_FREQ == 0:
lr = optimizer.param_groups[0]['lr']
wd = optimizer.param_groups[0]['weight_decay']
memory_used = torch.cuda.max_memory_allocated() / (1024.0 * 1024.0)
etas = batch_time.avg * (num_steps - idx)
logger.info(
f'Train: [{epoch}/{config.TRAIN.EPOCHS}][{idx}/{num_steps}]\t'
f'eta {datetime.timedelta(seconds=int(etas))} lr {lr:.6f}\t wd {wd:.4f}\t'
f'time {batch_time.val:.4f} ({batch_time.avg:.4f})\t'
f'loss {loss_meter.val:.4f} ({loss_meter.avg:.4f})\t'
f'grad_norm {norm_meter.val:.4f} ({norm_meter.avg:.4f})\t'
f'loss_scale {scaler_meter.val:.4f} ({scaler_meter.avg:.4f})\t'
f'mem {memory_used:.0f}MB')
epoch_time = time.time() - start
logger.info(f"EPOCH {epoch} training takes {datetime.timedelta(seconds=int(epoch_time))}")
# the inference routine
@torch.no_grad()
def evaluate_crowd_no_overlap(model, data_loader, vis_dir=None):
model.eval()
metric_logger = utils.MetricLogger(delimiter=" ")
metric_logger.add_meter('class_error', utils.SmoothedValue(window_size=1, fmt='{value:.2f}'))
# run inference on all images to calc MAE
maes = []
mses = []
for samples, targets in data_loader:
samples = samples.cuda()
outputs = model(samples)
outputs_scores = torch.nn.functional.softmax(outputs['pred_logits'], -1)[:, :, 1][0]
outputs_points = outputs['pred_points'][0]
gt_cnt = targets[0]['point'].shape[0]
# 0.5 is used by default
threshold = 0.5
points = outputs_points[outputs_scores > threshold].detach().cpu().numpy().tolist()
predict_cnt = int((outputs_scores > threshold).sum())
# if specified, save the visualized images
if vis_dir is not None:
vis(samples, targets, [points], vis_dir)
# accumulate MAE, MSE
mae = abs(predict_cnt - gt_cnt)
mse = (predict_cnt - gt_cnt) * (predict_cnt - gt_cnt)
maes.append(float(mae))
mses.append(float(mse))
# calc MAE, MSE
mae = np.mean(maes)
mse = np.sqrt(np.mean(mses))
return mae, mse
# the inference routine
@torch.no_grad()
def evaluate_crowd_no_overlap_eval(model, data_loader, vis_dir=None):
model.eval()
metric_logger = utils.MetricLogger(delimiter=" ")
metric_logger.add_meter('class_error', utils.SmoothedValue(window_size=1, fmt='{value:.2f}'))
# run inference on all images to calc MAE
maes = []
mses = []
pd_counts = []
gt_counts = []
out_class_names_file = os.path.join(vis_dir, "class_names.txt")
f = open(out_class_names_file, "w")
for samples, targets in data_loader:
samples = samples.cuda()
outputs = model(samples)
outputs_scores = torch.nn.functional.softmax(outputs['pred_logits'], -1)[:, :, 1][0]
outputs_points = outputs['pred_points'][0]
gt_cnt = targets[0]['point'].shape[0]
# 0.5 is used by default
threshold = 0.5
points = outputs_points[outputs_scores > threshold].detach().cpu().numpy().tolist()
predict_cnt = int((outputs_scores > threshold).sum())
# if specified, save the visualized images
if vis_dir is not None:
# vis(samples, targets, [points], vis_dir)
vis_pred(samples, targets, [points], vis_dir)
pd_counts.append(predict_cnt)
gt_counts.append(gt_cnt)
# accumulate MAE, MSE
mae = abs(predict_cnt - gt_cnt)
mse = (predict_cnt - gt_cnt) * (predict_cnt - gt_cnt)
maes.append(float(mae))
mses.append(float(mse))
name = targets[0]['image_id']
f.writelines(name + ": " + str(predict_cnt) + "\n")
# calc MAE, MSE
mae = np.mean(maes)
mse = np.sqrt(np.mean(mses))
rmae, rmse = compute_relerr(pd_counts, gt_counts)
r2 = rsquared(pd_counts, gt_counts)
acc = 100 - rmae
return mae, mse, rmae, acc, r2, rmse
@torch.no_grad()
def validate(config, data_loader, model):
criterion = torch.nn.CrossEntropyLoss()
model.eval()
batch_time = AverageMeter()
loss_meter = AverageMeter()
acc1_meter = AverageMeter()
acc5_meter = AverageMeter()
end = time.time()
for idx, (images, target) in enumerate(data_loader):
images = images.cuda(non_blocking=True)
target = target.cuda(non_blocking=True)
# compute output
with torch.cuda.amp.autocast(enabled=config.AMP_ENABLE):
output = model(images)
# measure accuracy and record loss
loss = criterion(output, target)
acc1, acc5 = accuracy(output, target, topk=(1, 5))
acc1 = reduce_tensor(acc1)
acc5 = reduce_tensor(acc5)
loss = reduce_tensor(loss)
loss_meter.update(loss.item(), target.size(0))
acc1_meter.update(acc1.item(), target.size(0))
acc5_meter.update(acc5.item(), target.size(0))
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if idx % config.PRINT_FREQ == 0:
memory_used = torch.cuda.max_memory_allocated() / (1024.0 * 1024.0)
logger.info(
f'Test: [{idx}/{len(data_loader)}]\t'
f'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
f'Loss {loss_meter.val:.4f} ({loss_meter.avg:.4f})\t'
f'Acc@1 {acc1_meter.val:.3f} ({acc1_meter.avg:.3f})\t'
f'Acc@5 {acc5_meter.val:.3f} ({acc5_meter.avg:.3f})\t'
f'Mem {memory_used:.0f}MB')
logger.info(f' * Acc@1 {acc1_meter.avg:.3f} Acc@5 {acc5_meter.avg:.3f}')
return acc1_meter.avg, acc5_meter.avg, loss_meter.avg
@torch.no_grad()
def throughput(data_loader, model, logger):
model.eval()
for idx, (images, _) in enumerate(data_loader):
images = images.cuda(non_blocking=True)
batch_size = images.shape[0]
for i in range(50):
model(images)
torch.cuda.synchronize()
logger.info(f"throughput averaged with 30 times")
tic1 = time.time()
for i in range(30):
model(images)
torch.cuda.synchronize()
tic2 = time.time()
logger.info(f"batch_size {batch_size} throughput {30 * batch_size / (tic2 - tic1)}")
return
if __name__ == '__main__':
args, config = parse_option()
if config.AMP_OPT_LEVEL:
print("[warning] Apex amp has been deprecated, please use pytorch amp instead!")
if 'RANK' in os.environ and 'WORLD_SIZE' in os.environ:
rank = int(os.environ["RANK"])
world_size = int(os.environ['WORLD_SIZE'])
print(f"RANK and WORLD_SIZE in environ: {rank}/{world_size}")
else:
rank = -1
world_size = -1
torch.cuda.set_device(config.LOCAL_RANK)
torch.distributed.init_process_group(backend='nccl', init_method='env://', world_size=world_size, rank=rank)
torch.distributed.barrier()
seed = config.SEED + dist.get_rank()
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
cudnn.benchmark = True
# ### config
# # linear scale the learning rate according to total batch size, may not be optimal
# linear_scaled_lr = config.TRAIN.BASE_LR * config.DATA.BATCH_SIZE * dist.get_world_size() / 512.0
# linear_scaled_warmup_lr = config.TRAIN.WARMUP_LR * config.DATA.BATCH_SIZE * dist.get_world_size() / 512.0
# linear_scaled_min_lr = config.TRAIN.MIN_LR * config.DATA.BATCH_SIZE * dist.get_world_size() / 512.0
# # gradient accumulation also need to scale the learning rate
# if config.TRAIN.ACCUMULATION_STEPS > 1:
# linear_scaled_lr = linear_scaled_lr * config.TRAIN.ACCUMULATION_STEPS
# linear_scaled_warmup_lr = linear_scaled_warmup_lr * config.TRAIN.ACCUMULATION_STEPS
# linear_scaled_min_lr = linear_scaled_min_lr * config.TRAIN.ACCUMULATION_STEPS
# config.defrost()
# config.TRAIN.BASE_LR = linear_scaled_lr
# config.TRAIN.WARMUP_LR = linear_scaled_warmup_lr
# config.TRAIN.MIN_LR = linear_scaled_min_lr
# config.freeze()
# ### config
os.makedirs(config.OUTPUT, exist_ok=True)
logger = create_logger(output_dir=config.OUTPUT, dist_rank=dist.get_rank(), name=f"{config.MODEL.NAME}")
if dist.get_rank() == 0:
path = os.path.join(config.OUTPUT, "config.json")
with open(path, "w") as f:
f.write(config.dump())
logger.info(f"Full config saved to {path}")
# print config
logger.info(config.dump())
logger.info(json.dumps(vars(args)))
main(config)