def test(): # get device device = get_device(0) # load net num_classes = 80 anchor_size = config.ANCHOR_SIZE_COCO if args.dataset == 'COCO': cfg = config.coco_ab testset = COCODataset( data_dir=args.dataset_root, json_file='instances_val2017.json', name='val2017', img_size=cfg['min_dim'][0], debug=args.debug) mean = config.MEANS elif args.dataset == 'VOC': cfg = config.voc_ab testset = VOCDetection(VOC_ROOT, [('2007', 'test')], None, VOCAnnotationTransform()) mean = config.MEANS if args.version == 'yolo_v2': from models.yolo_v2 import myYOLOv2 net = myYOLOv2(device, input_size=cfg['min_dim'], num_classes=num_classes, trainable=False, anchor_size=anchor_size) print('Let us test yolo-v2 on the MSCOCO dataset ......') elif args.version == 'yolo_v3': from models.yolo_v3 import myYOLOv3 net = myYOLOv3(device, input_size=cfg['min_dim'], num_classes=num_classes, trainable=False, anchor_size=anchor_size) elif args.version == 'tiny_yolo_v2': from models.tiny_yolo_v2 import YOLOv2tiny net = YOLOv2tiny(device, input_size=cfg['min_dim'], num_classes=num_classes, trainable=False, anchor_size=config.ANCHOR_SIZE) elif args.version == 'tiny_yolo_v3': from models.tiny_yolo_v3 import YOLOv3tiny net = YOLOv3tiny(device, input_size=cfg['min_dim'], num_classes=num_classes, trainable=False, anchor_size=config.MULTI_ANCHOR_SIZE) net.load_state_dict(torch.load(args.trained_model, map_location='cuda')) net.to(device).eval() print('Finished loading model!') # evaluation test_net(net, device, testset, BaseTransform(net.input_size, mean), thresh=args.visual_threshold)
def test(): # get device if args.cuda: print('use cuda') cudnn.benchmark = True device = torch.device("cuda") else: device = torch.device("cpu") # load net num_classes = len(VOC_CLASSES) testset = VOCDetection(args.voc_root, [('2007', 'test')], None, VOCAnnotationTransform()) mean = config.MEANS cfg = config.voc_ab if args.version == 'yolo_v2': from models.yolo_v2 import myYOLOv2 net = myYOLOv2(device, input_size=cfg['min_dim'], num_classes=num_classes, anchor_size=config.ANCHOR_SIZE) print('Let us test yolo-v2 on the VOC0712 dataset ......') elif args.version == 'yolo_v3': from models.yolo_v3 import myYOLOv3 net = myYOLOv3(device, input_size=cfg['min_dim'], num_classes=num_classes, anchor_size=config.MULTI_ANCHOR_SIZE) elif args.version == 'tiny_yolo_v2': from models.tiny_yolo_v2 import YOLOv2tiny net = YOLOv2tiny(device, input_size=cfg['min_dim'], num_classes=num_classes, anchor_size=config.ANCHOR_SIZE) print('Let us test tiny-yolo-v2 on the VOC0712 dataset ......') elif args.version == 'tiny_yolo_v3': from models.tiny_yolo_v3 import YOLOv3tiny net = YOLOv3tiny(device, input_size=cfg['min_dim'], num_classes=num_classes, anchor_size=config.MULTI_ANCHOR_SIZE) print('Let us test tiny-yolo-v3 on the VOC0712 dataset ......') net.load_state_dict(torch.load(args.trained_model, map_location=device)) net.to(device).eval() print('Finished loading model!') # evaluation test_net(net, device, testset, BaseTransform(net.input_size, mean=(0.406, 0.456, 0.485), std=(0.225, 0.224, 0.229)), thresh=args.visual_threshold)
def test(): # get device device = get_device(args.gpu_ind) # load net num_classes = len(VOC_CLASSES) testset = VOCDetection(args.voc_root, [('2007', 'test')], None, VOCAnnotationTransform()) mean = config.MEANS cfg = config.voc_ab if args.version == 'yolo_v2': from models.yolo_v2 import myYOLOv2 net = myYOLOv2(device, input_size=cfg['min_dim'], num_classes=num_classes, trainable=False, anchor_size=config.ANCHOR_SIZE) print('Let us test yolo-v2 on the VOC0712 dataset ......') elif args.version == 'yolo_v3': from models.yolo_v3 import myYOLOv3 net = myYOLOv3(device, input_size=cfg['min_dim'], num_classes=num_classes, trainable=False, anchor_size=config.MULTI_ANCHOR_SIZE) elif args.version == 'tiny_yolo_v2': from models.tiny_yolo_v2 import YOLOv2tiny net = YOLOv2tiny(device, input_size=cfg['min_dim'], num_classes=num_classes, trainable=False, anchor_size=config.ANCHOR_SIZE) print('Let us test tiny-yolo-v2 on the VOC0712 dataset ......') elif args.version == 'tiny_yolo_v3': from models.tiny_yolo_v3 import YOLOv3tiny net = YOLOv3tiny(device, input_size=cfg['min_dim'], num_classes=num_classes, trainable=False, anchor_size=config.MULTI_ANCHOR_SIZE) print('Let us test tiny-yolo-v3 on the VOC0712 dataset ......') net.load_state_dict(torch.load(args.trained_model, map_location='cuda')) net.to(device).eval() print('Finished loading model!') # evaluation test_net(net, device, testset, BaseTransform(net.input_size, mean), thresh=args.visual_threshold)
if __name__ == '__main__': num_classes = len(labelmap) device = get_device(args.gpu_ind) cfg = config.voc_ab if args.version == 'yolo_v2': from models.yolo_v2 import myYOLOv2 net = myYOLOv2(device, input_size=cfg['min_dim'], num_classes=num_classes, trainable=False, anchor_size=config.ANCHOR_SIZE) elif args.version == 'yolo_v3': from models.yolo_v3 import myYOLOv3 net = myYOLOv3(device, input_size=cfg['min_dim'], num_classes=num_classes, trainable=False, anchor_size=config.MULTI_ANCHOR_SIZE) elif args.version == 'tiny_yolo_v2': from models.tiny_yolo_v2 import YOLOv2tiny net = YOLOv2tiny(device, input_size=cfg['min_dim'], num_classes=num_classes, trainable=False, anchor_size=config.ANCHOR_SIZE) print('Let us eval tiny-yolo-v2 on the VOC0712 dataset ......') elif args.version == 'tiny_yolo_v3': from models.tiny_yolo_v3 import YOLOv3tiny net = YOLOv3tiny(device, input_size=cfg['min_dim'], num_classes=num_classes, trainable=False, anchor_size=config.MULTI_ANCHOR_SIZE) print('Let us eval tiny-yolo-v3 on the VOC0712 dataset ......') # load net net.load_state_dict(torch.load(args.trained_model, map_location='cuda')) net.eval() print('Finished loading model!') # load data dataset = VOCDetection(args.voc_root, [('2007', set_type)], BaseTransform(net.input_size, dataset_mean), VOCAnnotationTransform())
input_size=cfg['min_dim'], num_classes=args.num_classes, anchor_size=ANCHOR_SIZE_COCO) print('Let us test yolo-v2 on the MSCOCO dataset ......') elif args.version == 'yolo_v3': from models.yolo_v3 import myYOLOv3 model = myYOLOv3(device, input_size=cfg['min_dim'], num_classes=args.num_classes, anchor_size=MULTI_ANCHOR_SIZE_COCO) elif args.version == 'tiny_yolo_v2': from models.tiny_yolo_v2 import YOLOv2tiny model = YOLOv2tiny(device, input_size=cfg['min_dim'], num_classes=args.num_classes, anchor_size=ANCHOR_SIZE_COCO) elif args.version == 'tiny_yolo_v3': from models.tiny_yolo_v3 import YOLOv3tiny model = YOLOv3tiny(device, input_size=cfg['min_dim'], num_classes=args.num_classes, anchor_size=MULTI_ANCHOR_SIZE_COCO) else: print('Unknown Version !!!') exit() # load model
def run(): args = parse_args() if args.cuda: device = torch.device("cuda") else: device = torch.device("cpu") if args.setup == 'VOC': print('use VOC style') cfg = config.voc_ab num_classes = 20 elif args.setup == 'COCO': print('use COCO style') cfg = config.coco_ab num_classes = 80 else: print('Only support VOC and COCO !!!') exit(0) if args.version == 'yolo_v2': from models.yolo_v2 import myYOLOv2 if args.setup == 'VOC': anchor_size = config.ANCHOR_SIZE else: anchor_size = config.ANCHOR_SIZE_COCO net = myYOLOv2(device, input_size=cfg['min_dim'], num_classes=num_classes, trainable=False, anchor_size=anchor_size) elif args.version == 'yolo_v3': from models.yolo_v3 import myYOLOv3 if args.setup == 'VOC': anchor_size = config.MULTI_ANCHOR_SIZE else: anchor_size = config.MULTI_ANCHOR_SIZE_COCO net = myYOLOv3(device, input_size=cfg['min_dim'], num_classes=num_classes, trainable=False, anchor_size=anchor_size) elif args.version == 'tiny_yolo_v2': from models.tiny_yolo_v2 import YOLOv2tiny if args.setup == 'VOC': anchor_size = config.ANCHOR_SIZE else: anchor_size = config.ANCHOR_SIZE_COCO net = YOLOv2tiny(device, input_size=cfg['min_dim'], num_classes=num_classes, trainable=False, anchor_size=anchor_size) elif args.version == 'tiny_yolo_v3': from models.tiny_yolo_v3 import YOLOv3tiny if args.setup == 'VOC': anchor_size = config.MULTI_ANCHOR_SIZE else: anchor_size = config.MULTI_ANCHOR_SIZE_COCO net = YOLOv3tiny(device, input_size=cfg['min_dim'], num_classes=num_classes, trainable=False, anchor_size=anchor_size) net.load_state_dict(torch.load(args.trained_model, map_location='cuda')) net.to(device).eval() print('Finished loading model!') # run if args.mode == 'image': detect(net, device, BaseTransform(net.input_size, mean=(0.406, 0.456, 0.485), std=(0.225, 0.224, 0.229)), mode=args.mode, path_to_img=args.path_to_img, setup=args.setup) elif args.mode == 'video': detect(net, device, BaseTransform(net.input_size, mean=(0.406, 0.456, 0.485), std=(0.225, 0.224, 0.229)), mode=args.mode, path_to_vid=args.path_to_vid, path_to_save=args.path_to_saveVid, setup=args.setup)
if args.version == 'yolo_v2': from models.yolo_v2 import myYOLOv2 total_anchor_size = tools.get_total_anchor_size(name='COCO') yolo_net = myYOLOv2(device, input_size=cfg['min_dim'], num_classes=args.num_classes, trainable=True, anchor_size=total_anchor_size, hr=hr) print('Let us train yolo-v2 on the MSCOCO dataset ......') elif args.version == 'yolo_v3': from models.yolo_v3 import myYOLOv3 total_anchor_size = tools.get_total_anchor_size(multi_scale=True, name='COCO') yolo_net = myYOLOv3(device, input_size=cfg['min_dim'], num_classes=args.num_classes, trainable=True, anchor_size=total_anchor_size, hr=hr) print('Let us train yolo-v3 on the MSCOCO dataset ......') elif args.version == 'tiny_yolo_v2': from models.tiny_yolo_v2 import YOLOv2tiny total_anchor_size = tools.get_total_anchor_size(name='COCO') yolo_net = YOLOv2tiny(device, input_size=cfg['min_dim'], num_classes=args.num_classes, trainable=True, anchor_size=total_anchor_size, hr=hr) print('Let us train tiny-yolo-v2 on the MSCOCO dataset ......') elif args.version == 'tiny_yolo_v3': from models.tiny_yolo_v3 import YOLOv3tiny total_anchor_size = tools.get_total_anchor_size(multi_scale=True, name='COCO') yolo_net = YOLOv3tiny(device, input_size=cfg['min_dim'], num_classes=args.num_classes, trainable=True, anchor_size=total_anchor_size, hr=hr) print('Let us train tiny-yolo-v3 on the MSCOCO dataset ......') train(yolo_net, device)
def train(): args = parse_args() path_to_save = os.path.join(args.save_folder, args.version) os.makedirs(path_to_save, exist_ok=True) hr = False if args.high_resolution: print('use hi-res backbone') hr = True cfg = voc_ab if args.cuda: print('use cuda') cudnn.benchmark = True device = torch.device("cuda") else: device = torch.device("cpu") # use multi-scale trick if args.multi_scale: print('use multi-scale trick.') ms_inds = range(len(cfg['multi_scale'])) input_size = [608, 608] dataset = VOCDetection(root=args.dataset_root, transform=SSDAugmentation([608, 608], mean=(0.406, 0.456, 0.485), std=(0.225, 0.224, 0.229))) else: input_size = cfg['min_dim'] dataset = VOCDetection(root=args.dataset_root, transform=SSDAugmentation(cfg['min_dim'], mean=(0.406, 0.456, 0.485), std=(0.225, 0.224, 0.229))) # build model if args.version == 'yolo_v2': from models.yolo_v2 import myYOLOv2 total_anchor_size = tools.get_total_anchor_size() yolo_net = myYOLOv2(device, input_size=input_size, num_classes=args.num_classes, trainable=True, anchor_size=total_anchor_size, hr=hr) print('Let us train yolo-v2 on the VOC0712 dataset ......') elif args.version == 'yolo_v3': from models.yolo_v3 import myYOLOv3 total_anchor_size = tools.get_total_anchor_size(multi_level=True) yolo_net = myYOLOv3(device, input_size=input_size, num_classes=args.num_classes, trainable=True, anchor_size=total_anchor_size, hr=hr) print('Let us train yolo-v3 on the VOC0712 dataset ......') elif args.version == 'tiny_yolo_v2': from models.tiny_yolo_v2 import YOLOv2tiny total_anchor_size = tools.get_total_anchor_size() yolo_net = YOLOv2tiny(device, input_size=input_size, num_classes=args.num_classes, trainable=True, anchor_size=total_anchor_size, hr=hr) print('Let us train tiny-yolo-v2 on the VOC0712 dataset ......') elif args.version == 'tiny_yolo_v3': from models.tiny_yolo_v3 import YOLOv3tiny total_anchor_size = tools.get_total_anchor_size(multi_level=True) yolo_net = YOLOv3tiny(device, input_size=input_size, num_classes=args.num_classes, trainable=True, anchor_size=total_anchor_size, hr=hr) print('Let us train tiny-yolo-v3 on the VOC0712 dataset ......') else: print('Unknown version !!!') exit() # finetune the model trained on COCO if args.resume is not None: print('finetune COCO trained ') yolo_net.load_state_dict(torch.load(args.resume, map_location=device), strict=False) # use tfboard if args.tfboard: print('use tensorboard') from torch.utils.tensorboard import SummaryWriter c_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) log_path = os.path.join('log/voc/', args.version, c_time) os.makedirs(log_path, exist_ok=True) writer = SummaryWriter(log_path) print( "----------------------------------------Object Detection--------------------------------------------" ) model = yolo_net model.to(device) base_lr = args.lr tmp_lr = base_lr optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=args.momentum, weight_decay=args.weight_decay) # loss counters print("----------------------------------------------------------") print("Let's train OD network !") print('Training on:', dataset.name) print('The dataset size:', len(dataset)) print("----------------------------------------------------------") epoch_size = len(dataset) // args.batch_size max_epoch = cfg['max_epoch'] data_loader = data.DataLoader(dataset, args.batch_size, num_workers=args.num_workers, shuffle=True, collate_fn=detection_collate, pin_memory=True) # create batch iterator t0 = time.time() # start training for epoch in range(max_epoch): # use cos lr if args.cos and epoch > 20 and epoch <= max_epoch - 20: # use cos lr tmp_lr = 0.00001 + 0.5 * (base_lr - 0.00001) * ( 1 + math.cos(math.pi * (epoch - 20) * 1. / (max_epoch - 20))) set_lr(optimizer, tmp_lr) elif args.cos and epoch > max_epoch - 20: tmp_lr = 0.00001 set_lr(optimizer, tmp_lr) # use step lr else: if epoch in cfg['lr_epoch']: tmp_lr = tmp_lr * 0.1 set_lr(optimizer, tmp_lr) for iter_i, (images, targets) in enumerate(data_loader): # WarmUp strategy for learning rate if not args.no_warm_up: if epoch < args.wp_epoch: tmp_lr = base_lr * pow((iter_i + epoch * epoch_size) * 1. / (args.wp_epoch * epoch_size), 4) # tmp_lr = 1e-6 + (base_lr-1e-6) * (iter_i+epoch*epoch_size) / (epoch_size * (args.wp_epoch)) set_lr(optimizer, tmp_lr) elif epoch == args.wp_epoch and iter_i == 0: tmp_lr = base_lr set_lr(optimizer, tmp_lr) targets = [label.tolist() for label in targets] # make train label if args.version == 'yolo_v2' or args.version == 'tiny_yolo_v2': targets = tools.gt_creator(input_size, yolo_net.stride, targets) elif args.version == 'yolo_v3' or args.version == 'tiny_yolo_v3': targets = tools.multi_gt_creator(input_size, yolo_net.stride, targets) # to device images = images.to(device) targets = torch.tensor(targets).float().to(device) # forward and loss conf_loss, cls_loss, txtytwth_loss, total_loss = model( images, target=targets) # backprop and update total_loss.backward() optimizer.step() optimizer.zero_grad() if iter_i % 10 == 0: if args.tfboard: # viz loss writer.add_scalar('object loss', conf_loss.item(), iter_i + epoch * epoch_size) writer.add_scalar('class loss', cls_loss.item(), iter_i + epoch * epoch_size) writer.add_scalar('local loss', txtytwth_loss.item(), iter_i + epoch * epoch_size) t1 = time.time() print( '[Epoch %d/%d][Iter %d/%d][lr %.6f]' '[Loss: obj %.2f || cls %.2f || bbox %.2f || total %.2f || size %d || time: %.2f]' % (epoch + 1, max_epoch, iter_i, epoch_size, tmp_lr, conf_loss.item(), cls_loss.item(), txtytwth_loss.item(), total_loss.item(), input_size[0], t1 - t0), flush=True) t0 = time.time() # multi-scale trick if iter_i % 10 == 0 and iter_i > 0 and args.multi_scale: ms_ind = random.sample(ms_inds, 1)[0] input_size = cfg['multi_scale'][int(ms_ind)] model.set_grid(input_size) # change input dim # But this operation will report bugs when we use more workers in data loader, so I have to use 0 workers. # I don't know how to make it suit more workers, and I'm trying to solve this question. data_loader.dataset.reset_transform( SSDAugmentation(input_size, mean=(0.406, 0.456, 0.485), std=(0.225, 0.224, 0.229))) if (epoch + 1) % 10 == 0: print('Saving state, epoch:', epoch + 1) torch.save( model.state_dict(), os.path.join(path_to_save, args.version + '_' + repr(epoch + 1) + '.pth'))
def test(): # get device if args.cuda: print('use cuda') cudnn.benchmark = True device = torch.device("cuda") else: device = torch.device("cpu") # load net num_classes = 80 if args.dataset == 'COCO': cfg = config.coco_ab testset = COCODataset(data_dir=args.dataset_root, json_file='instances_val2017.json', name='val2017', img_size=cfg['min_dim'][0], debug=args.debug) elif args.dataset == 'VOC': cfg = config.voc_ab testset = VOCDetection(VOC_ROOT, [('2007', 'test')], None, VOCAnnotationTransform()) if args.version == 'yolo_v2': from models.yolo_v2 import myYOLOv2 net = myYOLOv2(device, input_size=cfg['min_dim'], num_classes=num_classes, anchor_size=config.ANCHOR_SIZE_COCO) print('Let us test yolo-v2 on the MSCOCO dataset ......') elif args.version == 'yolo_v3': from models.yolo_v3 import myYOLOv3 net = myYOLOv3(device, input_size=cfg['min_dim'], num_classes=num_classes, anchor_size=config.MULTI_ANCHOR_SIZE_COCO) elif args.version == 'tiny_yolo_v2': from models.tiny_yolo_v2 import YOLOv2tiny net = YOLOv2tiny(device, input_size=cfg['min_dim'], num_classes=num_classes, anchor_size=config.ANCHOR_SIZE_COCO) elif args.version == 'tiny_yolo_v3': from models.tiny_yolo_v3 import YOLOv3tiny net = YOLOv3tiny(device, input_size=cfg['min_dim'], num_classes=num_classes, anchor_size=config.MULTI_ANCHOR_SIZE_COCO) net.load_state_dict(torch.load(args.trained_model, map_location='cuda')) net.to(device).eval() print('Finished loading model!') # evaluation test_net(net, device, testset, BaseTransform(net.input_size, mean=(0.406, 0.456, 0.485), std=(0.225, 0.224, 0.229)), thresh=args.visual_threshold)
def train(): args = parse_args() data_dir = args.dataset_root path_to_save = os.path.join(args.save_folder, args.version) os.makedirs(path_to_save, exist_ok=True) hr = False if args.high_resolution: print('use hi-res backbone') hr = True cfg = coco_ab if args.cuda: print('use cuda') cudnn.benchmark = True device = torch.device("cuda") else: device = torch.device("cpu") if args.multi_scale: print('Let us use the multi-scale trick.') input_size = [608, 608] dataset = COCODataset(data_dir=data_dir, img_size=608, transform=SSDAugmentation([608, 608], mean=(0.406, 0.456, 0.485), std=(0.225, 0.224, 0.229)), debug=args.debug) else: input_size = cfg['min_dim'] dataset = COCODataset(data_dir=data_dir, img_size=cfg['min_dim'][0], transform=SSDAugmentation(cfg['min_dim'], mean=(0.406, 0.456, 0.485), std=(0.225, 0.224, 0.229)), debug=args.debug) # build model if args.version == 'yolo_v2': from models.yolo_v2 import myYOLOv2 total_anchor_size = tools.get_total_anchor_size(name='COCO') yolo_net = myYOLOv2(device, input_size=input_size, num_classes=args.num_classes, trainable=True, anchor_size=total_anchor_size, hr=hr) print('Let us train yolo-v2 on the COCO dataset ......') elif args.version == 'yolo_v3': from models.yolo_v3 import myYOLOv3 total_anchor_size = tools.get_total_anchor_size(multi_level=True, name='COCO') yolo_net = myYOLOv3(device, input_size=input_size, num_classes=args.num_classes, trainable=True, anchor_size=total_anchor_size, hr=hr) print('Let us train yolo-v3 on the COCO dataset ......') elif args.version == 'tiny_yolo_v2': from models.tiny_yolo_v2 import YOLOv2tiny total_anchor_size = tools.get_total_anchor_size(name='COCO') yolo_net = YOLOv2tiny(device, input_size=input_size, num_classes=args.num_classes, trainable=True, anchor_size=total_anchor_size, hr=hr) print('Let us train tiny-yolo-v2 on the COCO dataset ......') elif args.version == 'tiny_yolo_v3': from models.tiny_yolo_v3 import YOLOv3tiny total_anchor_size = tools.get_total_anchor_size(multi_level=True, name='COCO') yolo_net = YOLOv3tiny(device, input_size=input_size, num_classes=args.num_classes, trainable=True, anchor_size=total_anchor_size, hr=hr) print('Let us train tiny-yolo-v3 on the COCO dataset ......') else: print('Unknown version !!!') exit() print("Setting Arguments.. : ", args) print("----------------------------------------------------------") print('Loading the MSCOCO dataset...') print('Training model on:', dataset.name) print('The dataset size:', len(dataset)) print("----------------------------------------------------------") # use tfboard if args.tfboard: print('use tensorboard') from torch.utils.tensorboard import SummaryWriter c_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) log_path = os.path.join('log/coco/', args.version, c_time) os.makedirs(log_path, exist_ok=True) writer = SummaryWriter(log_path) print('Let us train yolo-v2 on the MSCOCO dataset ......') model = yolo_net model.to(device).train() dataloader = torch.utils.data.DataLoader(dataset, batch_size=args.batch_size, shuffle=True, collate_fn=detection_collate, num_workers=args.num_workers) evaluator = COCOAPIEvaluator(data_dir=data_dir, img_size=cfg['min_dim'], device=device, transform=BaseTransform(cfg['min_dim'], mean=(0.406, 0.456, 0.485), std=(0.225, 0.224, 0.229))) # optimizer setup base_lr = args.lr tmp_lr = base_lr optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=args.momentum, weight_decay=args.weight_decay) max_epoch = cfg['max_epoch'] epoch_size = len(dataset) // args.batch_size # start training loop t0 = time.time() for epoch in range(max_epoch): # use cos lr if args.cos and epoch > 20 and epoch <= max_epoch - 20: # use cos lr tmp_lr = 0.00001 + 0.5 * (base_lr - 0.00001) * ( 1 + math.cos(math.pi * (epoch - 20) * 1. / (max_epoch - 20))) set_lr(optimizer, tmp_lr) elif args.cos and epoch > max_epoch - 20: tmp_lr = 0.00001 set_lr(optimizer, tmp_lr) # use step lr else: if epoch in cfg['lr_epoch']: tmp_lr = tmp_lr * 0.1 set_lr(optimizer, tmp_lr) # COCO evaluation if (epoch + 1) % args.eval_epoch == 0: model.trainable = False model.set_grid(cfg['min_dim']) # evaluate ap50_95, ap50 = evaluator.evaluate(model) print('ap50 : ', ap50) print('ap50_95 : ', ap50_95) # convert to training mode. model.trainable = True model.set_grid(input_size) model.train() if args.tfboard: writer.add_scalar('val/COCOAP50', ap50, epoch + 1) writer.add_scalar('val/COCOAP50_95', ap50_95, epoch + 1) for iter_i, (images, targets) in enumerate(dataloader): # WarmUp strategy for learning rate if not args.no_warm_up: if epoch < args.wp_epoch: # tmp_lr = base_lr * pow((iter_i+epoch*epoch_size)*1. / (args.wp_epoch*epoch_size), 4) tmp_lr = 1e-6 + (base_lr - 1e-6) * ( iter_i + epoch * epoch_size) / (epoch_size * (args.wp_epoch)) set_lr(optimizer, tmp_lr) elif epoch == args.wp_epoch and iter_i == 0: tmp_lr = base_lr set_lr(optimizer, tmp_lr) targets = [label.tolist() for label in targets] if args.version == 'yolo_v2' or args.version == 'tiny_yolo_v2': targets = tools.gt_creator(input_size, yolo_net.stride, targets, name='COCO') elif args.version == 'yolo_v3' or args.version == 'tiny_yolo_v3': targets = tools.multi_gt_creator(input_size, yolo_net.stride, targets, name='COCO') # to device images = images.to(device) targets = torch.tensor(targets).float().to(device) # forward and loss conf_loss, cls_loss, txtytwth_loss, total_loss = model( images, target=targets) # backprop total_loss.backward() optimizer.step() optimizer.zero_grad() if args.tfboard: # viz loss writer.add_scalar('object loss', conf_loss.item(), iter_i + epoch * epoch_size) writer.add_scalar('class loss', cls_loss.item(), iter_i + epoch * epoch_size) writer.add_scalar('local loss', txtytwth_loss.item(), iter_i + epoch * epoch_size) if iter_i % 10 == 0: t1 = time.time() print( '[Epoch %d/%d][Iter %d/%d][lr %.6f]' '[Loss: obj %.2f || cls %.2f || bbox %.2f || total %.2f || size %d || time: %.2f]' % (epoch + 1, max_epoch, iter_i, epoch_size, tmp_lr, conf_loss.item(), cls_loss.item(), txtytwth_loss.item(), total_loss.item(), input_size[0], t1 - t0), flush=True) t0 = time.time() # multi-scale trick if iter_i % 10 == 0 and iter_i > 0 and args.multi_scale: # ms_ind = random.sample(ms_inds, 1)[0] # input_size = cfg['multi_scale'][int(ms_ind)] size = random.randint(10, 19) * 32 input_size = [size, size] model.set_grid(input_size) # change input dim # But this operation will report bugs when we use more workers in data loader, so I have to use 0 workers. # I don't know how to make it suit more workers, and I'm trying to solve this question. dataloader.dataset.reset_transform( SSDAugmentation(input_size, mean=(0.406, 0.456, 0.485), std=(0.225, 0.224, 0.229))) if (epoch + 1) % 10 == 0: print('Saving state, epoch:', epoch + 1) torch.save( model.state_dict(), os.path.join(path_to_save, args.version + '_' + repr(epoch + 1) + '.pth'))