def test_video(setup): import numpy as np def im(x, y): canvas = np.ones((640, 480), dtype=np.float32) * 0.001 for i in range(200): for j in range(200): if x - 5 < i < x + 5 and y - 5 < j < y + 5: canvas[i, j] = 1 return canvas frames = [im(100 + i, 80) for i in range(20)] logger.save_video(frames, "test_video.mp4")
def test_video_gif(setup): import numpy as np def im(x, y): canvas = np.zeros((200, 200)) for i in range(200): for j in range(200): if x - 5 < i < x + 5 and y - 5 < j < y + 5: canvas[i, j] = 1 return canvas frames = [im(100 + i, 80) for i in range(20)] logger.save_video(frames, "test_video.gif")
def test_video_gif(setup): import numpy as np def im(x, y): canvas = np.zeros((200, 200)) for i in range(200): for j in range(200): if x - 5 < i < x + 5 and y - 5 < j < y + 5: canvas[i, j] = 1 return canvas frames = [im(100 + i, 80) for i in range(20)] logger.save_video(frames, "test_video.gif") assert 'test_video.gif' in logger.glob('*.gif') logger.save_video(frames, "/tmp/ml-logger-test/videos/test_video.gif") assert 'ml-logger-test/videos/test_video.gif' in logger.glob( '**/videos/*.gif', wd="/tmp")
def evaluate(env, agent, num_episodes, save_video=None): from ml_logger import logger episode_rewards, frames = [], [] for i in trange(num_episodes, desc="Eval"): obs = env.reset() done = False episode_reward = 0 while not done: with utils.Eval(agent): action = agent.select_action(obs) obs, reward, done, _ = env.step(action) if save_video: frames.append(env.render('rgb_array', width=64, height=64)) episode_reward += reward if save_video: logger.save_video(frames, key=save_video) logger.store_metrics(episode_reward=episode_reward) episode_rewards.append(episode_reward) return np.mean(episode_rewards)