stable-baselines3/tests/test_cnn.py

41 lines
1.2 KiB
Python
Raw Normal View History

2020-04-22 09:05:46 +00:00
import os
import numpy as np
2020-04-21 14:22:46 +00:00
import pytest
2020-05-05 13:02:35 +00:00
from stable_baselines3 import A2C, PPO, SAC, TD3
from stable_baselines3.common.identity_env import FakeImageEnv
2020-04-21 14:22:46 +00:00
2020-04-22 09:05:46 +00:00
SAVE_PATH = './cnn_model.zip'
@pytest.mark.parametrize('model_class', [A2C, PPO, SAC, TD3])
2020-04-21 14:22:46 +00:00
def test_cnn(model_class):
# Fake grayscale with frameskip
# Atari after preprocessing: 84x84x1, here we are using lower resolution
# to check that the network handle it automatically
env = FakeImageEnv(screen_height=40, screen_width=40, n_channels=1,
2020-04-23 13:18:21 +00:00
discrete=model_class not in {SAC, TD3})
2020-04-21 14:22:46 +00:00
if model_class in {A2C, PPO}:
kwargs = dict(n_steps=100)
else:
# Avoid memory error when using replay buffer
# Reduce the size of the features
2020-04-22 11:14:22 +00:00
kwargs = dict(buffer_size=250,
policy_kwargs=dict(features_extractor_kwargs=dict(features_dim=32)))
2020-04-22 09:05:46 +00:00
model = model_class('CnnPolicy', env, **kwargs).learn(250)
obs = env.reset()
action, _ = model.predict(obs, deterministic=True)
model.save(SAVE_PATH)
del model
model = model_class.load(SAVE_PATH)
# Check that the prediction is the same
assert np.allclose(action, model.predict(obs, deterministic=True)[0])
os.remove(SAVE_PATH)