Exemplo n.º 1
0
    def setUp(self):
        unittest.TestCase.setUp(self)
        self.material = Argon()
        self.initialize_material()

        self.real_xtarget = 3.525
        self.reciprocal_xtarget = 1.94
        self.fourier_filter_cutoff = 1.5

        filename = self.material.reciprocal_space_filename
        self.kwargs_for_files = {
            'Files': [{
                'Filename': get_data_path(filename),
                'ReciprocalFunction': 'S(Q)',
                'Qmin': 0.02,
                'Qmax': 15.0,
                'Y': {
                    'Offset': 0.0,
                    'Scale': 1.0
                },
                'X': {
                    'Offset': 0.0
                }
            }, {
                'Filename':
                get_data_path(self.material.reciprocal_space_filename),
                'ReciprocalFunction':
                'S(Q)',
                'Qmin':
                1.90,
                'Qmax':
                35.2,
                'Y': {
                    'Offset': 0.0,
                    'Scale': 1.0
                },
                'X': {
                    'Offset': 0.0
                }
            }]
        }

        self.kwargs_for_stog_input = {
            'NumberDensity': self.material.kwargs['rho'],
            '<b_coh>^2': self.material.kwargs['<b_coh>^2'],
            '<b_tot^2>': self.material.kwargs['<b_tot^2>'],
            'FourierFilter': {
                'Cutoff': self.fourier_filter_cutoff
            },
            'OmittedXrangeCorrection': False,
            'Rdelta': self.r[1] - self.r[0],
            'Rmin': min(self.r),
            'Rmax': max(self.r)
        }
Exemplo n.º 2
0
def test_no_val_end_module(tmpdir):
    """Tests use case where trainer saves the model, and user loads it from tags independently."""
    tutils.reset_seed()

    class CurrentTestModel(LightningValidationStepMixin,
                           LightningTestModelBase):
        pass

    hparams = tutils.get_hparams()
    model = CurrentTestModel(hparams)

    # logger file to get meta
    logger = tutils.get_test_tube_logger(tmpdir, False)

    trainer_options = dict(max_epochs=1,
                           logger=logger,
                           checkpoint_callback=ModelCheckpoint(tmpdir))

    # fit model
    trainer = Trainer(**trainer_options)
    result = trainer.fit(model)

    # traning complete
    assert result == 1, 'amp + ddp model failed to complete'

    # save model
    new_weights_path = os.path.join(tmpdir, 'save_test.ckpt')
    trainer.save_checkpoint(new_weights_path)

    # load new model
    tags_path = tutils.get_data_path(logger, path_dir=tmpdir)
    tags_path = os.path.join(tags_path, 'meta_tags.csv')
    model_2 = LightningTestModel.load_from_metrics(
        weights_path=new_weights_path, tags_csv=tags_path)
    model_2.eval()
def test_model_saving_loading(tmpdir):
    """Tests use case where trainer saves the model, and user loads it from tags independently."""
    tutils.reset_seed()

    hparams = tutils.get_hparams()
    model = LightningTestModel(hparams)

    # logger file to get meta
    logger = tutils.get_test_tube_logger(tmpdir, False)

    trainer_options = dict(
        max_epochs=1,
        logger=logger,
        checkpoint_callback=ModelCheckpoint(tmpdir)
    )

    # fit model
    trainer = Trainer(**trainer_options)
    result = trainer.fit(model)

    # traning complete
    assert result == 1, 'amp + ddp model failed to complete'

    # make a prediction
    for dataloader in model.test_dataloader():
        for batch in dataloader:
            break

    x, y = batch
    x = x.view(x.size(0), -1)

    # generate preds before saving model
    model.eval()
    pred_before_saving = model(x)

    # save model
    new_weights_path = os.path.join(tmpdir, 'save_test.ckpt')
    trainer.save_checkpoint(new_weights_path)

    # load new model
    tags_path = tutils.get_data_path(logger, path_dir=tmpdir)
    tags_path = os.path.join(tags_path, 'meta_tags.csv')
    model_2 = LightningTestModel.load_from_metrics(weights_path=new_weights_path,
                                                   tags_csv=tags_path)
    model_2.eval()

    # make prediction
    # assert that both predictions are the same
    new_pred = model_2(x)
    assert torch.all(torch.eq(pred_before_saving, new_pred)).item() == 1
def test_running_test_pretrained_model_ddp(tmpdir):
    """Verify `test()` on pretrained model."""
    if not tutils.can_run_gpu_test():
        return

    tutils.reset_seed()
    tutils.set_random_master_port()

    hparams = tutils.get_hparams()
    model = LightningTestModel(hparams)

    # exp file to get meta
    logger = tutils.get_test_tube_logger(tmpdir, False)

    # exp file to get weights
    checkpoint = tutils.init_checkpoint_callback(logger)

    trainer_options = dict(
        show_progress_bar=False,
        max_epochs=1,
        train_percent_check=0.4,
        val_percent_check=0.2,
        checkpoint_callback=checkpoint,
        logger=logger,
        gpus=[0, 1],
        distributed_backend='ddp'
    )

    # fit model
    trainer = Trainer(**trainer_options)
    result = trainer.fit(model)

    log.info(os.listdir(tutils.get_data_path(logger, path_dir=tmpdir)))

    # correct result and ok accuracy
    assert result == 1, 'training failed to complete'
    pretrained_model = tutils.load_model(logger,
                                         trainer.checkpoint_callback.filepath,
                                         module_class=LightningTestModel)

    # run test set
    new_trainer = Trainer(**trainer_options)
    new_trainer.test(pretrained_model)

    for dataloader in model.test_dataloader():
        tutils.run_prediction(dataloader, pretrained_model)
Exemplo n.º 5
0
def test_loading_meta_tags(tmpdir):
    tutils.reset_seed()

    from argparse import Namespace
    hparams = tutils.get_hparams()

    # save tags
    logger = tutils.get_test_tube_logger(tmpdir, False)
    logger.log_hyperparams(Namespace(some_str='a_str', an_int=1, a_float=2.0))
    logger.log_hyperparams(hparams)
    logger.save()

    # load tags
    path_expt_dir = tutils.get_data_path(logger, path_dir=tmpdir)
    tags_path = os.path.join(path_expt_dir, 'meta_tags.csv')
    tags = load_hparams_from_tags_csv(tags_path)

    assert tags.batch_size == 32 and tags.hidden_dim == 1000
Exemplo n.º 6
0
    def test_stog_read_dataset(self):
        # Number of decimal places for precision
        places = 5

        # Load S(Q) for Argon from test data
        stog = StoG(
            **{
                '<b_coh>^2': self.kwargs['<b_coh>^2'],
                '<b_tot^2>': self.kwargs['<b_tot^2>']
            })
        info = {
            'Filename': get_data_path(self.material.reciprocal_space_filename),
            'ReciprocalFunction': 'S(Q)',
            'Qmin': 0.02,
            'Qmax': 35.2,
            'Y': {
                'Offset': 0.0,
                'Scale': 1.0
            },
            'X': {
                'Offset': 0.0
            }
        }

        info['index'] = 0
        stog.read_dataset(info)

        # Check S(Q) data against targets
        self.assertEqual(stog.df_individuals.iloc[self.first].name,
                         self.reciprocal_xtarget)
        self.assertAlmostEqual(
            stog.df_individuals.iloc[self.first]['S(Q)_%d' % info['index']],
            self.sq_target[0],
            places=places)
        self.assertAlmostEqual(
            stog.df_sq_individuals.iloc[self.first]['S(Q)_%d' % info['index']],
            self.sq_target[0],
            places=places)