Пример #1
0
def main(args):
    np.random.seed(432)
    torch.random.manual_seed(432)
    try:
        os.makedirs(args.outpath)
    except OSError:
        pass
    experiment_path = utils.get_new_model_path(args.outpath)
    print(experiment_path)
    train_writer = SummaryWriter(os.path.join(experiment_path, 'train_logs'))
    val_writer = SummaryWriter(os.path.join(experiment_path, 'val_logs'))
    scheduler = cyclical_lr(5, 1e-5, 2e-3)
    trainer = train.Trainer(train_writer, val_writer, scheduler=scheduler)

    train_transform = data.build_preprocessing()
    eval_transform = data.build_preprocessing()

    trainds, evalds = data.build_dataset(args.datadir, None)
    trainds.transform = train_transform
    evalds.transform = eval_transform

    model = models.resnet34()
    base_opt = torch.optim.Adam(model.parameters())
    opt = SWA(base_opt, swa_start=30, swa_freq=10)

    trainloader = DataLoader(trainds,
                             batch_size=args.batch_size,
                             shuffle=True,
                             num_workers=8,
                             pin_memory=True)
    evalloader = DataLoader(evalds,
                            batch_size=args.batch_size,
                            shuffle=False,
                            num_workers=16,
                            pin_memory=True)

    export_path = os.path.join(experiment_path, 'last.pth')

    best_lwlrap = 0

    for epoch in range(args.epochs):
        print('Epoch {} - lr {:.6f}'.format(epoch, scheduler(epoch)))
        trainer.train_epoch(model, opt, trainloader, scheduler(epoch))
        metrics = trainer.eval_epoch(model, evalloader)

        print('Epoch: {} - lwlrap: {:.4f}'.format(epoch, metrics['lwlrap']))

        # save best model
        if metrics['lwlrap'] > best_lwlrap:
            best_lwlrap = metrics['lwlrap']
            torch.save(model.state_dict(), export_path)

    print('Best metrics {:.4f}'.format(best_lwlrap))
    opt.swap_swa_sgd()
Пример #2
0
def main(args):
    np.random.seed(432)
    torch.random.manual_seed(432)
    try:
        os.makedirs(args.outpath)
    except OSError:
        pass
    experiment_path = utils.get_new_model_path(args.outpath)

    train_writer = SummaryWriter(os.path.join(experiment_path, 'train_logs'))
    val_writer = SummaryWriter(os.path.join(experiment_path, 'val_logs'))
    trainer = train.Trainer(train_writer, val_writer)

    # todo: add config
    train_transform = data.build_preprocessing()
    eval_transform = data.build_preprocessing()

    trainds, evalds = data.build_dataset(args.datadir, None)
    trainds.transform = train_transform
    evalds.transform = eval_transform

    model = models.resnet34()
    opt = torch.optim.Adam(model.parameters())

    trainloader = DataLoader(trainds,
                             batch_size=args.batch_size,
                             shuffle=True,
                             num_workers=8,
                             pin_memory=True)
    evalloader = DataLoader(evalds,
                            batch_size=args.batch_size,
                            shuffle=False,
                            num_workers=16,
                            pin_memory=True)

    for epoch in range(args.epochs):
        trainer.train_epoch(model, opt, trainloader, 3e-4)
        metrics = trainer.eval_epoch(model, evalloader)

        state = dict(
            epoch=epoch,
            model_state_dict=model.state_dict(),
            optimizer_state_dict=opt.state_dict(),
            loss=metrics['loss'],
            lwlrap=metrics['lwlrap'],
            global_step=trainer.global_step,
        )
        export_path = os.path.join(experiment_path, 'last.pth')
        torch.save(state, export_path)
Пример #3
0
def main(args):
    np.random.seed(432)
    torch.random.manual_seed(432)
    try:
        os.makedirs(args.outpath)
    except OSError:
        pass
    experiment_path = utils.get_new_model_path(args.outpath)
    print(experiment_path)

    train_writer = SummaryWriter(os.path.join(experiment_path, 'train_logs'))
    val_writer = SummaryWriter(os.path.join(experiment_path, 'val_logs'))
    trainer = train.Trainer(train_writer, val_writer)

    # todo: add config
    train_transform = data.build_preprocessing()
    eval_transform = data.build_preprocessing()

    trainds, evalds = data.build_dataset(args.datadir, None)
    trainds.transform = train_transform
    evalds.transform = eval_transform

    model = models.resnet34()
    opt = torch.optim.Adam(model.parameters(), lr=1e-8)

    trainloader = DataLoader(trainds,
                             batch_size=args.batch_size,
                             shuffle=True,
                             num_workers=8,
                             pin_memory=True)
    evalloader = DataLoader(evalds,
                            batch_size=args.batch_size,
                            shuffle=False,
                            num_workers=16,
                            pin_memory=True)

    #find lr fast ai
    criterion = torch.nn.BCEWithLogitsLoss()
    lr_finder = LRFinder(model, opt, criterion, device="cuda")
    #     lr_finder.range_test(trainloader, val_loader=evalloader, end_lr=1, num_iter=10, step_mode="exp")
    lr_finder.range_test(trainloader,
                         end_lr=100,
                         num_iter=100,
                         step_mode="exp")

    #plot graph fast ai
    skip_start = 6
    skip_end = 3
    lrs = lr_finder.history["lr"]
    losses = lr_finder.history["loss"]
    grad_norm = lr_finder.history["grad_norm"]

    #     ind = grad_norm.index(min(grad_norm))
    #     opt_lr = lrs[ind]
    #     print('LR with min grad_norm =', opt_lr)

    lrs = lrs[skip_start:-skip_end]
    losses = losses[skip_start:-skip_end]

    fig = plt.figure(figsize=(12, 9))
    plt.plot(lrs, losses)
    plt.xscale("log")
    plt.xlabel("Learning rate")
    plt.ylabel("Loss")
    train_writer.add_figure('loss_vs_lr', fig)

    lr_finder.reset()

    #     fixed_lr = 1e-3
    fixed_lr = 3e-4
    opt = torch.optim.Adam(model.parameters(), lr=fixed_lr)

    #     #new
    #     lr = 1e-3
    #     eta_min = 1e-5
    #     t_max = 10
    #     opt = torch.optim.Adam(model.parameters(), lr=lr)
    #     scheduler = CosineAnnealingLR(opt, T_max=t_max, eta_min=eta_min)
    #     #new

    #     one cycle for 5 ehoches
    #     scheduler = CosineAnnealingLR(opt, 519*4, eta_min=1e-4)
    scheduler = CosineAnnealingLR(opt, args.epochs)

    #     scheduler = CosineAnnealingLR(opt, 519, eta_min=1e-5)
    #     scheduler = StepLR(opt, step_size=3, gamma=0.1)

    state_list = []
    for epoch in range(args.epochs):
        #         t = epoch / args.epochs
        #         lr = np.exp((1 - t) * np.log(lr_begin) + t * np.log(lr_end))
        # выставляем lr для всех параметров
        trainer.train_epoch(model, opt, trainloader, fixed_lr, scheduler)
        #         trainer.train_epoch(model, opt, trainloader, 3e-4, scheduler)
        #         trainer.train_epoch(model, opt, trainloader, 9.0451e-4, scheduler)
        metrics = trainer.eval_epoch(model, evalloader)

        state = dict(
            epoch=epoch,
            model_state_dict=model.state_dict(),
            optimizer_state_dict=opt.state_dict(),
            loss=metrics['loss'],
            lwlrap=metrics['lwlrap'],
            global_step=trainer.global_step,
        )
        state_copy = copy.deepcopy(state)
        state_list.append(state_copy)
        export_path = os.path.join(experiment_path, 'last.pth')
        torch.save(state, export_path)

    # save the best path
    best_export_path = os.path.join(experiment_path, 'best.pth')

    max_lwlrap = 0
    max_lwlrap_ind = 0
    for i in range(args.epochs):
        if state_list[i]['lwlrap'] > max_lwlrap:
            max_lwlrap = state_list[i]['lwlrap']
            max_lwlrap_ind = i

    best_state = state_list[max_lwlrap_ind]
    torch.save(best_state, best_export_path)
Пример #4
0
def main(args):
    #np.random.seed(432)
    #torch.random.manual_seed(432)
    try:
        os.makedirs(args.outpath)
    except OSError:
        pass
    experiment_path = utils.get_new_model_path(args.outpath)

    train_writer = SummaryWriter(os.path.join(experiment_path, 'train_logs'))
    val_writer = SummaryWriter(os.path.join(experiment_path, 'val_logs'))
    trainer = train.Trainer(train_writer, val_writer)

    # making dataframes with file names and true answers
    # train
    ann_file = '/data/iNat/train2019.json'
    with open(ann_file) as data_file:
        train_anns = json.load(data_file)

    train_anns_df = pd.DataFrame(
        train_anns['annotations'])[['image_id', 'category_id']]
    train_img_df = pd.DataFrame(
        train_anns['images'])[['id',
                               'file_name']].rename(columns={'id': 'image_id'})
    df_train_file_cat = pd.merge(train_img_df, train_anns_df, on='image_id')

    # valid
    valid_ann_file = '/data/iNat//val2019.json'
    with open(valid_ann_file) as data_file:
        valid_anns = json.load(data_file)

    valid_anns_df = pd.DataFrame(
        valid_anns['annotations'])[['image_id', 'category_id']]
    valid_img_df = pd.DataFrame(
        valid_anns['images'])[['id',
                               'file_name']].rename(columns={'id': 'image_id'})
    df_valid_file_cat = pd.merge(valid_img_df, valid_anns_df, on='image_id')

    # test
    ann_file = '/data/iNat/test2019.json'
    with open(ann_file) as data_file:
        test_anns = json.load(data_file)

    test_img_df = pd.DataFrame(
        test_anns['images'])[['id',
                              'file_name']].rename(columns={'id': 'image_id'})

    # make dataloaders
    ID_train = df_train_file_cat.file_name.values
    labels_train = df_train_file_cat.category_id.values
    training_set = Dataset.Dataset(ID_train,
                                   labels_train,
                                   root='/data/iNat/train_val/')  # train

    ID_test = df_valid_file_cat.file_name.values
    labels_test = df_valid_file_cat.category_id.values
    test_set = Dataset.Dataset(ID_test,
                               labels_test,
                               root='/data/iNat/train_val/')  # valid

    ID_to_sent = test_img_df.file_name.values
    to_sent_dataset = Dataset.Dataset_to_sent(ID_to_sent,
                                              root='/data/iNat/test/')

    trainloader = DataLoader(training_set,
                             batch_size=args.batch_size,
                             shuffle=True,
                             num_workers=15)
    evalloader = DataLoader(test_set,
                            batch_size=args.batch_size,
                            shuffle=False,
                            num_workers=15)
    to_sent_loader = DataLoader(to_sent_dataset,
                                batch_size=args.batch_size,
                                shuffle=False,
                                num_workers=15)

    # pretrained model
    resnet = torchvision.models.resnet18(pretrained=True)
    classifier = nn.Linear(512, 1010)
    for param in resnet.parameters():
        param.requires_grad = False
    resnet.fc = classifier

    model = resnet
    #opt = torch.optim.Adam(model.parameters())
    opt = torch.optim.SGD(model.parameters(), lr=0.001)
    Loss_func = nn.CrossEntropyLoss()
    schedule = torch.optim.lr_scheduler.CosineAnnealingLR(opt,
                                                          T_max=args.epochs -
                                                          1)

    for epoch in range(args.epochs):
        print('Epoch {}/{}'.format(epoch, args.epochs - 1))
        #if epoch == args.epochs-1:
        if epoch == 15:
            for param in model.parameters():
                param.requires_grad = True

        trainer.train_epoch(model, opt, trainloader, schedule, Loss_func)
        metrics = trainer.eval_epoch(model, evalloader, Loss_func)

        state = dict(
            epoch=epoch,
            model_state_dict=model.state_dict(),
            optimizer_state_dict=opt.state_dict(),
            loss=metrics['loss'],
            accuracy=metrics['acc'],
            global_step=trainer.global_step,
        )
        export_path = os.path.join(experiment_path, 'last.pth')
        torch.save(state, export_path)
        print(metrics['loss'])

    # save predictions to csv
    utils.make_prediction(model,
                          to_sent_loader,
                          test_img_df.image_id.values,
                          submit_name=args.csv_name)
Пример #5
0
def main(args):
    np.random.seed(19)
    torch.random.manual_seed(19)
    try:
        os.makedirs(args.outpath)
    except OSError:
        pass
    experiment_path = utils.get_new_model_path(args.outpath)

    train_writer = SummaryWriter(os.path.join(experiment_path, 'train_logs'))
    val_writer = SummaryWriter(os.path.join(experiment_path, 'val_logs'))
    trainer = train.Trainer(train_writer, val_writer)

    # todo: add config
    train_transform = data.build_preprocessing()
    eval_transform = data.build_preprocessing()

    trainds, evalds = data.build_dataset(args.datadir, None)
    trainds.transform = train_transform
    evalds.transform = eval_transform

    model = models.resnet34()
    opt = torch.optim.Adam(model.parameters())

    #raspic
    eta_min = 1e-9
    t_max = 25
    sch = CosineAnnealingLR(opt, T_max=t_max, eta_min=eta_min)

    trainloader = DataLoader(trainds,
                             batch_size=args.batch_size,
                             shuffle=True,
                             num_workers=8,
                             pin_memory=True)
    evalloader = DataLoader(evalds,
                            batch_size=args.batch_size,
                            shuffle=False,
                            num_workers=16,
                            pin_memory=True)

    max_lwlrap = 0
    for epoch in range(args.epochs):
        print("Epoch:", epoch)
        trainer.train_epoch(model, opt, sch, trainloader, 3e-4)
        metrics = trainer.eval_epoch(model, evalloader)

        state = dict(
            epoch=epoch,
            model_state_dict=model.state_dict(),
            optimizer_state_dict=opt.state_dict(),
            loss=metrics['loss'],
            lwlrap=metrics['lwlrap'],
            global_step=trainer.global_step,
        )
        export_path = os.path.join(experiment_path, 'last.pth')
        torch.save(state, export_path)
        sch.step()

        if metrics['lwlrap'] > max_lwlrap:
            max_lwlrap = metrics['lwlrap']
            best_export_path = os.path.join(experiment_path, 'best.pth')
            torch.save(copy.deepcopy(state), best_export_path)