Created
June 1, 2020 09:40
-
-
Save EXJUSTICE/91e5112867513c80e737e24d81591689 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Utility functions to enable video recording of gym environment and displaying it | |
To enable video, just do "env = wrap_env(env)"" | |
""" | |
def show_video(): | |
mp4list = glob.glob('vid/*.mp4') | |
if len(mp4list) > 0: | |
mp4 = mp4list[0] | |
video = io.open(mp4, 'r+b').read() | |
encoded = base64.b64encode(video) | |
ipythondisplay.display(HTML(data='''<video alt="test" autoplay | |
loop controls style="height: 400px;"> | |
<source src="data:video/mp4;base64,{0}" type="video/mp4" /> | |
</video>'''.format(encoded.decode('ascii')))) | |
else: | |
print("Could not find video") | |
def wrap_env(env): | |
env = Monitor(env, './vid',video_callable=lambda episode_id:True, force=True) | |
return env | |
env = gym.make('VizdoomBasic-v0') | |
environment = wrap_env(env) | |
done = False | |
observation = environment.reset() | |
new_observation = observation | |
prev_input = None | |
with tf.compat.v1.Session() as sess: | |
init.run() | |
observation, stacked_frames = stack_frames(stacked_frames, observation, True) | |
while True: | |
#set input to network to be difference image | |
#print(observation.shape) | |
# feed the game screen and get the Q values for each action | |
actions = mainQ_outputs.eval(feed_dict={X:[observation], in_training_mode:False}) | |
# get the action | |
action = np.argmax(actions, axis=-1) | |
actions_counter[str(action)] += 1 | |
# select the action using epsilon greedy policy | |
action = epsilon_greedy(action, global_step) | |
environment.render() | |
new_observation, stacked_frames = stack_frames(stacked_frames, new_observation, False) | |
observation = new_observation | |
# now perform the action and move to the next state, next_obs, receive reward | |
new_observation, reward, done, _ = environment.step(action) | |
img_array.append(observation) | |
if done: | |
#observation = env.reset() | |
break | |
environment.close() | |
show_video() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment