fixing bugs in transform, expert demo processing, main train function, and behavior cloning class. need to get bc class parameters to return nonempty list
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -140,6 +140,7 @@ expert_data/
|
|||||||
|
|
||||||
# Results
|
# Results
|
||||||
experiments/results/
|
experiments/results/
|
||||||
|
output/
|
||||||
|
|
||||||
# Dependencies
|
# Dependencies
|
||||||
InteractionSimulator/
|
InteractionSimulator/
|
||||||
|
|||||||
20
src/bc/bc.py
20
src/bc/bc.py
@@ -2,7 +2,7 @@ import torch
|
|||||||
import torch.nn as nn
|
import torch.nn as nn
|
||||||
from torch.utils.data import DataLoader, RandomSampler
|
from torch.utils.data import DataLoader, RandomSampler
|
||||||
import pickle
|
import pickle
|
||||||
from torch.utils.tensorboard import SummaryWriter
|
#from torch.utils.tensorboard import SummaryWriter
|
||||||
|
|
||||||
from src.policies import DeepSetsPolicy
|
from src.policies import DeepSetsPolicy
|
||||||
from src.util.transform import SciKitMinMaxScaler
|
from src.util.transform import SciKitMinMaxScaler
|
||||||
@@ -13,7 +13,7 @@ class BehaviorCloningPolicy():
|
|||||||
Class for (continuous) behavior cloning policy
|
Class for (continuous) behavior cloning policy
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, config: dict transforms: dict={}):
|
def __init__(self, config: dict, transforms: dict={}):
|
||||||
"""
|
"""
|
||||||
Initialize BehaviorCloningPolicy
|
Initialize BehaviorCloningPolicy
|
||||||
Args:
|
Args:
|
||||||
@@ -29,7 +29,7 @@ class BehaviorCloningPolicy():
|
|||||||
return self._transforms
|
return self._transforms
|
||||||
|
|
||||||
@transforms.setter
|
@transforms.setter
|
||||||
def transforms(self, transforms)
|
def transforms(self, transforms):
|
||||||
self._transforms=transforms
|
self._transforms=transforms
|
||||||
|
|
||||||
def __call__(self, ob):
|
def __call__(self, ob):
|
||||||
@@ -93,11 +93,11 @@ def generate_transforms(dataset):
|
|||||||
dataset (Dataset): dataset of demo observations and actions
|
dataset (Dataset): dataset of demo observations and actions
|
||||||
"""
|
"""
|
||||||
transforms = {
|
transforms = {
|
||||||
'action': SciKitMinMaxScaler()
|
'action': SciKitMinMaxScaler(),
|
||||||
'state': SciKitMinMaxScaler()
|
'state': SciKitMinMaxScaler(),
|
||||||
'relative_state': SciKitMinMaxScaler(reduce_dim=2)
|
'relative_state': SciKitMinMaxScaler(reduce_dim=2),
|
||||||
'path_x': SciKitMinMaxScaler(reduce_dim=2)
|
'path_x': SciKitMinMaxScaler(reduce_dim=2),
|
||||||
'path_y': SciKitMinMaxScaler(reduce_dim=2)
|
'path_y': SciKitMinMaxScaler(reduce_dim=2),
|
||||||
}
|
}
|
||||||
for key in transforms.keys():
|
for key in transforms.keys():
|
||||||
transforms[key].fit(dataset[:][key])
|
transforms[key].fit(dataset[:][key])
|
||||||
@@ -126,6 +126,8 @@ 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')
|
||||||
|
import pdb
|
||||||
|
pdb.set_trace()
|
||||||
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)
|
||||||
|
|
||||||
for i in train_epochs:
|
for i in train_epochs:
|
||||||
@@ -156,5 +158,5 @@ def train(train_dataset, cv_dataset, policy, filestr, **kwargs):
|
|||||||
loss = loss_fn(pred_action, batch['action'])
|
loss = loss_fn(pred_action, batch['action'])
|
||||||
cv_loss += loss.item() / len(cv_dataset)
|
cv_loss += loss.item() / len(cv_dataset)
|
||||||
print('Epoch: {}, CV Loss: {}'.format(i, cv_loss))
|
print('Epoch: {}, CV Loss: {}'.format(i, cv_loss))
|
||||||
|
|
||||||
policy.save_model(filestr)
|
policy.save_model(filestr)
|
||||||
|
|||||||
@@ -43,7 +43,8 @@ def generate_expert_data(path: str='expert_data', loc: int = 0, track:int = 0, *
|
|||||||
env_state = env.projected_state
|
env_state = env.projected_state
|
||||||
nni = ~torch.isnan(env_state[:,0])
|
nni = ~torch.isnan(env_state[:,0])
|
||||||
norms = torch.norm(env_state[nni,:2]-states[i,nni,:2], dim=1)
|
norms = torch.norm(env_state[nni,:2]-states[i,nni,:2], dim=1)
|
||||||
max_devs.append(norms.max())
|
if len(norms)>0:
|
||||||
|
max_devs.append(norms.max())
|
||||||
|
|
||||||
# propagate environment
|
# propagate environment
|
||||||
ob, r, done, info = env.step(env.target_state(svt.simstate[i+1]))
|
ob, r, done, info = env.step(env.target_state(svt.simstate[i+1]))
|
||||||
|
|||||||
22
src/main.py
22
src/main.py
@@ -37,7 +37,7 @@ def main(method='bc', train=False, test=False, loc=0, config_path=None, **kwargs
|
|||||||
# make prefix of output files
|
# make prefix of output files
|
||||||
outdir = opj('output',method,'loc%02i'%(loc))
|
outdir = opj('output',method,'loc%02i'%(loc))
|
||||||
if not os.path.isdir(outdir):
|
if not os.path.isdir(outdir):
|
||||||
os.mkdir(outdir)
|
os.makedirs(outdir)
|
||||||
filestr = opj(outdir, basestr(**kwargs))
|
filestr = opj(outdir, basestr(**kwargs))
|
||||||
|
|
||||||
# load config
|
# load config
|
||||||
@@ -60,7 +60,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)
|
||||||
|
|
||||||
@@ -71,11 +71,11 @@ def main(method='bc', train=False, test=False, loc=0, config_path=None, **kwargs
|
|||||||
policy.eval()
|
policy.eval()
|
||||||
|
|
||||||
# simulate policy
|
# simulate policy
|
||||||
test_track = 4
|
track = 4
|
||||||
simulate_policy(policy, loc=loc, track=track, filestr=filestr)
|
simulate_policy(policy, loc=loc, track=track, filestr=filestr)
|
||||||
|
|
||||||
# run test metrics
|
# run test metrics
|
||||||
test_dataset = InteractionDatasetSingleAgent(loc=loc, tracks=[4])
|
test_dataset = InteractionDatasetSingleAgent(loc=loc, tracks=[track])
|
||||||
metrics(filestr, test_dataset, policy)
|
metrics(filestr, test_dataset, policy)
|
||||||
|
|
||||||
|
|
||||||
@@ -132,16 +132,14 @@ def parse_args():
|
|||||||
default='config/networks.json5', type=str)
|
default='config/networks.json5', type=str)
|
||||||
parser.add_argument('--seed', default=0, type=int,
|
parser.add_argument('--seed', default=0, type=int,
|
||||||
help='seed')
|
help='seed')
|
||||||
parser.add_argument()
|
|
||||||
parser.add_argument()
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
kwargs = {
|
kwargs = {
|
||||||
'train'=args.train,
|
'train':args.train,
|
||||||
'test'=args.test,
|
'test':args.test,
|
||||||
'method'=args.method,
|
'method':args.method,
|
||||||
'loc'=args.loc,
|
'loc':args.loc,
|
||||||
'config'=args.config,
|
'config_path':args.config,
|
||||||
'seed'=args.seed
|
'seed':args.seed
|
||||||
}
|
}
|
||||||
return kwargs
|
return kwargs
|
||||||
|
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ def average_velocity(x):
|
|||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def divergence(p, q, type='kl')
|
def divergence(p, q, type='kl'):
|
||||||
"""
|
"""
|
||||||
Calculate a divergence between p and q
|
Calculate a divergence between p and q
|
||||||
Args:
|
Args:
|
||||||
|
|||||||
@@ -52,27 +52,27 @@ class SciKitTransform(Transform):
|
|||||||
e.g. with reduce_dim=2, (A, B, C, D, E) will be reshaped to (A*B, C*D*E)
|
e.g. with reduce_dim=2, (A, B, C, D, E) will be reshaped to (A*B, C*D*E)
|
||||||
"""
|
"""
|
||||||
self.tf = tf
|
self.tf = tf
|
||||||
self.reduce_dim
|
self.reduce_dim = reduce_dim
|
||||||
super(SciKitTransform, self).__init__()
|
super(SciKitTransform, self).__init__()
|
||||||
|
|
||||||
def fit(self, X):
|
def fit(self, X):
|
||||||
nd = X.ndim
|
nd = X.ndim
|
||||||
if self.reduce_dim:
|
if self.reduce_dim:
|
||||||
self.nfeatures = X.shape[reduce_dim:].prod()
|
self.nfeatures = int(torch.tensor(X.shape[self.reduce_dim:]).prod())
|
||||||
else:
|
else:
|
||||||
assert nd==2, 'Invalid ndim'
|
assert nd==2, 'Invalid ndim'
|
||||||
self.nfeatures = X.shape[1]
|
self.nfeatures = X.shape[1]
|
||||||
|
|
||||||
self.tf.fit(X.reshape((-1,selfnfeatures)))
|
self.tf.fit(X.reshape((-1,self.nfeatures)))
|
||||||
|
|
||||||
def transform(self, X):
|
def transform(self, X):
|
||||||
shape = X.shape
|
shape = X.shape
|
||||||
t = torch.tensor(self.tf.transform(X.reshape((-1,selfnfeatures))), dtype=torch.float)
|
t = torch.tensor(self.tf.transform(X.reshape((-1,self.nfeatures))), dtype=torch.float)
|
||||||
return t.reshape(shape)
|
return t.reshape(shape)
|
||||||
|
|
||||||
def inverse_transform(self, X):
|
def inverse_transform(self, X):
|
||||||
shape = X.shape
|
shape = X.shape
|
||||||
it = torch.tensor(self.tf.inverse_transform(X.reshape((-1,selfnfeatures))), dtype=torch.float)
|
it = torch.tensor(self.tf.inverse_transform(X.reshape((-1,self.nfeatures))), dtype=torch.float)
|
||||||
return it.reshape(shape)
|
return it.reshape(shape)
|
||||||
|
|
||||||
class SciKitStandardScaler(SciKitTransform):
|
class SciKitStandardScaler(SciKitTransform):
|
||||||
|
|||||||
Reference in New Issue
Block a user