getting training and testing loop working, adding tqdm to simulator, and reduced number of frames, updating readme
This commit is contained in:
16
README.md
16
README.md
@@ -24,8 +24,22 @@ Once the repository has been set up, you can process and save expert track demon
|
|||||||
```
|
```
|
||||||
python src/expert_data.py --loc [LOCNUM] --track [TRACKNUM]
|
python src/expert_data.py --loc [LOCNUM] --track [TRACKNUM]
|
||||||
```
|
```
|
||||||
|
You can (and should) process all tracks at once at location 0 with:
|
||||||
|
```
|
||||||
|
python src/expert_data.py --all-tracks
|
||||||
|
```
|
||||||
|
|
||||||
You can then load the experts actions and observations using
|
You can then train a default behavior cloning policy with the following. Be sure to check help for main.py for running options.
|
||||||
|
```
|
||||||
|
python src/main.py --train
|
||||||
|
```
|
||||||
|
You can then test the learned policy with the following, and see the animation file in `output/`:
|
||||||
|
```
|
||||||
|
python src/main.py --test
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
You can load the experts actions manually
|
||||||
```
|
```
|
||||||
from src import expert_data
|
from src import expert_data
|
||||||
observations, actions = expert_data.load_expert_data(loc = [LOCNUM], track = [TRACKNUM])
|
observations, actions = expert_data.load_expert_data(loc = [LOCNUM], track = [TRACKNUM])
|
||||||
|
|||||||
@@ -3,3 +3,4 @@ torch
|
|||||||
sklearn
|
sklearn
|
||||||
pytest
|
pytest
|
||||||
json5
|
json5
|
||||||
|
tqdm
|
||||||
13
src/bc/bc.py
13
src/bc/bc.py
@@ -115,8 +115,9 @@ def generate_transforms(dataset):
|
|||||||
def train(train_dataset, cv_dataset, policy, filestr, **kwargs):
|
def train(train_dataset, cv_dataset, policy, filestr, **kwargs):
|
||||||
|
|
||||||
# hyperparams
|
# hyperparams
|
||||||
train_epochs = 10000
|
train_epochs = 100
|
||||||
cv_every = 100
|
cv_every = 10
|
||||||
|
epoch_every = 1
|
||||||
train_batch_size = 64
|
train_batch_size = 64
|
||||||
cv_batch_size = 256 # doesn't matter
|
cv_batch_size = 256 # doesn't matter
|
||||||
learning_rate = 1e-3
|
learning_rate = 1e-3
|
||||||
@@ -138,8 +139,7 @@ def train(train_dataset, cv_dataset, policy, filestr, **kwargs):
|
|||||||
# generate loss function, optimizer
|
# generate loss function, optimizer
|
||||||
loss_fn = nn.HuberLoss(reduction='sum')
|
loss_fn = nn.HuberLoss(reduction='sum')
|
||||||
optimizer = torch.optim.Adam(policy.parameters(), lr=learning_rate, weight_decay=weight_decay)
|
optimizer = torch.optim.Adam(policy.parameters(), lr=learning_rate, weight_decay=weight_decay)
|
||||||
pickle.dump(train_dataset[150:160], open(filestr+'_test_batch.pkl', 'wb'))
|
|
||||||
policy.save_model(filestr)
|
|
||||||
for i in range(train_epochs):
|
for i in range(train_epochs):
|
||||||
|
|
||||||
epoch_loss = 0
|
epoch_loss = 0
|
||||||
@@ -147,6 +147,9 @@ def train(train_dataset, cv_dataset, policy, filestr, **kwargs):
|
|||||||
|
|
||||||
# sample mini-batch and run through policy
|
# sample mini-batch and run through policy
|
||||||
pred_action = policy(batch)
|
pred_action = policy(batch)
|
||||||
|
if i == 0 and batch_idx==0:
|
||||||
|
pickle.dump(batch, open(filestr+'_test_batch.pkl', 'wb'))
|
||||||
|
policy.save_model(filestr)
|
||||||
loss = loss_fn(pred_action, batch['action'])
|
loss = loss_fn(pred_action, batch['action'])
|
||||||
|
|
||||||
# compute loss and step optimizer
|
# compute loss and step optimizer
|
||||||
@@ -157,7 +160,7 @@ def train(train_dataset, cv_dataset, policy, filestr, **kwargs):
|
|||||||
epoch_loss += loss.item() / len(train_dataset)
|
epoch_loss += loss.item() / len(train_dataset)
|
||||||
|
|
||||||
# Write epoch loss
|
# Write epoch loss
|
||||||
if i % 10 == 0:
|
if i % epoch_every == 0:
|
||||||
print('Epoch: {}, Training Loss: {}'.format(i, epoch_loss))
|
print('Epoch: {}, Training Loss: {}'.format(i, epoch_loss))
|
||||||
|
|
||||||
# measure cv loss
|
# measure cv loss
|
||||||
|
|||||||
22
src/main.py
22
src/main.py
@@ -5,9 +5,10 @@ import numpy as np
|
|||||||
import json5
|
import json5
|
||||||
import os
|
import os
|
||||||
opj = os.path.join
|
opj = os.path.join
|
||||||
|
from tqdm import tqdm
|
||||||
|
|
||||||
from src import InteractionDatasetSingleAgent, metrics
|
from src import InteractionDatasetSingleAgent, metrics
|
||||||
|
from intersim.utils import get_map_path, get_svt
|
||||||
|
|
||||||
def basestr(**kwargs):
|
def basestr(**kwargs):
|
||||||
"""
|
"""
|
||||||
@@ -60,7 +61,7 @@ def main(method='bc', train=False, test=False, loc=0, config_path=None, **kwargs
|
|||||||
|
|
||||||
# make policy, train and test datasets, and send to
|
# make policy, train and test datasets, and send to
|
||||||
policy = policy_class(config)
|
policy = policy_class(config)
|
||||||
train_dataset = InteractionDatasetSingleAgent(loc=loc, tracks=[0])#,1,2])
|
train_dataset = InteractionDatasetSingleAgent(loc=loc, tracks=[0,1,2])
|
||||||
cv_dataset = InteractionDatasetSingleAgent(loc=loc, tracks=[3])
|
cv_dataset = InteractionDatasetSingleAgent(loc=loc, tracks=[3])
|
||||||
train_fn(train_dataset, cv_dataset, policy, filestr, **kwargs)
|
train_fn(train_dataset, cv_dataset, policy, filestr, **kwargs)
|
||||||
|
|
||||||
@@ -72,14 +73,14 @@ def main(method='bc', train=False, test=False, loc=0, config_path=None, **kwargs
|
|||||||
|
|
||||||
# simulate policy
|
# simulate policy
|
||||||
track = 4
|
track = 4
|
||||||
simulate_policy(policy, loc=loc, track=track, filestr=filestr)
|
simulate_policy(policy, loc=loc, track=track, filestr=filestr, nframes=500)
|
||||||
|
|
||||||
# run test metrics
|
# run test metrics
|
||||||
test_dataset = InteractionDatasetSingleAgent(loc=loc, tracks=[track])
|
test_dataset = InteractionDatasetSingleAgent(loc=loc, tracks=[track])
|
||||||
metrics(filestr, test_dataset, policy)
|
metrics(filestr, test_dataset, policy)
|
||||||
|
|
||||||
|
|
||||||
def simulate_policy(policy, loc=0, track=0, filestr=''):
|
def simulate_policy(policy, loc=0, track=0, filestr='', nframes=float('inf')):
|
||||||
"""
|
"""
|
||||||
Simulate a trained policy
|
Simulate a trained policy
|
||||||
Args:
|
Args:
|
||||||
@@ -89,13 +90,20 @@ def simulate_policy(policy, loc=0, track=0, filestr=''):
|
|||||||
filestr (str): path prefix to save simulation to
|
filestr (str): path prefix to save simulation to
|
||||||
"""
|
"""
|
||||||
# animate from environment
|
# animate from environment
|
||||||
env = gym.make('intersim:intersim-v0', loc=loc, track=track,
|
svt, svt_path = get_svt(base='InteractionSimulator', loc=loc, track=track)
|
||||||
|
osm = get_map_path(base='InteractionSimulator', loc=loc)
|
||||||
|
env = gym.make('intersim:intersim-v0', svt=svt, map_path=osm,
|
||||||
min_acc=-np.inf, max_acc=np.inf)
|
min_acc=-np.inf, max_acc=np.inf)
|
||||||
|
# env = gym.make('intersim:intersim-v0', loc=loc, track=track,
|
||||||
|
# min_acc=-np.inf, max_acc=np.inf)
|
||||||
|
|
||||||
ob, _ = env.reset()
|
ob, _ = env.reset()
|
||||||
env.render()
|
env.render()
|
||||||
done = False
|
done = False
|
||||||
while not done:
|
i = 0
|
||||||
|
with tqdm(total=min(nframes, env._svt.Tind)) as pbar:
|
||||||
|
while not done and i < nframes:
|
||||||
|
i += 1
|
||||||
|
|
||||||
# get action
|
# get action
|
||||||
action = policy(ob)
|
action = policy(ob)
|
||||||
@@ -104,6 +112,8 @@ def simulate_policy(policy, loc=0, track=0, filestr=''):
|
|||||||
ob, r, done, info = env.step(action)
|
ob, r, done, info = env.step(action)
|
||||||
env.render()
|
env.render()
|
||||||
|
|
||||||
|
pbar.update()
|
||||||
|
|
||||||
env.close(filestr=filestr)
|
env.close(filestr=filestr)
|
||||||
|
|
||||||
def parse_args():
|
def parse_args():
|
||||||
|
|||||||
Reference in New Issue
Block a user