Merge branch 'main' of github.com:MarkFzp/act-plus-plus into main
这个提交包含在:
53
commands.txt
53
commands.txt
@ -58,7 +58,7 @@ CUDA_VISIBLE_DEVICES=0 python3 imitate_episodes.py \
|
||||
--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
|
||||
--num_steps 100000 --eval_every 2000 --validate_every 2000 --save_every 2000 --no_encoder
|
||||
|
||||
|
||||
# launch experiment on all data
|
||||
@ -70,8 +70,59 @@ CUDA_VISIBLE_DEVICES=0 python3 imitate_episodes.py \
|
||||
--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 --no_encoder
|
||||
|
||||
|
||||
####### DIFFUSION POLICY
|
||||
|
||||
- first install https://github.com/ARISE-Initiative/robomimic/tree/r2d2 (note the r2d2 branch)
|
||||
- on top of it pip install the current repo requirements
|
||||
|
||||
|
||||
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_0 \
|
||||
--policy_class Diffusion --chunk_size 32 \
|
||||
--batch_size 32 --lr 1e-5 --seed 0 \
|
||||
--num_steps 100000 --eval_every 2000 --validate_every 2000 --save_every 2000
|
||||
|
||||
|
||||
conda activate mobile
|
||||
export MUJOCO_GL=egl
|
||||
cd /home/tonyzhao/Research/act-plus-plus
|
||||
CUDA_VISIBLE_DEVICES=1 python3 imitate_episodes.py \
|
||||
--task_name sim_transfer_cube_scripted \
|
||||
--ckpt_dir /scr/tonyzhao/train_logs/cube_scripted_diffusion_sweep_1 \
|
||||
--policy_class Diffusion --chunk_size 16 \
|
||||
--batch_size 32 --lr 1e-5 --seed 0 \
|
||||
--num_steps 100000 --eval_every 2000 --validate_every 2000 --save_every 2000
|
||||
|
||||
|
||||
# above are all 100 train diffusion steps, 1e-5
|
||||
|
||||
conda activate mobile
|
||||
export MUJOCO_GL=egl
|
||||
cd /home/tonyzhao/Research/act-plus-plus
|
||||
CUDA_VISIBLE_DEVICES=1 python3 imitate_episodes.py \
|
||||
--task_name sim_transfer_cube_scripted \
|
||||
--ckpt_dir /scr/tonyzhao/train_logs/cube_scripted_diffusion_sweep_2_50step_1e-4 \
|
||||
--policy_class Diffusion --chunk_size 32 \
|
||||
--batch_size 32 --lr 1e-4 --seed 0 \
|
||||
--num_steps 100000 --eval_every 2000 --validate_every 2000 --save_every 2000
|
||||
|
||||
|
||||
|
||||
---------------------------------------------------------------------------------------
|
||||
|
||||
NOTE: chunk size cannot be any number, try before launching
|
||||
TODO: Add history, EMA at test time
|
||||
|
||||
conda activate mobile
|
||||
cd /home/tonyzhao/Research/act-plus-plus
|
||||
CUDA_VISIBLE_DEVICES=1 python3 train_actuator_network.py
|
||||
|
||||
|
||||
|
||||
|
||||
@ -99,7 +99,7 @@ def save_videos(video, dt, video_path=None):
|
||||
images = []
|
||||
for cam_name in cam_names:
|
||||
image = image_dict[cam_name]
|
||||
# image = image[:, :, [2, 1, 0]] # swap B and R channel
|
||||
image = image[:, :, [2, 1, 0]] # swap B and R channel
|
||||
images.append(image)
|
||||
images = np.concatenate(images, axis=1)
|
||||
out.write(images)
|
||||
@ -119,7 +119,7 @@ def save_videos(video, dt, video_path=None):
|
||||
out = cv2.VideoWriter(video_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))
|
||||
for t in range(n_frames):
|
||||
image = all_cam_videos[t]
|
||||
# image = image[:, :, [2, 1, 0]] # swap B and R channel
|
||||
image = image[:, :, [2, 1, 0]] # swap B and R channel
|
||||
out.write(image)
|
||||
out.release()
|
||||
print(f'Saved video to: {video_path}')
|
||||
|
||||
@ -75,6 +75,10 @@ def get_args_parser():
|
||||
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')
|
||||
parser.add_argument('--actuator_network_dir', action='store', type=str, help='actuator_network_dir', required=False)
|
||||
parser.add_argument('--history_len', action='store', type=int)
|
||||
parser.add_argument('--future_len', action='store', type=int)
|
||||
parser.add_argument('--prediction_len', action='store', type=int)
|
||||
|
||||
return parser
|
||||
|
||||
|
||||
@ -15,7 +15,7 @@ from constants import PUPPET_GRIPPER_JOINT_OPEN
|
||||
from utils import load_data # data functions
|
||||
from utils import sample_box_pose, sample_insertion_pose # robot functions
|
||||
from utils import compute_dict_mean, set_seed, detach_dict, calibrate_linear_vel, postprocess_base_action # helper functions
|
||||
from policy import ACTPolicy, CNNMLPPolicy
|
||||
from policy import ACTPolicy, CNNMLPPolicy, DiffusionPolicy
|
||||
from visualize_episodes import save_videos
|
||||
|
||||
from detr.models.latent_model import Latent_Model_Transformer
|
||||
@ -80,12 +80,32 @@ def main(args):
|
||||
'action_dim': 16,
|
||||
'no_encoder': args['no_encoder'],
|
||||
}
|
||||
elif policy_class == 'Diffusion':
|
||||
|
||||
policy_config = {'lr': args['lr'],
|
||||
'camera_names': camera_names,
|
||||
'action_dim': 16,
|
||||
'observation_horizon': 1,
|
||||
'action_horizon': 8,
|
||||
'prediction_horizon': args['chunk_size'],
|
||||
'num_queries': args['chunk_size'],
|
||||
'num_inference_timesteps': 10,
|
||||
'ema_power': 0.75,
|
||||
'vq': False,
|
||||
}
|
||||
elif policy_class == 'CNNMLP':
|
||||
policy_config = {'lr': args['lr'], 'lr_backbone': lr_backbone, 'backbone' : backbone, 'num_queries': 1,
|
||||
'camera_names': camera_names,}
|
||||
else:
|
||||
raise NotImplementedError
|
||||
|
||||
actuator_config = {
|
||||
'actuator_network_dir': args['actuator_network_dir'],
|
||||
'history_len': args['history_len'],
|
||||
'future_len': args['future_len'],
|
||||
'prediction_len': args['prediction_len'],
|
||||
}
|
||||
|
||||
config = {
|
||||
'num_steps': num_steps,
|
||||
'eval_every': eval_every,
|
||||
@ -104,7 +124,8 @@ def main(args):
|
||||
'temporal_agg': args['temporal_agg'],
|
||||
'camera_names': camera_names,
|
||||
'real_robot': not is_sim,
|
||||
'load_pretrain': args['load_pretrain']
|
||||
'load_pretrain': args['load_pretrain'],
|
||||
'actuator_config': actuator_config,
|
||||
}
|
||||
|
||||
if not os.path.isdir(ckpt_dir):
|
||||
@ -112,7 +133,7 @@ def main(args):
|
||||
config_path = os.path.join(ckpt_dir, 'config.pkl')
|
||||
expr_name = ckpt_dir.split('/')[-1]
|
||||
if not is_eval:
|
||||
wandb.init(project="mobile-aloha", reinit=True, entity="mobile-aloha", name=expr_name)
|
||||
wandb.init(project="mobile-aloha2", reinit=True, entity="mobile-aloha2", name=expr_name)
|
||||
wandb.config.update(config)
|
||||
with open(config_path, 'wb') as f:
|
||||
pickle.dump(config, f)
|
||||
@ -129,7 +150,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'], config['load_pretrain'])
|
||||
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'], config['load_pretrain'], policy_class)
|
||||
|
||||
# save dataset stats
|
||||
stats_path = os.path.join(ckpt_dir, f'dataset_stats.pkl')
|
||||
@ -151,6 +172,8 @@ def make_policy(policy_class, policy_config):
|
||||
policy = ACTPolicy(policy_config)
|
||||
elif policy_class == 'CNNMLP':
|
||||
policy = CNNMLPPolicy(policy_config)
|
||||
elif policy_class == 'Diffusion':
|
||||
policy = DiffusionPolicy(policy_config)
|
||||
else:
|
||||
raise NotImplementedError
|
||||
return policy
|
||||
@ -161,6 +184,8 @@ def make_optimizer(policy_class, policy):
|
||||
optimizer = policy.configure_optimizers()
|
||||
elif policy_class == 'CNNMLP':
|
||||
optimizer = policy.configure_optimizers()
|
||||
elif policy_class == 'Diffusion':
|
||||
optimizer = policy.configure_optimizers()
|
||||
else:
|
||||
raise NotImplementedError
|
||||
return optimizer
|
||||
@ -190,6 +215,8 @@ def eval_bc(config, ckpt_name, save_episode=True, num_rollouts=50):
|
||||
temporal_agg = config['temporal_agg']
|
||||
onscreen_cam = 'angle'
|
||||
vq = config['policy_config']['vq']
|
||||
actuator_config = config['actuator_config']
|
||||
use_actuator_net = actuator_config['actuator_network_dir'] is not None
|
||||
|
||||
# load policy and stats
|
||||
ckpt_path = os.path.join(ckpt_dir, ckpt_name)
|
||||
@ -212,9 +239,35 @@ def eval_bc(config, ckpt_name, save_episode=True, num_rollouts=50):
|
||||
stats_path = os.path.join(ckpt_dir, f'dataset_stats.pkl')
|
||||
with open(stats_path, 'rb') as f:
|
||||
stats = pickle.load(f)
|
||||
if use_actuator_net:
|
||||
prediction_len = actuator_config['prediction_len']
|
||||
future_len = actuator_config['future_len']
|
||||
history_len = actuator_config['history_len']
|
||||
actuator_network_dir = actuator_config['actuator_network_dir']
|
||||
|
||||
from train_actuator_network import ActuatorNetwork
|
||||
actuator_network = ActuatorNetwork(prediction_len)
|
||||
actuator_network_path = os.path.join(actuator_network_dir, 'actuator_net_last.ckpt')
|
||||
loading_status = actuator_network.load_state_dict(torch.load(actuator_network_path))
|
||||
actuator_network.eval()
|
||||
actuator_network.cuda()
|
||||
print(f'Loaded actuator network from: {actuator_network_path}, {loading_status}')
|
||||
|
||||
actuator_stats_path = os.path.join(actuator_network_dir, 'actuator_net_stats.pkl')
|
||||
with open(actuator_stats_path, 'rb') as f:
|
||||
actuator_stats = pickle.load(f)
|
||||
|
||||
actuator_unnorm = lambda x: x * actuator_stats['action_std'] + actuator_stats['action_mean']
|
||||
actuator_norm = lambda x: (x - actuator_stats['action_mean']) / actuator_stats['action_std']
|
||||
def collect_base_action(all_actions, norm_episode_all_base_actions):
|
||||
post_processed_actions = post_process(all_actions.squeeze(0).cpu().numpy())
|
||||
norm_episode_all_base_actions += actuator_norm(post_processed_actions[:, -2:]).tolist()
|
||||
|
||||
pre_process = lambda s_qpos: (s_qpos - stats['qpos_mean']) / stats['qpos_std']
|
||||
post_process = lambda a: a * stats['action_std'] + stats['action_mean']
|
||||
if policy_class == 'Diffusion':
|
||||
post_process = lambda a: ((a + 1) / 2) * (stats['action_max'] - stats['action_min']) + stats['action_min']
|
||||
else:
|
||||
post_process = lambda a: a * stats['action_std'] + stats['action_mean']
|
||||
|
||||
# load environment
|
||||
if real_robot:
|
||||
@ -263,6 +316,7 @@ def eval_bc(config, ckpt_name, save_episode=True, num_rollouts=50):
|
||||
qpos_list = []
|
||||
target_qpos_list = []
|
||||
rewards = []
|
||||
norm_episode_all_base_actions = [actuator_norm(np.zeros(history_len, 2)).tolist()]
|
||||
with torch.inference_mode():
|
||||
for t in range(max_timesteps):
|
||||
### update onscreen render and wait for DT
|
||||
@ -296,6 +350,8 @@ def eval_bc(config, ckpt_name, save_episode=True, num_rollouts=50):
|
||||
else:
|
||||
# e()
|
||||
all_actions = policy(qpos, curr_image)
|
||||
if use_actuator_net:
|
||||
collect_base_action(all_actions, norm_episode_all_base_actions)
|
||||
if temporal_agg:
|
||||
all_time_actions[[t], t:t+num_queries] = all_actions
|
||||
actions_for_curr_step = all_time_actions[:, t]
|
||||
@ -308,8 +364,17 @@ def eval_bc(config, ckpt_name, save_episode=True, num_rollouts=50):
|
||||
raw_action = (actions_for_curr_step * exp_weights).sum(dim=0, keepdim=True)
|
||||
else:
|
||||
raw_action = all_actions[:, t % query_frequency]
|
||||
elif config['policy_class'] == "Diffusion":
|
||||
if t % query_frequency == 0:
|
||||
all_actions = policy(qpos, curr_image)
|
||||
if use_actuator_net:
|
||||
collect_base_action(all_actions, norm_episode_all_base_actions)
|
||||
raw_action = all_actions[:, t % query_frequency]
|
||||
elif config['policy_class'] == "CNNMLP":
|
||||
raw_action = policy(qpos, curr_image)
|
||||
all_actions = raw_action.unsqueeze(0)
|
||||
if use_actuator_net:
|
||||
collect_base_action(all_actions, norm_episode_all_base_actions)
|
||||
else:
|
||||
raise NotImplementedError
|
||||
|
||||
@ -317,9 +382,20 @@ def eval_bc(config, ckpt_name, save_episode=True, num_rollouts=50):
|
||||
raw_action = raw_action.squeeze(0).cpu().numpy()
|
||||
action = post_process(raw_action)
|
||||
target_qpos = action[:-2]
|
||||
base_action = action[-2:]
|
||||
# base_action = calibrate_linear_vel(base_action, c=0.19)
|
||||
# base_action = postprocess_base_action(base_action)
|
||||
|
||||
if use_actuator_net:
|
||||
assert(not temporal_agg)
|
||||
if t % prediction_len == 0:
|
||||
offset_start_ts = t + history_len
|
||||
actuator_net_in = np.array(norm_episode_all_base_actions[offset_start_ts - history_len: offset_start_ts + future_len])
|
||||
actuator_net_in = torch.from_numpy(actuator_net_in).float().unsqueeze(dim=0).cuda()
|
||||
pred = actuator_network(actuator_net_in)
|
||||
base_action_chunk = actuator_unnorm(pred.detach().cpu().numpy()[0])
|
||||
base_action = base_action_chunk[t % prediction_len]
|
||||
else:
|
||||
base_action = action[-2:]
|
||||
# base_action = calibrate_linear_vel(base_action, c=0.19)
|
||||
# base_action = postprocess_base_action(base_action)
|
||||
|
||||
### step the environment
|
||||
if real_robot:
|
||||
@ -418,7 +494,7 @@ def train_bc(train_dataloader, val_dataloader, config):
|
||||
if epoch_val_loss < min_val_loss:
|
||||
min_val_loss = epoch_val_loss
|
||||
best_ckpt_info = (step, min_val_loss, deepcopy(policy.state_dict()))
|
||||
for k in validation_summary.keys():
|
||||
for k in list(validation_summary.keys()):
|
||||
validation_summary[f'val_{k}'] = validation_summary.pop(k)
|
||||
wandb.log(validation_summary, step=step)
|
||||
print(f'Val loss: {epoch_val_loss:.5f}')
|
||||
@ -487,6 +563,10 @@ if __name__ == '__main__':
|
||||
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')
|
||||
parser.add_argument('--actuator_network_dir', action='store', type=str, help='actuator_network_dir', required=False)
|
||||
parser.add_argument('--history_len', action='store', type=int)
|
||||
parser.add_argument('--future_len', action='store', type=int)
|
||||
parser.add_argument('--prediction_len', action='store', type=int)
|
||||
|
||||
# for ACT
|
||||
parser.add_argument('--kl_weight', action='store', type=int, help='KL Weight', required=False)
|
||||
|
||||
165
policy.py
165
policy.py
@ -2,11 +2,174 @@ import torch.nn as nn
|
||||
from torch.nn import functional as F
|
||||
import torchvision.transforms as transforms
|
||||
import torch
|
||||
|
||||
import numpy as np
|
||||
from detr.main import build_ACT_model_and_optimizer, build_CNNMLP_model_and_optimizer
|
||||
import IPython
|
||||
e = IPython.embed
|
||||
|
||||
from collections import OrderedDict
|
||||
from robomimic.models.base_nets import ResNet18Conv, SpatialSoftmax
|
||||
from robomimic.algo.diffusion_policy import replace_bn_with_gn, ConditionalUnet1D
|
||||
|
||||
|
||||
from diffusers.schedulers.scheduling_ddpm import DDPMScheduler
|
||||
from diffusers.schedulers.scheduling_ddim import DDIMScheduler
|
||||
from diffusers.training_utils import EMAModel
|
||||
|
||||
|
||||
class DiffusionPolicy(nn.Module):
|
||||
def __init__(self, args_override):
|
||||
super().__init__()
|
||||
|
||||
self.camera_names = args_override['camera_names']
|
||||
|
||||
self.observation_horizon = args_override['observation_horizon'] ### TODO TODO TODO DO THIS
|
||||
self.action_horizon = args_override['action_horizon'] # apply chunk size
|
||||
self.prediction_horizon = args_override['prediction_horizon'] # chunk size
|
||||
self.num_inference_timesteps = args_override['num_inference_timesteps']
|
||||
self.ema_power = args_override['ema_power']
|
||||
self.lr = args_override['lr']
|
||||
self.weight_decay = 0
|
||||
|
||||
self.num_kp = 32
|
||||
self.feature_dimension = 64
|
||||
self.ac_dim = args_override['action_dim'] # 14 + 2
|
||||
self.obs_dim = self.feature_dimension * len(self.camera_names) + 14 # camera features and proprio
|
||||
|
||||
backbones = []
|
||||
pools = []
|
||||
linears = []
|
||||
for _ in self.camera_names:
|
||||
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}))
|
||||
linears.append(torch.nn.Linear(int(np.prod([self.num_kp, 2])), self.feature_dimension))
|
||||
self.backbones = nn.ModuleList(backbones)
|
||||
self.pools = nn.ModuleList(pools)
|
||||
self.linears = nn.ModuleList(linears)
|
||||
|
||||
self.backbones = replace_bn_with_gn(self.backbones) # TODO
|
||||
|
||||
|
||||
self.noise_pred_net = ConditionalUnet1D(
|
||||
input_dim=self.ac_dim,
|
||||
global_cond_dim=self.obs_dim*self.observation_horizon
|
||||
)
|
||||
|
||||
nets = nn.ModuleDict({
|
||||
'policy': nn.ModuleDict({
|
||||
'backbones': self.backbones,
|
||||
'pools': self.pools,
|
||||
'linears': self.linears,
|
||||
'noise_pred_net': self.noise_pred_net
|
||||
})
|
||||
})
|
||||
|
||||
nets = nets.float()
|
||||
|
||||
self.ema = EMAModel(model=nets, power=self.ema_power)
|
||||
self.nets = nets
|
||||
|
||||
# setup noise scheduler
|
||||
self.noise_scheduler = DDIMScheduler(
|
||||
num_train_timesteps=50,
|
||||
beta_schedule='squaredcos_cap_v2',
|
||||
clip_sample=True,
|
||||
set_alpha_to_one=True,
|
||||
steps_offset=0,
|
||||
prediction_type='epsilon'
|
||||
)
|
||||
|
||||
|
||||
def configure_optimizers(self):
|
||||
optimizer = torch.optim.AdamW(self.nets.parameters(), lr=self.lr, weight_decay=self.weight_decay)
|
||||
return optimizer
|
||||
|
||||
|
||||
def __call__(self, qpos, image, actions=None, is_pad=None):
|
||||
B = qpos.shape[0]
|
||||
if actions is not None: # training time
|
||||
all_features = []
|
||||
for cam_id in range(len(self.camera_names)):
|
||||
cam_image = image[:, cam_id]
|
||||
cam_features = self.backbones[cam_id](cam_image)
|
||||
pool_features = self.pools[cam_id](cam_features)
|
||||
pool_features = torch.flatten(pool_features, start_dim=1)
|
||||
out_features = self.linears[cam_id](pool_features)
|
||||
all_features.append(out_features)
|
||||
|
||||
obs_cond = torch.cat(all_features + [qpos], dim=1)
|
||||
|
||||
# sample noise to add to actions
|
||||
noise = torch.randn(actions.shape, device=obs_cond.device)
|
||||
|
||||
# sample a diffusion iteration for each data point
|
||||
timesteps = torch.randint(
|
||||
0, self.noise_scheduler.config.num_train_timesteps,
|
||||
(B,), device=obs_cond.device
|
||||
).long()
|
||||
|
||||
# add noise to the clean actions according to the noise magnitude at each diffusion iteration
|
||||
# (this is the forward diffusion process)
|
||||
noisy_actions = self.noise_scheduler.add_noise(
|
||||
actions, noise, timesteps)
|
||||
|
||||
# predict the noise residual
|
||||
noise_pred = self.noise_pred_net(noisy_actions, timesteps, global_cond=obs_cond)
|
||||
|
||||
# L2 loss
|
||||
loss = F.mse_loss(noise_pred, noise)
|
||||
|
||||
loss_dict = {}
|
||||
loss_dict['l2_loss'] = loss
|
||||
loss_dict['loss'] = loss
|
||||
return loss_dict
|
||||
else: # inference time
|
||||
To = self.observation_horizon
|
||||
Ta = self.action_horizon
|
||||
Tp = self.prediction_horizon
|
||||
action_dim = self.ac_dim
|
||||
|
||||
# select network ### TODO TODO
|
||||
# nets = self.nets
|
||||
# if self.ema is not None:
|
||||
# nets = self.ema.averaged_model
|
||||
all_features = []
|
||||
for cam_id in range(len(self.camera_names)):
|
||||
cam_image = image[:, cam_id]
|
||||
cam_features = self.backbones[cam_id](cam_image)
|
||||
pool_features = self.pools[cam_id](cam_features)
|
||||
pool_features = torch.flatten(pool_features, start_dim=1)
|
||||
out_features = self.linears[cam_id](pool_features)
|
||||
all_features.append(out_features)
|
||||
|
||||
obs_cond = torch.cat(all_features + [qpos], dim=1)
|
||||
|
||||
# initialize action from Guassian noise
|
||||
noisy_action = torch.randn(
|
||||
(B, Tp, action_dim), device=obs_cond.device)
|
||||
naction = noisy_action
|
||||
|
||||
# init scheduler
|
||||
self.noise_scheduler.set_timesteps(self.num_inference_timesteps)
|
||||
|
||||
for k in self.noise_scheduler.timesteps:
|
||||
# predict noise
|
||||
noise_pred = self.noise_pred_net(
|
||||
sample=naction,
|
||||
timestep=k,
|
||||
global_cond=obs_cond
|
||||
)
|
||||
|
||||
# inverse diffusion step (remove noise)
|
||||
naction = self.noise_scheduler.step(
|
||||
model_output=noise_pred,
|
||||
timestep=k,
|
||||
sample=naction
|
||||
).prev_sample
|
||||
|
||||
return naction
|
||||
|
||||
|
||||
class ACTPolicy(nn.Module):
|
||||
def __init__(self, args_override):
|
||||
super().__init__()
|
||||
|
||||
@ -26,6 +26,7 @@ def load_hdf5(dataset_dir, dataset_name):
|
||||
|
||||
with h5py.File(dataset_path, 'r') as root:
|
||||
is_sim = root.attrs['sim']
|
||||
compressed = root.attrs.get('compress', False)
|
||||
qpos = root['/observations/qpos'][()]
|
||||
qvel = root['/observations/qvel'][()]
|
||||
action = root['/action'][()]
|
||||
@ -37,17 +38,30 @@ def load_hdf5(dataset_dir, dataset_name):
|
||||
base_action = root['/base_action'][()]
|
||||
else:
|
||||
base_action = None
|
||||
if compressed:
|
||||
compress_len = root['/compress_len'][()]
|
||||
|
||||
return qpos, qvel, action, base_action, image_dict
|
||||
if compressed:
|
||||
for cam_id, cam_name in enumerate(image_dict.keys()):
|
||||
# un-pad and uncompress
|
||||
padded_compressed_image_list = image_dict[cam_name]
|
||||
image_list = []
|
||||
for padded_compressed_image in padded_compressed_image_list: # [:1000] to save memory
|
||||
image = cv2.imdecode(padded_compressed_image, 1)
|
||||
image_list.append(image)
|
||||
image_dict[cam_name] = np.array(image_list)
|
||||
|
||||
return qpos, qvel, action, base_action, image_dict, is_sim
|
||||
|
||||
def main(args):
|
||||
dataset_dir = args['dataset_dir']
|
||||
num_episodes = args['num_episodes']
|
||||
|
||||
for episode_idx in range(num_episodes):
|
||||
start_idx = 0
|
||||
for episode_idx in range(start_idx, start_idx + num_episodes):
|
||||
dataset_name = f'episode_{episode_idx}'
|
||||
|
||||
qpos, qvel, action, base_action, image_dict = load_hdf5(dataset_dir, dataset_name)
|
||||
qpos, qvel, action, base_action, image_dict, is_sim = load_hdf5(dataset_dir, dataset_name)
|
||||
|
||||
# process proprioception
|
||||
qpos = np.concatenate([qpos[:, 7:] * MIRROR_STATE_MULTIPLY, qpos[:, :7] * MIRROR_STATE_MULTIPLY], axis=1)
|
||||
@ -59,7 +73,7 @@ def main(args):
|
||||
# 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':
|
||||
elif 'cam_left_wrist' in image_dict.keys():
|
||||
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')
|
||||
@ -86,18 +100,54 @@ def main(args):
|
||||
data_dict[f'/observations/images/{cam_name}'] = image_dict[cam_name]
|
||||
max_timesteps = len(qpos)
|
||||
|
||||
COMPRESS = True
|
||||
|
||||
if COMPRESS:
|
||||
# JPEG compression
|
||||
t0 = time.time()
|
||||
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 50] # tried as low as 20, seems fine
|
||||
compressed_len = []
|
||||
for cam_name in image_dict.keys():
|
||||
image_list = data_dict[f'/observations/images/{cam_name}']
|
||||
compressed_list = []
|
||||
compressed_len.append([])
|
||||
for image in image_list:
|
||||
result, encoded_image = cv2.imencode('.jpg', image, encode_param) # 0.02 sec # cv2.imdecode(encoded_image, 1)
|
||||
compressed_list.append(encoded_image)
|
||||
compressed_len[-1].append(len(encoded_image))
|
||||
data_dict[f'/observations/images/{cam_name}'] = compressed_list
|
||||
print(f'compression: {time.time() - t0:.2f}s')
|
||||
|
||||
# pad so it has same length
|
||||
t0 = time.time()
|
||||
compressed_len = np.array(compressed_len)
|
||||
padded_size = compressed_len.max()
|
||||
for cam_name in image_dict.keys():
|
||||
compressed_image_list = data_dict[f'/observations/images/{cam_name}']
|
||||
padded_compressed_image_list = []
|
||||
for compressed_image in compressed_image_list:
|
||||
padded_compressed_image = np.zeros(padded_size, dtype='uint8')
|
||||
image_len = len(compressed_image)
|
||||
padded_compressed_image[:image_len] = compressed_image
|
||||
padded_compressed_image_list.append(padded_compressed_image)
|
||||
data_dict[f'/observations/images/{cam_name}'] = padded_compressed_image_list
|
||||
print(f'padding: {time.time() - t0:.2f}s')
|
||||
|
||||
# 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
|
||||
root.attrs['sim'] = is_sim
|
||||
root.attrs['compress'] = COMPRESS
|
||||
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',
|
||||
if COMPRESS:
|
||||
_ = image.create_dataset(cam_name, (max_timesteps, padded_size), dtype='uint8',
|
||||
chunks=(1, padded_size), )
|
||||
else:
|
||||
_ = 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))
|
||||
@ -106,11 +156,17 @@ def main(args):
|
||||
|
||||
for name, array in data_dict.items():
|
||||
root[name][...] = array
|
||||
|
||||
if COMPRESS:
|
||||
_ = root.create_dataset('compress_len', (len(image_dict.keys()), max_timesteps))
|
||||
root['/compress_len'][...] = compressed_len
|
||||
|
||||
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 episode_idx == start_idx:
|
||||
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()
|
||||
|
||||
367
train_actuator_network.py
普通文件
367
train_actuator_network.py
普通文件
@ -0,0 +1,367 @@
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
from torch import nn
|
||||
from torch.nn import functional as F
|
||||
from torch.utils.data import DataLoader
|
||||
import os
|
||||
import h5py
|
||||
import math
|
||||
import wandb
|
||||
import pickle
|
||||
import matplotlib.pyplot as plt
|
||||
from copy import deepcopy
|
||||
from tqdm import tqdm
|
||||
from utils import find_all_hdf5
|
||||
from imitate_episodes import repeater, compute_dict_mean
|
||||
|
||||
import IPython
|
||||
e = IPython.embed
|
||||
|
||||
def main():
|
||||
### Idea
|
||||
# input : o o o o o o # observed speed
|
||||
# target: a a a a a a # commanded speed
|
||||
# at test time, input desired speed profile and convert that to command
|
||||
|
||||
#########################################################
|
||||
history_len = 50
|
||||
future_len = 50
|
||||
prediction_len = 50
|
||||
batch_size_train = 16
|
||||
batch_size_val = 16
|
||||
lr = 1e-4
|
||||
weight_decay = 1e-4
|
||||
|
||||
num_steps = 10000
|
||||
validate_every = 2000
|
||||
save_every = 2000
|
||||
|
||||
expr_name = f'actuator_network_test_{history_len}_{future_len}_{prediction_len}'
|
||||
ckpt_dir = f'/scr/tonyzhao/train_logs/{expr_name}' if os.getlogin() == 'tonyzhao' else f'./ckpts/{expr_name}'
|
||||
dataset_dir = '/scr/tonyzhao/compressed_datasets/aloha_mobile_fork/' if os.getlogin() == 'tonyzhao' else '/home/zfu/data/aloha_mobile_fork/'
|
||||
#########################################################
|
||||
assert(history_len + future_len >= prediction_len)
|
||||
assert(future_len % prediction_len == 0)
|
||||
|
||||
wandb.init(project="mobile-aloha2", reinit=True, entity="mobile-aloha2", name=expr_name) # mode='disabled',
|
||||
|
||||
if not os.path.isdir(ckpt_dir):
|
||||
os.makedirs(ckpt_dir)
|
||||
|
||||
dataset_path_list = find_all_hdf5(dataset_dir, skip_mirrored_data=True)
|
||||
dataset_path_list = [n for n in dataset_path_list if 'replayed' in n]
|
||||
num_episodes = len(dataset_path_list)
|
||||
|
||||
# obtain train test split
|
||||
train_ratio = 0.9
|
||||
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):]
|
||||
print(f'\n\nData from: {dataset_dir}\n- Train on {len(train_episode_ids)} episodes\n- Test on {len(val_episode_ids)} episodes\n\n')
|
||||
|
||||
# obtain normalization stats for qpos and action
|
||||
# if load_pretrain:
|
||||
# with open(os.path.join('/home/zfu/interbotix_ws/src/act/ckpts/pretrain_all', 'dataset_stats.pkl'), 'rb') as f:
|
||||
# norm_stats = pickle.load(f)
|
||||
# print('Loaded pretrain dataset stats')
|
||||
norm_stats, all_episode_len = get_norm_stats(dataset_path_list)
|
||||
train_episode_len = [all_episode_len[i] for i in train_episode_ids]
|
||||
val_episode_len = [all_episode_len[i] for i in val_episode_ids]
|
||||
assert(all_episode_len[0] % prediction_len == 0)
|
||||
|
||||
# save dataset stats
|
||||
stats_path = os.path.join(ckpt_dir, f'actuator_net_stats.pkl')
|
||||
with open(stats_path, 'wb') as f:
|
||||
pickle.dump(norm_stats, f)
|
||||
|
||||
# construct dataset and dataloader
|
||||
train_dataset = EpisodicDataset(dataset_path_list, norm_stats, train_episode_ids, train_episode_len, history_len, future_len, prediction_len)
|
||||
val_dataset = EpisodicDataset(dataset_path_list, norm_stats, val_episode_ids, val_episode_len, history_len, future_len, prediction_len)
|
||||
train_dataloader = DataLoader(train_dataset, batch_size=batch_size_train, shuffle=True, pin_memory=True, num_workers=1, prefetch_factor=1)
|
||||
val_dataloader = DataLoader(val_dataset, batch_size=batch_size_val, shuffle=True, pin_memory=True, num_workers=1, prefetch_factor=1)
|
||||
|
||||
policy = ActuatorNetwork(prediction_len).cuda()
|
||||
optimizer = torch.optim.AdamW(policy.parameters(), lr=lr, weight_decay=weight_decay)
|
||||
|
||||
n_parameters = sum(p.numel() for p in policy.parameters() if p.requires_grad)
|
||||
print("number of parameters: %.2fM" % (n_parameters/1e6,))
|
||||
|
||||
min_val_loss = np.inf
|
||||
best_ckpt_info = None
|
||||
train_dataloader = repeater(train_dataloader)
|
||||
for step in tqdm(range(num_steps+1)):
|
||||
# validation
|
||||
if step % validate_every == 0:
|
||||
print('validating')
|
||||
|
||||
with torch.inference_mode():
|
||||
policy.eval()
|
||||
validation_dicts = []
|
||||
for batch_idx, data in enumerate(val_dataloader):
|
||||
observed_speed, commanded_speed = data
|
||||
out, forward_dict = policy(observed_speed.cuda(), commanded_speed.cuda())
|
||||
validation_dicts.append(forward_dict)
|
||||
|
||||
validation_summary = compute_dict_mean(validation_dicts)
|
||||
|
||||
epoch_val_loss = validation_summary['loss']
|
||||
if epoch_val_loss < min_val_loss:
|
||||
min_val_loss = epoch_val_loss
|
||||
best_ckpt_info = (step, min_val_loss, deepcopy(policy.state_dict()))
|
||||
for k in list(validation_summary.keys()):
|
||||
validation_summary[f'val_{k}'] = validation_summary.pop(k)
|
||||
wandb.log(validation_summary, step=step)
|
||||
print(f'Val loss: {epoch_val_loss:.5f}')
|
||||
summary_string = ''
|
||||
for k, v in validation_summary.items():
|
||||
summary_string += f'{k}: {v.item():.3f} '
|
||||
print(summary_string)
|
||||
|
||||
visualize_prediction(dataset_path_list, val_episode_ids, policy, norm_stats, history_len, future_len, prediction_len, ckpt_dir, step, 'val')
|
||||
visualize_prediction(dataset_path_list, train_episode_ids, policy, norm_stats, history_len, future_len, prediction_len, ckpt_dir, step, 'train')
|
||||
|
||||
|
||||
# training
|
||||
policy.train()
|
||||
optimizer.zero_grad()
|
||||
data = next(train_dataloader)
|
||||
observed_speed, commanded_speed = data
|
||||
out, forward_dict = policy(observed_speed.cuda(), commanded_speed.cuda())
|
||||
# backward
|
||||
loss = forward_dict['loss']
|
||||
loss.backward()
|
||||
optimizer.step()
|
||||
wandb.log(forward_dict, step=step) # not great, make training 1-2% slower
|
||||
|
||||
if step % save_every == 0:
|
||||
ckpt_path = os.path.join(ckpt_dir, f'actuator_net_step_{step}.ckpt')
|
||||
torch.save(policy.state_dict(), ckpt_path)
|
||||
|
||||
ckpt_path = os.path.join(ckpt_dir, f'actuator_net_last.ckpt')
|
||||
torch.save(policy.state_dict(), ckpt_path)
|
||||
|
||||
best_step, min_val_loss, best_state_dict = best_ckpt_info
|
||||
ckpt_path = os.path.join(ckpt_dir, f'actuator_net_step_{best_step}.ckpt')
|
||||
torch.save(best_state_dict, ckpt_path)
|
||||
print(f'Training finished:\nval loss {min_val_loss:.6f} at step {best_step}')
|
||||
|
||||
|
||||
def visualize_prediction(dataset_path_list, episode_ids, policy, norm_stats, history_len, future_len, prediction_len, ckpt_dir, step, name):
|
||||
num_vis = 2
|
||||
episode_ids = episode_ids[:num_vis]
|
||||
vis_path = [dataset_path_list[i] for i in episode_ids]
|
||||
|
||||
for i, dataset_path in enumerate(vis_path):
|
||||
try:
|
||||
with h5py.File(dataset_path, 'r') as root:
|
||||
commanded_speed = root['/base_action'][()]
|
||||
observed_speed = root['/obs_tracer'][()]
|
||||
except Exception as ee:
|
||||
print(f'Error loading {dataset_path} in get_norm_stats')
|
||||
print(ee)
|
||||
quit()
|
||||
|
||||
# commanded_speed = (commanded_speed - norm_stats["commanded_speed_mean"]) / norm_stats["commanded_speed_std"]
|
||||
norm_observed_speed = (observed_speed - norm_stats["observed_speed_mean"]) / norm_stats["observed_speed_std"]
|
||||
out_unnorm_fn = lambda x: (x * norm_stats["commanded_speed_std"]) + norm_stats["commanded_speed_mean"]
|
||||
|
||||
history_pad = np.zeros((history_len, 2))
|
||||
future_pad = np.zeros((future_len, 2))
|
||||
norm_observed_speed = np.concatenate([history_pad, norm_observed_speed, future_pad], axis=0)
|
||||
|
||||
episode_len = commanded_speed.shape[0]
|
||||
|
||||
all_pred = []
|
||||
for t in range(0, episode_len, prediction_len):
|
||||
offset_start_ts = t + history_len
|
||||
policy_input = norm_observed_speed[offset_start_ts-history_len: offset_start_ts+future_len]
|
||||
policy_input = torch.from_numpy(policy_input).float().unsqueeze(dim=0).cuda()
|
||||
pred = policy(policy_input)
|
||||
pred = pred.detach().cpu().numpy()[0]
|
||||
all_pred += out_unnorm_fn(pred).tolist()
|
||||
all_pred = np.array(all_pred)
|
||||
|
||||
plot_path = os.path.join(ckpt_dir, f'{name}{i}_step{step}_linear')
|
||||
plt.figure()
|
||||
plt.plot(commanded_speed[:, 0], label='commanded_speed_linear')
|
||||
plt.plot(observed_speed[:, 0], label='observed_speed_linear')
|
||||
plt.plot(all_pred[:, 0], label='pred_commanded_speed_linear')
|
||||
# plot vertical grey dotted lines every prediction_len
|
||||
for t in range(0, episode_len, prediction_len):
|
||||
plt.axvline(t, linestyle='--', color='grey')
|
||||
plt.legend()
|
||||
plt.savefig(plot_path)
|
||||
plt.close()
|
||||
|
||||
plot_path = os.path.join(ckpt_dir, f'{name}{i}_step{step}_angular')
|
||||
plt.figure()
|
||||
plt.plot(commanded_speed[:, 1], label='commanded_speed_angular')
|
||||
plt.plot(observed_speed[:, 1], label='observed_speed_angular')
|
||||
plt.plot(all_pred[:, 1], label='pred_commanded_speed_angular')
|
||||
# plot vertical dotted lines every prediction_len
|
||||
for t in range(0, episode_len, prediction_len):
|
||||
plt.axvline(t, linestyle='--', color='grey')
|
||||
plt.legend()
|
||||
plt.savefig(plot_path)
|
||||
plt.close()
|
||||
|
||||
|
||||
|
||||
class ActuatorNetwork(nn.Module):
|
||||
|
||||
def __init__(self, prediction_len):
|
||||
super().__init__()
|
||||
d_model = 256
|
||||
encoder_layer = nn.TransformerEncoderLayer(d_model=d_model, nhead=8)
|
||||
self.transformer = nn.TransformerEncoder(encoder_layer, num_layers=3)
|
||||
self.pe = PositionalEncoding(d_model)
|
||||
self.in_proj = nn.Linear(2, d_model)
|
||||
self.out_proj = nn.Linear(d_model, 2)
|
||||
self.prediction_len = prediction_len
|
||||
|
||||
def forward(self, src, tgt=None):
|
||||
if tgt is not None: # training time
|
||||
# (batch, seq, feature) -> (seq, batch, feature)
|
||||
src = self.in_proj(src)
|
||||
src = torch.einsum('b s d -> s b d', src)
|
||||
src = self.pe(src)
|
||||
out = self.transformer(src)
|
||||
|
||||
tgt = torch.einsum('b s d -> s b d', tgt)
|
||||
assert(self.prediction_len == tgt.shape[0])
|
||||
out = out[0: self.prediction_len] # take first few tokens only for prediction
|
||||
out = self.out_proj(out)
|
||||
|
||||
l2_loss = loss = F.mse_loss(out, tgt)
|
||||
loss_dict = {'loss': l2_loss}
|
||||
out = torch.einsum('s b d -> b s d', out)
|
||||
return out, loss_dict
|
||||
else:
|
||||
src = self.in_proj(src)
|
||||
src = torch.einsum('b s d -> s b d', src)
|
||||
src = self.pe(src)
|
||||
out = self.transformer(src)
|
||||
out = out[0: self.prediction_len] # take first few tokens only for prediction
|
||||
out = self.out_proj(out)
|
||||
out = torch.einsum('s b d -> b s d', out)
|
||||
return out
|
||||
|
||||
|
||||
|
||||
class PositionalEncoding(nn.Module):
|
||||
def __init__(self, d_model: int, dropout: float = 0.1, max_len: int = 5000):
|
||||
super().__init__()
|
||||
self.dropout = nn.Dropout(p=dropout)
|
||||
position = torch.arange(max_len).unsqueeze(1)
|
||||
div_term = torch.exp(torch.arange(0, d_model, 2) * (-math.log(10000.0) / d_model))
|
||||
pe = torch.zeros(max_len, 1, d_model)
|
||||
pe[:, 0, 0::2] = torch.sin(position * div_term)
|
||||
pe[:, 0, 1::2] = torch.cos(position * div_term)
|
||||
self.register_buffer('pe', pe)
|
||||
|
||||
def forward(self, x):
|
||||
"""
|
||||
Arguments:
|
||||
x: Tensor, shape ``[seq_len, batch_size, embedding_dim]``
|
||||
"""
|
||||
x = x + self.pe[:x.size(0)]
|
||||
return self.dropout(x)
|
||||
|
||||
def get_norm_stats(dataset_path_list):
|
||||
all_commanded_speed = []
|
||||
all_observed_speed = []
|
||||
all_episode_len = []
|
||||
for dataset_path in dataset_path_list:
|
||||
try:
|
||||
with h5py.File(dataset_path, 'r') as root:
|
||||
commanded_speed = root['/base_action'][()]
|
||||
observed_speed = root['/obs_tracer'][()]
|
||||
except Exception as e:
|
||||
print(f'Error loading {dataset_path} in get_norm_stats')
|
||||
print(e)
|
||||
quit()
|
||||
all_commanded_speed.append(torch.from_numpy(commanded_speed))
|
||||
all_observed_speed.append(torch.from_numpy(observed_speed))
|
||||
all_episode_len.append(len(commanded_speed))
|
||||
all_commanded_speed = torch.cat(all_commanded_speed, dim=0)
|
||||
all_observed_speed = torch.cat(all_observed_speed, dim=0)
|
||||
|
||||
# normalize all_commanded_speed
|
||||
commanded_speed_mean = all_commanded_speed.mean(dim=[0]).float()
|
||||
commanded_speed_std = all_commanded_speed.std(dim=[0]).float()
|
||||
commanded_speed_std = torch.clip(commanded_speed_std, 1e-2, np.inf) # clipping
|
||||
|
||||
# normalize all_observed_speed
|
||||
observed_speed_mean = all_observed_speed.mean(dim=[0]).float()
|
||||
observed_speed_std = all_observed_speed.std(dim=[0]).float()
|
||||
observed_speed_std = torch.clip(observed_speed_std, 1e-2, np.inf) # clipping
|
||||
|
||||
stats = {"commanded_speed_mean": commanded_speed_mean.numpy(), "commanded_speed_std": commanded_speed_std.numpy(),
|
||||
"observed_speed_mean": observed_speed_mean.numpy(), "observed_speed_std": observed_speed_std.numpy()}
|
||||
|
||||
return stats, all_episode_len
|
||||
|
||||
|
||||
class EpisodicDataset(torch.utils.data.Dataset):
|
||||
def __init__(self, dataset_path_list, norm_stats, episode_ids, episode_len, history_len, future_len, prediction_len):
|
||||
super(EpisodicDataset).__init__()
|
||||
self.episode_ids = episode_ids
|
||||
self.dataset_path_list = dataset_path_list
|
||||
self.norm_stats = norm_stats
|
||||
self.episode_len = episode_len
|
||||
self.cumulative_len = np.cumsum(self.episode_len)
|
||||
self.max_episode_len = max(episode_len)
|
||||
self.history_len = history_len
|
||||
self.future_len = future_len
|
||||
self.prediction_len = prediction_len
|
||||
self.is_sim = False
|
||||
self.history_pad = np.zeros((self.history_len, 2))
|
||||
self.future_pad = np.zeros((self.future_len, 2))
|
||||
self.prediction_pad = np.zeros((self.prediction_len, 2))
|
||||
self.__getitem__(0) # initialize self.is_sim
|
||||
|
||||
def __len__(self):
|
||||
return sum(self.episode_len)
|
||||
|
||||
def _locate_transition(self, index):
|
||||
assert index < self.cumulative_len[-1]
|
||||
episode_index = np.argmax(self.cumulative_len > index) # argmax returns first True index
|
||||
start_ts = index - (self.cumulative_len[episode_index] - self.episode_len[episode_index])
|
||||
episode_id = self.episode_ids[episode_index]
|
||||
return episode_id, start_ts
|
||||
|
||||
def __getitem__(self, index):
|
||||
episode_id, start_ts = self._locate_transition(index)
|
||||
dataset_path = self.dataset_path_list[episode_id]
|
||||
try:
|
||||
# print(dataset_path)
|
||||
with h5py.File(dataset_path, 'r') as root:
|
||||
commanded_speed = root['/base_action'][()]
|
||||
observed_speed = root['/obs_tracer'][()]
|
||||
observed_speed = np.concatenate([self.history_pad, observed_speed, self.future_pad], axis=0)
|
||||
commanded_speed = np.concatenate([commanded_speed, self.prediction_pad], axis=0)
|
||||
|
||||
offset_start_ts = start_ts + self.history_len
|
||||
commanded_speed = commanded_speed[start_ts: start_ts+self.prediction_len]
|
||||
observed_speed = observed_speed[offset_start_ts-self.history_len: offset_start_ts+self.future_len]
|
||||
|
||||
commanded_speed = torch.from_numpy(commanded_speed).float()
|
||||
observed_speed = torch.from_numpy(observed_speed).float()
|
||||
|
||||
# normalize to mean 0 std 1
|
||||
commanded_speed = (commanded_speed - self.norm_stats["commanded_speed_mean"]) / self.norm_stats["commanded_speed_std"]
|
||||
observed_speed = (observed_speed - self.norm_stats["observed_speed_mean"]) / self.norm_stats["observed_speed_std"]
|
||||
|
||||
except:
|
||||
print(f'Error loading {dataset_path} in __getitem__')
|
||||
quit()
|
||||
|
||||
# print(image_data.dtype, qpos_data.dtype, action_data.dtype, is_pad.dtype)
|
||||
return observed_speed, commanded_speed
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
31
utils.py
31
utils.py
@ -4,13 +4,14 @@ import os
|
||||
import h5py
|
||||
import pickle
|
||||
import fnmatch
|
||||
import cv2
|
||||
from torch.utils.data import TensorDataset, DataLoader
|
||||
|
||||
import IPython
|
||||
e = IPython.embed
|
||||
|
||||
class EpisodicDataset(torch.utils.data.Dataset):
|
||||
def __init__(self, dataset_path_list, camera_names, norm_stats, episode_ids, episode_len, chunk_size):
|
||||
def __init__(self, dataset_path_list, camera_names, norm_stats, episode_ids, episode_len, chunk_size, policy_class):
|
||||
super(EpisodicDataset).__init__()
|
||||
self.episode_ids = episode_ids
|
||||
self.dataset_path_list = dataset_path_list
|
||||
@ -20,6 +21,7 @@ class EpisodicDataset(torch.utils.data.Dataset):
|
||||
self.chunk_size = chunk_size
|
||||
self.cumulative_len = np.cumsum(self.episode_len)
|
||||
self.max_episode_len = max(episode_len)
|
||||
self.policy_class = policy_class
|
||||
self.__getitem__(0) # initialize self.is_sim
|
||||
self.is_sim = False
|
||||
|
||||
@ -43,6 +45,7 @@ class EpisodicDataset(torch.utils.data.Dataset):
|
||||
is_sim = root.attrs['sim']
|
||||
except:
|
||||
is_sim = False
|
||||
compressed = root.attrs.get('compress', False)
|
||||
if '/base_action' in root:
|
||||
base_action = root['/base_action'][()]
|
||||
base_action = preprocess_base_action(base_action)
|
||||
@ -59,6 +62,12 @@ class EpisodicDataset(torch.utils.data.Dataset):
|
||||
image_dict = dict()
|
||||
for cam_name in self.camera_names:
|
||||
image_dict[cam_name] = root[f'/observations/images/{cam_name}'][start_ts]
|
||||
|
||||
if compressed:
|
||||
for cam_name in image_dict.keys():
|
||||
decompressed_image = cv2.imdecode(image_dict[cam_name], 1)
|
||||
image_dict[cam_name] = np.array(decompressed_image)
|
||||
|
||||
# get all actions after and including start_ts
|
||||
if is_sim:
|
||||
action = action[start_ts:]
|
||||
@ -93,7 +102,14 @@ class EpisodicDataset(torch.utils.data.Dataset):
|
||||
|
||||
# normalize image and change dtype to float
|
||||
image_data = image_data / 255.0
|
||||
action_data = (action_data - self.norm_stats["action_mean"]) / self.norm_stats["action_std"]
|
||||
|
||||
if self.policy_class == 'Diffusion':
|
||||
# normalize to [-1, 1]
|
||||
action_data = ((action_data - self.norm_stats["action_min"]) / (self.norm_stats["action_max"] - self.norm_stats["action_min"])) * 2 - 1
|
||||
else:
|
||||
# normalize to mean 0 std 1
|
||||
action_data = (action_data - self.norm_stats["action_mean"]) / self.norm_stats["action_std"]
|
||||
|
||||
qpos_data = (qpos_data - self.norm_stats["qpos_mean"]) / self.norm_stats["qpos_std"]
|
||||
|
||||
except:
|
||||
@ -142,7 +158,12 @@ def get_norm_stats(dataset_path_list):
|
||||
qpos_std = all_qpos_data.std(dim=[0]).float()
|
||||
qpos_std = torch.clip(qpos_std, 1e-2, np.inf) # clipping
|
||||
|
||||
action_min = all_action_data.min(dim=0).values.float()
|
||||
action_max = all_action_data.max(dim=0).values.float()
|
||||
|
||||
eps = 0.0001
|
||||
stats = {"action_mean": action_mean.numpy(), "action_std": action_std.numpy(),
|
||||
"action_min": action_min.numpy() - eps,"action_max": action_max.numpy() + eps,
|
||||
"qpos_mean": qpos_mean.numpy(), "qpos_std": qpos_std.numpy(),
|
||||
"example_qpos": qpos}
|
||||
|
||||
@ -159,7 +180,7 @@ def find_all_hdf5(dataset_dir, skip_mirrored_data):
|
||||
return hdf5_files
|
||||
|
||||
|
||||
def load_data(dataset_dir, name_filter, camera_names, batch_size_train, batch_size_val, chunk_size, skip_mirrored_data=False, load_pretrain=False):
|
||||
def load_data(dataset_dir, name_filter, camera_names, batch_size_train, batch_size_val, chunk_size, skip_mirrored_data=False, load_pretrain=False, policy_class=None):
|
||||
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)]
|
||||
num_episodes = len(dataset_path_list)
|
||||
@ -181,8 +202,8 @@ def load_data(dataset_dir, name_filter, camera_names, batch_size_train, batch_si
|
||||
val_episode_len = [all_episode_len[i] for i in val_episode_ids]
|
||||
|
||||
# construct dataset and dataloader
|
||||
train_dataset = EpisodicDataset(dataset_path_list, camera_names, norm_stats, train_episode_ids, train_episode_len, chunk_size)
|
||||
val_dataset = EpisodicDataset(dataset_path_list, camera_names, norm_stats, val_episode_ids, val_episode_len, chunk_size)
|
||||
train_dataset = EpisodicDataset(dataset_path_list, camera_names, norm_stats, train_episode_ids, train_episode_len, chunk_size, policy_class)
|
||||
val_dataset = EpisodicDataset(dataset_path_list, camera_names, norm_stats, val_episode_ids, val_episode_len, chunk_size, policy_class)
|
||||
train_dataloader = DataLoader(train_dataset, batch_size=batch_size_train, shuffle=True, pin_memory=True, num_workers=1, prefetch_factor=1)
|
||||
val_dataloader = DataLoader(val_dataset, batch_size=batch_size_val, shuffle=True, pin_memory=True, num_workers=1, prefetch_factor=1)
|
||||
|
||||
|
||||
在新工单中引用
屏蔽一个用户