コード例 #1
0
def evaluate(evalset, config, checkpoint=None):

    assert (evalset in ['validation', 'test'])

    logger_config = {
        'owner': 'Training.evaluate',
        'log_dir': config['log_dir'],
        'timezone': 'UTC',
    }
    logger = utils.Logger(logger_config)

    _trainer = get_trainer(config)  # 调用本py文件中函数get_trainer↑

    if checkpoint is None:  # 检查是否有可用checkpoint
        checkpoints = sorted(glob.glob(_trainer.checkpoints_dir + '/*.pt'))
        assert (len(checkpoints))
        checkpoint = checkpoints[-1]
    _trainer.load(checkpoint)  # 导入checkpoint

    #------------分为train,test,validation.根据选用的是validation or test执行以下步骤--------
    if evalset == 'validation':
        valid_loader_config = {
            'batch_size': config['batch_size'],
            'images_path': config['images_validation_path'],
            'formulas_path': config['formulas_validation_path'],
        }
        loader = utils.data_loader(_trainer.train_loader.vocab,
                                   valid_loader_config)  # 导入validation数据
    elif evalset == 'test':
        test_loader_config = {
            'batch_size': config['batch_size'],
            'images_path': config['images_test_path'],
        }
        loader = utils.data_loader(_trainer.train_loader.vocab,
                                   test_loader_config)  # 导入test数据

    #------------有label的情况下,计算validation的acc等,没有label,test不进行计算,仅进行预测---------
    #-----------实际上,validation的计算过程跟train一样;test只进行bleu和edit_distance的评估---------

    if loader.has_label:  # 是否存在validation_path
        predictions, loss, acc = _trainer.evaluate(loader,
                                                   config['generation_method'])
        logger('loss={}, acc={}'.format(loss, acc))
    else:
        predictions = _trainer.evaluate(loader, config['generation_method'])

    # 确定是test/validation的formula_path
    target_path = config[
        'formulas_validation_path'] if evalset == 'validation' else config[
            'formulas_test_path']
    predicted_path = 'tmp/predicted-{}.txt'.format(evalset)  # 预测的fomula_path

    bleu_message, edit_message = bleu_and_edit_distance(
        predictions, predicted_path, target_path)  # 评估↓

    logger(bleu_message)
    logger(edit_message)
コード例 #2
0
ファイル: main.py プロジェクト: whitesockcat/im2latex3
def evaluate(evalset, config, checkpoint=None):

    assert (evalset in ['validation', 'test'])

    logger_config = {
        'owner': 'Training.evaluate',
        'log_dir': config['log_dir'],
        'timezone': 'UTC',
    }
    logger = utils.Logger(logger_config)

    _trainer = get_trainer(config)

    if checkpoint is None:
        checkpoints = sorted(glob.glob(_trainer.checkpoints_dir + '/*.pt'))
        assert (len(checkpoints))
        checkpoint = checkpoints[-1]
    _trainer.load(checkpoint)

    if evalset == 'validation':
        valid_loader_config = {
            'batch_size': config['batch_size'],
            'images_path': config['images_validation_path'],
            'formulas_path': config['formulas_validation_path'],
        }
        loader = utils.data_loader(_trainer.train_loader.vocab,
                                   valid_loader_config)
    elif evalset == 'test':
        test_loader_config = {
            'batch_size': config['batch_size'],
            'images_path': config['images_test_path'],
        }
        loader = utils.data_loader(_trainer.train_loader.vocab,
                                   test_loader_config)

    if loader.has_label:
        predictions, attn_weights, loss, acc = _trainer.evaluate(
            loader, config['generation_method'])
        logger('loss={}, acc={}'.format(loss, acc))
    else:
        predictions, attn_weights = _trainer.evaluate(
            loader, config['generation_method'])

    target_path = config[
        'formulas_validation_path'] if evalset == 'validation' else config[
            'formulas_test_path']
    predicted_path = 'tmp/predicted-{}.txt'.format(evalset)

    bleu_message, edit_message = bleu_and_edit_distance(
        predictions, predicted_path, target_path)

    logger(bleu_message)
    logger(edit_message)
コード例 #3
0
    def __init__(self, instance_id):
        UTIL.Logger("TI_Gateway", "ti_gateway.log", logging.DEBUG,
                    logging.DEBUG)
        self.logger = logging.getLogger("TI_Gateway")

        self.rest_switches = REST.OpenHabRestInterface(
            "192.168.178.24", "8080", "pi", "raspberry",
            "%s_device_switch_group" % self.prefix, self.switch_queue)
        self.rest_switches.daemon = True  # REST classes can be daemonized
        self.rest_switches.set_logger("TI_Gateway")
        self.rest_values = REST.OpenHabRestInterface("192.168.178.24", "8080",
                                                     "pi", "raspberry",
                                                     "%s_values_group",
                                                     self.value_queue)
        self.rest_values.daemon = True  # REST classes can be daemonized
        self.rest_values.set_logger("TI_Gateway")

        self.sensortag = TIInterface("24:71:89:BC:1D:01", self.ble_queue, 1.0,
                                     self.update)
        self.sensortag.daemon = True
        self.sensortag.set_logger("TI_Gateway")
        self.sensortag.start()

        # Create Switch items
        self.rest_switches.add_item("%s_device_switch_group" % self.prefix,
                                    "Group",
                                    "Group of Switches for %s" % self.prefix,
                                    "rest", "")
        self.rest_switches.add_item("%s_device_switch" % self.prefix, "Switch",
                                    "Switch for %s" % self.prefix, "rest",
                                    "%s_device_switch_group" % self.prefix)
        self.rest_switches.update_item_state("%s_device_switch" % self.prefix,
                                             "ON")

        self.rest_switches.start()

        # Create Value items
        self.rest_values.add_item("%s_values_group" % self.prefix, "Group",
                                  "Group of Values for %s" % self.prefix,
                                  "rest", "")
        for service_id in self.sensortag.get_service_ids():
            self.rest_values.add_item("%s_%s" % (self.prefix, service_id),
                                      "String", service_id, "rest",
                                      "%s_values" % self.prefix)
コード例 #4
0
def train(config):

    logger_config = {
        'owner': 'Training.train',
        'log_dir': config['log_dir'],
        'timezone': 'UTC',
    }
    logger = utils.Logger(logger_config)

    _trainer = get_trainer(config)

    checkpoints = sorted(glob.glob(_trainer.checkpoints_dir + '/*.pt'))
    print('checkpoints')

    if len(checkpoints):
        _trainer.load(checkpoints[-1])

    last_epoch = _trainer.current_epoch
    print('last_epoch')

    predicted_path = 'tmp/predicted-train.txt'
    # if shuffled or sorted by length -> should update target_path accordingly
    target_path = config['formulas_train_path']
    dot = target_path.rfind('.')
    target_path = target_path[:dot] + '_' + target_path[dot:]

    if _trainer.train_loader.shuffle or _trainer.train_loader.sort_by_formulas_len:
        f = open(target_path, 'w')
        for formula in _trainer.train_loader.formulas:
            joinedformula = ' '.join(token for token in formula)
            f.write(joinedformula + '\n')
        f.close()

    for epoch in range(last_epoch, config['epochs']):
        predictions, epoch_loss, epoch_acc = _trainer.train_one_epoch()
        checkpoint = _trainer.save(epoch + 1, epoch_loss, epoch_acc)
        bleu_message, edit_message = bleu_and_edit_distance(
            predictions, predicted_path, target_path)
        logger(bleu_message)
        logger(edit_message)
        evaluate('validation', config, checkpoint)
コード例 #5
0
def main(unused_args):
    output_dir = FLAGS.output_dir
    print('-' * 80)
    if not gfile.IsDirectory(output_dir):
        print('Path {} does not exist. Creating'.format(output_dir))
        gfile.MakeDirs(output_dir)
    elif FLAGS.reset_output_dir:
        print('Path {} exists. Reseting'.format(output_dir))
        gfile.DeleteRecursively(output_dir)
        gfile.MakeDirs(output_dir)

    print('-' * 80)
    log_file = os.path.join(output_dir, 'stdout')
    print('Logging to {}'.format(log_file))
    sys.stdout = utils.Logger(log_file)

    params = tf.contrib.training.HParams(
        data_path=FLAGS.data_path,
        log_every=FLAGS.log_every,
        output_dir=FLAGS.output_dir,
    )

    train(params)
コード例 #6
0
ファイル: main.py プロジェクト: aioz-ai/ICCV19_VQA-CTI
    parser.add_argument('--use_feature', default='bottom', type=str, help='use bottom-up feature or grid feature')

    # SAN
    parser.add_argument('--num_stacks', default=2, type=int,
                        help='num of stacks in Stack Attention Networks')


    args = parser.parse_args()
    return args


if __name__ == '__main__':
    args = parse_args()

    utils.create_dir(args.output)
    logger = utils.Logger(os.path.join(args.output, 'log.txt'))
    logger.write(args.__repr__())
    # Initializes the distributed backend which will take care of sychronizing nodes/GPUs
    device = torch.device("cuda:" + str(args.gpu) if args.gpu >= 0 else "cpu")
    args.device = device

    torch.manual_seed(args.seed)
    torch.cuda.manual_seed(args.seed)
    torch.backends.cudnn.benchmark = True
    torch.cuda.set_device(args.gpu)

    dictionary = Dictionary.load_from_file('data_v7w/dictionary.pkl')
    train_dset = V7WDataset('train', args, dictionary, adaptive=True, max_boxes=args.max_boxes,
                            question_len=args.question_len)
    val_dset = V7WDataset('val', args, dictionary, adaptive=True, max_boxes=args.max_boxes,
                          question_len=args.question_len)
コード例 #7
0
import sys
sys.path.append('.')  # if user is outside tests dir.
sys.path.append("..")  # if user is inside  tests dir

import unittest
from src import utils, other_ciphers

logger = utils.Logger(print)


def _test_cipher(self, encryptor, decryptor, _121=True):
    """
    Tests single cipher text algorithm for correctness.
    _121: whether algo gives one text op for one text ip.
    """
    strings = [utils.get_rand_ascii_str() for i in range(20)]
    strings += ['']
    for string in strings:
        ct = encryptor(string)
        pt = decryptor(ct)
        if _121:
            self.assertEqual(len(ct), len(string))
        self.assertEqual(pt, string.upper())


class Tests(unittest.TestCase):
    def test_shift_chars(self):
        # shift char gets the element after displacing elements with dx.
        ip_op = {('a', 2): 'c', ('b', -1): 'a', ('z', 3): 'c', ('c', -4): 'y'}

        for ip, op in ip_op.items():
コード例 #8
0
def train(args,
          model,
          train_loader,
          eval_loader,
          num_epochs,
          output,
          opt=None,
          s_epoch=0):
    device = args.device
    lr_default = args.lr
    lr_decay_step = 2
    lr_decay_rate = .25
    lr_decay_epochs = range(
        10, 20, lr_decay_step) if eval_loader is not None else range(
            10, 20, lr_decay_step)
    gradual_warmup_steps = [
        0.5 * lr_default, 1.0 * lr_default, 1.5 * lr_default, 2.0 * lr_default
    ]
    saving_epoch = 0
    grad_clip = args.clip_norm
    utils.create_dir(output)
    optim = torch.optim.Adamax(filter(lambda p: p.requires_grad, model.parameters()), lr=lr_default) \
        if opt is None else opt

    # Initial loss function
    criterion = torch.nn.BCEWithLogitsLoss(reduction='sum')

    logger = utils.Logger(os.path.join(output, 'log.txt'))
    logger.write(args.__repr__())
    best_eval_score = 0

    utils.print_model(model, logger)
    logger.write('optim: adamax lr=%.4f, decay_step=%d, decay_rate=%.2f, grad_clip=%.2f' % \
        (lr_default, lr_decay_step, lr_decay_rate, grad_clip))

    # Create trainer
    trainer = Trainer(args, model, criterion, optim)
    update_freq = int(args.update_freq)
    wall_time_start = time.time()
    for epoch in range(s_epoch, num_epochs):
        total_loss = 0
        train_score = 0
        total_norm = 0
        count_norm = 0
        num_updates = 0
        t = time.time()
        N = len(train_loader.dataset)
        num_batches = int(N / args.batch_size + 1)
        if epoch < len(gradual_warmup_steps):
            trainer.optimizer.param_groups[0]['lr'] = gradual_warmup_steps[
                epoch]
            logger.write('gradual warmup lr: %.8f' %
                         trainer.optimizer.param_groups[0]['lr'])
        elif epoch in lr_decay_epochs:
            trainer.optimizer.param_groups[0]['lr'] *= lr_decay_rate
            logger.write('decreased lr: %.8f' %
                         trainer.optimizer.param_groups[0]['lr'])
        else:
            logger.write('lr: %.8f' % trainer.optimizer.param_groups[0]['lr'])
        for i, (v, b, q, a, ans_mc, ans_gt) in enumerate(train_loader):
            v = v.to(device)
            b = b.to(device)
            q = q.to(device)
            a = a.to(device)
            ans_mc = ans_mc.to(device)

            # Clone each sample to 4 samples
            v = v.unsqueeze(1).expand(v.size(0), 4, v.size(1),
                                      v.size(2)).contiguous().view(
                                          v.size(0) * 4, v.size(1), v.size(2))
            q = q.unsqueeze(1).expand(q.size(0), 4,
                                      q.size(1)).contiguous().view(
                                          q.size(0) * 4, q.size(1))
            ans_mc = ans_mc.view(
                ans_mc.size(0) * ans_mc.size(1), ans_mc.size(2))
            a = a.view(ans_mc.size(0), 1)
            labels = torch.cat([a, 1 - a], 1)
            labels = labels.to(device)

            sample = [v, b, q, labels, ans_mc]
            if i < num_batches - 1 and (i + 1) % update_freq > 0:
                trainer.train_step(sample, update_params=False)
            else:
                loss, grad_norm, batch_score = trainer.train_step(
                    sample, update_params=True)
                total_norm += grad_norm
                count_norm += 1
                total_loss += loss.item()
                train_score += batch_score
                num_updates += 1
                if num_updates % int(args.print_interval / update_freq) == 0:
                    print(
                        "Iter: {}, Loss {:.4f}, Norm: {:.4f}, Total norm: {:.4f}, Num updates: {}, Wall time: {:.2f},"
                        " ETA: {}".format(i + 1,
                                          total_loss / ((num_updates + 1)),
                                          grad_norm, total_norm, num_updates,
                                          time.time() - wall_time_start,
                                          utils.time_since(t,
                                                           i / num_batches)))

        total_loss /= num_updates
        train_score = 100 * train_score / (num_updates * args.batch_size)
        if eval_loader is not None:
            print("Evaluating...")
            trainer.model.train(False)
            eval_score, bound = evaluate(model, eval_loader, args)
            trainer.model.train(True)

        logger.write('epoch %d, time: %.2f' % (epoch, time.time() - t))
        logger.write('\ttrain_loss: %.2f, norm: %.4f, score: %.2f' %
                     (total_loss, total_norm / count_norm, train_score))
        if eval_loader is not None:
            logger.write('\teval score: %.2f (%.2f)' %
                         (100 * eval_score, 100 * bound))

        # Save per epoch
        if epoch >= saving_epoch:
            model_path = os.path.join(output, 'model_epoch%d.pth' % epoch)
            utils.save_model(model_path, model, epoch, trainer.optimizer)
            # Save best epoch
            if eval_loader is not None and eval_score > best_eval_score:
                model_path = os.path.join(output, 'model_epoch_best.pth')
                utils.save_model(model_path, model, epoch, trainer.optimizer)
                best_eval_score = eval_score
コード例 #9
0
# Configurations
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
STATION_CONFIG_DIR = os.path.join(CURRENT_DIR, '../config/station_config.json')
FULL_STATION_CONFIG_DIR = os.path.join(CURRENT_DIR,
                                       '../config/full_station_config.json')
FORECAST_STATION_CONFIG_DIR = os.path.join(
    CURRENT_DIR, '../config/forecast_station_config.json')
DB_CONFIG_DIR = os.path.join(CURRENT_DIR, '../config/db_config.json')
WEATHER_CODE_DIR = os.path.join(CURRENT_DIR, '../config/weather_code.json')

# Constants
ERROR_FETCH = 1
SUCCESS = 0

# logger config in this module
_logger = utils.Logger(__name__)
_db_config = utils.parse_json_file(DB_CONFIG_DIR)
_station_config = utils.parse_json_file(STATION_CONFIG_DIR)
_full_station_config = utils.parse_json_file(FULL_STATION_CONFIG_DIR)
_forecast_config = utils.parse_json_file(FORECAST_STATION_CONFIG_DIR)


def fetch_weather_data_of_site(grid_id):
    """
    Deprecated. Fetch weather data of a site by the site's grid_id from hko
    :param grid_id: grid id of a station, you can find the grid id in the station_config.json file
    :return:
        a dict containing weather data, if return a string, it means an error occurs when requesting data
    """
    if not isinstance(grid_id, str):
        grid_id = str(grid_id)