if square.strength <= 6 * square.production: d = STILL elif new_square.owner != my_id and new_square.strength > square.strength: d = STILL else: d = wanted_d moves.append(Move(square, d)) else: wframe = mlutils.center_frame(iframe, centroid, wrap_size=input_shape) moves_pred = model.predict(np.array([wframe])) moves_pred[:, :, :, 0] /= 5. moves_mat = np.argmax(moves_pred, axis=3)[0] moves_mat_rebased = rebase_moves(moves_mat, centroid, input_shape, frame.shape) moves = [ Move(square, (moves_mat_rebased[square.y, square.x] - 1) % 5) for square in gamemap if square.owner == my_id ]
while True: turn += 1 gamemap.get_frame() frame = gamemap_to_frame(gamemap) iframe = np.empty(shape=frame.shape[:2] + (4, )) iframe[:, :, 0] = frame[:, :, 0] == my_id iframe[:, :, 1] = (frame[:, :, 0] != my_id) & (frame[:, :, 0] != 0) iframe[:, :, 2] = frame[:, :, 1] / 20. iframe[:, :, 3] = frame[:, :, 2] / 255. centroid = mlutils.get_centroid(iframe) wframe = mlutils.center_frame(iframe, centroid, wrap_size=input_shape) prev_wmoves = mlutils.center_frame(prev_moves, centroid, wrap_size=input_shape) moves_pred = model.predict( [np.array([wframe]), np.array([prev_wmoves])]) moves_pred[:, :, :, 0] /= 100. moves_mat = np.argmax(moves_pred, axis=3)[0] moves_mat_rebased = rebase_moves(moves_mat, centroid, input_shape, frame.shape) moves_map = { square: (moves_mat_rebased[square.y, square.x] - 1) % 5
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
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 i, (frame, move) in enumerate(zip(iframes, categorical_moves)): centroid = get_centroid(frame) wframe = center_frame(frame, centroid, wrap_size=input_shape) wmoves = center_frame(move, centroid, wrap_size=input_shape) training_input.append(wframe) training_target.append(wmoves) print('Loaded') now = datetime.datetime.now() tensorboard = TensorBoard(log_dir='./logs/' + now.strftime('%Y.%m.%d %H.%M')) training_input = np.array(training_input) training_target = np.array(training_target) indices = np.arange(len(training_input)) np.random.shuffle(indices) #shuffle training samples training_input = training_input[indices] training_target = training_target[indices]
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 i, (frame, move) in enumerate(zip(iframes, categorical_moves)): centroid = mlutils.get_centroid(frame) wframe = mlutils.center_frame(frame, centroid, wrap_size=input_shape) wmoves = mlutils.center_frame(move, centroid, wrap_size=input_shape) input_.append(wframe) target_.append(wmoves) input_ = np.array(input_) target_ = np.array(target_) indices = np.arange(len(input_)) np.random.shuffle(indices) #shuffle training samples input_ = input_[indices] target_ = target_[indices] total_size = input_.shape[0] train_input = input_[:int(split_ratio * total_size)] test_input = input_[int(split_ratio * total_size):]