比较提交
1 次代码提交
| 作者 | SHA1 | 提交日期 | |
|---|---|---|---|
| f163ce50b9 |
@ -29,7 +29,7 @@
|
||||
<site pos="0.15 0 0" size="0.003 0.003 0.03" type="box" name="cali_left_site1" rgba="0 0 1 0"/>
|
||||
<site pos="0.15 0 0" size="0.003 0.03 0.003" type="box" name="cali_left_site2" rgba="0 0 1 0"/>
|
||||
<site pos="0.15 0 0" size="0.03 0.003 0.003" type="box" name="cali_left_site3" rgba="0 0 1 0"/>
|
||||
<camera name="left_wrist" pos="-0.1 0 0.16" fovy="20" mode="fixed" euler="0 -1.05 -1.5707" />
|
||||
<camera name="left_wrist" pos="-0.1 0 0.16" fovy="20" mode="targetbody" target="vx300s_left/camera_focus"/>
|
||||
<inertial pos="0.0395662 -2.56311e-07 0.00400649" quat="0.62033 0.619916 -0.339682 0.339869" mass="0.251652" diaginertia="0.000689546 0.000650316 0.000468142" />
|
||||
<joint name="vx300s_left/wrist_rotate" pos="0 0 0" axis="1 0 0" limited="true" range="-3.14158 3.14158" frictionloss="30" />
|
||||
<geom pos="-0.02 0 0" quat="0.707107 0 0 0.707107" type="mesh" mesh="vx300s_7_gripper" name="vx300s_left/7_gripper" />
|
||||
|
||||
@ -29,7 +29,7 @@
|
||||
<site pos="0.15 0 0" size="0.003 0.003 0.03" type="box" name="cali_right_site1" rgba="0 0 1 0"/>
|
||||
<site pos="0.15 0 0" size="0.003 0.03 0.003" type="box" name="cali_right_site2" rgba="0 0 1 0"/>
|
||||
<site pos="0.15 0 0" size="0.03 0.003 0.003" type="box" name="cali_right_site3" rgba="0 0 1 0"/>
|
||||
<camera name="right_wrist" pos="-0.1 0 0.16" fovy="20" mode="fixed" euler="0 -1.05 -1.5707" />
|
||||
<camera name="right_wrist" pos="-0.1 0 0.16" fovy="20" mode="targetbody" target="vx300s_right/camera_focus"/>
|
||||
<inertial pos="0.0395662 -2.56311e-07 0.00400649" quat="0.62033 0.619916 -0.339682 0.339869" mass="0.251652" diaginertia="0.000689546 0.000650316 0.000468142" />
|
||||
<joint name="vx300s_right/wrist_rotate" pos="0 0 0" axis="1 0 0" limited="true" range="-3.14158 3.14158" frictionloss="30" />
|
||||
<geom pos="-0.02 0 0" quat="0.707107 0 0 0.707107" type="mesh" mesh="vx300s_7_gripper" name="vx300s_right/7_gripper" />
|
||||
|
||||
47
commands.txt
47
commands.txt
@ -18,7 +18,6 @@ CUDA_VISIBLE_DEVICES=0 python3 imitate_episodes.py \
|
||||
--hidden_dim 512 --batch_size 24 --dim_feedforward 3200 --num_epochs 5000 --lr 1e-4 --seed 0
|
||||
|
||||
|
||||
#### NOTE to reproduce this experiment, uncomment the sim data filtering in utils.py
|
||||
conda activate mobile
|
||||
cd /home/tonyzhao/Research/act-plus-plus
|
||||
CUDA_VISIBLE_DEVICES=0 python3 imitate_episodes.py \
|
||||
@ -28,50 +27,4 @@ CUDA_VISIBLE_DEVICES=0 python3 imitate_episodes.py \
|
||||
--hidden_dim 512 --batch_size 24 --dim_feedforward 3200 --lr 1e-4 --seed 0 \
|
||||
--num_steps 1000000 --eval_every 10000000000 --validate_every 2000 --save_every 5000
|
||||
|
||||
# generate mirrored data
|
||||
conda activate mobile
|
||||
cd /home/tonyzhao/Research/act-plus-plus
|
||||
python3 record_sim_episodes.py --task_name sim_transfer_cube_scripted_mirror --dataset_dir /scr/tonyzhao/datasets/sim_transfer_cube_scripted_mirror --num_episodes 50
|
||||
python3 postprocess_episodes.py --dataset_dir /scr/tonyzhao/datasets/sim_transfer_cube_scripted_mirror --num_episodes 50
|
||||
# the sim_transfer_cube_scripted_mirror will have 100 episodes
|
||||
# I then copy the whole dir to sim_transfer_cube_scripted then removed all mirrored episodes
|
||||
# this gives sim_transfer_cube_scripted_mirror (100 episodes) and sim_transfer_cube_scripted (50 episodes)
|
||||
|
||||
# visualize the original data
|
||||
python3 visualize_episodes.py --dataset_dir /scr/tonyzhao/datasets/sim_transfer_cube_scripted_mirror --episode_idx 0
|
||||
# visualize the artificially mirrored data
|
||||
python3 visualize_episodes.py --dataset_dir /scr/tonyzhao/datasets/sim_transfer_cube_scripted_mirror --episode_idx 0 --ismirror
|
||||
|
||||
# sanity check
|
||||
# replay the mirrored data action in the original env
|
||||
python3 replay_episodes.py --dataset_path /scr/tonyzhao/datasets/sim_transfer_cube_scripted_mirror/mirror_episode_0.hdf5
|
||||
# replay the original data action in the original env
|
||||
python3 replay_episodes.py --dataset_path /scr/tonyzhao/datasets/sim_transfer_cube_scripted_mirror/episode_0.hdf5
|
||||
|
||||
|
||||
# launch experiment on original data
|
||||
conda activate mobile
|
||||
export MUJOCO_GL=egl
|
||||
cd /home/tonyzhao/Research/act-plus-plus
|
||||
CUDA_VISIBLE_DEVICES=0 python3 imitate_episodes.py \
|
||||
--task_name sim_transfer_cube_scripted \
|
||||
--ckpt_dir /scr/tonyzhao/train_logs/cube_scripted \
|
||||
--policy_class ACT --kl_weight 10 --chunk_size 50 \
|
||||
--hidden_dim 512 --batch_size 12 --dim_feedforward 3200 --lr 1e-5 --seed 0 \
|
||||
--num_steps 100000 --eval_every 2000 --validate_every 2000 --save_every 2000
|
||||
|
||||
|
||||
# launch experiment on all data
|
||||
conda activate mobile
|
||||
export MUJOCO_GL=egl
|
||||
cd /home/tonyzhao/Research/act-plus-plus
|
||||
CUDA_VISIBLE_DEVICES=0 python3 imitate_episodes.py \
|
||||
--task_name sim_transfer_cube_scripted_mirror \
|
||||
--ckpt_dir /scr/tonyzhao/train_logs/cube_scripted_mirror \
|
||||
--policy_class ACT --kl_weight 10 --chunk_size 50 \
|
||||
--hidden_dim 512 --batch_size 12 --dim_feedforward 3200 --lr 1e-5 --seed 0 \
|
||||
--num_steps 100000 --eval_every 2000 --validate_every 2000 --save_every 2000
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
22
constants.py
22
constants.py
@ -1,14 +1,13 @@
|
||||
import pathlib
|
||||
import os
|
||||
|
||||
### Task parameters
|
||||
DATA_DIR = '/home/zfu/interbotix_ws/src/act/data' if os.getlogin() == 'zfu' else '/scr/tonyzhao/datasets'
|
||||
DATA_DIR = '/scr/tonyzhao/datasets'
|
||||
SIM_TASK_CONFIGS = {
|
||||
'sim_transfer_cube_scripted':{
|
||||
'dataset_dir': DATA_DIR + '/sim_transfer_cube_scripted',
|
||||
'num_episodes': 50,
|
||||
'episode_len': 400,
|
||||
'camera_names': ['top', 'left_wrist', 'right_wrist']
|
||||
'camera_names': ['top']
|
||||
},
|
||||
|
||||
'sim_transfer_cube_human':{
|
||||
@ -22,7 +21,7 @@ SIM_TASK_CONFIGS = {
|
||||
'dataset_dir': DATA_DIR + '/sim_insertion_scripted',
|
||||
'num_episodes': 50,
|
||||
'episode_len': 400,
|
||||
'camera_names': ['top', 'left_wrist', 'right_wrist']
|
||||
'camera_names': ['top']
|
||||
},
|
||||
|
||||
'sim_insertion_human': {
|
||||
@ -31,27 +30,14 @@ SIM_TASK_CONFIGS = {
|
||||
'episode_len': 500,
|
||||
'camera_names': ['top']
|
||||
},
|
||||
|
||||
'all': {
|
||||
'dataset_dir': DATA_DIR + '/',
|
||||
'num_episodes': None,
|
||||
'episode_len': None,
|
||||
'name_filter': lambda n: 'sim' not in n,
|
||||
'camera_names': ['cam_high', 'cam_left_wrist', 'cam_right_wrist']
|
||||
},
|
||||
|
||||
'sim_transfer_cube_scripted_mirror':{
|
||||
'dataset_dir': DATA_DIR + '/sim_transfer_cube_scripted_mirror',
|
||||
'num_episodes': None,
|
||||
'episode_len': 400,
|
||||
'camera_names': ['top', 'left_wrist', 'right_wrist']
|
||||
},
|
||||
|
||||
'sim_insertion_scripted_mirror': {
|
||||
'dataset_dir': DATA_DIR + '/sim_insertion_scripted_mirror',
|
||||
'num_episodes': None,
|
||||
'episode_len': 400,
|
||||
'camera_names': ['top', 'left_wrist', 'right_wrist']
|
||||
},
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -71,9 +71,6 @@ def get_args_parser():
|
||||
parser.add_argument('--eval_every', action='store', type=int, default=500, help='eval_every', required=False)
|
||||
parser.add_argument('--validate_every', action='store', type=int, default=500, help='validate_every', required=False)
|
||||
parser.add_argument('--save_every', action='store', type=int, default=500, help='save_every', required=False)
|
||||
parser.add_argument('--resume_ckpt_path', action='store', type=str, help='load_ckpt_path', required=False)
|
||||
parser.add_argument('--no_encoder', action='store_true')
|
||||
parser.add_argument('--skip_mirrored_data', action='store_true')
|
||||
|
||||
return parser
|
||||
|
||||
|
||||
@ -88,14 +88,9 @@ class DETRVAE(nn.Module):
|
||||
|
||||
|
||||
def encode(self, qpos, actions=None, is_pad=None, vq_sample=None):
|
||||
bs, _ = qpos.shape
|
||||
if self.encoder is None:
|
||||
latent_sample = torch.zeros([bs, self.latent_dim], dtype=torch.float32).to(qpos.device)
|
||||
latent_input = self.latent_out_proj(latent_sample)
|
||||
probs = binaries = mu = logvar = None
|
||||
else:
|
||||
# cvae encoder
|
||||
is_training = actions is not None # train or val
|
||||
bs, _ = qpos.shape
|
||||
### Obtain latent z from action sequence
|
||||
if is_training:
|
||||
# project action sequence to embedding dim, and concat with a CLS token
|
||||
@ -141,6 +136,7 @@ class DETRVAE(nn.Module):
|
||||
latent_sample = torch.zeros([bs, self.latent_dim], dtype=torch.float32).to(qpos.device)
|
||||
latent_input = self.latent_out_proj(latent_sample)
|
||||
|
||||
|
||||
return latent_input, probs, binaries, mu, logvar
|
||||
|
||||
def forward(self, qpos, image, env_state, actions=None, is_pad=None, vq_sample=None):
|
||||
@ -279,10 +275,7 @@ def build(args):
|
||||
|
||||
transformer = build_transformer(args)
|
||||
|
||||
if args.no_encoder:
|
||||
encoder = None
|
||||
else:
|
||||
encoder = build_transformer(args)
|
||||
encoder = build_encoder(args)
|
||||
|
||||
model = DETRVAE(
|
||||
backbones,
|
||||
|
||||
@ -83,10 +83,10 @@ class BimanualViperXEETask(base.Task):
|
||||
# (2) get env._physics.named.data.xpos['vx300s_left/gripper_link']
|
||||
# get env._physics.named.data.xquat['vx300s_left/gripper_link']
|
||||
# repeat the same for right side
|
||||
np.copyto(physics.data.mocap_pos[0], [-0.31718881+0.1, 0.5, 0.29525084])
|
||||
np.copyto(physics.data.mocap_pos[0], [-0.31718881, 0.5, 0.29525084])
|
||||
np.copyto(physics.data.mocap_quat[0], [1, 0, 0, 0])
|
||||
# right
|
||||
np.copyto(physics.data.mocap_pos[1], np.array([0.31718881-0.1, 0.49999888, 0.29525084]))
|
||||
np.copyto(physics.data.mocap_pos[1], np.array([0.31718881, 0.49999888, 0.29525084]))
|
||||
np.copyto(physics.data.mocap_quat[1], [1, 0, 0, 0])
|
||||
|
||||
# reset gripper control
|
||||
@ -136,8 +136,8 @@ class BimanualViperXEETask(base.Task):
|
||||
obs['env_state'] = self.get_env_state(physics)
|
||||
obs['images'] = dict()
|
||||
obs['images']['top'] = physics.render(height=480, width=640, camera_id='top')
|
||||
# obs['images']['angle'] = physics.render(height=480, width=640, camera_id='angle')
|
||||
# obs['images']['vis'] = physics.render(height=480, width=640, camera_id='front_close')
|
||||
obs['images']['angle'] = physics.render(height=480, width=640, camera_id='angle')
|
||||
obs['images']['vis'] = physics.render(height=480, width=640, camera_id='front_close')
|
||||
# used in scripted policy to obtain starting pose
|
||||
obs['mocap_pose_left'] = np.concatenate([physics.data.mocap_pos[0], physics.data.mocap_quat[0]]).copy()
|
||||
obs['mocap_pose_right'] = np.concatenate([physics.data.mocap_pos[1], physics.data.mocap_quat[1]]).copy()
|
||||
|
||||
@ -39,21 +39,21 @@ def main(args):
|
||||
eval_every = args['eval_every']
|
||||
validate_every = args['validate_every']
|
||||
save_every = args['save_every']
|
||||
resume_ckpt_path = args['resume_ckpt_path']
|
||||
|
||||
# get task parameters
|
||||
is_sim = task_name[:4] == 'sim_'
|
||||
if is_sim or task_name == 'all':
|
||||
# if is_sim:
|
||||
# from constants import SIM_TASK_CONFIGS
|
||||
# task_config = SIM_TASK_CONFIGS[task_name]
|
||||
# else:
|
||||
# from aloha_scripts.constants import TASK_CONFIGS
|
||||
# task_config = TASK_CONFIGS[task_name]
|
||||
from constants import SIM_TASK_CONFIGS
|
||||
task_config = SIM_TASK_CONFIGS[task_name]
|
||||
else:
|
||||
from aloha_scripts.constants import TASK_CONFIGS
|
||||
task_config = TASK_CONFIGS[task_name]
|
||||
dataset_dir = task_config['dataset_dir']
|
||||
num_episodes = task_config['num_episodes']
|
||||
episode_len = task_config['episode_len']
|
||||
camera_names = task_config['camera_names']
|
||||
name_filter = task_config.get('name_filter', lambda n: True)
|
||||
|
||||
# fixed parameters
|
||||
state_dim = 14
|
||||
@ -77,8 +77,7 @@ def main(args):
|
||||
'vq': args['use_vq'],
|
||||
'vq_class': args['vq_class'],
|
||||
'vq_dim': args['vq_dim'],
|
||||
'action_dim': 16,
|
||||
'no_encoder': args['no_encoder'],
|
||||
'action_dim': 16
|
||||
}
|
||||
elif policy_class == 'CNNMLP':
|
||||
policy_config = {'lr': args['lr'], 'lr_backbone': lr_backbone, 'backbone' : backbone, 'num_queries': 1,
|
||||
@ -92,7 +91,6 @@ def main(args):
|
||||
'validate_every': validate_every,
|
||||
'save_every': save_every,
|
||||
'ckpt_dir': ckpt_dir,
|
||||
'resume_ckpt_path': resume_ckpt_path,
|
||||
'episode_len': episode_len,
|
||||
'state_dim': state_dim,
|
||||
'lr': args['lr'],
|
||||
@ -109,8 +107,7 @@ def main(args):
|
||||
if not os.path.isdir(ckpt_dir):
|
||||
os.makedirs(ckpt_dir)
|
||||
config_path = os.path.join(ckpt_dir, 'config.pkl')
|
||||
expr_name = ckpt_dir.split('/')[-1]
|
||||
wandb.init(project="mobile-aloha", reinit=True, entity="mobile-aloha", name=expr_name)
|
||||
wandb.init(project="mobile-aloha", reinit=True, entity="tonyzhao")
|
||||
wandb.config.update(config)
|
||||
with open(config_path, 'wb') as f:
|
||||
pickle.dump(config, f)
|
||||
@ -118,7 +115,7 @@ def main(args):
|
||||
ckpt_names = [f'policy_last.ckpt']
|
||||
results = []
|
||||
for ckpt_name in ckpt_names:
|
||||
success_rate, avg_return = eval_bc(config, ckpt_name, save_episode=True, num_rollouts=50)
|
||||
success_rate, avg_return = eval_bc(config, ckpt_name, save_episode=True)
|
||||
wandb.log({'success_rate': success_rate, 'avg_return': avg_return})
|
||||
results.append([ckpt_name, success_rate, avg_return])
|
||||
|
||||
@ -127,7 +124,7 @@ def main(args):
|
||||
print()
|
||||
exit()
|
||||
|
||||
train_dataloader, val_dataloader, stats, _ = load_data(dataset_dir, name_filter, camera_names, batch_size_train, batch_size_val, args['chunk_size'], args['skip_mirrored_data'])
|
||||
train_dataloader, val_dataloader, stats, _ = load_data(dataset_dir, num_episodes, camera_names, batch_size_train, batch_size_val, args['chunk_size'])
|
||||
|
||||
# save dataset stats
|
||||
stats_path = os.path.join(ckpt_dir, f'dataset_stats.pkl')
|
||||
@ -174,7 +171,7 @@ def get_image(ts, camera_names):
|
||||
return curr_image
|
||||
|
||||
|
||||
def eval_bc(config, ckpt_name, save_episode=True, num_rollouts=50):
|
||||
def eval_bc(config, ckpt_name, save_episode=True):
|
||||
set_seed(1000)
|
||||
ckpt_dir = config['ckpt_dir']
|
||||
state_dim = config['state_dim']
|
||||
@ -232,6 +229,7 @@ def eval_bc(config, ckpt_name, save_episode=True, num_rollouts=50):
|
||||
|
||||
max_timesteps = int(max_timesteps * 1) # may increase for real-world tasks
|
||||
|
||||
num_rollouts = 50
|
||||
episode_returns = []
|
||||
highest_rewards = []
|
||||
for rollout_id in range(num_rollouts):
|
||||
@ -315,10 +313,7 @@ def eval_bc(config, ckpt_name, save_episode=True, num_rollouts=50):
|
||||
base_action = action[-2:]
|
||||
|
||||
### step the environment
|
||||
if real_robot:
|
||||
ts = env.step(target_qpos, base_action)
|
||||
else:
|
||||
ts = env.step(target_qpos)
|
||||
|
||||
### for visualization
|
||||
qpos_list.append(qpos_numpy)
|
||||
@ -380,9 +375,6 @@ def train_bc(train_dataloader, val_dataloader, config):
|
||||
set_seed(seed)
|
||||
|
||||
policy = make_policy(policy_class, policy_config)
|
||||
if config['resume_ckpt_path'] is not None:
|
||||
loading_status = policy.load_state_dict(torch.load(config['resume_ckpt_path']))
|
||||
print(f'Resume policy from: {config["resume_ckpt_path"]}, Status: {loading_status}')
|
||||
policy.cuda()
|
||||
optimizer = make_optimizer(policy_class, policy)
|
||||
|
||||
@ -419,12 +411,10 @@ def train_bc(train_dataloader, val_dataloader, config):
|
||||
|
||||
# evaluation
|
||||
if (step > 0) and (step % eval_every == 0):
|
||||
# first save then eval
|
||||
ckpt_name = f'policy_step_{step}_seed_{seed}.ckpt'
|
||||
ckpt_path = os.path.join(ckpt_dir, ckpt_name)
|
||||
torch.save(policy.state_dict(), ckpt_path)
|
||||
success, _ = eval_bc(config, ckpt_name, save_episode=True, num_rollouts=20)
|
||||
wandb.log({'success': success}, step=step)
|
||||
state_dict = deepcopy(policy.state_dict())
|
||||
success_with_te, _ = eval_bc(config, f'TE_step{step}', state_dict, temporal_agg=True, save_episode=True, num_rollouts=5)
|
||||
success_without_te, _ = eval_bc(config, f'step{step}', state_dict, temporal_agg=False, save_episode=True, num_rollouts=5)
|
||||
wandb.log({'success_with_te':success_with_te, 'success_without_te':success_without_te}, step=step)
|
||||
|
||||
# training
|
||||
policy.train()
|
||||
@ -474,8 +464,6 @@ if __name__ == '__main__':
|
||||
parser.add_argument('--eval_every', action='store', type=int, default=500, help='eval_every', required=False)
|
||||
parser.add_argument('--validate_every', action='store', type=int, default=500, help='validate_every', required=False)
|
||||
parser.add_argument('--save_every', action='store', type=int, default=500, help='save_every', required=False)
|
||||
parser.add_argument('--resume_ckpt_path', action='store', type=str, help='resume_ckpt_path', required=False)
|
||||
parser.add_argument('--skip_mirrored_data', action='store_true')
|
||||
|
||||
# for ACT
|
||||
parser.add_argument('--kl_weight', action='store', type=int, help='KL Weight', required=False)
|
||||
@ -486,6 +474,5 @@ if __name__ == '__main__':
|
||||
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')
|
||||
parser.add_argument('--no_encoder', action='store_true')
|
||||
|
||||
main(vars(parser.parse_args()))
|
||||
|
||||
@ -28,12 +28,11 @@ class ACTPolicy(nn.Module):
|
||||
|
||||
loss_dict = dict()
|
||||
a_hat, is_pad_hat, (mu, logvar), probs, binaries = self.model(qpos, image, env_state, actions, is_pad, vq_sample)
|
||||
if self.vq or self.model.encoder is None:
|
||||
if self.vq:
|
||||
total_kld = [torch.tensor(0.0)]
|
||||
loss_dict['vq_discrepancy'] = F.l1_loss(probs, binaries, reduction='mean')
|
||||
else:
|
||||
total_kld, dim_wise_kld, mean_kld = kl_divergence(mu, logvar)
|
||||
if self.vq:
|
||||
loss_dict['vq_discrepancy'] = F.l1_loss(probs, binaries, reduction='mean')
|
||||
all_l1 = F.l1_loss(actions, a_hat, reduction='none')
|
||||
l1 = (all_l1 * ~is_pad.unsqueeze(-1)).mean()
|
||||
loss_dict['l1'] = l1
|
||||
|
||||
@ -1,119 +0,0 @@
|
||||
import os
|
||||
import numpy as np
|
||||
import cv2
|
||||
import h5py
|
||||
import argparse
|
||||
import time
|
||||
from visualize_episodes import visualize_joints, visualize_timestamp, save_videos
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
from constants import DT
|
||||
|
||||
import IPython
|
||||
e = IPython.embed
|
||||
|
||||
JOINT_NAMES = ["waist", "shoulder", "elbow", "forearm_roll", "wrist_angle", "wrist_rotate"]
|
||||
STATE_NAMES = JOINT_NAMES + ["gripper"]
|
||||
|
||||
MIRROR_STATE_MULTIPLY = np.array([-1, 1, 1, -1, 1, -1, 1]).astype('float32')
|
||||
MIRROR_BASE_MULTIPLY = np.array([1, -1]).astype('float32')
|
||||
|
||||
def load_hdf5(dataset_dir, dataset_name):
|
||||
dataset_path = os.path.join(dataset_dir, dataset_name + '.hdf5')
|
||||
if not os.path.isfile(dataset_path):
|
||||
print(f'Dataset does not exist at \n{dataset_path}\n')
|
||||
exit()
|
||||
|
||||
with h5py.File(dataset_path, 'r') as root:
|
||||
is_sim = root.attrs['sim']
|
||||
qpos = root['/observations/qpos'][()]
|
||||
qvel = root['/observations/qvel'][()]
|
||||
action = root['/action'][()]
|
||||
image_dict = dict()
|
||||
for cam_name in root[f'/observations/images/'].keys():
|
||||
image_dict[cam_name] = root[f'/observations/images/{cam_name}'][()]
|
||||
if 'base_action' in root.keys():
|
||||
print('base_action exists')
|
||||
base_action = root['/base_action'][()]
|
||||
else:
|
||||
base_action = None
|
||||
|
||||
return qpos, qvel, action, base_action, image_dict
|
||||
|
||||
def main(args):
|
||||
dataset_dir = args['dataset_dir']
|
||||
num_episodes = args['num_episodes']
|
||||
|
||||
for episode_idx in range(num_episodes):
|
||||
dataset_name = f'episode_{episode_idx}'
|
||||
|
||||
qpos, qvel, action, base_action, image_dict = load_hdf5(dataset_dir, dataset_name)
|
||||
|
||||
# process proprioception
|
||||
qpos = np.concatenate([qpos[:, 7:] * MIRROR_STATE_MULTIPLY, qpos[:, :7] * MIRROR_STATE_MULTIPLY], axis=1)
|
||||
qvel = np.concatenate([qvel[:, 7:] * MIRROR_STATE_MULTIPLY, qvel[:, :7] * MIRROR_STATE_MULTIPLY], axis=1)
|
||||
action = np.concatenate([action[:, 7:] * MIRROR_STATE_MULTIPLY, action[:, :7] * MIRROR_STATE_MULTIPLY], axis=1)
|
||||
if base_action is not None:
|
||||
base_action = base_action * MIRROR_BASE_MULTIPLY
|
||||
|
||||
# mirror image obs
|
||||
if 'left_wrist' in image_dict.keys():
|
||||
image_dict['left_wrist'], image_dict['right_wrist'] = image_dict['right_wrist'][:, :, ::-1], image_dict['left_wrist'][:, :, ::-1]
|
||||
elif 'cam_left_wrist':
|
||||
image_dict['cam_left_wrist'], image_dict['cam_right_wrist'] = image_dict['cam_right_wrist'][:, :, ::-1], image_dict['cam_left_wrist'][:, :, ::-1]
|
||||
else:
|
||||
raise Exception('No left_wrist or cam_left_wrist in image_dict')
|
||||
|
||||
if 'top' in image_dict.keys():
|
||||
image_dict['top'] = image_dict['top'][:, :, ::-1]
|
||||
elif 'cam_high' in image_dict.keys():
|
||||
image_dict['cam_high'] = image_dict['cam_high'][:, :, ::-1]
|
||||
else:
|
||||
raise Exception('No top or cam_high in image_dict')
|
||||
|
||||
# saving
|
||||
data_dict = {
|
||||
'/observations/qpos': qpos,
|
||||
'/observations/qvel': qvel,
|
||||
'/action': action,
|
||||
'/base_action': base_action,
|
||||
} if base_action is not None else {
|
||||
'/observations/qpos': qpos,
|
||||
'/observations/qvel': qvel,
|
||||
'/action': action,
|
||||
}
|
||||
for cam_name in image_dict.keys():
|
||||
data_dict[f'/observations/images/{cam_name}'] = image_dict[cam_name]
|
||||
max_timesteps = len(qpos)
|
||||
|
||||
# HDF5
|
||||
t0 = time.time()
|
||||
dataset_path = os.path.join(dataset_dir, f'mirror_episode_{episode_idx}')
|
||||
with h5py.File(dataset_path + '.hdf5', 'w', rdcc_nbytes=1024 ** 2 * 2) as root:
|
||||
root.attrs['sim'] = True
|
||||
obs = root.create_group('observations')
|
||||
image = obs.create_group('images')
|
||||
for cam_name in image_dict.keys():
|
||||
_ = image.create_dataset(cam_name, (max_timesteps, 480, 640, 3), dtype='uint8',
|
||||
chunks=(1, 480, 640, 3), )
|
||||
# compression='gzip',compression_opts=2,)
|
||||
# compression=32001, compression_opts=(0, 0, 0, 0, 9, 1, 1), shuffle=False)
|
||||
qpos = obs.create_dataset('qpos', (max_timesteps, 14))
|
||||
qvel = obs.create_dataset('qvel', (max_timesteps, 14))
|
||||
action = root.create_dataset('action', (max_timesteps, 14))
|
||||
if base_action is not None:
|
||||
base_action = root.create_dataset('base_action', (max_timesteps, 2))
|
||||
|
||||
for name, array in data_dict.items():
|
||||
root[name][...] = array
|
||||
print(f'Saving {dataset_path}: {time.time() - t0:.1f} secs\n')
|
||||
|
||||
# save_videos(image_dict, DT, video_path=os.path.join(dataset_dir, dataset_name + '_mirror_video.mp4'))
|
||||
# visualize_joints(qpos, action, plot_path=os.path.join(dataset_dir, dataset_name + '_mirror_qpos.png'))
|
||||
# visualize_timestamp(t_list, dataset_path) # TODO addn timestamp back
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--dataset_dir', action='store', type=str, help='Dataset dir.', required=True)
|
||||
parser.add_argument('--num_episodes', action='store', type=int, help='Number of episodes.', required=True)
|
||||
main(vars(parser.parse_args()))
|
||||
@ -28,7 +28,7 @@ def main(args):
|
||||
num_episodes = args['num_episodes']
|
||||
onscreen_render = args['onscreen_render']
|
||||
inject_noise = False
|
||||
render_cam_name = 'top'
|
||||
render_cam_name = 'angle'
|
||||
|
||||
if not os.path.isdir(dataset_dir):
|
||||
os.makedirs(dataset_dir, exist_ok=True)
|
||||
@ -39,8 +39,6 @@ def main(args):
|
||||
policy_cls = PickAndTransferPolicy
|
||||
elif task_name == 'sim_insertion_scripted':
|
||||
policy_cls = InsertionPolicy
|
||||
elif task_name == 'sim_transfer_cube_scripted_mirror':
|
||||
policy_cls = PickAndTransferPolicy
|
||||
else:
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@ -1,48 +0,0 @@
|
||||
import os
|
||||
import h5py
|
||||
import argparse
|
||||
from collections import defaultdict
|
||||
from sim_env import make_sim_env
|
||||
from utils import sample_box_pose, sample_insertion_pose
|
||||
from sim_env import BOX_POSE
|
||||
from constants import DT
|
||||
from visualize_episodes import save_videos
|
||||
|
||||
import IPython
|
||||
e = IPython.embed
|
||||
|
||||
|
||||
def main(args):
|
||||
dataset_path = args['dataset_path']
|
||||
|
||||
|
||||
if not os.path.isfile(dataset_path):
|
||||
print(f'Dataset does not exist at \n{dataset_path}\n')
|
||||
exit()
|
||||
|
||||
with h5py.File(dataset_path, 'r') as root:
|
||||
actions = root['/action'][()]
|
||||
|
||||
env = make_sim_env('sim_transfer_cube')
|
||||
BOX_POSE[0] = sample_box_pose() # used in sim reset
|
||||
ts = env.reset()
|
||||
episode_replay = [ts]
|
||||
for action in actions:
|
||||
ts = env.step(action)
|
||||
episode_replay.append(ts)
|
||||
|
||||
# saving
|
||||
image_dict = defaultdict(lambda: [])
|
||||
while episode_replay:
|
||||
ts = episode_replay.pop(0)
|
||||
for cam_name, image in ts.observation['images'].items():
|
||||
image_dict[cam_name].append(image)
|
||||
|
||||
video_path = dataset_path.replace('episode_', 'replay_episode_').replace('hdf5', 'mp4')
|
||||
save_videos(image_dict, DT, video_path=video_path)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--dataset_path', action='store', type=str, help='Dataset path.', required=True)
|
||||
main(vars(parser.parse_args()))
|
||||
@ -108,10 +108,8 @@ class BimanualViperXTask(base.Task):
|
||||
obs['env_state'] = self.get_env_state(physics)
|
||||
obs['images'] = dict()
|
||||
obs['images']['top'] = physics.render(height=480, width=640, camera_id='top')
|
||||
obs['images']['left_wrist'] = physics.render(height=480, width=640, camera_id='left_wrist')
|
||||
obs['images']['right_wrist'] = physics.render(height=480, width=640, camera_id='right_wrist')
|
||||
# obs['images']['angle'] = physics.render(height=480, width=640, camera_id='angle')
|
||||
# obs['images']['vis'] = physics.render(height=480, width=640, camera_id='front_close')
|
||||
obs['images']['angle'] = physics.render(height=480, width=640, camera_id='angle')
|
||||
obs['images']['vis'] = physics.render(height=480, width=640, camera_id='front_close')
|
||||
|
||||
return obs
|
||||
|
||||
|
||||
@ -47,7 +47,6 @@ def main(args):
|
||||
num_episodes = task_config['num_episodes']
|
||||
episode_len = task_config['episode_len']
|
||||
camera_names = task_config['camera_names']
|
||||
name_filter = task_config.get('name_filter', lambda n: True)
|
||||
|
||||
# fixed parameters
|
||||
state_dim = 14
|
||||
@ -106,7 +105,7 @@ def main(args):
|
||||
# print()
|
||||
# exit()
|
||||
|
||||
train_dataloader, val_dataloader, stats, _ = load_data(dataset_dir, name_filter, camera_names, batch_size_train, batch_size_val)
|
||||
train_dataloader, val_dataloader, stats, _ = load_data(dataset_dir, num_episodes, camera_names, batch_size_train, batch_size_val)
|
||||
|
||||
# save dataset stats
|
||||
# if not os.path.isdir(ckpt_dir):
|
||||
|
||||
28
utils.py
28
utils.py
@ -38,10 +38,7 @@ class EpisodicDataset(torch.utils.data.Dataset):
|
||||
try:
|
||||
# print(dataset_path)
|
||||
with h5py.File(dataset_path, 'r') as root:
|
||||
try: # some legacy data does not have this attribute
|
||||
is_sim = root.attrs['sim']
|
||||
except:
|
||||
is_sim = False
|
||||
is_sim = False # root.attrs['sim']
|
||||
if '/base_action' in root:
|
||||
base_action = root['/base_action'][()]
|
||||
base_action = preprocess_base_action(base_action)
|
||||
@ -96,7 +93,7 @@ class EpisodicDataset(torch.utils.data.Dataset):
|
||||
qpos_data = (qpos_data - self.norm_stats["qpos_mean"]) / self.norm_stats["qpos_std"]
|
||||
|
||||
except:
|
||||
print(f'Error loading {dataset_path} in __getitem__')
|
||||
print(f'Error loading {dataset_path}')
|
||||
quit()
|
||||
|
||||
# print(image_data.dtype, qpos_data.dtype, action_data.dtype, is_pad.dtype)
|
||||
@ -121,9 +118,8 @@ def get_norm_stats(dataset_path_list):
|
||||
action = root['/action'][()]
|
||||
dummy_base_action = np.zeros([action.shape[0], 2])
|
||||
action = np.concatenate([action, dummy_base_action], axis=-1)
|
||||
except Exception as e:
|
||||
print(f'Error loading {dataset_path} in get_norm_stats')
|
||||
print(e)
|
||||
except:
|
||||
print(f'Error loading {dataset_path}')
|
||||
quit()
|
||||
all_qpos_data.append(torch.from_numpy(qpos))
|
||||
all_action_data.append(torch.from_numpy(action))
|
||||
@ -147,24 +143,22 @@ def get_norm_stats(dataset_path_list):
|
||||
|
||||
return stats, all_episode_len
|
||||
|
||||
def find_all_hdf5(dataset_dir, skip_mirrored_data):
|
||||
def find_all_hdf5(dataset_dir):
|
||||
hdf5_files = []
|
||||
for root, dirs, files in os.walk(dataset_dir):
|
||||
for filename in fnmatch.filter(files, '*.hdf5'):
|
||||
if skip_mirrored_data and 'mirror' in filename:
|
||||
continue
|
||||
hdf5_files.append(os.path.join(root, filename))
|
||||
print(f'Found {len(hdf5_files)} hdf5 files')
|
||||
return hdf5_files
|
||||
|
||||
|
||||
def load_data(dataset_dir, name_filter, camera_names, batch_size_train, batch_size_val, chunk_size, skip_mirrored_data=False):
|
||||
dataset_path_list = find_all_hdf5(dataset_dir, skip_mirrored_data)
|
||||
dataset_path_list = [n for n in dataset_path_list if name_filter(n)]
|
||||
def load_data(dataset_dir, num_episodes, camera_names, batch_size_train, batch_size_val, chunk_size):
|
||||
del num_episodes
|
||||
dataset_path_list = find_all_hdf5(dataset_dir)
|
||||
dataset_path_list = [p for p in dataset_path_list if 'sim' not in p] ### TODO filter out sim data
|
||||
num_episodes = len(dataset_path_list)
|
||||
|
||||
# obtain train test split
|
||||
train_ratio = 0.995
|
||||
train_ratio = 0.998
|
||||
shuffled_episode_ids = np.random.permutation(num_episodes)
|
||||
train_episode_ids = shuffled_episode_ids[:int(train_ratio * num_episodes)]
|
||||
val_episode_ids = shuffled_episode_ids[int(train_ratio * num_episodes):]
|
||||
@ -194,7 +188,7 @@ def calibrate_linear_vel(base_action):
|
||||
def smooth_base_action(base_action):
|
||||
return np.stack([
|
||||
np.convolve(base_action[:, i], np.ones(20)/20, mode='same') for i in range(base_action.shape[1])
|
||||
], axis=-1).astype(np.float32)
|
||||
], axis=-1, dtype=np.float32)
|
||||
|
||||
def preprocess_base_action(base_action):
|
||||
base_action = calibrate_linear_vel(base_action)
|
||||
|
||||
@ -33,10 +33,6 @@ def load_hdf5(dataset_dir, dataset_name):
|
||||
def main(args):
|
||||
dataset_dir = args['dataset_dir']
|
||||
episode_idx = args['episode_idx']
|
||||
ismirror = args['ismirror']
|
||||
if ismirror:
|
||||
dataset_name = f'mirror_episode_{episode_idx}'
|
||||
else:
|
||||
dataset_name = f'episode_{episode_idx}'
|
||||
|
||||
qpos, qvel, action, image_dict = load_hdf5(dataset_dir, dataset_name)
|
||||
@ -48,7 +44,6 @@ def main(args):
|
||||
def save_videos(video, dt, video_path=None):
|
||||
if isinstance(video, list):
|
||||
cam_names = list(video[0].keys())
|
||||
cam_names = sorted(cam_names)
|
||||
h, w, _ = video[0][cam_names[0]].shape
|
||||
w = w * len(cam_names)
|
||||
fps = int(1/dt)
|
||||
@ -65,7 +60,6 @@ def save_videos(video, dt, video_path=None):
|
||||
print(f'Saved video to: {video_path}')
|
||||
elif isinstance(video, dict):
|
||||
cam_names = list(video.keys())
|
||||
cam_names = sorted(cam_names)
|
||||
all_cam_videos = []
|
||||
for cam_name in cam_names:
|
||||
all_cam_videos.append(video[cam_name])
|
||||
@ -150,5 +144,4 @@ if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--dataset_dir', action='store', type=str, help='Dataset dir.', required=True)
|
||||
parser.add_argument('--episode_idx', action='store', type=int, help='Episode index.', required=False)
|
||||
parser.add_argument('--ismirror', action='store_true')
|
||||
main(vars(parser.parse_args()))
|
||||
|
||||
在新工单中引用
屏蔽一个用户