How to run OpenAI Gym .render() over a server

前端 未结 15 2245
囚心锁ツ
囚心锁ツ 2020-12-12 10:07

I am running a python 2.7 script on a p2.xlarge AWS server through Jupyter (Ubuntu 14.04). I would like to be able to render my simulations.

Minimal working example<

15条回答
  •  渐次进展
    2020-12-12 10:48

    Referencing my other answer here: Display OpenAI gym in Jupyter notebook only

    I made a quick working example here which you could fork: https://kyso.io/eoin/openai-gym-jupyter with two examples of rendering in Jupyter - one as an mp4, and another as a realtime gif.

    The .mp4 example is quite simple.

    import gym
    from gym import wrappers
    
    env = gym.make('SpaceInvaders-v0')
    env = wrappers.Monitor(env, "./gym-results", force=True)
    env.reset()
    for _ in range(1000):
        action = env.action_space.sample()
        observation, reward, done, info = env.step(action)
        if done: break
    env.close()
    

    Then in a new cell Jupyter cell, or download it from the server onto some place where you can view the video.

    import io
    import base64
    from IPython.display import HTML
    
    video = io.open('./gym-results/openaigym.video.%s.video000000.mp4' % env.file_infix, 'r+b').read()
    encoded = base64.b64encode(video)
    HTML(data='''
        '''
    .format(encoded.decode('ascii')))
    

    If your on a server with public access you could run python -m http.server in the gym-results folder and just watch the videos there.

提交回复
热议问题