evaluation test Parameters ---------- model: any models for evaluation counter: int current training step counter test_episodes: int number of test episodes device: str 'cuda' or 'cpu' render: bool True -> render the image during ev
(config, model, counter, test_episodes, device, render, save_video=False, final_test=False, use_pb=False)
| 50 | |
| 51 | |
| 52 | def test(config, model, counter, test_episodes, device, render, save_video=False, final_test=False, use_pb=False): |
| 53 | """evaluation test |
| 54 | Parameters |
| 55 | ---------- |
| 56 | model: any |
| 57 | models for evaluation |
| 58 | counter: int |
| 59 | current training step counter |
| 60 | test_episodes: int |
| 61 | number of test episodes |
| 62 | device: str |
| 63 | 'cuda' or 'cpu' |
| 64 | render: bool |
| 65 | True -> render the image during evaluation |
| 66 | save_video: bool |
| 67 | True -> save the videos during evaluation |
| 68 | final_test: bool |
| 69 | True -> this test is the final test, and the max moves would be 108k/skip |
| 70 | use_pb: bool |
| 71 | True -> use tqdm bars |
| 72 | """ |
| 73 | model.to(device) |
| 74 | model.eval() |
| 75 | save_path = os.path.join(config.exp_path, 'recordings', 'step_{}'.format(counter)) |
| 76 | |
| 77 | with torch.no_grad(): |
| 78 | # new games |
| 79 | envs = [config.new_game(seed=i, save_video=save_video, save_path=save_path, test=True, final_test=final_test, |
| 80 | video_callable=lambda episode_id: True, uid=i) for i in range(test_episodes)] |
| 81 | max_episode_steps = envs[0].get_max_episode_steps() |
| 82 | if use_pb: |
| 83 | pb = tqdm(np.arange(max_episode_steps), leave=True) |
| 84 | # initializations |
| 85 | init_obses = [env.reset() for env in envs] |
| 86 | dones = np.array([False for _ in range(test_episodes)]) |
| 87 | game_histories = [GameHistory(envs[_].env.action_space, max_length=max_episode_steps, config=config) for _ in range(test_episodes)] |
| 88 | for i in range(test_episodes): |
| 89 | game_histories[i].init([init_obses[i] for _ in range(config.stacked_observations)]) |
| 90 | |
| 91 | step = 0 |
| 92 | ep_ori_rewards = np.zeros(test_episodes) |
| 93 | ep_clip_rewards = np.zeros(test_episodes) |
| 94 | # loop |
| 95 | while not dones.all(): |
| 96 | if render: |
| 97 | for i in range(test_episodes): |
| 98 | envs[i].render() |
| 99 | |
| 100 | if config.image_based: |
| 101 | stack_obs = [] |
| 102 | for game_history in game_histories: |
| 103 | stack_obs.append(game_history.step_obs()) |
| 104 | stack_obs = prepare_observation_lst(stack_obs) |
| 105 | stack_obs = torch.from_numpy(stack_obs).to(device).float() / 255.0 |
| 106 | else: |
| 107 | stack_obs = [game_history.step_obs() for game_history in game_histories] |
| 108 | stack_obs = torch.from_numpy(np.array(stack_obs)).to(device) |
| 109 |
no test coverage detected