在强化学习中将渲染转换为小视频

Convert render to small video in Reinforcement learning

下面是模拟atari游戏的示例代码:

import numpy as np
import matplotlib.pyplot as plt
import gym
import time
env =gym.make('BreakoutNoFrameskip-v4')
print("Observation Space :",env.observation_space)
print("Action Space :",env.action_space)
env.reset()
for i in range(1000):
    action =env.action_space.sample()
    obs,reward,done,info =env.step(action)
    env.render()
    time.sleep(0.01)
    if done :
        env.reset()
env.close()
plt.show()

问题:

这会将每 video_every 集的视频保存到“视频”文件夹,同时保持 post 的屏幕渲染功能。

import gym
import time
env = gym.make('BreakoutNoFrameskip-v4')

video_every = 1
env = gym.wrappers.Monitor(env, "./video", video_callable=lambda episode_id: (episode_id%video_every)==0, force=True)

print("Observation Space :",env.observation_space)
print("Action Space :",env.action_space)
env.reset()
for i in range(1000):
    action =env.action_space.sample()
    obs,reward,done,info =env.step(action)
    env.render()
    time.sleep(0.01)
    if done :
        env.reset()
env.close()