Example #1
0
def main():
    args = parse_args()
    update_config(args.cfg)
    fix_random_seed(cfg.SEED)

    # cudnn related setting
    cudnn.benchmark = cfg.CUDNN.BENCHMARK
    cudnn.deterministic = cfg.CUDNN.DETERMINISTIC
    cudnn.enabled = cfg.CUDNN.ENABLE

    # prepare output directory
    output_dir = os.path.join(cfg.ROOT_DIR, cfg.TRAIN.MODEL_DIR)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    # copy config ssad.yaml file to output directory
    cfg_file = os.path.join(output_dir, args.cfg.split('/')[-1])
    shutil.copyfile(args.cfg, cfg_file)

    # data loader
    # Notice: we discard the last data
    train_dset = TALDataset(cfg, cfg.DATASET.TRAIN_SPLIT)
    train_loader = DataLoader(train_dset,
                              batch_size=cfg.TRAIN.BATCH_SIZE,
                              shuffle=True,
                              drop_last=False,
                              num_workers=cfg.WORKERS,
                              pin_memory=cfg.PIN_MEMORY)
    val_dset = TALDataset(cfg, cfg.DATASET.VAL_SPLIT)
    val_loader = DataLoader(val_dset,
                            batch_size=cfg.TEST.BATCH_SIZE,
                            shuffle=False,
                            drop_last=False,
                            num_workers=cfg.WORKERS,
                            pin_memory=cfg.PIN_MEMORY)

    model = LocNet(cfg)
    model.apply(weight_init)
    model.cuda()

    optimizer = optim.Adam(model.parameters(), lr=cfg.TRAIN.LR)
    for epoch in range(cfg.TRAIN.BEGIN_EPOCH, cfg.TRAIN.END_EPOCH + 1):
        if epoch in cfg.TRAIN.LR_DECAY_EPOCHS:
            decay_lr(optimizer, factor=cfg.TRAIN.LR_DECAY_FACTOR)

        loss_train = train(cfg, train_loader, model, optimizer)
        print('epoch %d: loss: %f' % (epoch, loss_train))
        with open(os.path.join(cfg.ROOT_DIR, cfg.TRAIN.LOG_FILE), 'a') as f:
            f.write("epoch %d, loss: %.4f\n" % (epoch, loss_train))

        if epoch % cfg.TEST.EVAL_INTERVAL == 0:
            # model
            weight_file = save_model(cfg,
                                     epoch=epoch,
                                     model=model,
                                     optimizer=optimizer)
            out_df_af, out_df_ab = evaluation(val_loader, model, epoch, cfg)
            out_df_ab['conf'] = out_df_ab['conf'] * cfg.TEST.CONCAT_AB
            out_df = pd.concat([out_df_af, out_df_ab])
            post_process(out_df, epoch, cfg, is_soft_nms=False)
Example #2
0
def main():
    args = parse_args()
    update_config(args.cfg)
    # create output directory
    if cfg.BASIC.CREATE_OUTPUT_DIR:
        out_dir = os.path.join(cfg.BASIC.ROOT_DIR, cfg.TRAIN.MODEL_DIR)
        if not os.path.exists(out_dir):
            os.makedirs(out_dir)
    # copy config file
    if cfg.BASIC.BACKUP_CODES:
        backup_dir = os.path.join(cfg.BASIC.ROOT_DIR, cfg.TRAIN.MODEL_DIR,
                                  'code')
        backup_codes(cfg.BASIC.ROOT_DIR, backup_dir, cfg.BASIC.BACKUP_LISTS)
    fix_random_seed(cfg.BASIC.SEED)
    if cfg.BASIC.SHOW_CFG:
        pprint.pprint(cfg)

    # cudnn related setting
    cudnn.benchmark = cfg.CUDNN.BENCHMARK
    cudnn.deterministic = cfg.CUDNN.DETERMINISTIC
    cudnn.enabled = cfg.CUDNN.ENABLE

    # data loader
    train_dset = TALDataset(cfg, cfg.DATASET.TRAIN_SPLIT)
    train_loader = DataLoader(train_dset,
                              batch_size=cfg.TRAIN.BATCH_SIZE,
                              shuffle=True,
                              drop_last=False,
                              num_workers=cfg.BASIC.WORKERS,
                              pin_memory=cfg.DATASET.PIN_MEMORY)
    val_dset = TALDataset(cfg, cfg.DATASET.VAL_SPLIT)
    val_loader = DataLoader(val_dset,
                            batch_size=cfg.TEST.BATCH_SIZE,
                            shuffle=False,
                            drop_last=False,
                            num_workers=cfg.BASIC.WORKERS,
                            pin_memory=cfg.DATASET.PIN_MEMORY)

    model = LocNet(cfg)
    model.apply(weight_init)
    model.cuda()

    optimizer = optim.Adam(model.parameters(), lr=cfg.TRAIN.LR)
    for epoch in range(cfg.TRAIN.BEGIN_EPOCH, cfg.TRAIN.END_EPOCH + 1):
        loss_train = train(cfg, train_loader, model, optimizer)
        print('epoch %d: loss: %f' % (epoch, loss_train))
        with open(os.path.join(cfg.BASIC.ROOT_DIR, cfg.TRAIN.LOG_FILE),
                  'a') as f:
            f.write("epoch %d, loss: %.4f\n" % (epoch, loss_train))

        # decay lr
        if epoch in cfg.TRAIN.LR_DECAY_EPOCHS:
            decay_lr(optimizer, factor=cfg.TRAIN.LR_DECAY_FACTOR)

        if epoch in cfg.TEST.EVAL_INTERVAL:
            save_model(cfg, epoch=epoch, model=model, optimizer=optimizer)
            out_df_ab, out_df_af = evaluation(val_loader, model, epoch, cfg)
            out_df_list = [out_df_ab, out_df_af]
            final_result_process(out_df_list, epoch, cfg, flag=0)
Example #3
0
def main():
    args = args_parser()
    update_config(args.cfg)
    if cfg.BASIC.SHOW_CFG:
        pprint.pprint(cfg)
    # prepare running environment for the whole project
    prepare_env(cfg)

    # log
    writer = SummaryWriter(
        log_dir=os.path.join(cfg.BASIC.CKPT_DIR, cfg.BASIC.LOG_DIR))

    # dataloader
    train_dset = WtalDataset(cfg, cfg.DATASET.TRAIN_SPLIT)
    train_loader = DataLoader(train_dset,
                              batch_size=cfg.TRAIN.BATCH_SIZE,
                              shuffle=True,
                              num_workers=cfg.BASIC.WORKERS,
                              pin_memory=cfg.BASIC.PIN_MEMORY)
    val_dset = WtalDataset(cfg, cfg.DATASET.VAL_SPLIT)
    val_loader = DataLoader(val_dset,
                            batch_size=cfg.TEST.BATCH_SIZE,
                            shuffle=False,
                            num_workers=cfg.BASIC.WORKERS,
                            pin_memory=cfg.BASIC.PIN_MEMORY)

    # network
    model = LocNet(cfg)
    # model.apply(weight_init)

    model.cuda()

    # weight_file = "/disk3/zt/code/actloc/thumos/17_CAS_CAM_fast_tuning/output/20class_seed_0_save_model/checkpoint_best_cas_0.2701.pth"
    weight_file = '/disk3/zt/code/actloc/thumos/20_0.2701_try/output/debug_save_epoch30/checkpoint_best_cas.pth'

    from utils.utils import load_weights
    model = load_weights(model, weight_file)

    # optimizer
    optimizer = optim.Adam(model.parameters(),
                           lr=cfg.TRAIN.LR,
                           betas=cfg.TRAIN.BETAS,
                           weight_decay=cfg.TRAIN.WEIGHT_DECAY)

    optimizer.load_state_dict(torch.load(weight_file)['optimizer'])

    # criterion
    criterion = BasNetLoss()

    for epoch in range(1, cfg.TRAIN.EPOCH_NUM + 1):
        print('Epoch: %d:' % epoch)
        loss_average_cas, loss_average_cam, loss_average_consistency, loss_average_norm, loss_average_cas_inv, loss_average_cam_inv = train(
            cfg, train_loader, model, optimizer, criterion)

        writer.add_scalar('train_loss/cas', loss_average_cas, epoch)
        writer.add_scalar('train_loss/cam', loss_average_cam, epoch)
        writer.add_scalar('train_loss/consistency', loss_average_consistency,
                          epoch)
        writer.add_scalar('train_loss/norm', loss_average_norm, epoch)
        writer.add_scalar('train_loss/cas_inv', loss_average_cas_inv, epoch)
        writer.add_scalar('train_loss/cam_inv', loss_average_cam_inv, epoch)
        if cfg.BASIC.VERBOSE:
            print(
                'loss: cas %f, cam %f, consistency %f, norm %f, cas_inv %f, cam_inv %f'
                % (loss_average_cas, loss_average_cam,
                   loss_average_consistency, loss_average_norm,
                   loss_average_cas_inv, loss_average_cam_inv))

        # decay learning rate
        if epoch in cfg.TRAIN.LR_DECAY_EPOCHS:
            decay_lr(optimizer, factor=cfg.TRAIN.LR_DECAY_FACTOR)

        if epoch % cfg.TEST.EVAL_INTERVAL == 0:
            _, _, test_acc_cas, test_acc_cam = evaluate(
                cfg, val_loader, model, epoch)
            if cfg.BASIC.VERBOSE:
                print('test_acc, cas %f, cam %f' %
                      (test_acc_cas, test_acc_cam))
            writer.add_scalar('test_acc/cas', test_acc_cas, epoch)
            writer.add_scalar('test_acc/cam', test_acc_cam, epoch)

    writer.close()
Example #4
0
def main():
    args = args_parser()
    update_config(args.cfg)
    if cfg.BASIC.SHOW_CFG:
        pprint.pprint(cfg)
    # prepare running environment for the whole project
    prepare_env(cfg)

    # log
    writer = SummaryWriter(
        log_dir=os.path.join(cfg.BASIC.CKPT_DIR, cfg.BASIC.LOG_DIR))

    # dataloader
    train_dset = WtalDataset(cfg, cfg.DATASET.TRAIN_SPLIT)
    train_loader = DataLoader(train_dset,
                              batch_size=cfg.TRAIN.BATCH_SIZE,
                              shuffle=True,
                              num_workers=cfg.BASIC.WORKERS,
                              pin_memory=cfg.BASIC.PIN_MEMORY)
    val_dset = WtalDataset(cfg, cfg.DATASET.VAL_SPLIT)
    val_loader = DataLoader(val_dset,
                            batch_size=cfg.TEST.BATCH_SIZE,
                            shuffle=False,
                            num_workers=cfg.BASIC.WORKERS,
                            pin_memory=cfg.BASIC.PIN_MEMORY)

    # network
    model = LocNet(cfg)

    model.cuda()

    # optimizer
    optimizer = optim.Adam(model.parameters(),
                           lr=cfg.TRAIN.LR,
                           betas=cfg.TRAIN.BETAS,
                           weight_decay=cfg.TRAIN.WEIGHT_DECAY)
    # criterion
    criterion = BasNetLoss()

    for epoch in range(1, cfg.TRAIN.EPOCH_NUM + 1):
        print('Epoch: %d:' % epoch)
        loss_average_cas, loss_average_cam, loss_average_consistency, loss_average_norm, loss_average_cam_inv = train(
            cfg, train_loader, model, optimizer, criterion)

        writer.add_scalar('train_loss/cas', loss_average_cas, epoch)
        writer.add_scalar('train_loss/cam', loss_average_cam, epoch)
        writer.add_scalar('train_loss/consistency', loss_average_consistency,
                          epoch)
        writer.add_scalar('train_loss/norm', loss_average_norm, epoch)
        writer.add_scalar('train_loss/cam_inv', loss_average_cam_inv, epoch)
        if cfg.BASIC.VERBOSE:
            print(
                'loss: cas %f, cam %f, consistency %f, norm %f, cam_inv %f' %
                (loss_average_cas, loss_average_cam, loss_average_consistency,
                 loss_average_norm, loss_average_cam_inv))

        # decay learning rate
        if epoch in cfg.TRAIN.LR_DECAY_EPOCHS:
            decay_lr(optimizer, factor=cfg.TRAIN.LR_DECAY_FACTOR)

        if epoch % cfg.TEST.EVAL_INTERVAL == 0:
            _, test_acc_cas = evaluate(cfg, val_loader, model, epoch)
            if cfg.BASIC.VERBOSE:
                print('test_acc, cas %f' % (test_acc_cas))
            writer.add_scalar('test_acc/cas', test_acc_cas, epoch)

            save_best_model(cfg,
                            epoch=epoch,
                            model=model,
                            optimizer=optimizer,
                            name='cas')
    writer.close()