fix vq
这个提交包含在:
3
.gitignore
vendored
3
.gitignore
vendored
@ -139,4 +139,5 @@ dmypy.json
|
|||||||
# Pyre type checker
|
# Pyre type checker
|
||||||
.pyre/
|
.pyre/
|
||||||
|
|
||||||
ckpts/
|
ckpts/
|
||||||
|
*log*
|
||||||
|
|||||||
@ -63,7 +63,10 @@ def get_args_parser():
|
|||||||
parser.add_argument('--kl_weight', action='store', type=int, help='KL Weight', required=False)
|
parser.add_argument('--kl_weight', action='store', type=int, help='KL Weight', required=False)
|
||||||
parser.add_argument('--chunk_size', action='store', type=int, help='chunk_size', required=False)
|
parser.add_argument('--chunk_size', action='store', type=int, help='chunk_size', required=False)
|
||||||
parser.add_argument('--temporal_agg', action='store_true')
|
parser.add_argument('--temporal_agg', action='store_true')
|
||||||
parser.add_argument('--vq', action='store_true')
|
|
||||||
|
parser.add_argument('--use_vq', action='store_true')
|
||||||
|
parser.add_argument('--vq_class', action='store', type=int, help='vq_class')
|
||||||
|
parser.add_argument('--vq_dim', action='store', type=int, help='vq_dim')
|
||||||
|
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
|||||||
@ -71,7 +71,7 @@ class DETRVAE(nn.Module):
|
|||||||
self.encoder_action_proj = nn.Linear(14, hidden_dim) # project action to embedding
|
self.encoder_action_proj = nn.Linear(14, hidden_dim) # project action to embedding
|
||||||
self.encoder_joint_proj = nn.Linear(14, hidden_dim) # project qpos to embedding
|
self.encoder_joint_proj = nn.Linear(14, hidden_dim) # project qpos to embedding
|
||||||
|
|
||||||
print('Use VQ: ', self.vq)
|
print(f'Use VQ: {self.vq}, {self.vq_class}, {self.vq_dim}')
|
||||||
if self.vq:
|
if self.vq:
|
||||||
self.latent_proj = nn.Linear(hidden_dim, self.vq_class * self.vq_dim)
|
self.latent_proj = nn.Linear(hidden_dim, self.vq_class * self.vq_dim)
|
||||||
else:
|
else:
|
||||||
@ -121,6 +121,7 @@ class DETRVAE(nn.Module):
|
|||||||
latent_input = self.latent_out_proj(straigt_through)
|
latent_input = self.latent_out_proj(straigt_through)
|
||||||
mu = logvar = None
|
mu = logvar = None
|
||||||
else:
|
else:
|
||||||
|
probs = binaries = None
|
||||||
mu = latent_info[:, :self.latent_dim]
|
mu = latent_info[:, :self.latent_dim]
|
||||||
logvar = latent_info[:, self.latent_dim:]
|
logvar = latent_info[:, self.latent_dim:]
|
||||||
latent_sample = reparametrize(mu, logvar)
|
latent_sample = reparametrize(mu, logvar)
|
||||||
|
|||||||
@ -56,8 +56,6 @@ def main(args):
|
|||||||
enc_layers = 4
|
enc_layers = 4
|
||||||
dec_layers = 7
|
dec_layers = 7
|
||||||
nheads = 8
|
nheads = 8
|
||||||
vq_class = 10
|
|
||||||
vq_dim = 10
|
|
||||||
policy_config = {'lr': args['lr'],
|
policy_config = {'lr': args['lr'],
|
||||||
'num_queries': args['chunk_size'],
|
'num_queries': args['chunk_size'],
|
||||||
'kl_weight': args['kl_weight'],
|
'kl_weight': args['kl_weight'],
|
||||||
@ -69,9 +67,9 @@ def main(args):
|
|||||||
'dec_layers': dec_layers,
|
'dec_layers': dec_layers,
|
||||||
'nheads': nheads,
|
'nheads': nheads,
|
||||||
'camera_names': camera_names,
|
'camera_names': camera_names,
|
||||||
'vq': args['vq'],
|
'vq': args['use_vq'],
|
||||||
'vq_class': vq_class,
|
'vq_class': args['vq_class'],
|
||||||
'vq_dim': vq_dim,
|
'vq_dim': args['vq_dim'],
|
||||||
}
|
}
|
||||||
elif policy_class == 'CNNMLP':
|
elif policy_class == 'CNNMLP':
|
||||||
policy_config = {'lr': args['lr'], 'lr_backbone': lr_backbone, 'backbone' : backbone, 'num_queries': 1,
|
policy_config = {'lr': args['lr'], 'lr_backbone': lr_backbone, 'backbone' : backbone, 'num_queries': 1,
|
||||||
@ -96,7 +94,7 @@ def main(args):
|
|||||||
}
|
}
|
||||||
|
|
||||||
if is_eval:
|
if is_eval:
|
||||||
ckpt_names = [f'policy_best.ckpt']
|
ckpt_names = [f'policy_last.ckpt']
|
||||||
results = []
|
results = []
|
||||||
for ckpt_name in ckpt_names:
|
for ckpt_name in ckpt_names:
|
||||||
success_rate, avg_return = eval_bc(config, ckpt_name, save_episode=True)
|
success_rate, avg_return = eval_bc(config, ckpt_name, save_episode=True)
|
||||||
@ -181,8 +179,9 @@ def eval_bc(config, ckpt_name, save_episode=True):
|
|||||||
vq_dim = config['policy_config']['vq_dim']
|
vq_dim = config['policy_config']['vq_dim']
|
||||||
vq_class = config['policy_config']['vq_class']
|
vq_class = config['policy_config']['vq_class']
|
||||||
latent_model = Latent_Model_Transformer(vq_dim, vq_dim, vq_class)
|
latent_model = Latent_Model_Transformer(vq_dim, vq_dim, vq_class)
|
||||||
latent_model_ckpt_path = os.path.join(ckpt_dir, 'latent_model_best.ckpt')
|
latent_model_ckpt_path = os.path.join(ckpt_dir, 'latent_model_last.ckpt')
|
||||||
latent_model.load_state_dict(torch.load(latent_model_ckpt_path))
|
latent_model.load_state_dict(torch.load(latent_model_ckpt_path))
|
||||||
|
latent_model.eval()
|
||||||
latent_model.cuda()
|
latent_model.cuda()
|
||||||
print(f'Loaded policy from: {ckpt_path}, latent model from: {latent_model_ckpt_path}')
|
print(f'Loaded policy from: {ckpt_path}, latent model from: {latent_model_ckpt_path}')
|
||||||
else:
|
else:
|
||||||
@ -264,7 +263,11 @@ def eval_bc(config, ckpt_name, save_episode=True):
|
|||||||
if config['policy_class'] == "ACT":
|
if config['policy_class'] == "ACT":
|
||||||
if t % query_frequency == 0:
|
if t % query_frequency == 0:
|
||||||
if vq:
|
if vq:
|
||||||
vq_sample = latent_model.generate(1, temperature=0.01, x=None)
|
if rollout_id == 0:
|
||||||
|
for _ in range(10):
|
||||||
|
vq_sample = latent_model.generate(1, temperature=1, x=None)
|
||||||
|
print(torch.nonzero(vq_sample[0])[:, 1].cpu().numpy())
|
||||||
|
vq_sample = latent_model.generate(1, temperature=1, x=None)
|
||||||
all_actions = policy(qpos, curr_image, vq_sample=vq_sample)
|
all_actions = policy(qpos, curr_image, vq_sample=vq_sample)
|
||||||
else:
|
else:
|
||||||
all_actions = policy(qpos, curr_image)
|
all_actions = policy(qpos, curr_image)
|
||||||
@ -452,6 +455,8 @@ if __name__ == '__main__':
|
|||||||
parser.add_argument('--hidden_dim', action='store', type=int, help='hidden_dim', required=False)
|
parser.add_argument('--hidden_dim', action='store', type=int, help='hidden_dim', required=False)
|
||||||
parser.add_argument('--dim_feedforward', action='store', type=int, help='dim_feedforward', required=False)
|
parser.add_argument('--dim_feedforward', action='store', type=int, help='dim_feedforward', required=False)
|
||||||
parser.add_argument('--temporal_agg', action='store_true')
|
parser.add_argument('--temporal_agg', action='store_true')
|
||||||
parser.add_argument('--vq', action='store_true')
|
parser.add_argument('--use_vq', action='store_true')
|
||||||
|
parser.add_argument('--vq_class', action='store', type=int, help='vq_class')
|
||||||
|
parser.add_argument('--vq_dim', action='store', type=int, help='vq_dim')
|
||||||
|
|
||||||
main(vars(parser.parse_args()))
|
main(vars(parser.parse_args()))
|
||||||
|
|||||||
@ -27,11 +27,10 @@ class ACTPolicy(nn.Module):
|
|||||||
is_pad = is_pad[:, :self.model.num_queries]
|
is_pad = is_pad[:, :self.model.num_queries]
|
||||||
|
|
||||||
loss_dict = dict()
|
loss_dict = dict()
|
||||||
print(vq_sample)
|
|
||||||
a_hat, is_pad_hat, (mu, logvar), probs, binaries = self.model(qpos, image, env_state, actions, is_pad, vq_sample)
|
a_hat, is_pad_hat, (mu, logvar), probs, binaries = self.model(qpos, image, env_state, actions, is_pad, vq_sample)
|
||||||
if self.vq:
|
if self.vq:
|
||||||
total_kld = [torch.tensor(0.0)]
|
total_kld = [torch.tensor(0.0)]
|
||||||
loss_dict['vq_loss'] = F.l1_loss(binaries, probs, reduction='mean')
|
loss_dict['vq_discrepancy'] = F.l1_loss(probs, binaries, reduction='mean')
|
||||||
else:
|
else:
|
||||||
total_kld, dim_wise_kld, mean_kld = kl_divergence(mu, logvar)
|
total_kld, dim_wise_kld, mean_kld = kl_divergence(mu, logvar)
|
||||||
all_l1 = F.l1_loss(actions, a_hat, reduction='none')
|
all_l1 = F.l1_loss(actions, a_hat, reduction='none')
|
||||||
|
|||||||
@ -56,8 +56,6 @@ def main(args):
|
|||||||
enc_layers = 4
|
enc_layers = 4
|
||||||
dec_layers = 7
|
dec_layers = 7
|
||||||
nheads = 8
|
nheads = 8
|
||||||
vq_class = 10
|
|
||||||
vq_dim = 10
|
|
||||||
policy_config = {'lr': args['lr'],
|
policy_config = {'lr': args['lr'],
|
||||||
'num_queries': args['chunk_size'],
|
'num_queries': args['chunk_size'],
|
||||||
'kl_weight': args['kl_weight'],
|
'kl_weight': args['kl_weight'],
|
||||||
@ -70,8 +68,8 @@ def main(args):
|
|||||||
'nheads': nheads,
|
'nheads': nheads,
|
||||||
'camera_names': camera_names,
|
'camera_names': camera_names,
|
||||||
'vq': True,
|
'vq': True,
|
||||||
'vq_class': vq_class,
|
'vq_class': args['vq_class'],
|
||||||
'vq_dim': vq_dim,
|
'vq_dim': args['vq_dim'],
|
||||||
}
|
}
|
||||||
elif policy_class == 'CNNMLP':
|
elif policy_class == 'CNNMLP':
|
||||||
policy_config = {'lr': args['lr'], 'lr_backbone': lr_backbone, 'backbone' : backbone, 'num_queries': 1,
|
policy_config = {'lr': args['lr'], 'lr_backbone': lr_backbone, 'backbone' : backbone, 'num_queries': 1,
|
||||||
@ -116,7 +114,7 @@ def main(args):
|
|||||||
# with open(stats_path, 'wb') as f:
|
# with open(stats_path, 'wb') as f:
|
||||||
# pickle.dump(stats, f)
|
# pickle.dump(stats, f)
|
||||||
|
|
||||||
ckpt_name = f'policy_best.ckpt'
|
ckpt_name = f'policy_last.ckpt'
|
||||||
best_ckpt_info = train_bc(train_dataloader, val_dataloader, config, ckpt_name)
|
best_ckpt_info = train_bc(train_dataloader, val_dataloader, config, ckpt_name)
|
||||||
best_epoch, min_val_loss, best_state_dict = best_ckpt_info
|
best_epoch, min_val_loss, best_state_dict = best_ckpt_info
|
||||||
|
|
||||||
@ -464,5 +462,8 @@ if __name__ == '__main__':
|
|||||||
parser.add_argument('--hidden_dim', action='store', type=int, help='hidden_dim', required=False)
|
parser.add_argument('--hidden_dim', action='store', type=int, help='hidden_dim', required=False)
|
||||||
parser.add_argument('--dim_feedforward', action='store', type=int, help='dim_feedforward', required=False)
|
parser.add_argument('--dim_feedforward', action='store', type=int, help='dim_feedforward', required=False)
|
||||||
parser.add_argument('--temporal_agg', action='store_true')
|
parser.add_argument('--temporal_agg', action='store_true')
|
||||||
|
parser.add_argument('--use_vq', action='store_true')
|
||||||
|
parser.add_argument('--vq_class', action='store', type=int, help='vq_class')
|
||||||
|
parser.add_argument('--vq_dim', action='store', type=int, help='vq_dim')
|
||||||
|
|
||||||
main(vars(parser.parse_args()))
|
main(vars(parser.parse_args()))
|
||||||
|
|||||||
在新工单中引用
屏蔽一个用户