fix action dim for real world
这个提交包含在:
@ -65,8 +65,9 @@ def get_args_parser():
|
|||||||
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('--use_vq', action='store_true')
|
||||||
parser.add_argument('--vq_class', action='store', type=int, help='vq_class')
|
parser.add_argument('--vq_class', action='store', type=int, help='vq_class', required=False)
|
||||||
parser.add_argument('--vq_dim', action='store', type=int, help='vq_dim')
|
parser.add_argument('--vq_dim', action='store', type=int, help='vq_dim', required=False)
|
||||||
|
parser.add_argument('--action_dim', action='store', type=int, required=False)
|
||||||
|
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
|||||||
@ -34,7 +34,7 @@ def get_sinusoid_encoding_table(n_position, d_hid):
|
|||||||
|
|
||||||
class DETRVAE(nn.Module):
|
class DETRVAE(nn.Module):
|
||||||
""" This is the DETR module that performs object detection """
|
""" This is the DETR module that performs object detection """
|
||||||
def __init__(self, backbones, transformer, encoder, state_dim, num_queries, camera_names, vq, vq_class, vq_dim, ):
|
def __init__(self, backbones, transformer, encoder, state_dim, num_queries, camera_names, vq, vq_class, vq_dim, action_dim):
|
||||||
""" Initializes the model.
|
""" Initializes the model.
|
||||||
Parameters:
|
Parameters:
|
||||||
backbones: torch module of the backbone to be used. See backbone.py
|
backbones: torch module of the backbone to be used. See backbone.py
|
||||||
@ -50,17 +50,18 @@ class DETRVAE(nn.Module):
|
|||||||
self.transformer = transformer
|
self.transformer = transformer
|
||||||
self.encoder = encoder
|
self.encoder = encoder
|
||||||
self.vq, self.vq_class, self.vq_dim = vq, vq_class, vq_dim
|
self.vq, self.vq_class, self.vq_dim = vq, vq_class, vq_dim
|
||||||
|
self.state_dim, self.action_dim = state_dim, action_dim
|
||||||
hidden_dim = transformer.d_model
|
hidden_dim = transformer.d_model
|
||||||
self.action_head = nn.Linear(hidden_dim, state_dim)
|
self.action_head = nn.Linear(hidden_dim, action_dim)
|
||||||
self.is_pad_head = nn.Linear(hidden_dim, 1)
|
self.is_pad_head = nn.Linear(hidden_dim, 1)
|
||||||
self.query_embed = nn.Embedding(num_queries, hidden_dim)
|
self.query_embed = nn.Embedding(num_queries, hidden_dim)
|
||||||
if backbones is not None:
|
if backbones is not None:
|
||||||
self.input_proj = nn.Conv2d(backbones[0].num_channels, hidden_dim, kernel_size=1)
|
self.input_proj = nn.Conv2d(backbones[0].num_channels, hidden_dim, kernel_size=1)
|
||||||
self.backbones = nn.ModuleList(backbones)
|
self.backbones = nn.ModuleList(backbones)
|
||||||
self.input_proj_robot_state = nn.Linear(14, hidden_dim)
|
self.input_proj_robot_state = nn.Linear(state_dim, hidden_dim)
|
||||||
else:
|
else:
|
||||||
# input_dim = 14 + 7 # robot_state + env_state
|
# input_dim = 14 + 7 # robot_state + env_state
|
||||||
self.input_proj_robot_state = nn.Linear(14, hidden_dim)
|
self.input_proj_robot_state = nn.Linear(state_dim, hidden_dim)
|
||||||
self.input_proj_env_state = nn.Linear(7, hidden_dim)
|
self.input_proj_env_state = nn.Linear(7, hidden_dim)
|
||||||
self.pos = torch.nn.Embedding(2, hidden_dim)
|
self.pos = torch.nn.Embedding(2, hidden_dim)
|
||||||
self.backbones = None
|
self.backbones = None
|
||||||
@ -68,8 +69,8 @@ class DETRVAE(nn.Module):
|
|||||||
# encoder extra parameters
|
# encoder extra parameters
|
||||||
self.latent_dim = 32 # final size of latent z # TODO tune
|
self.latent_dim = 32 # final size of latent z # TODO tune
|
||||||
self.cls_embed = nn.Embedding(1, hidden_dim) # extra cls token embedding
|
self.cls_embed = nn.Embedding(1, hidden_dim) # extra cls token embedding
|
||||||
self.encoder_action_proj = nn.Linear(14, hidden_dim) # project action to embedding
|
self.encoder_action_proj = nn.Linear(action_dim, 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(state_dim, hidden_dim) # project qpos to embedding
|
||||||
|
|
||||||
print(f'Use VQ: {self.vq}, {self.vq_class}, {self.vq_dim}')
|
print(f'Use VQ: {self.vq}, {self.vq_class}, {self.vq_dim}')
|
||||||
if self.vq:
|
if self.vq:
|
||||||
@ -153,7 +154,7 @@ class DETRVAE(nn.Module):
|
|||||||
all_cam_features = []
|
all_cam_features = []
|
||||||
all_cam_pos = []
|
all_cam_pos = []
|
||||||
for cam_id, cam_name in enumerate(self.camera_names):
|
for cam_id, cam_name in enumerate(self.camera_names):
|
||||||
features, pos = self.backbones[0](image[:, cam_id]) # HARDCODED
|
features, pos = self.backbones[cam_id](image[:, cam_id])
|
||||||
features = features[0] # take the last layer feature
|
features = features[0] # take the last layer feature
|
||||||
pos = pos[0]
|
pos = pos[0]
|
||||||
all_cam_features.append(self.input_proj(features))
|
all_cam_features.append(self.input_proj(features))
|
||||||
@ -201,8 +202,8 @@ class CNNMLP(nn.Module):
|
|||||||
backbone_down_projs.append(down_proj)
|
backbone_down_projs.append(down_proj)
|
||||||
self.backbone_down_projs = nn.ModuleList(backbone_down_projs)
|
self.backbone_down_projs = nn.ModuleList(backbone_down_projs)
|
||||||
|
|
||||||
mlp_in_dim = 768 * len(backbones) + 14
|
mlp_in_dim = 768 * len(backbones) + state_dim
|
||||||
self.mlp = mlp(input_dim=mlp_in_dim, hidden_dim=1024, output_dim=14, hidden_depth=2)
|
self.mlp = mlp(input_dim=mlp_in_dim, hidden_dim=1024, output_dim=self.action_dim, hidden_depth=2)
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
@ -268,8 +269,9 @@ def build(args):
|
|||||||
# backbone = None # from state for now, no need for conv nets
|
# backbone = None # from state for now, no need for conv nets
|
||||||
# From image
|
# From image
|
||||||
backbones = []
|
backbones = []
|
||||||
backbone = build_backbone(args)
|
for _ in args.camera_names:
|
||||||
backbones.append(backbone)
|
backbone = build_backbone(args)
|
||||||
|
backbones.append(backbone)
|
||||||
|
|
||||||
transformer = build_transformer(args)
|
transformer = build_transformer(args)
|
||||||
|
|
||||||
@ -285,6 +287,7 @@ def build(args):
|
|||||||
vq=args.vq,
|
vq=args.vq,
|
||||||
vq_class=args.vq_class,
|
vq_class=args.vq_class,
|
||||||
vq_dim=args.vq_dim,
|
vq_dim=args.vq_dim,
|
||||||
|
action_dim=args.action_dim,
|
||||||
)
|
)
|
||||||
|
|
||||||
n_parameters = sum(p.numel() for p in model.parameters() if p.requires_grad)
|
n_parameters = sum(p.numel() for p in model.parameters() if p.requires_grad)
|
||||||
|
|||||||
@ -70,6 +70,7 @@ def main(args):
|
|||||||
'vq': args['use_vq'],
|
'vq': args['use_vq'],
|
||||||
'vq_class': args['vq_class'],
|
'vq_class': args['vq_class'],
|
||||||
'vq_dim': args['vq_dim'],
|
'vq_dim': args['vq_dim'],
|
||||||
|
'action_dim': 16
|
||||||
}
|
}
|
||||||
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,
|
||||||
@ -197,7 +198,7 @@ def eval_bc(config, ckpt_name, save_episode=True):
|
|||||||
if real_robot:
|
if real_robot:
|
||||||
from aloha_scripts.robot_utils import move_grippers # requires aloha
|
from aloha_scripts.robot_utils import move_grippers # requires aloha
|
||||||
from aloha_scripts.real_env import make_real_env # requires aloha
|
from aloha_scripts.real_env import make_real_env # requires aloha
|
||||||
env = make_real_env(init_node=True)
|
env = make_real_env(init_node=True, setup_base=True)
|
||||||
env_max_reward = 0
|
env_max_reward = 0
|
||||||
else:
|
else:
|
||||||
from sim_env import make_sim_env
|
from sim_env import make_sim_env
|
||||||
@ -291,10 +292,11 @@ def eval_bc(config, ckpt_name, save_episode=True):
|
|||||||
### post-process actions
|
### post-process actions
|
||||||
raw_action = raw_action.squeeze(0).cpu().numpy()
|
raw_action = raw_action.squeeze(0).cpu().numpy()
|
||||||
action = post_process(raw_action)
|
action = post_process(raw_action)
|
||||||
target_qpos = action
|
target_qpos = action[:-2]
|
||||||
|
base_action = action[-2:]
|
||||||
|
|
||||||
### step the environment
|
### step the environment
|
||||||
ts = env.step(target_qpos)
|
ts = env.step(target_qpos, base_action)
|
||||||
|
|
||||||
### for visualization
|
### for visualization
|
||||||
qpos_list.append(qpos_numpy)
|
qpos_list.append(qpos_numpy)
|
||||||
|
|||||||
12
utils.py
12
utils.py
@ -27,7 +27,11 @@ class EpisodicDataset(torch.utils.data.Dataset):
|
|||||||
dataset_path = os.path.join(self.dataset_dir, f'episode_{episode_id}.hdf5')
|
dataset_path = os.path.join(self.dataset_dir, f'episode_{episode_id}.hdf5')
|
||||||
with h5py.File(dataset_path, 'r') as root:
|
with h5py.File(dataset_path, 'r') as root:
|
||||||
is_sim = root.attrs['sim']
|
is_sim = root.attrs['sim']
|
||||||
original_action_shape = root['/action'].shape
|
if '/base_action' in root:
|
||||||
|
action = np.concatenate([root['/action'][()], root['/base_action'][()]], axis=-1)
|
||||||
|
else:
|
||||||
|
action = root['/action'][()]
|
||||||
|
original_action_shape = action.shape
|
||||||
episode_len = original_action_shape[0]
|
episode_len = original_action_shape[0]
|
||||||
if sample_full_episode:
|
if sample_full_episode:
|
||||||
start_ts = 0
|
start_ts = 0
|
||||||
@ -41,10 +45,10 @@ class EpisodicDataset(torch.utils.data.Dataset):
|
|||||||
image_dict[cam_name] = root[f'/observations/images/{cam_name}'][start_ts]
|
image_dict[cam_name] = root[f'/observations/images/{cam_name}'][start_ts]
|
||||||
# get all actions after and including start_ts
|
# get all actions after and including start_ts
|
||||||
if is_sim:
|
if is_sim:
|
||||||
action = root['/action'][start_ts:]
|
action = action[start_ts:]
|
||||||
action_len = episode_len - start_ts
|
action_len = episode_len - start_ts
|
||||||
else:
|
else:
|
||||||
action = root['/action'][max(0, start_ts - 1):] # hack, to make timesteps more aligned
|
action = action[max(0, start_ts - 1):] # hack, to make timesteps more aligned
|
||||||
action_len = episode_len - max(0, start_ts - 1) # hack, to make timesteps more aligned
|
action_len = episode_len - max(0, start_ts - 1) # hack, to make timesteps more aligned
|
||||||
|
|
||||||
self.is_sim = is_sim
|
self.is_sim = is_sim
|
||||||
@ -85,7 +89,7 @@ def get_norm_stats(dataset_dir, num_episodes):
|
|||||||
with h5py.File(dataset_path, 'r') as root:
|
with h5py.File(dataset_path, 'r') as root:
|
||||||
qpos = root['/observations/qpos'][()]
|
qpos = root['/observations/qpos'][()]
|
||||||
qvel = root['/observations/qvel'][()]
|
qvel = root['/observations/qvel'][()]
|
||||||
action = root['/action'][()]
|
action = np.concatenate([root['/action'][()], root['/base_action'][()]], axis=-1)
|
||||||
except:
|
except:
|
||||||
print(f'Error loading {dataset_path}')
|
print(f'Error loading {dataset_path}')
|
||||||
quit()
|
quit()
|
||||||
|
|||||||
在新工单中引用
屏蔽一个用户