125 lines
5.9 KiB
Python
125 lines
5.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Offline import using mjlab's Python environment.
|
|
Usage: /path/to/mjlab/.venv/bin/python app/scripts/import-t1.py /path/to/mjlab
|
|
Requires BOOSTER_ASSETS_ROOT if booster_assets is not at mjlab's default path.
|
|
"""
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
import re
|
|
import sys
|
|
import xml.etree.ElementTree as ET
|
|
|
|
import onnx
|
|
|
|
source = Path(sys.argv[1]).resolve()
|
|
sys.path.insert(0, str(source / 'src'))
|
|
from mjlab.entity import Entity
|
|
from mjlab.asset_zoo.robots.booster_t1.t1_constants import (
|
|
get_t1_robot_cfg, get_t1_xml, T1_ACTION_SCALE,
|
|
)
|
|
|
|
# Select the newest training run, then its highest numbered checkpoint.
|
|
runs = sorted(p for p in (source / 'logs/rsl_rl/t1_flat_velocity').iterdir()
|
|
if p.is_dir() and list(p.glob('model_*.pt')))
|
|
run = runs[-1]
|
|
checkpoint = max(run.glob('model_*.pt'), key=lambda p: int(p.stem.split('_')[-1]))
|
|
policy = run / f'{run.name}.onnx'
|
|
if not policy.exists() or policy.stat().st_mtime < checkpoint.stat().st_mtime:
|
|
raise RuntimeError(f'Export the latest checkpoint first: {checkpoint}')
|
|
# Snapshot the export before model compilation: training may still be running.
|
|
policy_bytes = policy.read_bytes()
|
|
metadata = {p.key: p.value for p in onnx.load_model_from_string(policy_bytes).metadata_props}
|
|
names = metadata['joint_names'].split(',')
|
|
if len(names) != 23 or metadata['observation_names'] != 'base_lin_vel,base_ang_vel,projected_gravity,joint_pos,joint_vel,actions,command':
|
|
raise RuntimeError('Unexpected T1 policy contract')
|
|
defaults = dict(zip(names, map(float, metadata['default_joint_pos'].split(',')), strict=True))
|
|
target = Path(__file__).resolve().parents[1] / 'public/robots/t1'
|
|
target.mkdir(parents=True, exist_ok=True)
|
|
records = []
|
|
|
|
def copy(src, dst, data=None):
|
|
data = src.read_bytes() if data is None else data
|
|
dest = target / dst
|
|
dest.parent.mkdir(parents=True, exist_ok=True)
|
|
dest.write_bytes(data)
|
|
records.append({'source': str(src), 'file': dst, 'bytes': len(data),
|
|
'sha256': hashlib.sha256(data).hexdigest()})
|
|
|
|
# Entity applies the same actuator and collision editors used during training.
|
|
entity = Entity(get_t1_robot_cfg())
|
|
entity.spec.compile()
|
|
root = ET.fromstring(entity.spec.to_xml())
|
|
for tag in ('keyframe', 'size'):
|
|
for element in root.findall(tag):
|
|
root.remove(element)
|
|
ET.SubElement(root, 'option', timestep='0.005', integrator='implicitfast',
|
|
solver='Newton', cone='pyramidal', iterations='10', ls_iterations='20',
|
|
ccd_iterations='50')
|
|
assets = []
|
|
xml = get_t1_xml()
|
|
for mesh in root.findall('./asset/mesh'):
|
|
filename = mesh.attrib['file']
|
|
dst = f'meshes/{filename}'
|
|
copy(xml.parent / 'meshes' / filename, dst)
|
|
assets.append({'path': dst, 'vfs': dst})
|
|
ET.indent(root)
|
|
ET.ElementTree(root).write(target / 'model.xml', encoding='unicode')
|
|
copy(policy, 'policies/walk.onnx', policy_bytes)
|
|
copy(xml.parents[2] / 'LICENSE', 'LICENSES/booster-assets.txt')
|
|
copy(source / 'LICENSE', 'LICENSES/mjlab.txt')
|
|
groups = {'policy': names}
|
|
actions = []
|
|
for name in names:
|
|
groups[name] = [name]
|
|
scale, = [value for pattern, value in T1_ACTION_SCALE.items() if re.fullmatch(pattern, name)]
|
|
actions.append({'type': 'joint_position', 'group': name, 'scale': scale, 'offset': 'default'})
|
|
config = {
|
|
'schema': 1, 'id': 't1', 'name': 'Booster T1', 'family': 't1', 'adapter': 'generic',
|
|
'dimensions': {'height': 1.18}, 'model': 'model.xml', 'assets': assets,
|
|
'base': {'type': 'floating', 'root_body': 'trunk', 'freejoint': 'world_joint',
|
|
'spawn': {'pos': [0, 0, 0.6811], 'yaw': 0}},
|
|
'sim': {'timestep': 0.005, 'decimation': 4},
|
|
'joints': {'groups': groups, 'defaults': defaults},
|
|
'observations': {'obs': {'terms': [
|
|
{'type': 'linear_velocity', 'sensor': 'imu_lin_vel'},
|
|
{'type': 'gyro', 'sensor': 'imu_ang_vel'},
|
|
{'type': 'projected_gravity', 'body': 'trunk'},
|
|
{'type': 'joint_pos_rel', 'group': 'policy'},
|
|
{'type': 'joint_vel', 'group': 'policy'},
|
|
{'type': 'last_action', 'group': 'policy'},
|
|
{'type': 'command', 'size': 3},
|
|
]}},
|
|
'actions': actions, 'actuation': {'type': 'native_position'},
|
|
'commands': {'size': 3, 'limits': [0.6, -0.4, 0.6]},
|
|
'skills': [{'id': 'walk', 'kind': 'primary', 'owns': 'policy',
|
|
'controller': {'type': 'onnx', 'policy': 'policies/walk.onnx'}}],
|
|
'input': {'bindings': [
|
|
{'action': action, 'codes': codes, 'label': label}
|
|
for action, codes, label in [
|
|
('move_forward', ['KeyW', 'ArrowUp'], 'Forward'),
|
|
('move_backward', ['KeyS', 'ArrowDown'], 'Backward'),
|
|
('turn_left', ['KeyA', 'ArrowLeft'], 'Turn left'),
|
|
('turn_right', ['KeyD', 'ArrowRight'], 'Turn right'),
|
|
('reset', ['Space'], 'Reset'),
|
|
('pause', ['Escape'], 'Pause / continue'),
|
|
('chaseToggle', ['KeyC'], 'Restore chase camera'),
|
|
]]},
|
|
'render': {'source': 'mjcf_visual', 'groups': [1], 'camera': {'distance': 2.5}},
|
|
'presentation': {'theme': 'comic'},
|
|
'termination': {'tilt_z': -0.5, 'min_height': 0.42, 'on_fall': 'hold'},
|
|
'license': 'See LICENSES/booster-assets.txt and LICENSES/mjlab.txt; policy is locally trained.',
|
|
}
|
|
(target / 'robot.json').write_text(json.dumps(config, indent=2) + '\n')
|
|
(target / 'source-manifest.json').write_text(json.dumps({
|
|
'checkpoint': str(checkpoint),
|
|
'checkpoint_sha256': hashlib.sha256(checkpoint.read_bytes()).hexdigest(),
|
|
'model_source': str(xml), 'files': records,
|
|
'behaviorReferences': [
|
|
{'file': str(p.relative_to(source)), 'sha256': hashlib.sha256(p.read_bytes()).hexdigest()}
|
|
for p in [source / 'src/mjlab/asset_zoo/robots/booster_t1/t1_constants.py',
|
|
source / 'src/mjlab/tasks/velocity/config/booster_t1/env_cfgs.py',
|
|
source / 'src/mjlab/tasks/velocity/velocity_env_cfg.py']],
|
|
}, indent=2) + '\n')
|
|
print(f'Imported {checkpoint.name} / {policy.name} into {target}')
|