Пример #1
0
def main(replays_folder, output_folder, player, max_replays, double_input,
         prev_frame):

    if replays_folder[-1] == '/':
        replays_folder = replays_folder[:-1]

    out_dir = "{}/{}".format(output_folder, replays_folder.split('/')[-1])
    os.mkdir(out_dir)

    np.random.seed(0)

    replay_files = [
        fname for fname in os.listdir(replays_folder) if fname[-4:] == '.hlt'
    ]
    # replay_files = replay_files[:50]
    n_replays = len(replay_files)

    progbar = ProgressBar(n_replays)

    print('Selecting winning replays')

    selected = []

    for replay_name in replay_files:

        progbar.update()

        replay = json.load(open('{}/{}'.format(replays_folder, replay_name)))

        frames = get_frames(replay)

        target_id = get_winner(frames)
        if target_id == 0: continue
        if player not in replay['player_names'][target_id - 1].lower():
            continue

        selected.append(replay_name)

        del replay

    print("{}/{} replays have been selected.".format(len(selected), n_replays))

    print('Shuffling Replays')

    np.random.shuffle(selected)

    print("Computing chunk sizes")

    chunks = get_chunks(len(selected), max_replays)

    print("Chunk sizes will be: {}".format(', '.join(
        [str(chunk) for chunk in chunks])))

    print('Processing replays')

    progbar2 = ProgressBar(len(selected))

    cursor = 0
    for i_chunk, chunk in enumerate(chunks):
        # print("Chunk {}".format(i_chunk))
        # print("{} to {}".format(cursor,cursor+chunk))

        training_input = []
        training_target = []

        if double_input:
            training_input_alt = []

        if prev_frame:
            training_input_prev = []

        test_input = []
        test_target = []

        if double_input:
            test_input_alt = []

        if prev_frame:
            test_input_prev = []

        for i, replay_name in enumerate(selected[cursor:cursor + chunk]):

            progbar2.update()

            is_training = i < 0.8 * chunk

            replay = json.load(
                open('{}/{}'.format(replays_folder, replay_name)))
            frames = get_frames(replay)
            target_id = get_winner(frames)

            moves = np.array(replay['moves'])

            is_player = frames[:, :, :, 0] == target_id
            filtered_moves = np.where(is_player[:-1], moves,
                                      np.zeros_like(moves))
            categorical_moves = (
                np.arange(5) == filtered_moves[:, :, :, None]).astype(int)

            # wrapped_frames = np.empty(shape=(frames.shape[0],input_shape[0],input_shape[1],frames.shape[3]))
            # wrapped_moves = np.empty(shape=(categorical_moves.shape[0],input_shape[0],input_shape[1],categorical_moves.shape[3]))

            iframes = np.empty(shape=frames.shape[:3] + (4, ))

            iframes[:, :, :, 0] = frames[:, :, :, 0] == target_id
            iframes[:, :, :, 1] = (frames[:, :, :, 0] !=
                                   target_id) & (frames[:, :, :, 0] != 0)
            iframes[:, :, :, 2] = frames[:, :, :, 1] / 20.
            iframes[:, :, :, 3] = frames[:, :, :, 2] / 255.

            for j, (iframe, move) in enumerate(zip(iframes,
                                                   categorical_moves)):
                centroid = get_centroid(iframe)
                wframe = center_frame(iframe, centroid, wrap_size=input_shape)
                wmoves = center_frame(move, centroid, wrap_size=input_shape)

                if double_input:
                    prev_move = categorical_moves[j - 1] if j > 0 else move
                    wprev_move = center_frame(prev_move,
                                              centroid,
                                              wrap_size=input_shape)

                if prev_frame:
                    prev_iframe = iframes[j - 1] if j > 0 else iframe
                    wprev_iframe = center_frame(prev_iframe,
                                                centroid,
                                                wrap_size=input_shape)

                if is_training:
                    if double_input:
                        training_input_alt.append(wprev_move)
                    if prev_frame:
                        training_input_prev.append(wprev_iframe)
                    training_input.append(wframe)
                    training_target.append(wmoves)
                else:
                    if double_input:
                        test_input_alt.append(wprev_move)
                    if prev_frame:
                        test_input_prev.append(wprev_iframe)
                    test_input.append(wframe)
                    test_target.append(wmoves)

            del replay

        np.save("{}/training_input_{}.npy".format(out_dir, i_chunk),
                training_input)
        np.save("{}/training_target_{}.npy".format(out_dir, i_chunk),
                training_target)
        np.save("{}/test_input_{}.npy".format(out_dir, i_chunk), test_input)
        np.save("{}/test_target_{}.npy".format(out_dir, i_chunk), test_target)

        if double_input:
            np.save("{}/training_input_alt_{}.npy".format(out_dir, i_chunk),
                    training_input_alt)
            np.save("{}/test_input_alt_{}.npy".format(out_dir, i_chunk),
                    test_input_alt)

        if prev_frame:
            np.save("{}/training_input_prev_{}.npy".format(out_dir, i_chunk),
                    training_input_prev)
            np.save("{}/test_input_prev_{}.npy".format(out_dir, i_chunk),
                    test_input_prev)

        cursor += chunk
Пример #2
0
    model.add(Convolution2D(64, 5, 5, activation='relu', border_mode='same'))
    model.add(Convolution2D(5, 5, 5, activation='relu', border_mode='same'))

    opt = RMSprop(lr=0.00001)
    model.compile(loss='mse', optimizer=opt, metrics=['accuracy'])

n_replays = len(os.listdir(REPLAY_FOLDER))
print('Loading')
for i, replay_name in enumerate(os.listdir(REPLAY_FOLDER)):
    if i <= 120:  # or i>600:
        continue
    if replay_name[-4:] != '.hlt': continue
    print('Loading {}/{}'.format(i + 1, n_replays))
    replay = json.load(open('{}/{}'.format(REPLAY_FOLDER, replay_name)))

    frames = get_frames(replay)

    player = frames[:, :, :, 0]
    players, counts = np.unique(player[-1], return_counts=True)
    target_id = players[counts.argmax()]
    if target_id == 0: continue
    if 'nmalaguti' not in replay['player_names'][target_id - 1].lower():
        print "not using replay..."
        continue
    print "using replay"

    moves = np.array(replay['moves'])

    is_player = frames[:, :, :, 0] == target_id
    filtered_moves = np.where(is_player[:-1], moves, np.zeros_like(moves))
    categorical_moves = (np.arange(5) == filtered_moves[:, :, :,
Пример #3
0
split_ratio = 0.8

input_ = []
target_ = []

np.random.seed(0)

n_replays = len(os.listdir(REPLAY_FOLDER))
for i, replay_name in enumerate(os.listdir(REPLAY_FOLDER)):
    if i > 300:
        break
    if replay_name[-4:] != '.hlt': continue
    print('Loading {}/{}'.format(i + 1, n_replays))
    replay = json.load(open('{}/{}'.format(REPLAY_FOLDER, replay_name)))

    frames = mlutils.get_frames(replay)

    player = frames[:, :, :, 0]
    players, counts = np.unique(player[-1], return_counts=True)
    target_id = players[counts.argmax()]
    if target_id == 0: continue
    # if 'erdman' not in replay['player_names'][target_id-1].lower():
    #     continue
    n_max = min(len(replay['frames']), 20)
    frames = frames[:n_max]

    moves = np.array(replay['moves'])[:n_max - 1]

    is_player = frames[:, :, :, 0] == target_id
    filtered_moves = np.where(is_player[:-1], moves, np.zeros_like(moves))
    categorical_moves = (np.arange(5) == filtered_moves[:, :, :,