MobileNetV2-SSDLite代码分析-5 train

2020-07-22  本文已影响0人  祁晏晏

Github库地址:pytorch-ssd/train_ssd.py

创建网络

create_net = lambda num: create_mobilenetv2_ssd_lite(num, width_mult = args.mb2_width_mult) #这个写法有点意思哈。相当于create_net是一个能接受参数的函数!基于create_mobilenetv2_ssd_lite的函数,这个写法第一次见还纠结了一下,看完觉得很棒
config = mobilenetv1_ssd_config

net = create_net(num_classes)

设定transform

train_transform = TrainAugmentation(config.image_size, config.image_mean, config.image_std) #测试图像做的变换
target_transform = MatchPrior(config.priors, config.center_variance,
config.size_variance, 0.5) #对anchor做的变换(分配到具体网格中)
test_transform = TestTransform(config.image_size, config.image_mean, config.image_std) #对测试图像做的变换

获得数据集

dataset = VOCDataset(dataset_path, transform=train_transform,
                                 target_transform=target_transform)
datasets.append(dataset)
train_dataset = ConcatDataset(datasets) # 因为可能有多个数据集,作者写了一个拼接
train_loader = DataLoader(train_dataset, args.batch_size,
                              num_workers=args.num_workers,
                              shuffle=True)

设置freeze

作者的逻辑很棒。它将mobilenet+SSD各个部分拆开来,使得后续的处理也方便了很多。比如这里想要freeze,直接设置net.base_net即可

freeze_net_layers(net.base_net)

def freeze_net_layers(net):
    for param in net.parameters():
        param.requires_grad = False

网络初始化

if args.resume:
    net.load(args.resume)
elif args.base_net:
    net.init_from_base_net(args.base_net)
elif args.pretrained_ssd:
    net.init_from_pretrained_ssd(args.pretrained_ssd)

可选择是继续上一次的模型,还是仅训练base_net还是加载预训练好的模型

损失和优化函数

criterion = MultiboxLoss(config.priors, iou_threshold=0.5, neg_pos_ratio=3,
                             center_variance=0.1, size_variance=0.2, device=DEVICE)
optimizer = torch.optim.SGD(params, lr=args.lr, momentum=args.momentum,
                                weight_decay=args.weight_decay)

如果想要调整学习率,通过会定义一种学习率调整的策略scheduler,其通用流程如下:

scheduler = XXXLR(optimizer,...) #设定一种学习率调整的策略
for epoch in range(100):
    scheduler.step() 
    train(...) # optimizer.zero_grad() optimizer.step()仍然需要
    validate(...)

这块的代码具体如下。

if args.scheduler == 'multi-step':
    milestones = [int(v.strip()) for v in args.milestones.split(",")]
    scheduler = MultiStepLR(optimizer, milestones=milestones,gamma=0.1, last_epoch=last_epoch)
elif args.scheduler == 'cosine':
    scheduler = CosineAnnealingLR(optimizer, args.t_max, last_epoch=last_epoch)

对学习率改变的策略做一个总结, 参考

训练网络做迭代

for epoch in range(last_epoch + 1, args.num_epochs):
    scheduler.step()
    # 训练
    train(train_loader, net, criterion, optimizer,
          device=DEVICE, debug_steps=args.debug_steps, epoch=epoch)

    if epoch % args.validation_epochs == 0 or epoch == args.num_epochs - 1:
        # 验证
        val_loss, val_regression_loss, val_classification_loss = test(val_loader, net, criterion, DEVICE)
        model_path = os.path.join(args.checkpoint_folder, f"{args.net}-Epoch-{epoch}-Loss-{val_loss}.pth")
        net.save(model_path)
        logging.info(f"Saved model {model_path}")

训练过程

def train(loader, net, criterion, optimizer, device, debug_steps=100, epoch=-1):
    net.train(True) # 还可以写成net.train()
    running_loss = 0.0
    running_regression_loss = 0.0
    running_classification_loss = 0.0
    for i, data in enumerate(loader):
        images, boxes, labels = data
        images = images.to(device)
        boxes = boxes.to(device)
        labels = labels.to(device)

        optimizer.zero_grad() # 虽然用了scheduler,但还是要将optimizer初始化
        confidence, locations = net(images)
        regression_loss, classification_loss = criterion(confidence, locations, labels, boxes)  # TODO CHANGE BOXES
        loss = regression_loss + classification_loss
        loss.backward()
        optimizer.step()

        # 这一块儿没看懂,好像是为了调试用的,需要测试
        running_loss += loss.item()
        running_regression_loss += regression_loss.item()
        running_classification_loss += classification_loss.item()
        if i and i % debug_steps == 0:
            avg_loss = running_loss / debug_steps
            avg_reg_loss = running_regression_loss / debug_steps
            avg_clf_loss = running_classification_loss / debug_steps
            running_loss = 0.0
            running_regression_loss = 0.0
            running_classification_loss = 0.0

测试过程

def test(loader, net, criterion, device):
    net.eval()
    running_loss = 0.0
    running_regression_loss = 0.0
    running_classification_loss = 0.0
    num = 0
    for _, data in enumerate(loader):
        images, boxes, labels = data
        images = images.to(device)
        boxes = boxes.to(device)
        labels = labels.to(device)
        num += 1

        with torch.no_grad():
            confidence, locations = net(images)
            regression_loss, classification_loss = criterion(confidence, locations, labels, boxes)
            loss = regression_loss + classification_loss

        running_loss += loss.item()
        running_regression_loss += regression_loss.item()
        running_classification_loss += classification_loss.item()
    return running_loss / num, running_regression_loss / num, running_classification_loss / num
上一篇下一篇

猜你喜欢

热点阅读