Example #1
0
def main():
    args = arg_parse()

    # ---- setup device ----
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    print('==> Using device ' + device)

    # ---- setup config ----
    cfg = get_cfg_defaults()
    cfg.merge_from_file(args.cfg)
    cfg.freeze()
    seed.set_seed(cfg.SOLVER.SEED)

    # ---- setup logger ----
    os.makedirs(cfg.OUTPUT_DIR, exist_ok=True)
    logger = logging.construct_logger('context_cnns', cfg.OUTPUT_DIR)
    logger.info(f'Using {device}')
    logger.info('\n' + cfg.dump())

    # ---- setup dataset ----
    train_loader, val_loader = get_cifar(cfg)

    # ---- setup model ----
    print('==> Building model..')
    net = get_model(cfg)
    net = net.to(device)

    model_stats = summary(net, (3, 32, 32), depth=10)
    logger.info('\n'+str(model_stats))

    if device=='cuda':
        net = torch.nn.DataParallel(net)

    # ---- setup trainers ----
    optim = torch.optim.SGD(net.parameters(), lr=cfg.SOLVER.BASE_LR,
                            momentum=cfg.SOLVER.MOMENTUM,
                            weight_decay=cfg.SOLVER.WEIGHT_DECAY)

    trainer = Trainer(
        device,
        train_loader,
        val_loader,
        net,
        optim,
        logger,
        cfg
    )

    if args.resume:
        # Load checkpoint
        print('==> Resuming from checkpoint..')
        cp = torch.load(args.resume)
        trainer.model.load_state_dict(cp['net'])
        trainer.optim.load_state_dict(cp['optim'])
        trainer.epochs = cp['epoch']
        trainer.train_acc = cp['train_accuracy']
        trainer.val_acc = cp['test_accuracy']

    trainer.train()
Example #2
0
def main():
    """The main for this domain adapation example, showing the workflow"""
    args = arg_parse()

    # ---- setup device ----
    device = "cuda" if torch.cuda.is_available() else "cpu"
    print("==> Using device " + device)

    # ---- setup configs ----
    cfg = get_cfg_defaults()
    cfg.merge_from_file(args.cfg)
    cfg.freeze()
    set_seed(cfg.SOLVER.SEED)

    # ---- setup logger and output ----
    output_dir = os.path.join(cfg.OUTPUT_DIR, cfg.DATASET.NAME, args.output)
    os.makedirs(output_dir, exist_ok=True)
    logger = construct_logger("isonet", output_dir)
    logger.info("Using " + device)
    logger.info("\n" + cfg.dump())

    # ---- setup dataset ----
    train_loader, val_loader = get_cifar(cfg)

    print("==> Building model..")
    net = get_model(cfg)
    # print(net)
    net = net.to(device)
    # model_stats = summary(net, (3, 32, 32))
    # logger.info('\n'+str(model_stats))

    # Needed even for single GPU https://discuss.pytorch.org/t/attributeerror-net-object-has-no-attribute-module/45652
    if device == "cuda":
        net = torch.nn.DataParallel(net)

    optim = torch.optim.SGD(
        net.parameters(),
        lr=cfg.SOLVER.BASE_LR,
        momentum=cfg.SOLVER.MOMENTUM,
        weight_decay=cfg.SOLVER.WEIGHT_DECAY,
        dampening=cfg.SOLVER.DAMPENING,
        nesterov=cfg.SOLVER.NESTEROV,
    )

    trainer = Trainer(device, train_loader, val_loader, net, optim, logger,
                      output_dir, cfg)

    if args.resume:
        # Load checkpoint
        print("==> Resuming from checkpoint..")
        cp = torch.load(args.resume)
        trainer.model.load_state_dict(cp["net"])
        trainer.optim.load_state_dict(cp["optim"])
        trainer.epochs = cp["epoch"]
        trainer.train_acc = cp["train_accuracy"]
        trainer.val_acc = cp["test_accuracy"]

    trainer.train()
Example #3
0
def main():
    args = arg_parse()

    # ---- setup device ----
    device = "cuda" if torch.cuda.is_available() else "cpu"
    print("==> Using device " + device)

    # ---- setup config ----
    cfg = get_cfg_defaults()
    cfg.merge_from_file(args.cfg)
    cfg.freeze()
    seed.set_seed(cfg.SOLVER.SEED)

    # ---- setup logger ----
    os.makedirs(cfg.OUTPUT_DIR, exist_ok=True)
    logger = logging.construct_logger("context_cnns", cfg.OUTPUT_DIR)
    logger.info(f"Using {device}")
    logger.info("\n" + cfg.dump())

    # ---- setup dataset ----
    train_loader, valid_loader = get_cifar(cfg)

    # ---- setup model ----
    print("==> Building model..")
    net = get_model(cfg)
    net = net.to(device)

    model_stats = summary(net, (3, 32, 32))
    logger.info("\n" + str(model_stats))

    if device == "cuda":
        net = torch.nn.DataParallel(net)

    # ---- setup trainers ----
    optim = torch.optim.SGD(
        net.parameters(), lr=cfg.SOLVER.BASE_LR, momentum=cfg.SOLVER.MOMENTUM, weight_decay=cfg.SOLVER.WEIGHT_DECAY
    )

    trainer = Trainer(device, train_loader, valid_loader, net, optim, logger, cfg)

    if args.resume:
        # Load checkpoint
        print("==> Resuming from checkpoint..")
        cp = torch.load(args.resume)
        trainer.model.load_state_dict(cp["net"])
        trainer.optim.load_state_dict(cp["optim"])
        trainer.epochs = cp["epoch"]
        trainer.train_acc = cp["train_accuracy"]
        trainer.valid_acc = cp["test_accuracy"]

    trainer.train()
Example #4
0
File: main.py Project: sz144/pykale
def main():
    args = arg_parse()
    # ---- setup device ----
    device = "cuda" if torch.cuda.is_available() else "cpu"
    print("==> Using device " + device)

    # ---- setup configs ----
    cfg = get_cfg_defaults()
    cfg.merge_from_file(args.cfg)
    cfg.freeze()
    seed.set_seed(cfg.SOLVER.SEED)
    # ---- setup logger and output ----
    output_dir = os.path.join(cfg.OUTPUT_DIR, cfg.DATASET.NAME, args.output)
    os.makedirs(output_dir, exist_ok=True)
    logger = lu.construct_logger("gripnet", output_dir)
    logger.info("Using " + device)
    logger.info(cfg.dump())
    # ---- setup dataset ----
    download_file_by_url(cfg.DATASET.URL, cfg.DATASET.ROOT, "pose.pt", "pt")
    data = torch.load(os.path.join(cfg.DATASET.ROOT, "pose.pt"))
    device = torch.device(device)
    data = data.to(device)
    # ---- setup model ----
    print("==> Building model..")
    model = GripNet(
        cfg.GRIPN.GG_LAYERS, cfg.GRIPN.GD_LAYERS, cfg.GRIPN.DD_LAYERS, data.n_d_node, data.n_g_node, data.n_dd_edge_type
    ).to(device)
    # TODO Visualize model
    # ---- setup trainers ----
    optimizer = torch.optim.Adam(model.parameters(), lr=cfg.SOLVER.BASE_LR)
    # TODO
    trainer = Trainer(cfg, device, data, model, optimizer, logger, output_dir)

    if args.resume:
        # Load checkpoint
        print("==> Resuming from checkpoint..")
        cp = torch.load(args.resume)
        trainer.model.load_state_dict(cp["net"])
        trainer.optim.load_state_dict(cp["optim"])
        trainer.epochs = cp["epoch"]
        trainer.train_auprc = cp["train_auprc"]
        trainer.valid_auprc = cp["valid_auprc"]
        trainer.train_auroc = cp["train_auroc"]
        trainer.valid_auroc = cp["valid_auroc"]
        trainer.train_ap = cp["train_ap"]
        trainer.valid_ap = cp["valid_ap"]

    trainer.train()
Example #5
0
def main():
    args = arg_parse()
    # ---- setup device ----
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    print('==> Using device ' + device)

    # ---- setup configs ----
    C.merge_from_file(args.cfg)
    C.freeze()
    seed.set_seed(C.SOLVER.SEED)
    # ---- setup logger and output ----
    output_dir = os.path.join(C.OUTPUT_DIR, C.DATASET.NAME, args.output)
    os.makedirs(output_dir, exist_ok=True)
    logger = lu.construct_logger('gripnet', output_dir)
    logger.info('Using ' + device)
    logger.info(C.dump())
    # ---- setup dataset ----
    data = construct_dataset(C)
    device = torch.device(device)
    data = data.to(device)
    # ---- setup model ----
    print('==> Building model..')
    model = GripNet(C.GRIPN.GG_LAYERS, C.GRIPN.GD_LAYERS, C.GRIPN.DD_LAYERS,
                    data.n_d_node, data.n_g_node,
                    data.n_dd_edge_type).to(device)
    # TODO Visualize model
    # ---- setup trainers ----
    optimizer = torch.optim.Adam(model.parameters(), lr=C.SOLVER.BASE_LR)
    # TODO
    trainer = Trainer(C, device, data, model, optimizer, logger, output_dir)

    if args.resume:
        # Load checkpoint
        print('==> Resuming from checkpoint..')
        cp = torch.load(args.resume)
        trainer.model.load_state_dict(cp['net'])
        trainer.optim.load_state_dict(cp['optim'])
        trainer.epochs = cp['epoch']
        trainer.train_auprc = cp['train_auprc']
        trainer.val_auprc = cp['val_auprc']
        trainer.train_auroc = cp['train_auroc']
        trainer.val_auroc = cp['val_auroc']
        trainer.train_ap = cp['train_ap']
        trainer.val_ap = cp['val_ap']

    trainer.train()
Example #6
0
def testing_logger():
    save_dir = os.getcwd()
    testing_logger = logger.construct_logger("test", save_dir)
    yield testing_logger

    # Gather info
    filehandler = testing_logger.handlers[0]
    log_file_name = filehandler.baseFilename

    # Teardown log file
    filehandler.close()
    os.remove(log_file_name)

    # Teardown gitdiff.patch file
    [folder, file] = os.path.split(log_file_name)
    file_core = os.path.splitext(file)[0]
    gitdiff_file_name = os.path.join(folder, file_core + ".gitdiff.patch")
    os.remove(gitdiff_file_name)