Views
No views yet
339.0gym==0.19 since it includes Atari Roms.1# Import the libraries
2import os
3
4import gym
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("ThomasSimonini/ppo-BreakoutNoFrameskip-v4", "ppo-BreakoutNoFrameskip-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('BreakoutNoFrameskip-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 gym
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": "BreakoutNoFrameskip-v4",
15 "num_envs": 8,
16 "total_timesteps": int(10e6),
17 "seed": 661550378,
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"]) #BreakoutNoFrameskip-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
40model = PPO(policy = "CnnPolicy",
41 env = env,
42 batch_size = 256,
43 clip_range = 0.1,
44 ent_coef = 0.01,
45 gae_lambda = 0.9,
46 gamma = 0.99,
47 learning_rate = 2.5e-4,
48 max_grad_norm = 0.5,
49 n_epochs = 4,
50 n_steps = 128,
51 vf_coef = 0.5,
52 tensorboard_log = f"runs",
53 verbose=1,
54 )
55
56model.learn(
57 total_timesteps = config["total_timesteps"],
58 callback = [
59 WandbCallback(
60 gradient_save_freq = 1000,
61 model_save_path = f"models/{run.id}",
62 ),
63 CheckpointCallback(save_freq=10000, save_path='./breakout',
64 name_prefix=config["env_name"]),
65 ]
66)
67
68model.save("ppo-BreakoutNoFrameskip-v4.zip")
69push_to_hub(repo_id="ThomasSimonini/ppo-BreakoutNoFrameskip-v4",
70 filename="ppo-BreakoutNoFrameskip-v4.zip",
71 commit_message="Added Breakout trained agent")