Lesson 5 of 7 · 2 min
Let it drive
The honest score is not accuracy but how far it gets.
# run locally: drive.py
import gymnasium as gym, numpy as np, torch
ACTIONS = {0: [0, 0, 0], 1: [-1, 0, 0], 2: [1, 0, 0], 3: [0, 1, 0], 4: [0, 0, 0.8]} # same as record.py
net = torch.load("driver.pt", weights_only=False).eval()
env = gym.make("CarRacing-v3", render_mode="human")
scores = []
for seed in range(100, 105):
obs, _ = env.reset(seed=seed)
total = 0
for _ in range(1000):
frame = torch.tensor(obs[:84].mean(axis=2)).float()[None, None] / 255
with torch.no_grad():
a = int(net(frame).argmax())
obs, reward, done, cut, _ = env.step(np.array(ACTIONS[a], dtype=np.float32))
total += reward
if done or cut:
break
scores.append(total)
print(seed, round(total))
print("average over unseen tracks:", round(sum(scores) / len(scores)))