Python predictor.Predictor() Examples

The following are 4 code examples of predictor.Predictor(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module predictor , or try the search function .
Example #1
Source File: exec.py    From RegRCNN with Apache License 2.0 6 votes vote down vote up
def test(cf, logger, max_fold=None):
    """performs testing for a given fold (or held out set). saves stats in evaluator.
    """
    logger.time("test_fold")
    logger.info('starting testing model of fold {} in exp {}'.format(cf.fold, cf.exp_dir))
    net = model.net(cf, logger).cuda()
    batch_gen = data_loader.get_test_generator(cf, logger)

    test_predictor = Predictor(cf, net, logger, mode='test')
    test_results_list = test_predictor.predict_test_set(batch_gen, return_results = not hasattr(
        cf, "eval_test_separately") or not cf.eval_test_separately)

    if test_results_list is not None:
        test_evaluator = Evaluator(cf, logger, mode='test')
        test_evaluator.evaluate_predictions(test_results_list)
        test_evaluator.score_test_df(max_fold=max_fold)

    logger.info('Testing of fold {} took {}.\n'.format(cf.fold, logger.get_time("test_fold", reset=True, format="hms"))) 
Example #2
Source File: exec.py    From medicaldetectiontoolkit with Apache License 2.0 5 votes vote down vote up
def test(logger):
    """
    perform testing for a given fold (or hold out set). save stats in evaluator.
    """
    logger.info('starting testing model of fold {} in exp {}'.format(cf.fold, cf.exp_dir))
    net = model.net(cf, logger).cuda()
    test_predictor = Predictor(cf, net, logger, mode='test')
    test_evaluator = Evaluator(cf, logger, mode='test')
    batch_gen = data_loader.get_test_generator(cf, logger)
    test_results_list = test_predictor.predict_test_set(batch_gen, return_results=True)
    test_evaluator.evaluate_predictions(test_results_list)
    test_evaluator.score_test_df() 
Example #3
Source File: main.py    From Describing_a_Knowledge_Base with MIT License 5 votes vote down vote up
def train_epoches(t_dataset, v_dataset, model, n_epochs, teacher_forcing_ratio):
    eval_f = Evaluate_test()
    best_dev = 0
    train_loader = t_dataset.corpus
    len_batch = len(train_loader)
    epoch_examples_total = t_dataset.len
    for epoch in range(1, n_epochs + 1):
        model.train(True)
        torch.set_grad_enabled(True)
        epoch_loss = 0
        for batch_idx in range(len_batch):
            loss, num_examples = train_batch(t_dataset, batch_idx, model, teacher_forcing_ratio)
            epoch_loss += loss * num_examples
            sys.stdout.write(
                '%d batches processed. current batch loss: %f\r' %
                (batch_idx, loss)
            )
            sys.stdout.flush()
        epoch_loss /= epoch_examples_total
        log_msg = "Finished epoch %d with losses: %.4f" % (epoch, epoch_loss)
        print(log_msg)
        predictor = Predictor(model, v_dataset.vocab, args.cuda)
        print("Start Evaluating")
        cand, ref = predictor.preeval_batch(v_dataset)
        print('Result:')
        print('ref: ', ref[1][0])
        print('cand: ', cand[1])
        final_scores = eval_f.evaluate(live=True, cand=cand, ref=ref)
        epoch_score = 2*final_scores['ROUGE_L']*final_scores['Bleu_4']/(final_scores['Bleu_4']+ final_scores['ROUGE_L'])
        if epoch_score > best_dev:
            torch.save(model.state_dict(), args.save)
            print("model saved")
            best_dev = epoch_score 
Example #4
Source File: tester.py    From TuSimple-DUC with Apache License 2.0 5 votes vote down vote up
def __init__(self, config):
        self.config = config
        # model
        self.model_dir = config.get('model', 'model_dir')
        self.model_prefix = config.get('model', 'model_prefix')
        self.model_epoch = config.getint('model', 'model_epoch')
        self.label_num = config.getint('model', 'label_num')
        self.ctx = mx.gpu(config.getint('model', 'gpu'))

        # data
        self.ds_rate = int(config.get('data', 'ds_rate'))
        self.cell_width = int(config.get('data', 'cell_width'))
        self.test_shape = [int(f) for f in config.get('data', 'test_shape').split(',')]
        self.result_shape = [int(f) for f in config.get('data', 'result_shape').split(',')]
        self.rgb_mean = [float(f) for f in config.get('data', 'rgb_mean').split(',')]
        # rescale for test
        self.test_scales = [float(f) for f in config.get('data', 'test_scales').split(',')]
        self.cell_shapes = [[math.ceil(l * s / self.ds_rate)*self.ds_rate for l in self.test_shape]
                            for s in self.test_scales]
        self.modules = []
        for i, test_scale in enumerate(self.test_scales):
            predictor = mx.module.Module.load(
                prefix=os.path.join(self.model_dir, self.model_prefix),
                epoch=self.model_epoch,
                context=self.ctx)
            data_shape = (1, 3, int(self.cell_shapes[i][0]), int(self.cell_shapes[i][1]))
            predictor.bind(data_shapes=[('data', data_shape)], for_training=False)
            self.modules.append(predictor)
        self.predictor = Predictor(
            modules=self.modules,
            label_num=self.label_num,
            ds_rate=self.ds_rate,
            cell_width=self.cell_width,
            result_shape=self.result_shape,
            test_scales=self.test_scales
        )