示例#1
0
def valid_speaker(tok, val_envs):
    import tqdm
    listner = Seq2SeqAgent(None, "", tok, args.maxAction)
    speaker = Speaker(None, listner, tok)
    speaker.load(os.path.join(log_dir, 'state_dict', 'best_val_seen_bleu'))
    # speaker.load(os.path.join(log_dir, 'state_dict', 'best_val_unseen_loss'))

    for args.beam in [False, True]:
        print("Using Beam Search %s" % args.beam)
        for env_name, (env, evaluator) in val_envs.items():
            if env_name == 'train':
                continue
            print("............ Evaluating %s ............." % env_name)
            speaker.env = env
            path2inst, loss, word_accu, sent_accu = speaker.valid(
                beam=args.beam, wrapper=tqdm.tqdm)
            path_id = next(iter(path2inst.keys()))
            print("Inference: ", tok.decode_sentence(path2inst[path_id]))
            print("GT: ", evaluator.gt[path_id]['instructions'])
            bleu_score, precisions, _ = evaluator.bleu_score(path2inst)
            print(
                "Bleu, Loss, Word_Accu, Sent_Accu for %s is: %0.4f, %0.4f, %0.4f, %0.4f"
                % (env_name, bleu_score, loss, word_accu, sent_accu))
            print(
                "Bleu 1: %0.4f Bleu 2: %0.4f, Bleu 3 :%0.4f,  Bleu 4: %0.4f" %
                tuple(precisions))
            print("Average Length %0.4f" % utils.average_length(path2inst))
def food(game):
    """Return good moves towards food goodness is determined by the weighted value function."""
    moves = []

    snake_length_ratio = utils.average_length(
        snakes=game.snakes) / game.me.length()

    closest_foods = []
    # Only consider food we are the closest snake too
    for f in game.foods:
        min_dist = game.width * game.height
        for s in game.other_snakes:
            if s.head().distance(f) < min_dist:
                min_dist = s.head().distance(f)

        if game.me.head().distance(f) <= min_dist:
            closest_foods.append(f)

    if len(closest_foods) <= 0:
        closest_foods = game.foods

    def weighted_value(distance, health):
        distance_weight = 1 / distance
        health_weight = 1 / (health + 0.01)
        length_compare_weight = 1 if snake_length_ratio > 1 else 0

        if health < 50:
            health_weight += 0.20 * (50 - health)

        # Return a value between 0 and 1
        return (distance_weight + health_weight + length_compare_weight) / 3

    if len(closest_foods) <= 0:
        return moves

    # Get a list of distances to all foods
    food_distances = map(lambda c: game.me.head().distance(c), closest_foods)

    health = game.me.health_points

    for idx, d in enumerate(food_distances):
        # Find possible moves towards the foods
        #   i.e. if food is to the top right, possible moves are up and right
        mt = game.me.moves_to(closest_foods[idx])
        for m in mt:
            # Add possible move to possible moves with goodness from weighted values
            moves.append(Move(m, weighted_value(d, health), 'food'))

    return moves
示例#3
0
def value(game, distance):
    """Return a value representing how much we want to move towards the food.

    game: Game object
    distance: manhattan distance to food
    """
    health = game.me.health()

    avg_length = utils.average_length(snakes=game.snakes)

    if health > params.FOOD_THRESH and game.me.length() >= avg_length:
        return 0
    if health < distance:
        return 100

    distance_weight = 1 / distance
    health_weight = (100 / health) * params.FOOD_HEALTH_MULTI

    return distance_weight + health_weight
示例#4
0
def valid_speaker(tok, val_envs):
    import tqdm
    listner = Seq2SeqAgent(None, "", tok, args.maxAction)
    speaker = Speaker(None, listner, tok)
    speaker.load(args.load)

    for env_name, (env, evaluator) in val_envs.items():
        if env_name == 'train':
            continue
        print("............ Evaluating %s ............." % env_name)
        speaker.env = env
        path2inst, loss, word_accu, sent_accu = speaker.valid(wrapper=tqdm.tqdm)
        path_id = next(iter(path2inst.keys()))
        print("Inference: ", tok.decode_sentence(path2inst[path_id]))
        print("GT: ", evaluator.gt[path_id]['instructions'])
        pathXinst = list(path2inst.items())
        name2score = evaluator.lang_eval(pathXinst, no_metrics={'METEOR'})
        score_string = " "
        for score_name, score in name2score.items():
            score_string += "%s_%s: %0.4f " % (env_name, score_name, score)
        print("For env %s" % env_name)
        print(score_string)
        print("Average Length %0.4f" % utils.average_length(path2inst))
示例#5
0
def create_augment_data():
    setup()

    # Create a batch training environment that will also preprocess text
    vocab = read_vocab(TRAIN_VOCAB)
    tok = Tokenizer(vocab=vocab, encoding_length=args.maxInput)

    # Load features
    feat_dict = read_img_features(features)
    candidate_dict = utils.read_candidates(CANDIDATE_FEATURES)

    # The datasets to be augmented
    print("Start to augment the data")
    aug_envs = []
    # aug_envs.append(
    #     R2RBatch(
    #         feat_dict, candidate_dict, batch_size=args.batchSize, splits=['train'], tokenizer=tok
    #     )
    # )
    # aug_envs.append(
    #     SemiBatch(False, 'tasks/R2R/data/all_paths_46_removetrain.json',
    #         feat_dict, candidate_dict, batch_size=args.batchSize, splits=['train', 'val_seen'], tokenizer=tok)
    # )
    aug_envs.append(
        SemiBatch(False,
                  'tasks/R2R/data/all_paths_46_removevalunseen.json',
                  "unseen",
                  feat_dict,
                  candidate_dict,
                  batch_size=args.batchSize,
                  splits=['val_unseen'],
                  tokenizer=tok))
    aug_envs.append(
        SemiBatch(False,
                  'tasks/R2R/data/all_paths_46_removetest.json',
                  "test",
                  feat_dict,
                  candidate_dict,
                  batch_size=args.batchSize,
                  splits=['test'],
                  tokenizer=tok))
    # aug_envs.append(
    #     R2RBatch(
    #         feat_dict, candidate_dict, batch_size=args.batchSize, splits=['val_seen'], tokenizer=tok
    #     )
    # )
    # aug_envs.append(
    #     R2RBatch(
    #         feat_dict, candidate_dict, batch_size=args.batchSize, splits=['val_unseen'], tokenizer=tok
    #     )
    # )

    for snapshot in os.listdir(os.path.join(log_dir, 'state_dict')):
        # if snapshot != "best_val_unseen_bleu":  # Select a particular snapshot to process. (O/w, it will make for every snapshot)
        if snapshot != "best_val_unseen_bleu":
            continue

        # Create Speaker
        listner = Seq2SeqAgent(aug_envs[0], "", tok, args.maxAction)
        speaker = Speaker(aug_envs[0], listner, tok)

        # Load Weight
        load_iter = speaker.load(os.path.join(log_dir, 'state_dict', snapshot))
        print("Load from iter %d" % (load_iter))

        # Augment the env from aug_envs
        for aug_env in aug_envs:
            speaker.env = aug_env

            # Create the aug data
            import tqdm
            path2inst = speaker.get_insts(beam=args.beam, wrapper=tqdm.tqdm)
            data = []
            for datum in aug_env.fake_data:
                datum = datum.copy()
                path_id = datum['path_id']
                if path_id in path2inst:
                    datum['instructions'] = [
                        tok.decode_sentence(path2inst[path_id])
                    ]
                    datum.pop('instr_encoding')  # Remove Redundant keys
                    datum.pop('instr_id')
                    data.append(datum)

            print("Totally, %d data has been generated for snapshot %s." %
                  (len(data), snapshot))
            print("Average Length %0.4f" % utils.average_length(path2inst))
            print(datum)  # Print a Sample

            # Save the data
            import json
            os.makedirs(os.path.join(log_dir, 'aug_data'), exist_ok=True)
            beam_tag = "_beam" if args.beam else ""
            json.dump(data,
                      open(
                          os.path.join(
                              log_dir, 'aug_data', '%s_%s%s.json' %
                              (snapshot, aug_env.name, beam_tag)), 'w'),
                      sort_keys=True,
                      indent=4,
                      separators=(',', ': '))