天天看點

jupyter notebook中使用gym

直接使用env.render()

In [ ]:

import gym
env = gym.make("MountainCar-v0")
env.reset()
for _ in range(1000):
    env.render()
    env.step(env.action_space.sample())
env.close()
           

使用matplotlib

import gym
import matplotlib.pyplot as plt
%matplotlib inline

env = gym.make('MountainCar-v0')
render = lambda :plt.imshow(env.render(mode='rgb_array'))
env.reset()
render()
# arr = env.render(mode='rgb_array')
# plt.imshow(arr)  or  scipy.misc.imsave('sample.png', arr)
           

在一個單元中多次渲染

import gym
from IPython import display
import matplotlib.pyplot as plt
%matplotlib inline

env = gym.make('MountainCar-v0')
env.reset()
for _ in range(100):
    plt.imshow(env.render(mode='rgb_array'))
    display.display(plt.gcf())
    display.clear_output(wait=True)
    action = env.action_space.sample()
    env.step(action)
           

提高效率

import gym
from IPython import display
import matplotlib.pyplot as plt
%matplotlib inline
env = gym.make('MountainCar-v0')
env.reset()
img = plt.imshow(env.render(mode='rgb_array')) #only call this once
for _ in range(100):
    img.set_data(env.render(mode='rgb_array')) #just update the data
    display.display(plt.gcf())
    display.clear_output(wait=True)
    action = env.action_space.sample()
    env.step(action)
           

使用PIL.Image

import gym
from IPython import display
import matplotlib.pyplot as plt
%matplotlib inline

env = gym.make('MountainCar-v0')
env.reset()

import PIL.Image
import io, time
import numpy as np

def show_array(a, fmt='png'):
    a = np.uint8(a)
    f = io.BytesIO()
    ima = PIL.Image.fromarray(a).save(f, fmt)
    return f.getvalue()

data = show_array(env.render(mode='rgb_array'))
image_handle = display.display(display.Image(data=data,width=450),display_id='gymscr')

while True:
    time.sleep(0.01)
    env.step(env.action_space.sample())
    data = show_array(env.render(mode='rgb_array'))
    display.update_display(display.Image(data=data,width=450),display_id='gymscr')
           

使用wrapper的monitor捕獲

# 依賴安裝
!apt install python-opengl
!apt install ffmpeg
!apt install xvfb
!pip3 install pyvirtualdisplay
           
# Virtual display
from pyvirtualdisplay import Display

virtual_display = Display(visible=0, size=(1400, 900))
virtual_display.start()
           
import gym
from gym import wrappers

env = gym.make("MountainCar-v0")
env = wrappers.Monitor(env, "./MountainCar-v0")

for episode in range(2):
    observation = env.reset()
    step = 0
    total_reward = 0

    while True:
        step += 1
        env.render()
        action = env.action_space.sample()
        observation, reward, done, info = env.step(action)
        total_reward += reward
        if done:
            print("Episode: {0},\tSteps: {1},\tscore: {2}"
                  .format(episode, step, total_reward)
            )
            break
env.close()
           
import os
import io
import base64
from IPython.display import display, HTML

def ipython_show_video(path):
    """Show a video at `path` within IPython Notebook
    """
    if not os.path.isfile(path):
        raise NameError("Cannot access: {}".format(path))

    video = io.open(path, 'r+b').read()
    encoded = base64.b64encode(video)

    display(HTML(
        data="""
        <video alt="test" controls>
        <source src="data:video/mp4;base64,{0}" type="video/mp4" />
        </video>
        """.format(encoded.decode('ascii'))
    ))

ipython_show_video("./MountainCar-v0/openaigym.video.0.5776.video000000.mp4")