def async_episode(best_model_num) -> tuple:
    logging.debug("episode process started")
    import tensorflow as tf
    import chess
    from MonteCarloTS import MonteCarloTS
    physical_devices = tf.config.list_physical_devices('GPU')
    if len(physical_devices) != 0:
        tf.config.experimental.set_memory_growth(physical_devices[0], True)
    logging.debug("tensorflow input processed")
    valids, states, improv_policy, win_loss = [], [], [], []
    best_model = CNNModel(best_model_num)
    logging.info("model initialised")
    best_model.load_weights()
    logging.debug("model loaded")
    board = chess.Board()
    logging.debug("chess board created")
    mcts = MonteCarloTS(board.copy(), best_model)
    logging.debug("mcts tree created")

    while not board.is_game_over() and board.fullmove_number < 150:
        move = mcts.search()
        board.push(move)
        print(move)
    reward_white = {"1-0": 1, "1/2-1/2": 0, "*": -1, "0-1": -1}
    logging.info(f'finished game with {board.result()}')
    for node in mcts.visited:
        policy = mcts.get_improved_policy(node, include_empty_spots=True)
        z = reward_white[board.result()]
        if node.state.board.turn == chess.BLACK:
            z *= -1
        states.append(node.state.get_representation())
        valids.append(node.state.get_valid_vector())
        improv_policy.append(policy)
        win_loss.append(z)
    return states, valids, improv_policy, win_loss
Beispiel #2
0
class ACAgent():
    def __init__(self, dir):
        super(ACAgent, self).__init__()
        self.Model = CNNModel()
        self.Model.load_weights(dir)

    def choice_action(self, state):
        data = self.Model(np.array(state)[np.newaxis, :].astype(np.float32))
        prob_weights = data[0].numpy()

        action = np.random.choice(range(prob_weights.shape[1]),
                                  p=prob_weights.ravel())

        return action
def async_arena(iteration, best_model_num, new_model_num):
    import tensorflow as tf
    physical_devices = tf.config.list_physical_devices('GPU')
    if len(physical_devices) != 0:
        tf.config.experimental.set_memory_growth(physical_devices[0], True)

    new_model_wins = 0
    board = chess.Board()
    best_model = CNNModel(best_model_num)
    best_model.load_weights()
    new_model = CNNModel(new_model_num)
    new_model.load_weights()
    mcts_best = MonteCarloTS(chess.Board(), best_model)
    mcts_new = MonteCarloTS(chess.Board(), new_model)
    if iteration % 2 == 0:
        turns = {"best": chess.WHITE, "new": chess.BLACK}
    else:
        turns = {"best": chess.BLACK, "new": chess.WHITE}
    while not board.is_game_over(
    ) and board.fullmove_number < 150 and not board.is_repetition(count=4):
        if turns["best"] == chess.WHITE:
            move = mcts_best.search(training=True)
            board.push(move)
            mcts_new.enemy_move(move)

            move = mcts_new.search(training=True)
            if move is None:
                break
            board.push(move)
            mcts_best.enemy_move(move)
        else:
            move = mcts_new.search(training=True)
            board.push(move)
            mcts_best.enemy_move(move)

            move = mcts_best.search(training=True)
            if move is None:
                break
            board.push(move)
            mcts_new.enemy_move(move)
    s = board.result()
    if s == "1-0" and turns["new"] == chess.WHITE:
        new_model_wins += 1
    elif s == "0-1" and turns["new"] == chess.BLACK:
        new_model_wins += 1
    if new_model_wins == 1:
        logging.info("new_model won")
    return new_model_wins
Beispiel #4
0
        flow_image_bgr_prev2 = flow_image_bgr_prev1
        flow_image_bgr_prev1 = flow_image_bgr_next

        count += 1
        sys.stdout.write('\rprocessed frames: %d of %d' % (count, num_frames))

    t2 = time.time()
    video_reader.release()
    video_writer.release()
    video_writer_combined.release()
    print(' Prediction completed !')
    print(' Time Taken:', (t2 - t1), 'seconds')

    predicted_labels[0] = predicted_labels[1]
    return predicted_labels


if __name__ == '__main__':

    model = CNNModel()
    model.load_weights(PRE_TRAINED_WEIGHTS)

    print('Testing model...')
    predicted_labels = predict_from_video(PATH_TEST_VIDEO,
                                          PATH_TEST_VIDEO_OUTPUT,
                                          PATH_COMBINED_TEST_VIDEO_OUTPUT)

    with open(PATH_TEST_LABEL, mode="w") as outfile:
        for label in predicted_labels:
            outfile.write("%s\n" % str(label))