Views
No views yet
20.3 +/- 0.0gymnasium==0.29.1 since it includes Atari Roms.1# Import the libraries
2import os
3
4import gymnasium
5
6from stable_baselines3 import PPO
7from stable_baselines3.common.vec_env import VecNormalize
8
9from stable_baselines3.common.env_util import make_atari_env
10from stable_baselines3.common.vec_env import VecFrameStack
11
12from huggingface_sb3 import load_from_hub, push_to_hub
13
14# Load the model
15checkpoint = load_from_hub("tk-42/ppo-PongNoFrameskip-v4", "ppo-PongNoFrameskip-v4.zip")
16
17# Because we using 3.7 on Colab and this agent was trained with 3.8 to avoid Pickle errors:
18custom_objects = {
19 "learning_rate": 0.0,
20 "lr_schedule": lambda _: 0.0,
21 "clip_range": lambda _: 0.0,
22 }
23
24model= PPO.load(checkpoint, custom_objects=custom_objects)
25
26env = make_atari_env('PongNoFrameskip-v4', n_envs=1)
27env = VecFrameStack(env, n_stack=4)
28
29obs = env.reset()
30while True:
31 action, _states = model.predict(obs)
32 obs, rewards, dones, info = env.step(action)
33 env.render()1import wandb
2import gymnasium
3
4from stable_baselines3 import PPO
5from stable_baselines3.common.env_util import make_atari_env
6from stable_baselines3.common.vec_env import VecFrameStack, VecVideoRecorder
7from stable_baselines3.common.callbacks import CheckpointCallback
8
9from wandb.integration.sb3 import WandbCallback
10
11from huggingface_sb3 import load_from_hub, push_to_hub
12
13config = {
14 "env_name": "PongNoFrameskip-v4",
15 "num_envs": 8,
16 "total_timesteps": int(10e6),
17 "seed": 4089164106,
18}
19
20run = wandb.init(
21 project="HFxSB3",
22 config = config,
23 sync_tensorboard = True, # Auto-upload sb3's tensorboard metrics
24 monitor_gym = True, # Auto-upload the videos of agents playing the game
25 save_code = True, # Save the code to W&B
26 )
27
28# There already exists an environment generator
29# that will make and wrap atari environments correctly.
30# Here we are also multi-worker training (n_envs=8 => 8 environments)
31env = make_atari_env(config["env_name"], n_envs=config["num_envs"], seed=config["seed"]) #PongNoFrameskip-v4
32
33print("ENV ACTION SPACE: ", env.action_space.n)
34
35# Frame-stacking with 4 frames
36env = VecFrameStack(env, n_stack=4)
37# Video recorder
38env = VecVideoRecorder(env, "videos", record_video_trigger=lambda x: x % 100000 == 0, video_length=2000)
39
40# https://github.com/DLR-RM/rl-trained-agents/blob/10a9c31e806820d59b20d8b85ca67090338ea912/ppo/PongNoFrameskip-v4_1/PongNoFrameskip-v4/config.yml
41model = PPO(policy = "CnnPolicy",
42 env = env,
43 batch_size = 256,
44 clip_range = 0.1,
45 ent_coef = 0.01,
46 gae_lambda = 0.9,
47 gamma = 0.99,
48 learning_rate = 2.5e-4,
49 max_grad_norm = 0.5,
50 n_epochs = 4,
51 n_steps = 128,
52 vf_coef = 0.5,
53 tensorboard_log = f"runs",
54 verbose=1,
55 )
56
57model.learn(
58 total_timesteps = config["total_timesteps"],
59 callback = [
60 WandbCallback(
61 gradient_save_freq = 1000,
62 model_save_path = f"models/{run.id}",
63 ),
64 CheckpointCallback(save_freq=10000, save_path='./pong',
65 name_prefix=config["env_name"]),
66 ]
67)
68
69model.save("ppo-PongNoFrameskip-v4.zip")
70push_to_hub(repo_id="tk-42/ppo-PongNoFrameskip-v4",
71 filename="ppo-PongNoFrameskip-v4.zip",
72 commit_message="Added Pong trained agent")