比较提交
15 次代码提交
| 作者 | SHA1 | 提交日期 | |
|---|---|---|---|
| ace063440d | |||
| d89430f987 | |||
| 4cec62314b | |||
| 5ab307d5c4 | |||
| b401b8f9d6 | |||
| 902dae2236 | |||
| 60d0da09f8 | |||
| 6afe28b795 | |||
| 8119013853 | |||
| d6bb889d7d | |||
| d0aa344ff0 | |||
| 8f22418a7b | |||
| 654248d96f | |||
| 3474cc38e7 | |||
| ca82042c1c |
31
align.py
普通文件
31
align.py
普通文件
@ -0,0 +1,31 @@
|
||||
from interbotix_xs_modules.arm import InterbotixManipulatorXS
|
||||
from aloha_scripts.robot_utils import move_arms, torque_on, move_grippers
|
||||
from constants import PUPPET_GRIPPER_JOINT_OPEN, PUPPET_GRIPPER_JOINT_CLOSE
|
||||
import argparse
|
||||
import numpy as np
|
||||
|
||||
# for calibrating head cam and arms being symmetrical
|
||||
|
||||
def main():
|
||||
argparser = argparse.ArgumentParser()
|
||||
argparser.add_argument('--all', action='store_true', default=False)
|
||||
args = argparser.parse_args()
|
||||
|
||||
puppet_bot_left = InterbotixManipulatorXS(robot_model="vx300s", group_name="arm", gripper_name="gripper", robot_name=f'puppet_left', init_node=True)
|
||||
puppet_bot_right = InterbotixManipulatorXS(robot_model="vx300s", group_name="arm", gripper_name="gripper", robot_name=f'puppet_right', init_node=False)
|
||||
|
||||
all_bots = [puppet_bot_left, puppet_bot_right]
|
||||
for bot in all_bots:
|
||||
torque_on(bot)
|
||||
|
||||
multiplier = np.array([-1, 1, 1, -1, 1, 1])
|
||||
puppet_sleep_position_left = np.array([-0.8, -0.5, 0.5, 0, 0.65, 0])
|
||||
puppet_sleep_position_right = puppet_sleep_position_left * multiplier
|
||||
all_positions = [puppet_sleep_position_left, puppet_sleep_position_right]
|
||||
move_arms(all_bots, all_positions, move_time=2)
|
||||
|
||||
# move_grippers(all_bots, [PUPPET_GRIPPER_JOINT_OPEN] * 2, move_time=1) # open
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
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
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
182
compress_data.py
普通文件
182
compress_data.py
普通文件
@ -0,0 +1,182 @@
|
||||
"""
|
||||
Example usage:
|
||||
$ python3 script/compress_data.py --dataset_dir /scr/lucyshi/dataset/aloha_test
|
||||
"""
|
||||
import os
|
||||
import h5py
|
||||
import cv2
|
||||
import numpy as np
|
||||
import argparse
|
||||
from tqdm import tqdm
|
||||
|
||||
# Constants
|
||||
DT = 0.02
|
||||
JOINT_NAMES = ["waist", "shoulder", "elbow", "forearm_roll", "wrist_angle", "wrist_rotate"]
|
||||
STATE_NAMES = JOINT_NAMES + ["gripper"]
|
||||
|
||||
|
||||
def compress_dataset(input_dataset_path, output_dataset_path):
|
||||
# Check if output path exists
|
||||
if os.path.exists(output_dataset_path):
|
||||
print(f"The file {output_dataset_path} already exists. Exiting...")
|
||||
return
|
||||
|
||||
# Load the uncompressed dataset
|
||||
with h5py.File(input_dataset_path, 'r') as infile:
|
||||
# Create the compressed dataset
|
||||
with h5py.File(output_dataset_path, 'w') as outfile:
|
||||
|
||||
outfile.attrs['sim'] = infile.attrs['sim']
|
||||
outfile.attrs['compress'] = True
|
||||
|
||||
# Copy non-image data directly
|
||||
for key in infile.keys():
|
||||
if key != 'observations':
|
||||
outfile.copy(infile[key], key)
|
||||
|
||||
obs_group = infile['observations']
|
||||
|
||||
# Create observation group in the output
|
||||
out_obs_group = outfile.create_group('observations')
|
||||
|
||||
# Copy non-image data in observations directly
|
||||
for key in obs_group.keys():
|
||||
if key != 'images':
|
||||
out_obs_group.copy(obs_group[key], key)
|
||||
|
||||
image_group = obs_group['images']
|
||||
out_image_group = out_obs_group.create_group('images')
|
||||
|
||||
# JPEG compression parameters
|
||||
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 50]
|
||||
|
||||
compressed_lens = [] # List to store compressed lengths for each camera
|
||||
|
||||
for cam_name in image_group.keys():
|
||||
if "_depth" in cam_name: # Depth images are not compressed
|
||||
out_image_group.copy(image_group[cam_name], cam_name)
|
||||
else:
|
||||
images = image_group[cam_name]
|
||||
compressed_images = []
|
||||
cam_compressed_lens = [] # List to store compressed lengths for this camera
|
||||
|
||||
# Compress each image
|
||||
for image in images:
|
||||
result, encoded_image = cv2.imencode('.jpg', image, encode_param)
|
||||
compressed_images.append(encoded_image)
|
||||
cam_compressed_lens.append(len(encoded_image)) # Store the length
|
||||
|
||||
compressed_lens.append(cam_compressed_lens)
|
||||
|
||||
# Find the maximum length of the compressed images
|
||||
max_len = max(len(img) for img in compressed_images)
|
||||
|
||||
# Create dataset to store compressed images
|
||||
compressed_dataset = out_image_group.create_dataset(cam_name, (len(compressed_images), max_len), dtype='uint8')
|
||||
|
||||
# Store compressed images
|
||||
for i, img in enumerate(compressed_images):
|
||||
compressed_dataset[i, :len(img)] = img
|
||||
|
||||
# Save the compressed lengths to the HDF5 file
|
||||
compressed_lens = np.array(compressed_lens)
|
||||
_ = outfile.create_dataset('compress_len', compressed_lens.shape)
|
||||
outfile['/compress_len'][...] = compressed_lens
|
||||
|
||||
print(f"Compressed dataset saved to {output_dataset_path}")
|
||||
|
||||
|
||||
def save_videos(video, dt, video_path=None):
|
||||
if isinstance(video, list):
|
||||
cam_names = list(video[0].keys())
|
||||
h, w, _ = video[0][cam_names[0]].shape
|
||||
w = w * len(cam_names)
|
||||
fps = int(1/dt)
|
||||
out = cv2.VideoWriter(video_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))
|
||||
# bitrate = 1000000
|
||||
# out.set(cv2.VIDEOWRITER_PROP_BITRATE, bitrate)
|
||||
for ts, image_dict in enumerate(video):
|
||||
images = []
|
||||
for cam_name in cam_names:
|
||||
image = image_dict[cam_name]
|
||||
# image = image[:, :, [2, 1, 0]] # swap B and R channel
|
||||
images.append(image)
|
||||
images = np.concatenate(images, axis=1)
|
||||
out.write(images)
|
||||
out.release()
|
||||
print(f'Saved video to: {video_path}')
|
||||
elif isinstance(video, dict):
|
||||
cam_names = list(video.keys())
|
||||
# Remove depth images
|
||||
cam_names = [cam_name for cam_name in cam_names if '_depth' not in cam_name]
|
||||
all_cam_videos = []
|
||||
for cam_name in cam_names:
|
||||
all_cam_videos.append(video[cam_name])
|
||||
all_cam_videos = np.concatenate(all_cam_videos, axis=2) # width dimension
|
||||
|
||||
n_frames, h, w, _ = all_cam_videos.shape
|
||||
fps = int(1 / dt)
|
||||
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
|
||||
out.write(image)
|
||||
out.release()
|
||||
print(f'Saved video to: {video_path}')
|
||||
|
||||
|
||||
def load_and_save_first_episode_video(dataset_dir, video_path):
|
||||
dataset_name = 'episode_0'
|
||||
_, _, _, _, image_dict = load_hdf5(dataset_dir, dataset_name)
|
||||
save_videos(image_dict, DT, video_path=video_path)
|
||||
|
||||
|
||||
def load_hdf5(dataset_dir, dataset_name):
|
||||
dataset_path = os.path.join(dataset_dir, dataset_name + '.hdf5')
|
||||
if not os.path.isfile(dataset_path):
|
||||
print(f'Dataset does not exist at \n{dataset_path}\n')
|
||||
exit()
|
||||
|
||||
with h5py.File(dataset_path, 'r') as root:
|
||||
compressed = root.attrs.get('compress', False)
|
||||
image_dict = dict()
|
||||
for cam_name in root[f'/observations/images/'].keys():
|
||||
image_dict[cam_name] = root[f'/observations/images/{cam_name}'][()]
|
||||
if compressed:
|
||||
compress_len = root['/compress_len'][()]
|
||||
|
||||
if compressed:
|
||||
for cam_id, cam_name in enumerate(image_dict.keys()):
|
||||
padded_compressed_image_list = image_dict[cam_name]
|
||||
image_list = []
|
||||
for frame_id, padded_compressed_image in enumerate(padded_compressed_image_list):
|
||||
image_len = int(compress_len[cam_id, frame_id])
|
||||
compressed_image = padded_compressed_image
|
||||
image = cv2.imdecode(compressed_image, 1)
|
||||
image_list.append(image)
|
||||
image_dict[cam_name] = image_list
|
||||
|
||||
return None, None, None, None, image_dict # Return only the image dict for this application
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser(description="Compress all HDF5 datasets in a directory.")
|
||||
parser.add_argument('--dataset_dir', action='store', type=str, required=True, help='Directory containing the uncompressed datasets.')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
output_dataset_dir = args.dataset_dir + '_compressed'
|
||||
os.makedirs(output_dataset_dir, exist_ok=True)
|
||||
|
||||
# Iterate over each file in the directory
|
||||
for filename in tqdm(os.listdir(args.dataset_dir), desc="Compressing data"):
|
||||
if filename.endswith('.hdf5'):
|
||||
input_path = os.path.join(args.dataset_dir, filename)
|
||||
output_path = os.path.join(output_dataset_dir, filename)
|
||||
compress_dataset(input_path, output_path)
|
||||
|
||||
# After processing all datasets, load and save the video for the first episode
|
||||
print(f'Saving video for episode 0 in {output_dataset_dir}')
|
||||
video_path = os.path.join(output_dataset_dir, 'episode_0_video.mp4')
|
||||
load_and_save_first_episode_video(args.dataset_dir, video_path)
|
||||
|
||||
@ -67,6 +67,7 @@ def get_args_parser():
|
||||
parser.add_argument('--use_vq', action='store_true')
|
||||
parser.add_argument('--vq_class', action='store', type=int, help='vq_class', required=False)
|
||||
parser.add_argument('--vq_dim', action='store', type=int, help='vq_dim', required=False)
|
||||
parser.add_argument('--load_pretrain', action='store_true', default=False)
|
||||
parser.add_argument('--action_dim', action='store', type=int, required=False)
|
||||
parser.add_argument('--eval_every', action='store', type=int, default=500, help='eval_every', required=False)
|
||||
parser.add_argument('--validate_every', action='store', type=int, default=500, help='validate_every', required=False)
|
||||
@ -74,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,8 +274,12 @@ def build(args):
|
||||
# From image
|
||||
backbones = []
|
||||
for _ in args.camera_names:
|
||||
backbone = build_backbone(args)
|
||||
backbones.append(backbone)
|
||||
if args.history >= 1:
|
||||
backbone = build_backbone_channel_concat(args)
|
||||
backbones.append(backbone)
|
||||
else:
|
||||
backbone = build_backbone(args)
|
||||
backbones.append(backbone)
|
||||
|
||||
transformer = build_transformer(args)
|
||||
|
||||
|
||||
@ -8,13 +8,14 @@ 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
|
||||
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 # helper 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 visualize_episodes import save_videos
|
||||
|
||||
@ -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,
|
||||
@ -103,23 +106,26 @@ def main(args):
|
||||
'seed': args['seed'],
|
||||
'temporal_agg': args['temporal_agg'],
|
||||
'camera_names': camera_names,
|
||||
'real_robot': not is_sim
|
||||
'real_robot': not is_sim,
|
||||
'load_pretrain': args['load_pretrain'],
|
||||
'history': history,
|
||||
}
|
||||
|
||||
if not os.path.isdir(ckpt_dir):
|
||||
os.makedirs(ckpt_dir)
|
||||
config_path = os.path.join(ckpt_dir, 'config.pkl')
|
||||
expr_name = ckpt_dir.split('/')[-1]
|
||||
wandb.init(project="mobile-aloha", reinit=True, entity="mobile-aloha", name=expr_name)
|
||||
wandb.config.update(config)
|
||||
if not is_eval:
|
||||
wandb.init(project="mobile-aloha", reinit=True, entity="mobile-aloha", name=expr_name)
|
||||
wandb.config.update(config)
|
||||
with open(config_path, 'wb') as f:
|
||||
pickle.dump(config, f)
|
||||
if is_eval:
|
||||
ckpt_names = [f'policy_last.ckpt']
|
||||
results = []
|
||||
for ckpt_name in ckpt_names:
|
||||
success_rate, avg_return = eval_bc(config, ckpt_name, save_episode=True, num_rollouts=50)
|
||||
wandb.log({'success_rate': success_rate, 'avg_return': avg_return})
|
||||
success_rate, avg_return = eval_bc(config, ckpt_name, save_episode=True, num_rollouts=1)
|
||||
# wandb.log({'success_rate': success_rate, 'avg_return': avg_return})
|
||||
results.append([ckpt_name, success_rate, avg_return])
|
||||
|
||||
for ckpt_name, success_rate, avg_return in results:
|
||||
@ -127,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'])
|
||||
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')
|
||||
@ -186,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)
|
||||
@ -218,7 +226,7 @@ def eval_bc(config, ckpt_name, save_episode=True, num_rollouts=50):
|
||||
if real_robot:
|
||||
from aloha_scripts.robot_utils import move_grippers # requires aloha
|
||||
from aloha_scripts.real_env import make_real_env # requires aloha
|
||||
env = make_real_env(init_node=True, setup_base=True)
|
||||
env = make_real_env(init_node=True, setup_robots=True, setup_base=True)
|
||||
env_max_reward = 0
|
||||
else:
|
||||
from sim_env import make_sim_env
|
||||
@ -252,13 +260,15 @@ def eval_bc(config, ckpt_name, save_episode=True, num_rollouts=50):
|
||||
|
||||
### evaluation loop
|
||||
if temporal_agg:
|
||||
all_time_actions = torch.zeros([max_timesteps, max_timesteps+num_queries, state_dim]).cuda()
|
||||
all_time_actions = torch.zeros([max_timesteps, max_timesteps+num_queries, 16]).cuda()
|
||||
|
||||
qpos_history = torch.zeros((1, max_timesteps, state_dim)).cuda()
|
||||
image_list = [] # for visualization
|
||||
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
|
||||
@ -279,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:
|
||||
@ -290,6 +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:
|
||||
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
|
||||
@ -313,6 +329,8 @@ def eval_bc(config, ckpt_name, save_episode=True, num_rollouts=50):
|
||||
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)
|
||||
|
||||
### step the environment
|
||||
if real_robot:
|
||||
@ -380,6 +398,9 @@ def train_bc(train_dataloader, val_dataloader, config):
|
||||
set_seed(seed)
|
||||
|
||||
policy = make_policy(policy_class, policy_config)
|
||||
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')))
|
||||
print(f'loaded! {loading_status}')
|
||||
if config['resume_ckpt_path'] is not None:
|
||||
loading_status = policy.load_state_dict(torch.load(config['resume_ckpt_path']))
|
||||
print(f'Resume policy from: {config["resume_ckpt_path"]}, Status: {loading_status}')
|
||||
@ -423,7 +444,7 @@ def train_bc(train_dataloader, val_dataloader, config):
|
||||
ckpt_name = f'policy_step_{step}_seed_{seed}.ckpt'
|
||||
ckpt_path = os.path.join(ckpt_dir, ckpt_name)
|
||||
torch.save(policy.state_dict(), ckpt_path)
|
||||
success, _ = eval_bc(config, ckpt_name, save_episode=True, num_rollouts=20)
|
||||
success, _ = eval_bc(config, ckpt_name, save_episode=True, num_rollouts=10)
|
||||
wandb.log({'success': success}, step=step)
|
||||
|
||||
# training
|
||||
@ -471,6 +492,7 @@ if __name__ == '__main__':
|
||||
parser.add_argument('--seed', action='store', type=int, help='seed', required=True)
|
||||
parser.add_argument('--num_steps', action='store', type=int, help='num_steps', required=True)
|
||||
parser.add_argument('--lr', action='store', type=float, help='lr', required=True)
|
||||
parser.add_argument('--load_pretrain', action='store_true', default=False)
|
||||
parser.add_argument('--eval_every', action='store', type=int, default=500, help='eval_every', required=False)
|
||||
parser.add_argument('--validate_every', action='store', type=int, default=500, help='validate_every', required=False)
|
||||
parser.add_argument('--save_every', action='store', type=int, default=500, help='save_every', required=False)
|
||||
@ -487,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()))
|
||||
|
||||
11
policy.py
11
policy.py
@ -19,8 +19,15 @@ class ACTPolicy(nn.Module):
|
||||
|
||||
def __call__(self, qpos, image, actions=None, is_pad=None, vq_sample=None):
|
||||
env_state = None
|
||||
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
||||
std=[0.229, 0.224, 0.225])
|
||||
|
||||
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]
|
||||
|
||||
44
utils.py
44
utils.py
@ -2,6 +2,7 @@ import numpy as np
|
||||
import torch
|
||||
import os
|
||||
import h5py
|
||||
import pickle
|
||||
import fnmatch
|
||||
from torch.utils.data import TensorDataset, DataLoader
|
||||
|
||||
@ -9,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
|
||||
@ -17,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
|
||||
@ -57,7 +59,13 @@ class EpisodicDataset(torch.utils.data.Dataset):
|
||||
qvel = root['/observations/qvel'][start_ts]
|
||||
image_dict = dict()
|
||||
for cam_name in self.camera_names:
|
||||
image_dict[cam_name] = root[f'/observations/images/{cam_name}'][start_ts]
|
||||
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:
|
||||
action = action[start_ts:]
|
||||
@ -158,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):
|
||||
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)
|
||||
@ -171,37 +179,47 @@ def load_data(dataset_dir, name_filter, camera_names, batch_size_train, batch_si
|
||||
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]
|
||||
|
||||
# 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)
|
||||
|
||||
return train_dataloader, val_dataloader, norm_stats, train_dataset.is_sim
|
||||
|
||||
|
||||
def calibrate_linear_vel(base_action):
|
||||
c = 0.19
|
||||
v = base_action[:, 0]
|
||||
w = base_action[:, 1]
|
||||
base_action[:, 0] = v - c * w
|
||||
def calibrate_linear_vel(base_action, c=None):
|
||||
if c is None:
|
||||
c = 0.0 # 0.19
|
||||
v = base_action[..., 0]
|
||||
w = base_action[..., 1]
|
||||
base_action = base_action.copy()
|
||||
base_action[..., 0] = v - c * w
|
||||
return base_action
|
||||
|
||||
def smooth_base_action(base_action):
|
||||
return np.stack([
|
||||
np.convolve(base_action[:, i], np.ones(20)/20, mode='same') for i in range(base_action.shape[1])
|
||||
np.convolve(base_action[:, i], np.ones(5)/5, mode='same') for i in range(base_action.shape[1])
|
||||
], axis=-1).astype(np.float32)
|
||||
|
||||
def preprocess_base_action(base_action):
|
||||
base_action = calibrate_linear_vel(base_action)
|
||||
# base_action = calibrate_linear_vel(base_action)
|
||||
base_action = smooth_base_action(base_action)
|
||||
|
||||
return base_action
|
||||
|
||||
def postprocess_base_action(self, base_action):
|
||||
linear_vel, angular_vel = base_action
|
||||
angular_vel *= 0.9
|
||||
return np.array([linear_vel, angular_vel])
|
||||
|
||||
### env utils
|
||||
|
||||
def sample_box_pose():
|
||||
|
||||
在新工单中引用
屏蔽一个用户