Exemplo n.º 1
0
def main(args):
    now = datetime.now()

    for size in args.sizes:
        device = "cpu"
        if torch.cuda.is_available():
            from torch.backends import cudnn

            cudnn.benchmark = True
            device = "cuda"

        assert args.model_class in MODEL_MAP, "Model Class not in {}".format(
            MODEL_MAP.keys())
        model = MODEL_MAP[args.model_class].from_file(
            args.model_path.name,
            device,
            gcn_hidden_dimension=args.gcn_hidden_dimension,
            fc_hidden_dimension=args.fc_hidden_dimension,
        )
        # model = torch.load(args.model_path.name, map_location=device)

        positive_ratio, test_loader = get_graph_test_loader(
            args.dataset_path,
            GraphsDataset,
            args.batch_size,
            args.num_workers,
            dataset_size=size,
            positive_ratio=args.positive_ratio,
            collate_fn=collate,
            pin_memory="cuda" in device if device is not None else False,
        )
        dataset_size = size or len(test_loader.dataset)

        log_dir = os.path.join(
            args.output,
            "testing_{}_{}_{}".format(args.model_name, dataset_size,
                                      now.strftime("%Y%m%d_%H%M")),
        )
        if not os.path.exists(log_dir):
            os.makedirs(log_dir)

        log_level = logging.INFO
        if args.debug:
            log_level = logging.DEBUG
            print("Activated debug mode")

        logger = logging.getLogger("Spread Classification: Test")
        setup_logger(logger, log_dir, log_level)

        logger.debug("Setup model: {}".format(args.model_name))

        configuration = Configuration.from_dict(
            **{
                **vars(args),
                "optimizer": "N/A",
                "epochs": -1,
                "train_batch_size": -1,
                "val_batch_size": args.batch_size,
                "lr": -1,
                "lr_update_every": -1,
                "weight_decay": -1,
                "gamma": -1,
                "restart_every": -1,
                "restart_factor": -1,
                "init_lr_factor": -1,
                "lr_reduce_patience": -1,
                "lr_reduce_factor": -1,
                "early_stop_patience": -1,
                "positive_ratio": positive_ratio,
                "run_index": -1,
                "log_dir": log_dir,
                "log_level": log_level,
                "log_interval": -1,
                "device": device,
                "dataset_size": dataset_size,
                "data_type": "graph",
                "model_name": args.model_path.name,
            })

        metrics = test_on_loader(model, test_loader, logger, configuration)
        with open(os.path.join(log_dir, "metrics.json"), "w") as file:
            json.dump(metrics, file)
Exemplo n.º 2
0
def main(args):
    now = datetime.now()

    for size in args.sizes:
        log_dir = os.path.join(
            args.output,
            "training_{}_{}_{}".format(
                args.model_name, size, now.strftime("%Y%m%d_%H%M")
            ),
        )
        if not os.path.exists(log_dir):
            os.makedirs(log_dir)

        log_level = logging.INFO
        if args.debug:
            log_level = logging.DEBUG
            print("Activated debug mode")

        logger = logging.getLogger("Spread Classification: Train")
        setup_logger(logger, log_dir, log_level)

        device = "cpu"
        if torch.cuda.is_available():
            logger.debug("CUDA is enabled")
            from torch.backends import cudnn

            cudnn.benchmark = True
            device = "cuda"

        logger.debug("Setup model: {}".format(args.model_name))

        logger.debug("Setup train/val dataloaders")
        positive_ratio, loaders = get_text_loaders(
            args.dataset_path,
            args.train_batch_size,
            args.val_batch_size,
            args.num_workers,
            get_model_cls(args.model_class).MAX_LEN,
            dataset_size=size,
            positive_ratio=args.positive_ratio,
            model_class=args.model_class,
        )

        for run_index, (train_loader, val_loader) in it.islice(
            enumerate(loaders), 0, args.max_runs
        ):
            configuration = Configuration.from_dict(
                **{
                    **vars(args),
                    "positive_ratio": positive_ratio,
                    "run_index": run_index,
                    "log_dir": log_dir,
                    "log_level": log_level,
                    "device": device,
                    "dataset_size": size
                    or (len(train_loader.dataset) + len(val_loader.dataset)),
                    "data_type": "text",
                    "gcn_hidden_dimension": -1,
                    "fc_hidden_dimension": -1,
                }
            )

            train_on_loaders(train_loader, val_loader, logger, configuration)