Ejemplo n.º 1
0
def run_nsfw(model, epoch):
    """
    run NSFW
    :param model:
    :param epoch:
    :return:
    """
    criterion = nn.CrossEntropyLoss()

    optimizer_ft = optim.SGD(model.parameters(),
                             lr=cfg['init_lr'],
                             momentum=0.9,
                             weight_decay=cfg['weight_decay'])

    exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft,
                                           step_size=cfg['lr_decay_step'],
                                           gamma=0.1)

    print('start loading NSFWDataset...')
    trainloader, valloader, testloader = data_loader.load_nsfw_data()

    dataloaders = {
        'train': trainloader,
        'val': valloader,
        'test': testloader,
    }

    train_model(model=model,
                dataloaders=dataloaders,
                criterion=criterion,
                optimizer=optimizer_ft,
                scheduler=exp_lr_scheduler,
                num_epochs=epoch,
                inference=False)
Ejemplo n.º 2
0
def finetune_nsfw_classification(model, epoch):
    """
    finetune NSFW classification
    :param model:
    :param epoch:
    :return:
    """
    criterion = nn.CrossEntropyLoss()

    optimizer_ft = optim.SGD(model.parameters(), lr=args['learning_rate'], momentum=0.9,
                             weight_decay=args['weight_decay'])

    exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=args['step'], gamma=0.1)

    print('start loading NSFW Dataset...')
    trainloader, valloader, testloader = data_loader.load_nsfw_data()

    dataloaders = {
        'train': trainloader,
        'val': valloader,
        'test': testloader,
    }

    finetune_model(model=model, dataloaders=dataloaders, criterion=criterion, optimizer=optimizer_ft,
                   scheduler=exp_lr_scheduler, num_epochs=epoch, inference=args['inference'])