|
| 1 | +from cgi import test |
| 2 | +import os |
| 3 | +import gym |
| 4 | +import yaml |
| 5 | + |
| 6 | +from stable_baselines3 import PPO |
| 7 | +from stable_baselines3.common.monitor import Monitor |
| 8 | +from stable_baselines3.common.vec_env import DummyVecEnv, VecTransposeImage |
| 9 | +from scripts.network import NatureCNN |
| 10 | + |
| 11 | + |
| 12 | +# Load train environment configs |
| 13 | +with open('scripts/env_config.yml', 'r') as f: |
| 14 | + env_config = yaml.safe_load(f) |
| 15 | + |
| 16 | +# Load inference configs |
| 17 | +with open('config.yml', 'r') as f: |
| 18 | + config = yaml.safe_load(f) |
| 19 | + |
| 20 | +# Model name |
| 21 | +model_name = "best_model_" + config["test_mode"] |
| 22 | + |
| 23 | +# Determine input image shape |
| 24 | +image_shape = (50,50,1) if config["test_mode"]=="depth" else (50,50,3) |
| 25 | + |
| 26 | +# Create a DummyVecEnv |
| 27 | +env = DummyVecEnv([lambda: Monitor( |
| 28 | + gym.make( |
| 29 | + "scripts:test-env-v0", |
| 30 | + ip_address="127.0.0.1", |
| 31 | + image_shape=image_shape, |
| 32 | + # Train and test envs shares same config for the test |
| 33 | + env_config=env_config["TrainEnv"], |
| 34 | + input_mode=config["test_mode"], |
| 35 | + test_mode=config["test_type"] |
| 36 | + ) |
| 37 | +)]) |
| 38 | + |
| 39 | +# Wrap env as VecTransposeImage (Channel last to channel first) |
| 40 | +env = VecTransposeImage(env) |
| 41 | + |
| 42 | +policy_kwargs = dict(features_extractor_class=NatureCNN) |
| 43 | + |
| 44 | +# Load an existing model |
| 45 | +model = PPO.load( |
| 46 | + env=env, |
| 47 | + path=os.path.join("saved_policy", model_name), |
| 48 | + policy_kwargs=policy_kwargs |
| 49 | +) |
| 50 | + |
| 51 | +# Run the trained policy |
| 52 | +obs = env.reset() |
| 53 | +for i in range(2300): |
| 54 | + action, _ = model.predict(obs, deterministic=True) |
| 55 | + obs, _, dones, info = env.step(action) |
0 commit comments