比较提交
1 次代码提交
async_quer
...
history
| 作者 | SHA1 | 提交日期 | |
|---|---|---|---|
| ace063440d |
18
commands.txt
18
commands.txt
@ -74,4 +74,22 @@ CUDA_VISIBLE_DEVICES=0 python3 imitate_episodes.py \
|
||||
|
||||
|
||||
|
||||
## Nov 10 history
|
||||
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_insertion_scripted_mirror \
|
||||
--ckpt_dir /scr/tonyzhao/train_logs/cube_scripted_test_history \
|
||||
--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 100000 --validate_every 5000 --save_every 2000 \
|
||||
--skip_mirrored_data --no_encoder --history 100 --eval
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -75,6 +75,7 @@ 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('--history', action='store', type=int, required=False, default=0) # default no history
|
||||
|
||||
return parser
|
||||
|
||||
|
||||
@ -111,6 +111,22 @@ class Joiner(nn.Sequential):
|
||||
|
||||
return out, pos
|
||||
|
||||
class JoinerChannelConcat(nn.Sequential):
|
||||
def __init__(self, backbone, position_embedding, conv):
|
||||
super().__init__(backbone, position_embedding, conv)
|
||||
|
||||
def forward(self, tensor_list: NestedTensor):
|
||||
tensor_list = self[2](tensor_list) # 6 to 3 channels
|
||||
xs = self[0](tensor_list)
|
||||
out: List[NestedTensor] = []
|
||||
pos = []
|
||||
for name, x in xs.items():
|
||||
out.append(x)
|
||||
# position encoding
|
||||
pos.append(self[1](x).to(x.dtype))
|
||||
|
||||
return out, pos
|
||||
|
||||
|
||||
def build_backbone(args):
|
||||
position_embedding = build_position_encoding(args)
|
||||
@ -120,3 +136,15 @@ def build_backbone(args):
|
||||
model = Joiner(backbone, position_embedding)
|
||||
model.num_channels = backbone.num_channels
|
||||
return model
|
||||
|
||||
def build_backbone_channel_concat(args):
|
||||
position_embedding = build_position_encoding(args)
|
||||
train_backbone = args.lr_backbone > 0
|
||||
return_interm_layers = args.masks
|
||||
backbone = Backbone(args.backbone, train_backbone, return_interm_layers, args.dilation)
|
||||
conv = nn.Conv2d(in_channels=6, out_channels=3, kernel_size=3, stride=1, padding=1)
|
||||
model = JoinerChannelConcat(backbone, position_embedding, conv)
|
||||
model.num_channels = backbone.num_channels
|
||||
return model
|
||||
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@ import torch
|
||||
from torch import nn
|
||||
from torch.autograd import Variable
|
||||
import torch.nn.functional as F
|
||||
from .backbone import build_backbone
|
||||
from .backbone import build_backbone, build_backbone_channel_concat
|
||||
from .transformer import build_transformer, TransformerEncoder, TransformerEncoderLayer
|
||||
|
||||
import numpy as np
|
||||
@ -274,6 +274,10 @@ def build(args):
|
||||
# From image
|
||||
backbones = []
|
||||
for _ in args.camera_names:
|
||||
if args.history >= 1:
|
||||
backbone = build_backbone_channel_concat(args)
|
||||
backbones.append(backbone)
|
||||
else:
|
||||
backbone = build_backbone(args)
|
||||
backbones.append(backbone)
|
||||
|
||||
|
||||
@ -8,6 +8,7 @@ from copy import deepcopy
|
||||
from itertools import repeat
|
||||
from tqdm import tqdm
|
||||
from einops import rearrange
|
||||
from collections import deque
|
||||
import wandb
|
||||
|
||||
from constants import DT
|
||||
@ -40,6 +41,7 @@ def main(args):
|
||||
validate_every = args['validate_every']
|
||||
save_every = args['save_every']
|
||||
resume_ckpt_path = args['resume_ckpt_path']
|
||||
history = args['history']
|
||||
|
||||
# get task parameters
|
||||
is_sim = task_name[:4] == 'sim_'
|
||||
@ -79,6 +81,7 @@ def main(args):
|
||||
'vq_dim': args['vq_dim'],
|
||||
'action_dim': 16,
|
||||
'no_encoder': args['no_encoder'],
|
||||
'history': history,
|
||||
}
|
||||
elif policy_class == 'CNNMLP':
|
||||
policy_config = {'lr': args['lr'], 'lr_backbone': lr_backbone, 'backbone' : backbone, 'num_queries': 1,
|
||||
@ -104,7 +107,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'],
|
||||
'history': history,
|
||||
}
|
||||
|
||||
if not os.path.isdir(ckpt_dir):
|
||||
@ -129,7 +133,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'], history, args['skip_mirrored_data'], config['load_pretrain'])
|
||||
|
||||
# save dataset stats
|
||||
stats_path = os.path.join(ckpt_dir, f'dataset_stats.pkl')
|
||||
@ -188,8 +192,10 @@ def eval_bc(config, ckpt_name, save_episode=True, num_rollouts=50):
|
||||
max_timesteps = config['episode_len']
|
||||
task_name = config['task_name']
|
||||
temporal_agg = config['temporal_agg']
|
||||
history = config['history']
|
||||
onscreen_cam = 'angle'
|
||||
vq = config['policy_config']['vq']
|
||||
has_history = history >= 1
|
||||
|
||||
# load policy and stats
|
||||
ckpt_path = os.path.join(ckpt_dir, ckpt_name)
|
||||
@ -261,6 +267,8 @@ def eval_bc(config, ckpt_name, save_episode=True, num_rollouts=50):
|
||||
qpos_list = []
|
||||
target_qpos_list = []
|
||||
rewards = []
|
||||
if has_history:
|
||||
image_obs_history = deque(maxlen=history+1)
|
||||
with torch.inference_mode():
|
||||
for t in range(max_timesteps):
|
||||
### update onscreen render and wait for DT
|
||||
@ -281,6 +289,9 @@ def eval_bc(config, ckpt_name, save_episode=True, num_rollouts=50):
|
||||
qpos_history[:, t] = qpos
|
||||
curr_image = get_image(ts, camera_names)
|
||||
|
||||
if has_history:
|
||||
image_obs_history.append(curr_image)
|
||||
|
||||
### query policy
|
||||
if config['policy_class'] == "ACT":
|
||||
if t % query_frequency == 0:
|
||||
@ -292,7 +303,9 @@ def eval_bc(config, ckpt_name, save_episode=True, num_rollouts=50):
|
||||
vq_sample = latent_model.generate(1, temperature=1, x=None)
|
||||
all_actions = policy(qpos, curr_image, vq_sample=vq_sample)
|
||||
else:
|
||||
# e()
|
||||
if has_history:
|
||||
history_image = image_obs_history[0]
|
||||
curr_image = torch.cat([curr_image, history_image], dim=2)
|
||||
all_actions = policy(qpos, curr_image)
|
||||
if temporal_agg:
|
||||
all_time_actions[[t], t:t+num_queries] = all_actions
|
||||
@ -496,5 +509,6 @@ if __name__ == '__main__':
|
||||
parser.add_argument('--vq_class', action='store', type=int, help='vq_class')
|
||||
parser.add_argument('--vq_dim', action='store', type=int, help='vq_dim')
|
||||
parser.add_argument('--no_encoder', action='store_true')
|
||||
parser.add_argument('--history', action='store', type=int, required=False, default=0) # default no history
|
||||
|
||||
main(vars(parser.parse_args()))
|
||||
|
||||
@ -19,8 +19,15 @@ class ACTPolicy(nn.Module):
|
||||
|
||||
def __call__(self, qpos, image, actions=None, is_pad=None, vq_sample=None):
|
||||
env_state = None
|
||||
|
||||
if image.shape[2] == 6:
|
||||
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406] * 2,
|
||||
std=[0.229, 0.224, 0.225] * 2)
|
||||
else:
|
||||
assert image.shape[2] == 3
|
||||
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
||||
std=[0.229, 0.224, 0.225])
|
||||
|
||||
image = normalize(image)
|
||||
if actions is not None: # training time
|
||||
actions = actions[:, :self.model.num_queries]
|
||||
|
||||
15
utils.py
15
utils.py
@ -10,7 +10,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, history):
|
||||
super(EpisodicDataset).__init__()
|
||||
self.episode_ids = episode_ids
|
||||
self.dataset_path_list = dataset_path_list
|
||||
@ -18,6 +18,7 @@ class EpisodicDataset(torch.utils.data.Dataset):
|
||||
self.norm_stats = norm_stats
|
||||
self.episode_len = episode_len
|
||||
self.chunk_size = chunk_size
|
||||
self.history = history
|
||||
self.cumulative_len = np.cumsum(self.episode_len)
|
||||
self.max_episode_len = max(episode_len)
|
||||
self.__getitem__(0) # initialize self.is_sim
|
||||
@ -58,6 +59,12 @@ class EpisodicDataset(torch.utils.data.Dataset):
|
||||
qvel = root['/observations/qvel'][start_ts]
|
||||
image_dict = dict()
|
||||
for cam_name in self.camera_names:
|
||||
if self.history >= 1:
|
||||
curr_frame = root[f'/observations/images/{cam_name}'][start_ts]
|
||||
history_frame = root[f'/observations/images/{cam_name}'][max(0, start_ts - self.history)]
|
||||
concat_image = np.concatenate([curr_frame, history_frame], axis=2)
|
||||
image_dict[cam_name] = concat_image
|
||||
else:
|
||||
image_dict[cam_name] = root[f'/observations/images/{cam_name}'][start_ts]
|
||||
# get all actions after and including start_ts
|
||||
if is_sim:
|
||||
@ -159,7 +166,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, history, skip_mirrored_data=False, load_pretrain=False):
|
||||
dataset_path_list = find_all_hdf5(dataset_dir, skip_mirrored_data)
|
||||
dataset_path_list = [n for n in dataset_path_list if name_filter(n)]
|
||||
num_episodes = len(dataset_path_list)
|
||||
@ -181,8 +188,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, history)
|
||||
val_dataset = EpisodicDataset(dataset_path_list, camera_names, norm_stats, val_episode_ids, val_episode_len, chunk_size, history)
|
||||
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)
|
||||
|
||||
|
||||
在新工单中引用
屏蔽一个用户