Merge branch 'main' of github.com:MarkFzp/act-plus-plus into main
这个提交包含在:
1
byol_pytorch
子模块
1
byol_pytorch
子模块
子模块 byol_pytorch 已添加到 bcd1b066c3
155
commands.txt
155
commands.txt
@ -113,6 +113,161 @@ CUDA_VISIBLE_DEVICES=1 python3 imitate_episodes.py \
|
|||||||
--batch_size 32 --lr 1e-4 --seed 0 \
|
--batch_size 32 --lr 1e-4 --seed 0 \
|
||||||
--num_steps 100000 --eval_every 2000 --validate_every 2000 --save_every 2000
|
--num_steps 100000 --eval_every 2000 --validate_every 2000 --save_every 2000
|
||||||
|
|
||||||
|
# Dec 10
|
||||||
|
|
||||||
|
######################## more diffusion ########################
|
||||||
|
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_diffusion_sweep_3_chunk64 \
|
||||||
|
--policy_class Diffusion --chunk_size 64 \
|
||||||
|
--batch_size 32 --lr 1e-4 --seed 0 \
|
||||||
|
--num_steps 200000 --eval_every 4000 --validate_every 4000 --save_every 4000
|
||||||
|
|
||||||
|
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_diffusion_sweep_4_regressionTest \
|
||||||
|
--policy_class Diffusion --chunk_size 32 \
|
||||||
|
--batch_size 32 --lr 1e-4 --seed 0 \
|
||||||
|
--num_steps 200000 --eval_every 6000 --validate_every 6000 --save_every 6000
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
######################## VINN ########################
|
||||||
|
|
||||||
|
|
||||||
|
conda activate mobile
|
||||||
|
cd /home/tonyzhao/Research/act-plus-plus/byol_pytorch/examples/lightning
|
||||||
|
CUDA_VISIBLE_DEVICES=1 python3 train.py --dataset_dir /scr/tonyzhao/datasets/sim_transfer_cube_scripted --cam_name top --seed 0
|
||||||
|
|
||||||
|
conda activate mobile
|
||||||
|
cd /home/tonyzhao/Research/act-plus-plus/byol_pytorch/examples/lightning
|
||||||
|
CUDA_VISIBLE_DEVICES=0 python3 train.py --dataset_dir /scr/tonyzhao/datasets/sim_transfer_cube_scripted --cam_name left_wrist --seed 0
|
||||||
|
|
||||||
|
conda activate mobile
|
||||||
|
cd /home/tonyzhao/Research/act-plus-plus/byol_pytorch/examples/lightning
|
||||||
|
CUDA_VISIBLE_DEVICES=1 python3 train.py --dataset_dir /scr/tonyzhao/datasets/sim_transfer_cube_scripted --cam_name right_wrist --seed 0
|
||||||
|
|
||||||
|
conda activate mobile
|
||||||
|
cd /home/tonyzhao/Research/act-plus-plus
|
||||||
|
TASK_NAME=sim_transfer_cube_scripted
|
||||||
|
python3 vinn_cache_feature.py --ckpt_path /home/tonyzhao/Research/act-plus-plus/byol_pytorch/examples/lightning/byol-${TASK_NAME}-DUMMY-seed-0.pt
|
||||||
|
|
||||||
|
TASK_NAME=sim_transfer_cube_scripted
|
||||||
|
python3 vinn_select_k.py \
|
||||||
|
--dataset_dir /scr/tonyzhao/datasets/sim_transfer_cube_scripted \
|
||||||
|
--ckpt_dir /scr/tonyzhao/train_logs/VINN-eval-seed-0-test
|
||||||
|
|
||||||
|
python3 vinn_eval.py \
|
||||||
|
--dataset_dir /scr/tonyzhao/datasets/sim_transfer_cube_scripted \
|
||||||
|
--model_dir /home/tonyzhao/Research/act-plus-plus/byol_pytorch/examples/lightning/byol-${TASK_NAME}-DUMMY-seed-0.pt \
|
||||||
|
--ckpt_dir /scr/tonyzhao/train_logs/VINN-eval-seed-0-test \
|
||||||
|
--task_name $TASK_NAME
|
||||||
|
|
||||||
|
## TODO
|
||||||
|
make sure env is consistent
|
||||||
|
tune a bit more
|
||||||
|
|
||||||
|
|
||||||
|
######################## VINN Real ########################
|
||||||
|
|
||||||
|
### test backward compatibility
|
||||||
|
|
||||||
|
conda activate mobile
|
||||||
|
cd /home/tonyzhao/Research/act-plus-plus/byol_pytorch/examples/lightning
|
||||||
|
CUDA_VISIBLE_DEVICES=1 python3 train.py --task sim_transfer_cube_scripted --cam_name top --seed 0
|
||||||
|
CUDA_VISIBLE_DEVICES=1 python3 train.py --task sim_transfer_cube_scripted --cam_name left_wrist --seed 0
|
||||||
|
|
||||||
|
conda activate mobile
|
||||||
|
cd /home/tonyzhao/Research/act-plus-plus/byol_pytorch/examples/lightning
|
||||||
|
CUDA_VISIBLE_DEVICES=1 python3 train.py --task sim_transfer_cube_scripted --cam_name right_wrist --seed 0
|
||||||
|
|
||||||
|
|
||||||
|
conda activate mobile
|
||||||
|
cd /home/tonyzhao/Research/act-plus-plus
|
||||||
|
TASK_NAME=sim_transfer_cube_scripted
|
||||||
|
python3 vinn_cache_feature.py --ckpt_path /home/tonyzhao/Research/act-plus-plus/byol_pytorch/examples/lightning/byol-${TASK_NAME}-DUMMY-seed-0.pt
|
||||||
|
|
||||||
|
TASK_NAME=sim_transfer_cube_scripted
|
||||||
|
python3 vinn_select_k.py \
|
||||||
|
--dataset_dir /scr/tonyzhao/datasets/sim_transfer_cube_scripted \
|
||||||
|
--ckpt_dir /scr/tonyzhao/train_logs/VINN-eval-seed-0-test
|
||||||
|
|
||||||
|
python3 vinn_eval.py \
|
||||||
|
--dataset_dir /scr/tonyzhao/datasets/sim_transfer_cube_scripted \
|
||||||
|
--model_dir /home/tonyzhao/Research/act-plus-plus/byol_pytorch/examples/lightning/byol-${TASK_NAME}-DUMMY-seed-0.pt \
|
||||||
|
--ckpt_dir /scr/tonyzhao/train_logs/VINN-eval-seed-0-test \
|
||||||
|
--task_name $TASK_NAME
|
||||||
|
|
||||||
|
### new data loader passed backward compatibility
|
||||||
|
|
||||||
|
|
||||||
|
conda activate mobile
|
||||||
|
cd /home/tonyzhao/Research/act-plus-plus/byol_pytorch/examples/lightning
|
||||||
|
#CUDA_VISIBLE_DEVICES=1 python3 train.py --task aloha_mobile_wipe_wine --cam_name cam_high --seed 0
|
||||||
|
#CUDA_VISIBLE_DEVICES=1 python3 train.py --task aloha_mobile_wipe_wine --cam_name cam_left_wrist --seed 0
|
||||||
|
#CUDA_VISIBLE_DEVICES=1 python3 train.py --task aloha_mobile_wipe_wine --cam_name cam_right_wrist --seed 0
|
||||||
|
|
||||||
|
#CUDA_VISIBLE_DEVICES=1 python3 train.py --task aloha_mobile_wipe_wine_cotrain --cam_name cam_high --seed 0
|
||||||
|
#CUDA_VISIBLE_DEVICES=1 python3 train.py --task aloha_mobile_wipe_wine_cotrain --cam_name cam_left_wrist --seed 0
|
||||||
|
CUDA_VISIBLE_DEVICES=1 python3 train.py --task aloha_mobile_wipe_wine_cotrain --cam_name cam_right_wrist --seed 0
|
||||||
|
|
||||||
|
|
||||||
|
conda activate mobile
|
||||||
|
cd /home/tonyzhao/Research/act-plus-plus/byol_pytorch/examples/lightning
|
||||||
|
#CUDA_VISIBLE_DEVICES=1 python3 train.py --task aloha_mobile_wash_pan --cam_name cam_high --seed 0
|
||||||
|
#CUDA_VISIBLE_DEVICES=1 python3 train.py --task aloha_mobile_wash_pan --cam_name cam_left_wrist --seed 0
|
||||||
|
#CUDA_VISIBLE_DEVICES=1 python3 train.py --task aloha_mobile_wash_pan --cam_name cam_right_wrist --seed 0
|
||||||
|
|
||||||
|
#CUDA_VISIBLE_DEVICES=1 python3 train.py --task aloha_mobile_wash_pan_cotrain --cam_name cam_high --seed 0
|
||||||
|
#CUDA_VISIBLE_DEVICES=1 python3 train.py --task aloha_mobile_wash_pan_cotrain --cam_name cam_left_wrist --seed 0
|
||||||
|
CUDA_VISIBLE_DEVICES=1 python3 train.py --task aloha_mobile_wash_pan_cotrain --cam_name cam_right_wrist --seed 0
|
||||||
|
|
||||||
|
|
||||||
|
conda activate mobile
|
||||||
|
cd /home/tonyzhao/Research/act-plus-plus/byol_pytorch/examples/lightning
|
||||||
|
CUDA_VISIBLE_DEVICES=1 python3 train.py --task aloha_mobile_wipe_wine_cotrain --cam_name cam_right_wrist --seed 0
|
||||||
|
CUDA_VISIBLE_DEVICES=1 python3 train.py --task aloha_mobile_elevator_truncated --cam_name cam_high --seed 0
|
||||||
|
CUDA_VISIBLE_DEVICES=1 python3 train.py --task aloha_mobile_elevator_truncated --cam_name cam_left_wrist --seed 0
|
||||||
|
CUDA_VISIBLE_DEVICES=1 python3 train.py --task aloha_mobile_elevator_truncated --cam_name cam_right_wrist --seed 0
|
||||||
|
|
||||||
|
|
||||||
|
conda activate mobile
|
||||||
|
cd /home/tonyzhao/Research/act-plus-plus/byol_pytorch/examples/lightning
|
||||||
|
CUDA_VISIBLE_DEVICES=1 python3 train.py --task aloha_mobile_wash_pan_cotrain --cam_name cam_right_wrist --seed 0
|
||||||
|
CUDA_VISIBLE_DEVICES=1 python3 train.py --task aloha_mobile_elevator_truncated_cotrain --cam_name cam_high --seed 0
|
||||||
|
CUDA_VISIBLE_DEVICES=1 python3 train.py --task aloha_mobile_elevator_truncated_cotrain --cam_name cam_left_wrist --seed 0
|
||||||
|
CUDA_VISIBLE_DEVICES=1 python3 train.py --task aloha_mobile_elevator_truncated_cotrain --cam_name cam_right_wrist --seed 0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# eval on real robot
|
||||||
|
|
||||||
|
conda activate aloha
|
||||||
|
cd /home/mobile-aloha/interbotix_ws/src/act
|
||||||
|
TASK_NAME=aloha_mobile_wipe_wine
|
||||||
|
python3 vinn_cache_feature.py --ckpt_path /home/mobile-aloha/interbotix_ws/src/act/ckpts/vinn_ckpts/byol-${TASK_NAME}-DUMMY-seed-0.pt
|
||||||
|
|
||||||
|
|
||||||
|
TASK_NAME=aloha_mobile_wipe_wine
|
||||||
|
python3 vinn_select_k.py \
|
||||||
|
--dataset_dir /home/mobile-aloha/data/${TASK_NAME} \
|
||||||
|
--ckpt_dir /home/mobile-aloha/interbotix_ws/src/act/ckpts/vinn_ckpts/VINN-eval-seed-0-test \
|
||||||
|
|
||||||
|
|
||||||
|
TASK_NAME=aloha_mobile_wipe_wine
|
||||||
|
python3 vinn_eval.py \
|
||||||
|
--dataset_dir /home/mobile-aloha/data/${TASK_NAME} \
|
||||||
|
--model_dir /home/mobile-aloha/interbotix_ws/src/act/ckpts/vinn_ckpts/byol-${TASK_NAME}-DUMMY-seed-0.pt \
|
||||||
|
--ckpt_dir /home/mobile-aloha/interbotix_ws/src/act/ckpts/vinn_ckpts/VINN-eval-seed-0-test \
|
||||||
|
--task_name $TASK_NAME
|
||||||
|
|
||||||
|
|
||||||
---------------------------------------------------------------------------------------
|
---------------------------------------------------------------------------------------
|
||||||
|
|||||||
@ -232,7 +232,7 @@ def eval_bc(config, ckpt_name, save_episode=True, num_rollouts=50):
|
|||||||
# load policy and stats
|
# load policy and stats
|
||||||
ckpt_path = os.path.join(ckpt_dir, ckpt_name)
|
ckpt_path = os.path.join(ckpt_dir, ckpt_name)
|
||||||
policy = make_policy(policy_class, policy_config)
|
policy = make_policy(policy_class, policy_config)
|
||||||
loading_status = policy.load_state_dict(torch.load(ckpt_path))
|
loading_status = policy.deserialize(torch.load(ckpt_path))
|
||||||
print(loading_status)
|
print(loading_status)
|
||||||
policy.cuda()
|
policy.cuda()
|
||||||
policy.eval()
|
policy.eval()
|
||||||
@ -241,7 +241,7 @@ def eval_bc(config, ckpt_name, save_episode=True, num_rollouts=50):
|
|||||||
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_last.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.deserialize(torch.load(latent_model_ckpt_path))
|
||||||
latent_model.eval()
|
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}')
|
||||||
@ -527,10 +527,10 @@ def train_bc(train_dataloader, val_dataloader, config):
|
|||||||
|
|
||||||
policy = make_policy(policy_class, policy_config)
|
policy = make_policy(policy_class, policy_config)
|
||||||
if config['load_pretrain']:
|
if config['load_pretrain']:
|
||||||
loading_status = policy.load_state_dict(torch.load(os.path.join('/home/zfu/interbotix_ws/src/act/ckpts/pretrain_all', 'policy_step_50000_seed_0.ckpt')))
|
loading_status = policy.deserialize(torch.load(os.path.join('/home/zfu/interbotix_ws/src/act/ckpts/pretrain_all', 'policy_step_50000_seed_0.ckpt')))
|
||||||
print(f'loaded! {loading_status}')
|
print(f'loaded! {loading_status}')
|
||||||
if config['resume_ckpt_path'] is not None:
|
if config['resume_ckpt_path'] is not None:
|
||||||
loading_status = policy.load_state_dict(torch.load(config['resume_ckpt_path']))
|
loading_status = policy.deserialize(torch.load(config['resume_ckpt_path']))
|
||||||
print(f'Resume policy from: {config["resume_ckpt_path"]}, Status: {loading_status}')
|
print(f'Resume policy from: {config["resume_ckpt_path"]}, Status: {loading_status}')
|
||||||
policy.cuda()
|
policy.cuda()
|
||||||
optimizer = make_optimizer(policy_class, policy)
|
optimizer = make_optimizer(policy_class, policy)
|
||||||
@ -558,7 +558,7 @@ def train_bc(train_dataloader, val_dataloader, config):
|
|||||||
epoch_val_loss = validation_summary['loss']
|
epoch_val_loss = validation_summary['loss']
|
||||||
if epoch_val_loss < min_val_loss:
|
if epoch_val_loss < min_val_loss:
|
||||||
min_val_loss = epoch_val_loss
|
min_val_loss = epoch_val_loss
|
||||||
best_ckpt_info = (step, min_val_loss, deepcopy(policy.state_dict()))
|
best_ckpt_info = (step, min_val_loss, deepcopy(policy.serialize()))
|
||||||
for k in list(validation_summary.keys()):
|
for k in list(validation_summary.keys()):
|
||||||
validation_summary[f'val_{k}'] = validation_summary.pop(k)
|
validation_summary[f'val_{k}'] = validation_summary.pop(k)
|
||||||
wandb.log(validation_summary, step=step)
|
wandb.log(validation_summary, step=step)
|
||||||
@ -573,7 +573,7 @@ def train_bc(train_dataloader, val_dataloader, config):
|
|||||||
# first save then eval
|
# first save then eval
|
||||||
ckpt_name = f'policy_step_{step}_seed_{seed}.ckpt'
|
ckpt_name = f'policy_step_{step}_seed_{seed}.ckpt'
|
||||||
ckpt_path = os.path.join(ckpt_dir, ckpt_name)
|
ckpt_path = os.path.join(ckpt_dir, ckpt_name)
|
||||||
torch.save(policy.state_dict(), ckpt_path)
|
torch.save(policy.serialize(), ckpt_path)
|
||||||
success, _ = eval_bc(config, ckpt_name, save_episode=True, num_rollouts=10)
|
success, _ = eval_bc(config, ckpt_name, save_episode=True, num_rollouts=10)
|
||||||
wandb.log({'success': success}, step=step)
|
wandb.log({'success': success}, step=step)
|
||||||
|
|
||||||
@ -590,10 +590,10 @@ def train_bc(train_dataloader, val_dataloader, config):
|
|||||||
|
|
||||||
if step % save_every == 0:
|
if step % save_every == 0:
|
||||||
ckpt_path = os.path.join(ckpt_dir, f'policy_step_{step}_seed_{seed}.ckpt')
|
ckpt_path = os.path.join(ckpt_dir, f'policy_step_{step}_seed_{seed}.ckpt')
|
||||||
torch.save(policy.state_dict(), ckpt_path)
|
torch.save(policy.serialize(), ckpt_path)
|
||||||
|
|
||||||
ckpt_path = os.path.join(ckpt_dir, f'policy_last.ckpt')
|
ckpt_path = os.path.join(ckpt_dir, f'policy_last.ckpt')
|
||||||
torch.save(policy.state_dict(), ckpt_path)
|
torch.save(policy.serialize(), ckpt_path)
|
||||||
|
|
||||||
best_step, min_val_loss, best_state_dict = best_ckpt_info
|
best_step, min_val_loss, best_state_dict = best_ckpt_info
|
||||||
ckpt_path = os.path.join(ckpt_dir, f'policy_step_{best_step}_seed_{seed}.ckpt')
|
ckpt_path = os.path.join(ckpt_dir, f'policy_step_{best_step}_seed_{seed}.ckpt')
|
||||||
|
|||||||
77
policy.py
77
policy.py
@ -43,31 +43,35 @@ class DiffusionPolicy(nn.Module):
|
|||||||
backbones.append(ResNet18Conv(**{'input_channel': 3, 'pretrained': False, 'input_coord_conv': False}))
|
backbones.append(ResNet18Conv(**{'input_channel': 3, 'pretrained': False, 'input_coord_conv': False}))
|
||||||
pools.append(SpatialSoftmax(**{'input_shape': [512, 15, 20], 'num_kp': self.num_kp, 'temperature': 1.0, 'learnable_temperature': False, 'noise_std': 0.0}))
|
pools.append(SpatialSoftmax(**{'input_shape': [512, 15, 20], 'num_kp': self.num_kp, 'temperature': 1.0, 'learnable_temperature': False, 'noise_std': 0.0}))
|
||||||
linears.append(torch.nn.Linear(int(np.prod([self.num_kp, 2])), self.feature_dimension))
|
linears.append(torch.nn.Linear(int(np.prod([self.num_kp, 2])), self.feature_dimension))
|
||||||
self.backbones = nn.ModuleList(backbones)
|
backbones = nn.ModuleList(backbones)
|
||||||
self.pools = nn.ModuleList(pools)
|
pools = nn.ModuleList(pools)
|
||||||
self.linears = nn.ModuleList(linears)
|
linears = nn.ModuleList(linears)
|
||||||
|
|
||||||
self.backbones = replace_bn_with_gn(self.backbones) # TODO
|
backbones = replace_bn_with_gn(backbones) # TODO
|
||||||
|
|
||||||
|
|
||||||
self.noise_pred_net = ConditionalUnet1D(
|
noise_pred_net = ConditionalUnet1D(
|
||||||
input_dim=self.ac_dim,
|
input_dim=self.ac_dim,
|
||||||
global_cond_dim=self.obs_dim*self.observation_horizon
|
global_cond_dim=self.obs_dim*self.observation_horizon
|
||||||
)
|
)
|
||||||
|
|
||||||
nets = nn.ModuleDict({
|
nets = nn.ModuleDict({
|
||||||
'policy': nn.ModuleDict({
|
'policy': nn.ModuleDict({
|
||||||
'backbones': self.backbones,
|
'backbones': backbones,
|
||||||
'pools': self.pools,
|
'pools': pools,
|
||||||
'linears': self.linears,
|
'linears': linears,
|
||||||
'noise_pred_net': self.noise_pred_net
|
'noise_pred_net': noise_pred_net
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
nets = nets.float()
|
nets = nets.float().cuda()
|
||||||
|
ENABLE_EMA = True
|
||||||
self.ema = EMAModel(model=nets, power=self.ema_power)
|
if ENABLE_EMA:
|
||||||
|
ema = EMAModel(model=nets, power=self.ema_power)
|
||||||
|
else:
|
||||||
|
ema = None
|
||||||
self.nets = nets
|
self.nets = nets
|
||||||
|
self.ema = ema
|
||||||
|
|
||||||
# setup noise scheduler
|
# setup noise scheduler
|
||||||
self.noise_scheduler = DDIMScheduler(
|
self.noise_scheduler = DDIMScheduler(
|
||||||
@ -79,6 +83,9 @@ class DiffusionPolicy(nn.Module):
|
|||||||
prediction_type='epsilon'
|
prediction_type='epsilon'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
n_parameters = sum(p.numel() for p in self.parameters())
|
||||||
|
print("number of parameters: %.2fM" % (n_parameters/1e6,))
|
||||||
|
|
||||||
|
|
||||||
def configure_optimizers(self):
|
def configure_optimizers(self):
|
||||||
optimizer = torch.optim.AdamW(self.nets.parameters(), lr=self.lr, weight_decay=self.weight_decay)
|
optimizer = torch.optim.AdamW(self.nets.parameters(), lr=self.lr, weight_decay=self.weight_decay)
|
||||||
@ -88,13 +95,14 @@ class DiffusionPolicy(nn.Module):
|
|||||||
def __call__(self, qpos, image, actions=None, is_pad=None):
|
def __call__(self, qpos, image, actions=None, is_pad=None):
|
||||||
B = qpos.shape[0]
|
B = qpos.shape[0]
|
||||||
if actions is not None: # training time
|
if actions is not None: # training time
|
||||||
|
nets = self.nets
|
||||||
all_features = []
|
all_features = []
|
||||||
for cam_id in range(len(self.camera_names)):
|
for cam_id in range(len(self.camera_names)):
|
||||||
cam_image = image[:, cam_id]
|
cam_image = image[:, cam_id]
|
||||||
cam_features = self.backbones[cam_id](cam_image)
|
cam_features = nets['policy']['backbones'][cam_id](cam_image)
|
||||||
pool_features = self.pools[cam_id](cam_features)
|
pool_features = nets['policy']['pools'][cam_id](cam_features)
|
||||||
pool_features = torch.flatten(pool_features, start_dim=1)
|
pool_features = torch.flatten(pool_features, start_dim=1)
|
||||||
out_features = self.linears[cam_id](pool_features)
|
out_features = nets['policy']['linears'][cam_id](pool_features)
|
||||||
all_features.append(out_features)
|
all_features.append(out_features)
|
||||||
|
|
||||||
obs_cond = torch.cat(all_features + [qpos], dim=1)
|
obs_cond = torch.cat(all_features + [qpos], dim=1)
|
||||||
@ -114,7 +122,7 @@ class DiffusionPolicy(nn.Module):
|
|||||||
actions, noise, timesteps)
|
actions, noise, timesteps)
|
||||||
|
|
||||||
# predict the noise residual
|
# predict the noise residual
|
||||||
noise_pred = self.noise_pred_net(noisy_actions, timesteps, global_cond=obs_cond)
|
noise_pred = nets['policy']['noise_pred_net'](noisy_actions, timesteps, global_cond=obs_cond)
|
||||||
|
|
||||||
# L2 loss
|
# L2 loss
|
||||||
loss = F.mse_loss(noise_pred, noise)
|
loss = F.mse_loss(noise_pred, noise)
|
||||||
@ -122,6 +130,9 @@ class DiffusionPolicy(nn.Module):
|
|||||||
loss_dict = {}
|
loss_dict = {}
|
||||||
loss_dict['l2_loss'] = loss
|
loss_dict['l2_loss'] = loss
|
||||||
loss_dict['loss'] = loss
|
loss_dict['loss'] = loss
|
||||||
|
|
||||||
|
if self.training and self.ema is not None:
|
||||||
|
self.ema.step(nets)
|
||||||
return loss_dict
|
return loss_dict
|
||||||
else: # inference time
|
else: # inference time
|
||||||
To = self.observation_horizon
|
To = self.observation_horizon
|
||||||
@ -129,17 +140,17 @@ class DiffusionPolicy(nn.Module):
|
|||||||
Tp = self.prediction_horizon
|
Tp = self.prediction_horizon
|
||||||
action_dim = self.ac_dim
|
action_dim = self.ac_dim
|
||||||
|
|
||||||
# select network ### TODO TODO
|
nets = self.nets
|
||||||
# nets = self.nets
|
if self.ema is not None:
|
||||||
# if self.ema is not None:
|
nets = self.ema.averaged_model
|
||||||
# nets = self.ema.averaged_model
|
|
||||||
all_features = []
|
all_features = []
|
||||||
for cam_id in range(len(self.camera_names)):
|
for cam_id in range(len(self.camera_names)):
|
||||||
cam_image = image[:, cam_id]
|
cam_image = image[:, cam_id]
|
||||||
cam_features = self.backbones[cam_id](cam_image)
|
cam_features = nets['policy']['backbones'][cam_id](cam_image)
|
||||||
pool_features = self.pools[cam_id](cam_features)
|
pool_features = nets['policy']['pools'][cam_id](cam_features)
|
||||||
pool_features = torch.flatten(pool_features, start_dim=1)
|
pool_features = torch.flatten(pool_features, start_dim=1)
|
||||||
out_features = self.linears[cam_id](pool_features)
|
out_features = nets['policy']['linears'][cam_id](pool_features)
|
||||||
all_features.append(out_features)
|
all_features.append(out_features)
|
||||||
|
|
||||||
obs_cond = torch.cat(all_features + [qpos], dim=1)
|
obs_cond = torch.cat(all_features + [qpos], dim=1)
|
||||||
@ -154,7 +165,7 @@ class DiffusionPolicy(nn.Module):
|
|||||||
|
|
||||||
for k in self.noise_scheduler.timesteps:
|
for k in self.noise_scheduler.timesteps:
|
||||||
# predict noise
|
# predict noise
|
||||||
noise_pred = self.noise_pred_net(
|
noise_pred = nets['policy']['noise_pred_net'](
|
||||||
sample=naction,
|
sample=naction,
|
||||||
timestep=k,
|
timestep=k,
|
||||||
global_cond=obs_cond
|
global_cond=obs_cond
|
||||||
@ -169,6 +180,20 @@ class DiffusionPolicy(nn.Module):
|
|||||||
|
|
||||||
return naction
|
return naction
|
||||||
|
|
||||||
|
def serialize(self):
|
||||||
|
return {
|
||||||
|
"nets": self.nets.state_dict(),
|
||||||
|
"ema": self.ema.averaged_model.state_dict() if self.ema is not None else None,
|
||||||
|
}
|
||||||
|
|
||||||
|
def deserialize(self, model_dict):
|
||||||
|
status = self.nets.load_state_dict(model_dict["nets"])
|
||||||
|
print('Loaded model')
|
||||||
|
if model_dict.get("ema", None) is not None:
|
||||||
|
print('Loaded EMA')
|
||||||
|
status_ema = self.ema.averaged_model.load_state_dict(model_dict["ema"])
|
||||||
|
status = [status, status_ema]
|
||||||
|
return status
|
||||||
|
|
||||||
class ACTPolicy(nn.Module):
|
class ACTPolicy(nn.Module):
|
||||||
def __init__(self, args_override):
|
def __init__(self, args_override):
|
||||||
@ -219,7 +244,11 @@ class ACTPolicy(nn.Module):
|
|||||||
|
|
||||||
return binaries
|
return binaries
|
||||||
|
|
||||||
|
def serialize(self):
|
||||||
|
return self.state_dict()
|
||||||
|
|
||||||
|
def deserialize(self, model_dict):
|
||||||
|
return self.load_state_dict(model_dict)
|
||||||
|
|
||||||
|
|
||||||
class CNNMLPPolicy(nn.Module):
|
class CNNMLPPolicy(nn.Module):
|
||||||
|
|||||||
1
utils.py
1
utils.py
@ -175,6 +175,7 @@ def find_all_hdf5(dataset_dir, skip_mirrored_data):
|
|||||||
hdf5_files = []
|
hdf5_files = []
|
||||||
for root, dirs, files in os.walk(dataset_dir):
|
for root, dirs, files in os.walk(dataset_dir):
|
||||||
for filename in fnmatch.filter(files, '*.hdf5'):
|
for filename in fnmatch.filter(files, '*.hdf5'):
|
||||||
|
if 'features' in filename: continue
|
||||||
if skip_mirrored_data and 'mirror' in filename:
|
if skip_mirrored_data and 'mirror' in filename:
|
||||||
continue
|
continue
|
||||||
hdf5_files.append(os.path.join(root, filename))
|
hdf5_files.append(os.path.join(root, filename))
|
||||||
|
|||||||
143
vinn_cache_feature.py
普通文件
143
vinn_cache_feature.py
普通文件
@ -0,0 +1,143 @@
|
|||||||
|
import torch
|
||||||
|
import argparse
|
||||||
|
import pathlib
|
||||||
|
from torch import nn
|
||||||
|
import torchvision
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
import h5py
|
||||||
|
import h5py
|
||||||
|
from torchvision import models, transforms
|
||||||
|
from PIL import Image
|
||||||
|
from tqdm import tqdm
|
||||||
|
import cv2
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
import IPython
|
||||||
|
e = IPython.embed
|
||||||
|
|
||||||
|
|
||||||
|
def chunks(lst, n):
|
||||||
|
"""Yield successive n-sized chunks from lst."""
|
||||||
|
for i in range(0, len(lst), n):
|
||||||
|
yield lst[i:i + n]
|
||||||
|
|
||||||
|
def expand_greyscale(t):
|
||||||
|
return t.expand(3, -1, -1)
|
||||||
|
|
||||||
|
|
||||||
|
def main(args):
|
||||||
|
#################################################
|
||||||
|
batch_size = 256
|
||||||
|
#################################################
|
||||||
|
|
||||||
|
ckpt_path = args.ckpt_path
|
||||||
|
ckpt_name = pathlib.PurePath(ckpt_path).name
|
||||||
|
dataset_name = ckpt_name.split('-')[1]
|
||||||
|
repr_type = ckpt_name.split('-')[0]
|
||||||
|
seed = int(ckpt_name.split('-')[-1][:-3])
|
||||||
|
|
||||||
|
dataset_dir = f'/home/mobile-aloha/data/{dataset_name}'
|
||||||
|
|
||||||
|
episode_idxs = [int(name.split('_')[1].split('.')[0]) for name in os.listdir(dataset_dir) if ('.hdf5' in name) and ('features' not in name)]
|
||||||
|
episode_idxs.sort()
|
||||||
|
assert len(episode_idxs) == episode_idxs[-1] + 1 # no holes
|
||||||
|
num_episodes = len(episode_idxs)
|
||||||
|
|
||||||
|
feature_extractors = {}
|
||||||
|
|
||||||
|
for episode_idx in range(num_episodes):
|
||||||
|
|
||||||
|
# load all images
|
||||||
|
dataset_path = os.path.join(dataset_dir, f'episode_{episode_idx}.hdf5')
|
||||||
|
with h5py.File(dataset_path, 'r') as root:
|
||||||
|
image_dict = {}
|
||||||
|
camera_names = list(root[f'/observations/images/'].keys())
|
||||||
|
print(f'Camera names: {camera_names}')
|
||||||
|
for cam_name in camera_names:
|
||||||
|
image = root[f'/observations/images/{cam_name}'][:]
|
||||||
|
uncompressed_image = []
|
||||||
|
for im in image:
|
||||||
|
im = np.array(cv2.imdecode(im, 1))
|
||||||
|
uncompressed_image.append(im)
|
||||||
|
image = np.stack(uncompressed_image, axis=0)
|
||||||
|
|
||||||
|
image_dict[cam_name] = image
|
||||||
|
|
||||||
|
# load pretrain nets after cam names are known
|
||||||
|
if not feature_extractors:
|
||||||
|
for cam_name in camera_names:
|
||||||
|
resnet = torchvision.models.resnet18(pretrained=True)
|
||||||
|
loading_status = resnet.load_state_dict(torch.load(ckpt_path.replace('DUMMY', cam_name)))
|
||||||
|
print(cam_name, loading_status)
|
||||||
|
resnet = nn.Sequential(*list(resnet.children())[:-1])
|
||||||
|
resnet = resnet.cuda()
|
||||||
|
resnet.eval()
|
||||||
|
feature_extractors[cam_name] = resnet
|
||||||
|
|
||||||
|
# inference with resnet
|
||||||
|
feature_dict = {}
|
||||||
|
for cam_name, images in image_dict.items():
|
||||||
|
# Preprocess images
|
||||||
|
image_size = 120 # TODO NOTICE: reduced resolution
|
||||||
|
transform = transforms.Compose([
|
||||||
|
transforms.Resize(image_size), # will scale the image
|
||||||
|
transforms.CenterCrop(image_size),
|
||||||
|
transforms.ToTensor(),
|
||||||
|
transforms.Lambda(expand_greyscale),
|
||||||
|
transforms.Normalize(
|
||||||
|
mean=torch.tensor([0.485, 0.456, 0.406]),
|
||||||
|
std=torch.tensor([0.229, 0.224, 0.225])),
|
||||||
|
])
|
||||||
|
processed_images = []
|
||||||
|
for image in tqdm(images):
|
||||||
|
image = Image.fromarray(image)
|
||||||
|
image = transform(image)
|
||||||
|
processed_images.append(image)
|
||||||
|
processed_images = torch.stack(processed_images).cuda()
|
||||||
|
|
||||||
|
# query the model
|
||||||
|
all_features = []
|
||||||
|
with torch.inference_mode():
|
||||||
|
for batch in chunks(processed_images, batch_size):
|
||||||
|
print('inference')
|
||||||
|
features = feature_extractors[cam_name](batch)
|
||||||
|
features = features.squeeze(axis=3).squeeze(axis=2)
|
||||||
|
all_features.append(features)
|
||||||
|
all_features = torch.cat(all_features, axis=0)
|
||||||
|
max_timesteps = all_features.shape[0]
|
||||||
|
feature_dict[cam_name] = all_features
|
||||||
|
|
||||||
|
# TODO START diagnostics
|
||||||
|
# first_image = images[0]
|
||||||
|
# first_processed_image = processed_images[0].cpu().numpy()
|
||||||
|
# first_feature = all_features[0].cpu().numpy()
|
||||||
|
# import numpy as np
|
||||||
|
# np.save('first_image.npy', first_image)
|
||||||
|
# np.save('first_processed_image.npy', first_processed_image)
|
||||||
|
# np.save('first_feature.npy', first_feature)
|
||||||
|
# torch.save(resnet.state_dict(), 'rn.ckpt')
|
||||||
|
# e()
|
||||||
|
# exit()
|
||||||
|
# TODO END diagnostics
|
||||||
|
|
||||||
|
|
||||||
|
# save
|
||||||
|
dataset_path = os.path.join(dataset_dir, f'{repr_type}_features_seed{seed}_episode_{episode_idx}.hdf5')
|
||||||
|
print(dataset_path)
|
||||||
|
# HDF5
|
||||||
|
t0 = time.time()
|
||||||
|
with h5py.File(dataset_path, 'w', rdcc_nbytes=1024 ** 2 * 2) as root:
|
||||||
|
features = root.create_group('features')
|
||||||
|
for cam_name, array in feature_dict.items():
|
||||||
|
cam_feature = features.create_dataset(cam_name, (max_timesteps, 512))
|
||||||
|
features[cam_name][...] = array.cpu().numpy()
|
||||||
|
print(f'Saving: {time.time() - t0:.1f} secs\n')
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
parser = argparse.ArgumentParser(description='cache features')
|
||||||
|
parser.add_argument('--ckpt_path', type=str, required=True, help='ckpt_path')
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
main(args)
|
||||||
331
vinn_eval.py
普通文件
331
vinn_eval.py
普通文件
@ -0,0 +1,331 @@
|
|||||||
|
import torch
|
||||||
|
from torch import nn
|
||||||
|
import torch.nn.functional as F
|
||||||
|
import numpy as np
|
||||||
|
import h5py
|
||||||
|
import pathlib
|
||||||
|
import os
|
||||||
|
import argparse
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
from PIL import Image
|
||||||
|
import torchvision
|
||||||
|
from torchvision import transforms
|
||||||
|
# from visualize_episodes import visualize_joints
|
||||||
|
from utils import set_seed, sample_box_pose
|
||||||
|
# from imitate_episodes import get_image
|
||||||
|
from sim_env import BOX_POSE
|
||||||
|
from constants import DT
|
||||||
|
from imitate_episodes import save_videos
|
||||||
|
from einops import rearrange
|
||||||
|
import time
|
||||||
|
|
||||||
|
DT = 0.02
|
||||||
|
import IPython
|
||||||
|
e = IPython.embed
|
||||||
|
|
||||||
|
# modified from https://github.com/jyopari/VINN/blob/main/nearest-neighbor-eval/handle_nn.ipynb
|
||||||
|
|
||||||
|
def calculate_nearest_neighbors(curr_feature, support_inputs, support_targets, k, state_weight):
|
||||||
|
has_skip = len(support_targets.shape) == 3
|
||||||
|
if has_skip: # when there is action skip
|
||||||
|
num_targets, skip, a_dim = support_targets.shape
|
||||||
|
support_targets = support_targets.view((num_targets, -1))
|
||||||
|
|
||||||
|
curr_vis_feature, curr_s_feature = curr_feature
|
||||||
|
support_vis_feature, support_s_feature = support_inputs
|
||||||
|
|
||||||
|
pairwise_dist_vis = torch.norm(curr_vis_feature - support_vis_feature, dim=1).unsqueeze(0)
|
||||||
|
pairwise_dist_s = torch.norm(curr_s_feature - support_s_feature, dim=1).unsqueeze(0)
|
||||||
|
pairwise_dist = pairwise_dist_vis + pairwise_dist_s * state_weight
|
||||||
|
|
||||||
|
sorted_dist, index = torch.sort(pairwise_dist, dim=1) # sort the support axis
|
||||||
|
permuted_support_targets = support_targets[index]
|
||||||
|
topk_dist = pairwise_dist[:, :k]
|
||||||
|
topk_support_targets = permuted_support_targets[:, :k]
|
||||||
|
weights = F.softmax(-topk_dist, dim=1)
|
||||||
|
weighted_support_targets = weights.unsqueeze(2) * topk_support_targets
|
||||||
|
prediction = torch.sum(weighted_support_targets, dim=1)
|
||||||
|
|
||||||
|
if has_skip:
|
||||||
|
num_predictions = prediction.shape[0]
|
||||||
|
prediction = prediction.reshape((num_predictions, skip, a_dim))
|
||||||
|
|
||||||
|
return prediction
|
||||||
|
|
||||||
|
|
||||||
|
def main(args):
|
||||||
|
# TODO ######################
|
||||||
|
k = None # for scripted box transfer
|
||||||
|
skip = 100
|
||||||
|
real_robot = True
|
||||||
|
save_episode = True
|
||||||
|
# TODO ######################
|
||||||
|
onscreen_cam = 'main'
|
||||||
|
state_dim = 14
|
||||||
|
dataset_dir = args['dataset_dir']
|
||||||
|
onscreen_render = args['onscreen_render']
|
||||||
|
ckpt_dir = args['ckpt_dir']
|
||||||
|
model_dir = args['model_dir']
|
||||||
|
task_name = args['task_name']
|
||||||
|
|
||||||
|
if 'insertion' in task_name:
|
||||||
|
sim_episode_len = 400
|
||||||
|
env_max_reward = 4
|
||||||
|
ks = [None]
|
||||||
|
elif 'transfer_cube' in task_name:
|
||||||
|
sim_episode_len = 400
|
||||||
|
env_max_reward = 4
|
||||||
|
ks = [1, 1, 1]
|
||||||
|
if 'human' in dataset_dir:
|
||||||
|
state_weight = 5
|
||||||
|
else:
|
||||||
|
state_weight = 10
|
||||||
|
print(f'{state_weight=}')
|
||||||
|
elif task_name == 'ziploc_slide':
|
||||||
|
env_max_reward = 1
|
||||||
|
ks = [71]
|
||||||
|
state_weight = 0
|
||||||
|
elif task_name == 'aloha_mobile_wipe_wine':
|
||||||
|
sim_episode_len = 1300
|
||||||
|
env_max_reward = 4
|
||||||
|
ks = [2, 2, 2]
|
||||||
|
state_weight = 5
|
||||||
|
print(f'{state_weight=}')
|
||||||
|
else:
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
model_name = pathlib.PurePath(model_dir).name
|
||||||
|
seed = int(model_name.split('-')[-1][:-3])
|
||||||
|
|
||||||
|
k = ks[seed]
|
||||||
|
|
||||||
|
if real_robot:
|
||||||
|
BASE_DELAY = 15
|
||||||
|
query_freq = skip - BASE_DELAY
|
||||||
|
|
||||||
|
# load train data
|
||||||
|
vis_features = []
|
||||||
|
state_features = []
|
||||||
|
Y = []
|
||||||
|
for episode_id in range(0, 40):
|
||||||
|
dataset_path = os.path.join(dataset_dir, f'episode_{episode_id}.hdf5')
|
||||||
|
with h5py.File(dataset_path, 'r') as root:
|
||||||
|
action = root['/action'][:]
|
||||||
|
base_action = root['/base_action'][:]
|
||||||
|
action = np.concatenate([action, base_action], axis=1)
|
||||||
|
camera_names = list(root[f'/observations/images/'].keys())
|
||||||
|
|
||||||
|
# Visual feature
|
||||||
|
all_cam_feature = []
|
||||||
|
for cam_name in camera_names:
|
||||||
|
feature_dataset_path = os.path.join(dataset_dir, f'byol_features_seed{seed}_episode_{episode_id}.hdf5')
|
||||||
|
with h5py.File(feature_dataset_path, 'r') as root:
|
||||||
|
cam_feature = root[f'/features/{cam_name}'][:]
|
||||||
|
all_cam_feature.append(cam_feature)
|
||||||
|
vis_fea = np.concatenate(all_cam_feature, axis=1)
|
||||||
|
|
||||||
|
## State feature
|
||||||
|
dataset_path = os.path.join(dataset_dir, f'episode_{episode_id}.hdf5')
|
||||||
|
with h5py.File(dataset_path, 'r') as root:
|
||||||
|
s_fea = root['/observations/qpos'][:]
|
||||||
|
|
||||||
|
# stack actions together
|
||||||
|
eps_len = len(action)
|
||||||
|
indices = np.tile(np.arange(skip), eps_len).reshape(eps_len, skip) # each row is 0, 1, ... skip
|
||||||
|
offset = np.expand_dims(np.arange(eps_len), axis=1)
|
||||||
|
indices = indices + offset # row1: 0, 1, ... skip; row2: 1, 2, ... skip+1
|
||||||
|
# indices will exceed eps_len, thus clamp to eps_len-1
|
||||||
|
indices = np.clip(indices, 0, eps_len-1)
|
||||||
|
# stack action
|
||||||
|
action = action[indices] # new shape: eps_len, skip, a_dim
|
||||||
|
|
||||||
|
vis_features.append(vis_fea)
|
||||||
|
state_features.append(s_fea)
|
||||||
|
Y.append(action)
|
||||||
|
|
||||||
|
vis_features = np.concatenate(vis_features)
|
||||||
|
state_features = np.concatenate(state_features)
|
||||||
|
Y = np.concatenate(Y)
|
||||||
|
train_inputs = [torch.from_numpy(vis_features).cuda(), torch.from_numpy(state_features).cuda()]
|
||||||
|
train_targets = torch.from_numpy(Y).cuda()
|
||||||
|
|
||||||
|
set_seed(1000)
|
||||||
|
feature_extractors = {}
|
||||||
|
for cam_name in camera_names:
|
||||||
|
resnet = torchvision.models.resnet18(pretrained=True)
|
||||||
|
loading_status = resnet.load_state_dict(torch.load(model_dir.replace('DUMMY', cam_name)))
|
||||||
|
print(cam_name, loading_status)
|
||||||
|
resnet = nn.Sequential(*list(resnet.children())[:-1])
|
||||||
|
resnet = resnet.cuda()
|
||||||
|
resnet.eval()
|
||||||
|
feature_extractors[cam_name] = resnet
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# load environment
|
||||||
|
if real_robot:
|
||||||
|
from aloha_scripts.real_env import make_real_env #### TODO TODO
|
||||||
|
env = make_real_env(init_node=True, setup_robots=True, setup_base=True)
|
||||||
|
max_timesteps = sim_episode_len
|
||||||
|
camera_names = ['cam_high', 'cam_left_wrist', 'cam_right_wrist']
|
||||||
|
else:
|
||||||
|
from sim_env import make_sim_env
|
||||||
|
env = make_sim_env(task_name)
|
||||||
|
max_timesteps = sim_episode_len
|
||||||
|
|
||||||
|
|
||||||
|
num_rollouts = 50
|
||||||
|
episode_returns = []
|
||||||
|
max_rewards = []
|
||||||
|
for rollout_id in range(num_rollouts):
|
||||||
|
### set task
|
||||||
|
BOX_POSE[0] = sample_box_pose() # used in sim reset
|
||||||
|
ts = env.reset()
|
||||||
|
|
||||||
|
### evaluation loop
|
||||||
|
qpos_history = torch.zeros((1, max_timesteps, state_dim)).cuda()
|
||||||
|
image_list = [] # for visualization
|
||||||
|
qpos_list = []
|
||||||
|
target_qpos_list = []
|
||||||
|
rewards = []
|
||||||
|
with torch.inference_mode():
|
||||||
|
for t in range(sim_episode_len):
|
||||||
|
start_time = time.time()
|
||||||
|
if t % 100 == 0: print(t)
|
||||||
|
if t % query_freq == 0:
|
||||||
|
### process previous timestep to get qpos and image_list
|
||||||
|
obs = ts.observation
|
||||||
|
if 'images' in obs:
|
||||||
|
image_list.append(obs['images'])
|
||||||
|
else:
|
||||||
|
image_list.append({'main': obs['image']})
|
||||||
|
qpos_numpy = np.array(obs['qpos'])
|
||||||
|
# qpos = pre_process(qpos_numpy)
|
||||||
|
qpos = torch.from_numpy(qpos_numpy).float().cuda().unsqueeze(0)
|
||||||
|
qpos_history[:, t] = qpos
|
||||||
|
_, curr_image_raw = get_image(ts, camera_names)
|
||||||
|
|
||||||
|
image_size = 120
|
||||||
|
transform = transforms.Compose([
|
||||||
|
transforms.Resize(image_size), # will scale the image
|
||||||
|
transforms.CenterCrop(image_size),
|
||||||
|
transforms.ToTensor(),
|
||||||
|
transforms.Lambda(expand_greyscale),
|
||||||
|
transforms.Normalize(
|
||||||
|
mean=torch.tensor([0.485, 0.456, 0.406]),
|
||||||
|
std=torch.tensor([0.229, 0.224, 0.225])),
|
||||||
|
])
|
||||||
|
|
||||||
|
all_cam_features = []
|
||||||
|
for cam_id, curr_image in enumerate(curr_image_raw):
|
||||||
|
curr_image = Image.fromarray(curr_image) # TODO only one camera
|
||||||
|
curr_image = transform(curr_image)
|
||||||
|
curr_image = curr_image.unsqueeze(dim=0).cuda()
|
||||||
|
curr_image_feature = feature_extractors[camera_names[cam_id]](curr_image)
|
||||||
|
curr_image_feature = curr_image_feature.squeeze(3).squeeze(2)
|
||||||
|
all_cam_features.append(curr_image_feature)
|
||||||
|
curr_image_feature = torch.cat(all_cam_features, dim=1)
|
||||||
|
|
||||||
|
### Visual feature
|
||||||
|
# curr_feature = curr_image_feature
|
||||||
|
|
||||||
|
### State feature
|
||||||
|
# curr_feature = qpos
|
||||||
|
|
||||||
|
### Both features
|
||||||
|
curr_feature = [curr_image_feature, qpos]
|
||||||
|
|
||||||
|
action = calculate_nearest_neighbors(curr_feature, train_inputs, train_targets, k, state_weight) # TODO use this
|
||||||
|
action = action.squeeze(0).cpu().numpy()
|
||||||
|
action = np.concatenate([action[:-BASE_DELAY, :-2], action[BASE_DELAY:, -2:]], axis=1)
|
||||||
|
print(f'Query: {(time.time() - start_time):.3f}s')
|
||||||
|
|
||||||
|
curr_action = action[t % query_freq]
|
||||||
|
target_qpos = curr_action[:-2]
|
||||||
|
base_action = curr_action[-2:]
|
||||||
|
|
||||||
|
# ### SAFETY
|
||||||
|
# max_a = 0.05
|
||||||
|
# curr_qpos = qpos.squeeze().cpu().numpy()
|
||||||
|
# target_qpos = target_qpos.clip(curr_qpos - max_a, curr_qpos + max_a)
|
||||||
|
# ### SAFETY
|
||||||
|
|
||||||
|
### step the environment
|
||||||
|
ts = env.step(target_qpos, base_action=base_action)
|
||||||
|
duration = time.time() - start_time
|
||||||
|
# print(f'{duration:.3f}')
|
||||||
|
time.sleep(max(0, DT - duration))
|
||||||
|
|
||||||
|
### save things for visualization
|
||||||
|
qpos_list.append(qpos_numpy)
|
||||||
|
target_qpos_list.append(target_qpos)
|
||||||
|
rewards.append(ts.reward)
|
||||||
|
|
||||||
|
# if real_robot and t != 0 and t % 60 == 0:
|
||||||
|
# e()
|
||||||
|
plt.close()
|
||||||
|
if real_robot:
|
||||||
|
env.puppet_bot_left.dxl.robot_set_operating_modes("single", "gripper", "position")
|
||||||
|
env.puppet_bot_right.dxl.robot_set_operating_modes("single", "gripper", "position")
|
||||||
|
env.puppet_bot_left.dxl.robot_set_operating_modes("single", "gripper", "pwm")
|
||||||
|
env.puppet_bot_right.dxl.robot_set_operating_modes("single", "gripper", "pwm")
|
||||||
|
|
||||||
|
rewards = np.array(rewards)
|
||||||
|
episode_return = np.sum(rewards[rewards!=None])
|
||||||
|
episode_returns.append(episode_return)
|
||||||
|
max_reward = np.max(rewards)
|
||||||
|
max_rewards.append(max_reward)
|
||||||
|
|
||||||
|
print(f'{episode_return=}, {max_reward=}')
|
||||||
|
if save_episode:
|
||||||
|
save_videos(image_list, DT, video_path=os.path.join(ckpt_dir, f'video{rollout_id}.mp4'))
|
||||||
|
# visualize_joints(qpos_list, target_qpos_list, plot_path=os.path.join(ckpt_dir, f'qpos{rollout_id}.png'))
|
||||||
|
# visualize_joints(qpos_list, example_qpos, plot_path=os.path.join(ckpt_dir, f'qpos_reference{rollout_id}.png'), label_overwrite=("policy", "dataset"))
|
||||||
|
|
||||||
|
success_rate = np.mean(np.array(max_rewards) == env_max_reward)
|
||||||
|
avg_return = np.mean(episode_returns)
|
||||||
|
summary_str = f'\nSuccess rate: {success_rate}\nAverage return: {avg_return}\n\n'
|
||||||
|
for r in range(env_max_reward+1):
|
||||||
|
more_or_equal_r = (np.array(max_rewards) >= r).sum()
|
||||||
|
more_or_equal_r_rate = more_or_equal_r / num_rollouts
|
||||||
|
summary_str += f'Reward >= {r}: {more_or_equal_r}/{num_rollouts} = {more_or_equal_r_rate*100}%\n'
|
||||||
|
|
||||||
|
print(summary_str)
|
||||||
|
|
||||||
|
# save success rate to txt
|
||||||
|
result_file_name = f'result_{skip}_{k}' + '.txt'
|
||||||
|
with open(os.path.join(ckpt_dir, result_file_name), 'w') as f:
|
||||||
|
f.write(summary_str)
|
||||||
|
f.write(repr(episode_returns))
|
||||||
|
f.write('\n\n')
|
||||||
|
f.write(repr(max_rewards))
|
||||||
|
|
||||||
|
return success_rate, avg_return
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def get_image(ts, camera_names):
|
||||||
|
if 'images' in ts.observation:
|
||||||
|
curr_images = []
|
||||||
|
for cam_name in camera_names:
|
||||||
|
curr_image = rearrange(ts.observation['images'][cam_name], 'h w c -> c h w')
|
||||||
|
curr_images.append(curr_image)
|
||||||
|
curr_image_raw = np.stack(curr_images, axis=0)
|
||||||
|
else:
|
||||||
|
curr_image_raw = rearrange(ts.observation['image'], 'h w c -> c h w')
|
||||||
|
curr_image = torch.from_numpy(curr_image_raw / 255.0).float().cuda().unsqueeze(0)
|
||||||
|
curr_image_raw = rearrange(curr_image_raw, 'b c h w -> b h w c')
|
||||||
|
return curr_image, curr_image_raw
|
||||||
|
|
||||||
|
|
||||||
|
def expand_greyscale(t):
|
||||||
|
return t.expand(3, -1, -1)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument('--onscreen_render', action='store_true')
|
||||||
|
parser.add_argument('--dataset_dir', action='store', type=str, help='The text to parse.', required=True)
|
||||||
|
parser.add_argument('--model_dir', action='store', type=str, help='model_dir', required=True)
|
||||||
|
parser.add_argument('--task_name', action='store', type=str, help='task_name', required=True)
|
||||||
|
parser.add_argument('--ckpt_dir', action='store', type=str, help='The text to parse.', required=True)
|
||||||
|
main(vars(parser.parse_args()))
|
||||||
129
vinn_select_k.py
普通文件
129
vinn_select_k.py
普通文件
@ -0,0 +1,129 @@
|
|||||||
|
import torch
|
||||||
|
import torch.nn.functional as F
|
||||||
|
import numpy as np
|
||||||
|
import h5py
|
||||||
|
import pathlib
|
||||||
|
import os
|
||||||
|
import argparse
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
import IPython
|
||||||
|
e = IPython.embed
|
||||||
|
|
||||||
|
# modified from https://github.com/jyopari/VINN/blob/main/nearest-neighbor-eval/handle_nn.ipynb
|
||||||
|
|
||||||
|
def calculate_nearest_neighbors(query_inputs, query_targets, support_inputs, support_targets, max_k):
|
||||||
|
with torch.no_grad():
|
||||||
|
pairwise_dist = []
|
||||||
|
for q_in in query_inputs:
|
||||||
|
diff = support_inputs - q_in.unsqueeze(0)
|
||||||
|
dist = torch.norm(diff, dim=1)
|
||||||
|
pairwise_dist.append(dist)
|
||||||
|
pairwise_dist = torch.stack(pairwise_dist)
|
||||||
|
|
||||||
|
sorted_dist, index = torch.sort(pairwise_dist, dim=1) # sort the support axis
|
||||||
|
permuted_support_targets = support_targets[index]
|
||||||
|
errors = []
|
||||||
|
for k in range(1, max_k):
|
||||||
|
topk_dist = pairwise_dist[:, :k]
|
||||||
|
topk_support_targets = permuted_support_targets[:, :k]
|
||||||
|
weights = F.softmax(-topk_dist, dim=1)
|
||||||
|
weighted_support_targets = weights.unsqueeze(2) * topk_support_targets
|
||||||
|
prediction = torch.sum(weighted_support_targets, dim=1)
|
||||||
|
error = F.mse_loss(prediction, query_targets)
|
||||||
|
errors.append(error)
|
||||||
|
return errors
|
||||||
|
|
||||||
|
def chunks(lst, n):
|
||||||
|
"""Yield successive n-sized chunks from lst."""
|
||||||
|
for i in range(0, len(lst), n):
|
||||||
|
yield lst[i:i + n]
|
||||||
|
|
||||||
|
def main(args):
|
||||||
|
# TODO ######################
|
||||||
|
dataset_dir = args['dataset_dir']
|
||||||
|
ckpt_dir = args['ckpt_dir']
|
||||||
|
seed = 0
|
||||||
|
max_k = 400
|
||||||
|
batch_size = 100
|
||||||
|
# TODO ######################
|
||||||
|
|
||||||
|
if not os.path.isdir(ckpt_dir):
|
||||||
|
os.makedirs(ckpt_dir)
|
||||||
|
|
||||||
|
episode_idxs = [int(name.split('_')[1].split('.')[0]) for name in os.listdir(dataset_dir) if ('.hdf5' in name) and ('features' not in name)]
|
||||||
|
episode_idxs.sort()
|
||||||
|
assert len(episode_idxs) == episode_idxs[-1] + 1 # no holes
|
||||||
|
num_episodes = len(episode_idxs)
|
||||||
|
val_split = int(num_episodes * 0.8)
|
||||||
|
|
||||||
|
# load train data
|
||||||
|
X = []
|
||||||
|
Y = []
|
||||||
|
for episode_id in range(0, val_split):
|
||||||
|
dataset_path = os.path.join(dataset_dir, f'episode_{episode_id}.hdf5')
|
||||||
|
with h5py.File(dataset_path, 'r') as root:
|
||||||
|
action = root['/action'][:]
|
||||||
|
camera_names = list(root[f'/observations/images/'].keys())
|
||||||
|
|
||||||
|
all_cam_feature = []
|
||||||
|
feature_dataset_path = os.path.join(dataset_dir, f'byol_features_seed{seed}_episode_{episode_id}.hdf5')
|
||||||
|
with h5py.File(feature_dataset_path, 'r') as root:
|
||||||
|
for cam_name in camera_names:
|
||||||
|
cam_feature = root[f'/features/{cam_name}'][:]
|
||||||
|
all_cam_feature.append(cam_feature)
|
||||||
|
cam_feature = np.concatenate(all_cam_feature, axis=1)
|
||||||
|
|
||||||
|
X.append(cam_feature)
|
||||||
|
Y.append(action)
|
||||||
|
|
||||||
|
X = np.concatenate(X)
|
||||||
|
Y = np.concatenate(Y)
|
||||||
|
train_inputs = torch.from_numpy(X).cuda()
|
||||||
|
train_targets = torch.from_numpy(Y).cuda()
|
||||||
|
print(f'All features: {train_inputs.shape}')
|
||||||
|
|
||||||
|
# load test data
|
||||||
|
X = []
|
||||||
|
Y = []
|
||||||
|
for episode_id in range(val_split, num_episodes):
|
||||||
|
dataset_path = os.path.join(dataset_dir, f'episode_{episode_id}.hdf5')
|
||||||
|
with h5py.File(dataset_path, 'r') as root:
|
||||||
|
action = root['/action'][:]
|
||||||
|
|
||||||
|
all_cam_feature = []
|
||||||
|
feature_dataset_path = os.path.join(dataset_dir, f'byol_features_seed{seed}_episode_{episode_id}.hdf5')
|
||||||
|
with h5py.File(feature_dataset_path, 'r') as root:
|
||||||
|
for cam_name in camera_names:
|
||||||
|
cam_feature = root[f'/features/{cam_name}'][:]
|
||||||
|
all_cam_feature.append(cam_feature)
|
||||||
|
cam_feature = np.concatenate(all_cam_feature, axis=1)
|
||||||
|
|
||||||
|
X.append(cam_feature)
|
||||||
|
Y.append(action)
|
||||||
|
|
||||||
|
X = np.concatenate(X)
|
||||||
|
Y = np.concatenate(Y)
|
||||||
|
val_inputs = torch.from_numpy(X).cuda()
|
||||||
|
val_targets = torch.from_numpy(Y).cuda()
|
||||||
|
|
||||||
|
val_losses = []
|
||||||
|
for inputs, targets in zip(chunks(val_inputs, batch_size), chunks(val_targets, batch_size)):
|
||||||
|
val_loss = calculate_nearest_neighbors(inputs, targets, train_inputs, train_targets, max_k)
|
||||||
|
val_loss = torch.stack(val_loss)
|
||||||
|
val_losses.append(val_loss)
|
||||||
|
val_losses = torch.mean(torch.stack(val_losses), dim=0)
|
||||||
|
val_loss = val_losses
|
||||||
|
|
||||||
|
val_loss = torch.tensor(val_loss).cpu().numpy()
|
||||||
|
print(f'min val loss of {np.min(val_loss)} at k={np.argmin(val_loss)}')
|
||||||
|
|
||||||
|
plt.plot(np.arange(1, max_k), val_loss)
|
||||||
|
plt.savefig(os.path.join(ckpt_dir, f'k_select-seed{seed}.png'))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument('--dataset_dir', action='store', type=str, help='The text to parse.', required=True)
|
||||||
|
parser.add_argument('--ckpt_dir', action='store', type=str, help='The text to parse.', required=True)
|
||||||
|
main(vars(parser.parse_args()))
|
||||||
在新工单中引用
屏蔽一个用户