Exemplo n.º 1
0
def create_retrieval_datasets_and_loaders(
    cfg: coot.configs_retrieval.RetrievalConfig, path_data: Union[str, Path]
) -> (Tuple[RetrievalDataset, RetrievalDataset, th_data.DataLoader,
            th_data.DataLoader]):
    """
    Create training and validation datasets and dataloaders for retrieval.

    Args:
        cfg: Experiment configuration class.
        path_data: Dataset base path.

    Returns:
        Tuple of:
            Train dataset.
            Val dataset.
            Train dataloader.
            Val dataloader.
    """
    train_set = RetrievalDataset(cfg.dataset_train, path_data)
    train_loader = nn_data.create_loader(train_set,
                                         cfg.dataset_train,
                                         cfg.train.batch_size,
                                         collate_fn=train_set.collate_fn)
    val_set = RetrievalDataset(cfg.dataset_val, path_data)
    val_loader = nn_data.create_loader(val_set,
                                       cfg.dataset_val,
                                       cfg.val.batch_size,
                                       collate_fn=val_set.collate_fn)
    return train_set, val_set, train_loader, val_loader
Exemplo n.º 2
0
    def setup_test():
        # reset the seed
        set_seed(0)
        # create dataset
        _train_set = MNIST(str(dataset_path),
                           train=True,
                           download=True,
                           transform=ToTensor())
        _val_set = MNIST(str(dataset_path),
                         train=False,
                         download=True,
                         transform=ToTensor())
        # make datasets smaller if requested in config
        if cfg.dataset_train.max_datapoints > -1:
            _train_set.data = _train_set.data[:cfg.dataset_train.
                                              max_datapoints]
        if cfg.dataset_val.max_datapoints > -1:
            _val_set.data = _val_set.data[:cfg.dataset_val.max_datapoints]
        # create dataloader
        _train_loader = create_loader(_train_set,
                                      cfg.dataset_train,
                                      batch_size=cfg.train.batch_size)
        _val_loader = create_loader(_val_set,
                                    cfg.dataset_val,
                                    batch_size=cfg.val.batch_size)

        # create model and trainer
        _model_mgr = MLPModelManager(cfg)
        _trainer = MLPMNISTTrainer(cfg,
                                   _model_mgr,
                                   exp_group,
                                   exp_name,
                                   run_name,
                                   len(_train_loader),
                                   reset=True)
        return _trainer, _model_mgr, _train_loader, _val_loader
Exemplo n.º 3
0
def test_save_load():
    # load repository and experiment config
    exp_group, exp_name, run_name = "default", "mnist", "run1"
    config_file = setup_config_file_from_experiment_identifier(MNISTExperimentType, exp_group, exp_name,
                                                               config_dir=TrainerPathConst.DIR_CONFIG)
    config = load_yaml_config_file(config_file)
    dataset_path = "data"
    cfg = MLPMNISTExperimentConfig(config)
    cfg.dataset_train.num_workers = 0
    cfg.dataset_val.num_workers = 0
    set_seed(0, set_deterministic=True)

    # create dataset and dataloader
    train_set = MNIST(str(dataset_path), train=True, download=True, transform=ToTensor())
    val_set = MNIST(str(dataset_path), train=False, download=True, transform=ToTensor())
    train_loader = create_loader(train_set, cfg.dataset_train, batch_size=cfg.train.batch_size)
    val_loader = create_loader(val_set, cfg.dataset_val, batch_size=cfg.val.batch_size)

    # create model
    model_mgr = MLPModelManager(cfg)

    # create trainer
    trainer = MLPMNISTTrainer(cfg, model_mgr, exp_group, exp_name, run_name, len(train_loader), reset=True)

    # run training for some epochs
    cfg.train.num_epochs = 2
    trainer.train_model(train_loader, val_loader)

    # reload without cuda and train an epoch on the CPU
    del trainer
    del model_mgr
    cfg.train.num_epochs = 3
    cfg.use_multi_gpu = False
    cfg.use_cuda = False

    model_mgr = MLPModelManager(cfg)
    trainer = MLPMNISTTrainer(cfg, model_mgr, exp_group, exp_name, run_name, len(train_loader))
    trainer.train_model(train_loader, val_loader)

    # one more epoch with cuda only (no multi_gpu)
    del trainer
    del model_mgr
    cfg.train.num_epochs = 4
    cfg.use_multi_gpu = False
    cfg.use_cuda = True

    model_mgr = MLPModelManager(cfg)
    trainer = MLPMNISTTrainer(cfg, model_mgr, exp_group, exp_name, run_name, len(train_loader))
    trainer.train_model(train_loader, val_loader)
    old_metrics = deepcopy(trainer.metrics.storage_epoch)
    print(old_metrics)

    # test inference from loaded model
    del trainer
    del model_mgr
    cfg.train.num_epochs = 4
    cfg.use_multi_gpu = False
    cfg.use_cuda = True

    model_mgr = MLPModelManager(cfg)
    trainer = MLPMNISTTrainer(cfg, model_mgr, exp_group, exp_name, run_name, len(train_loader), inference_only=True)
    loss, acc, _, _ = trainer.validate_epoch(val_loader)
    print(loss, acc)
Exemplo n.º 4
0
def main():
    # setup arguments
    parser = utils.ArgParser(description=__doc__)
    arguments.add_default_args(parser)
    arguments.add_trainer_args(parser)
    arguments.add_exp_identifier_args(parser)
    arguments.add_dataset_test_arg(parser)
    args = parser.parse_args()

    # load experiment config
    exp_group, exp_name, config_file = arguments.setup_experiment_identifier_from_args(
        args, EXP_TYPE)
    config = load_yaml_config_file(config_file)

    # update experiment config and dataset path given the script arguments
    config = arguments.update_config_from_args(config, args)
    dataset_path = arguments.update_path_from_args(args)

    # create configuration object
    cfg = MLPMNISTExperimentConfig(config)
    if args.print_config:
        print(cfg)

    # set seed
    if cfg.random_seed is not None:
        print(f"Set seed to {cfg.random_seed}")
        set_seed(
            cfg.random_seed,
            set_deterministic=False)  # set deterministic via config if needed

    # create datasets
    train_set = MNIST(str(dataset_path),
                      train=True,
                      download=True,
                      transform=ToTensor())
    val_set = MNIST(str(dataset_path),
                    train=False,
                    download=True,
                    transform=ToTensor())

    # make datasets smaller if requested in config
    if cfg.dataset_train.max_datapoints > -1:
        train_set.data = train_set.data[:cfg.dataset_train.max_datapoints]
    if cfg.dataset_val.max_datapoints > -1:
        val_set.data = val_set.data[:cfg.dataset_val.max_datapoints]

    # create dataloaders
    train_loader = create_loader(train_set,
                                 cfg.dataset_train,
                                 batch_size=cfg.train.batch_size)
    val_loader = create_loader(val_set,
                               cfg.dataset_val,
                               batch_size=cfg.val.batch_size)

    # create model
    model_mgr = MLPModelManager(cfg)

    if args.test_dataset:
        # run dataset test and exit
        run_mlpmnist_dataset_test(train_set, train_loader)
        return

    # always load best epoch during validation
    load_best = args.load_best or args.validate

    # create trainer
    trainer = MLPMNISTTrainer(cfg,
                              model_mgr,
                              exp_group,
                              exp_name,
                              args.run_name,
                              len(train_loader),
                              log_dir=args.log_dir,
                              log_level=args.log_level,
                              logger=None,
                              print_graph=args.print_graph,
                              reset=args.reset,
                              load_best=load_best,
                              load_epoch=args.load_epoch,
                              inference_only=args.validate)

    if args.validate:
        # run validation
        trainer.validate_epoch(val_loader)
    else:
        # run training
        trainer.train_model(train_loader, val_loader)
Exemplo n.º 5
0
def main():
    # setup arguments
    parser = utils.ArgParser(description=__doc__)
    arguments.add_default_args(parser)
    arguments.add_exp_identifier_args(parser)
    arguments.add_trainer_args(parser)
    arguments.add_dataset_test_arg(parser)
    args = parser.parse_args()

    # load repository config yaml file to dict
    exp_group, exp_name, config_file = arguments.setup_experiment_identifier_from_args(
        args, EXP_TYPE)
    config = load_yaml_config_file(config_file)

    # update experiment config and dataset path given the script arguments
    config = arguments.update_config_from_args(config, args)
    dataset_path = arguments.update_path_from_args(args)

    # read experiment config dict
    cfg = MLPMNISTExperimentConfig(config)
    if args.print_config:
        print(cfg)

    # set seed
    verb = "Set seed"
    if cfg.random_seed is None:
        cfg.random_seed = np.random.randint(0, 2**15, dtype=np.int32)
        verb = "Randomly generated seed"
    print(f"{verb} {cfg.random_seed} deterministic {cfg.cudnn_deterministic} "
          f"benchmark {cfg.cudnn_benchmark}")
    set_seed(cfg.random_seed,
             cudnn_deterministic=cfg.cudnn_deterministic,
             cudnn_benchmark=cfg.cudnn_benchmark)

    # create datasets
    train_set = MNIST(str(dataset_path),
                      train=True,
                      download=True,
                      transform=ToTensor())
    val_set = MNIST(str(dataset_path),
                    train=False,
                    download=True,
                    transform=ToTensor())

    # make datasets smaller if requested in config
    if cfg.dataset_train.max_datapoints > -1:
        train_set.data = train_set.data[:cfg.dataset_train.max_datapoints]
    if cfg.dataset_val.max_datapoints > -1:
        val_set.data = val_set.data[:cfg.dataset_val.max_datapoints]

    # create dataloaders
    train_loader = create_loader(train_set,
                                 cfg.dataset_train,
                                 batch_size=cfg.train.batch_size)
    val_loader = create_loader(val_set,
                               cfg.dataset_val,
                               batch_size=cfg.val.batch_size)

    if args.test_dataset:
        # run dataset test and exit
        run_mlpmnist_dataset_test(train_set, train_loader)
        return
    print("---------- Setup done!")

    for run_number in range(1, args.num_runs + 1):
        run_name = f"{args.run_name}{run_number}"

        # create model
        model_mgr = MLPModelManager(cfg)

        # always load best epoch during validation
        load_best = args.load_best or args.validate

        # create trainer
        trainer = MLPMNISTTrainer(cfg,
                                  model_mgr,
                                  exp_group,
                                  exp_name,
                                  run_name,
                                  len(train_loader),
                                  log_dir=args.log_dir,
                                  log_level=args.log_level,
                                  logger=None,
                                  print_graph=args.print_graph,
                                  reset=args.reset,
                                  load_best=load_best,
                                  load_epoch=args.load_epoch,
                                  inference_only=args.validate)

        if args.validate:
            # run validation
            trainer.validate_epoch(val_loader)
        else:
            # run training
            trainer.train_model(train_loader, val_loader)

        # done with this round
        trainer.close()
        del model_mgr
        del trainer