Merge branch 'main' of github.com:MarkFzp/act-plus-plus into main
这个提交包含在:
49
commands.txt
49
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,55 @@ 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
|
||||
|
||||
|
||||
|
||||
|
||||
@ -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,6 +80,19 @@ 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,}
|
||||
@ -129,7 +142,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 +164,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 +176,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
|
||||
@ -214,7 +231,10 @@ def eval_bc(config, ckpt_name, save_episode=True, num_rollouts=50):
|
||||
stats = pickle.load(f)
|
||||
|
||||
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:
|
||||
@ -306,6 +326,10 @@ 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)
|
||||
raw_action = all_actions[:, t % query_frequency]
|
||||
elif config['policy_class'] == "CNNMLP":
|
||||
raw_action = policy(qpos, curr_image)
|
||||
else:
|
||||
@ -416,7 +440,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}')
|
||||
|
||||
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__()
|
||||
|
||||
23
utils.py
23
utils.py
@ -11,7 +11,7 @@ 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
|
||||
@ -21,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
|
||||
|
||||
@ -101,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:
|
||||
@ -150,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}
|
||||
|
||||
@ -167,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)
|
||||
@ -189,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)
|
||||
|
||||
|
||||
在新工单中引用
屏蔽一个用户