def main_with_asoftmaxloss(model): """ train model with vanilla ASoftmaxLoss as supervision :param model: :return: """ criterion_aloss = AngularLoss() optimizer = optim.SGD(model.parameters(), lr=cfg['init_lr'], momentum=0.9, weight_decay=cfg['weight_decay']) cosine_anneal_warmup_lr_scheduler = lr_scheduler.CosineAnnealingWarmRestarts( optimizer, T_0=10, T_mult=10, eta_min=0, last_epoch=-1) print('start loading ImageDataset...') trainloader, valloader, testloader = data_loader.load_imagedataset_data() dataloaders = {'train': trainloader, 'val': valloader, 'test': testloader} train_model_with_modified_softmax_loss( model=model, dataloaders=dataloaders, criterion=criterion_aloss, optimizer=optimizer, scheduler=cosine_anneal_warmup_lr_scheduler)
def main_with_arcloss(model): """ train model :param model: :param epoch: :return: """ criterion_xent_loss = nn.CrossEntropyLoss() arc_metric = ArcLoss(args['dim'], cfg['out_num']) optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9, weight_decay=1e-4) exp_lr_scheduler = lr_scheduler.StepLR(optimizer, step_size=60, gamma=0.1) print('start loading ImageDataset...') trainloader, valloader, testloader = data_loader.load_imagedataset_data() dataloaders = {'train': trainloader, 'val': valloader, 'test': testloader} train_model_for_arcloss(model=model, dataloaders=dataloaders, criterion=criterion_xent_loss, optimizer=optimizer, metric=arc_metric, scheduler=exp_lr_scheduler)
def main_with_centerloss(model): """ train model :param model: :param epoch: :param data_name: :return: """ criterion_xent = nn.CrossEntropyLoss() criterion_cent = CenterLoss(num_classes=cfg['out_num'], feat_dim=1024) optimizer_model = optim.SGD(model.parameters(), lr=0.01, momentum=0.9, weight_decay=1e-4) optimizer_centloss = optim.SGD(criterion_cent.parameters(), lr=0.5) cosine_anneal_warmup_lr_scheduler = lr_scheduler.CosineAnnealingWarmRestarts( optimizer_model, T_0=10, T_mult=10, eta_min=0, last_epoch=-1) print('start loading ImageDataset...') trainloader, valloader, testloader = data_loader.load_imagedataset_data() dataloaders = {'train': trainloader, 'val': valloader, 'test': testloader} train_model_for_centerloss(model=model, dataloaders=dataloaders, criterion_xent=criterion_xent, criterion_cent=criterion_cent, optimizer_model=optimizer_model, optimizer_centloss=optimizer_centloss, scheduler=cosine_anneal_warmup_lr_scheduler)