Skip to content

timjm25/LSTM_Robotics

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LSTM Robotics — Video-Conditioned Robot Action Policy

A general ConvLSTM model for training robots to perform manipulation tasks from video observations, inspired by Google Research's spatiotemporal learning architecture.


Business Problem

Modern robotics applications in manufacturing, logistics, and healthcare require robots that can generalise across variable environments from raw visual input. Collecting hand-coded policies for every task is impractical. Instead, robots should learn to act from demonstration videos — the same way humans learn from watching.

This project provides a complete training pipeline where:

  • A robot camera stream is the primary input
  • An LSTM with convolutional gates models how the scene evolves over time
  • The model predicts end-effector actions at each timestep
  • The trained policy can run on-robot for closed-loop control

Target domains:

  • Bin picking / pick-and-place on assembly lines
  • Surgical instrument handling (tele-op imitation learning)
  • Autonomous mobile robot navigation from ego-camera
  • Inspection and quality control using visual servoing

Model Architecture

The model is a VisuoMotor ConvLSTM — a video-to-action policy network inspired by Google Brain's work on spatiotemporal LSTM and MobileNet-based on-device robotics inference.

Video frames (B, T, 3, H, W)
        │
        ▼ (applied independently at each timestep)
┌───────────────────────────┐
│  CNN Backbone              │   Lightweight depthwise-separable CNN
│  (Lightweight / MobileNetV2│   or pretrained MobileNetV2 (Google, 2018)
│   stride-16 feature map)   │   Output: (B·T, C, H/16, W/16)
└───────────┬───────────────┘
            │ reshape → (B, T, C, H', W')
            ▼
┌───────────────────────────┐
│  ConvLSTM Stack            │   Shi et al. NeurIPS 2015 — gates replaced
│  (3 layers, configurable)  │   with 2D convolutions to preserve spatial
│                            │   structure across timesteps
└───────────┬───────────────┘
            │ (B, T, Ch, H', W')
            ▼
        Flatten spatial dims
            │
            ▼
┌───────────────────────────┐
│  Action MLP Head           │   Separate heads for motion + gripper
│  LayerNorm + GELU          │   Motion : tanh → [-1, 1]  (6 DOF delta)
│                            │   Gripper: sigmoid → [0, 1] (open/close)
└───────────────────────────┘
            │
            ▼
Robot actions (B, T, 7)
  [Δx, Δy, Δz, Δroll, Δpitch, Δyaw, gripper]

ConvLSTM Cell

The core recurrent unit replaces standard LSTM fully-connected gates with spatial convolutions:

i_t = σ(W_xi * X_t  +  W_hi * H_{t-1}  +  b_i)
f_t = σ(W_xf * X_t  +  W_hf * H_{t-1}  +  b_f)   ← bias init 1.0
g_t = tanh(W_xg * X_t  +  W_hg * H_{t-1}  +  b_g)
o_t = σ(W_xo * X_t  +  W_ho * H_{t-1}  +  b_o)
C_t = f_t ⊙ C_{t-1}  +  i_t ⊙ g_t
H_t = o_t ⊙ tanh(C_t)

where * denotes 2D convolution (not matrix multiply).

Action Space (7 DOF)

Index Dimension Range Meaning
0 Δx [-1, 1] End-effector x delta
1 Δy [-1, 1] End-effector y delta
2 Δz [-1, 1] End-effector z delta
3 Δroll [-1, 1] Wrist roll delta
4 Δpitch [-1, 1] Wrist pitch delta
5 Δyaw [-1, 1] Wrist yaw delta
6 gripper [0, 1] 0=open, 1=close

Loss Function

L = w_motion · MSE(pred[0:6], target[0:6])
  + w_gripper · BCE(pred[6],   target[6])

Default: w_motion=1.0, w_gripper=2.0 (gripper accuracy is critical for task completion).

Default Hyperparameters

Parameter Default Notes
Image size 224 × 224 Matches ImageNet pre-training norm
Sequence length 8 frames ~0.3s at 25 fps
Backbone channels 256 Feature map at H/16, W/16
ConvLSTM layers 3 Channels: [256, 128, 64]
Action hidden dim 256 MLP intermediate width
Learning rate 1e-4 AdamW + cosine decay
Batch size 8 Sequences (not frames)
Trainable params ~10.2M Lightweight backbone

Google Model Reference

This architecture directly references Google Research publications:

Component Google Reference
ConvLSTM cell Shi et al., NeurIPS 2015 (co-authored with Google Brain)
MobileNetV2 opt. Sandler et al., CVPR 2018 (Google)
Backbone design MobileNet depthwise-separable blocks (Google, 2017)
Video prediction "Video Prediction for Model-Based Deep RL" (Google, 2018)
Action space RT-1 / RT-2 action tokenisation (Google Robotics, 2022/2023)
Training scheme SayCan visuomotor policy structure (Google, 2022)

The lightweight backbone mirrors Google's on-device MobileNet family, enabling deployment on robot compute (Jetson, Coral, Raspberry Pi 5) without a GPU.


Data Formats

1. Episode Directory (default)

data/
  episode_0000/
    frames/
      0000.png   0001.png   ...   (RGB images, any resolution)
    actions.json               # [[x,y,z,roll,pitch,yaw,gripper], ...]
  episode_0001/
    ...

Generate synthetic demo data:

python scripts/generate_demo_data.py --n_episodes 100 --episode_len 30

2. HDF5 (RoboNet / Open X-Embodiment)

episode.h5
  /episode_NNN/
    observations/images   (T, H, W, 3)  uint8
    actions               (T, 7)        float32

3. Synthetic (smoke-test, no data needed)

If data_dir is empty or missing the loader automatically generates a SyntheticRobotDataset with 200 episodes — useful for validating the architecture before you have real robot data.


Quick Start

Install

# Python 3.9+ required
pip install torch torchvision numpy Pillow matplotlib

Train on synthetic data (no robot required)

python run_train.py
# → trains for 50 epochs, checkpoints saved to checkpoints/

Train on real robot data

# 1. Organise episodes (see Data Formats above)
# 2. Run training
python run_train.py \
  --data_dir /path/to/robot/episodes \
  --epochs 100 \
  --batch_size 16 \
  --backbone mobilenetv2

Evaluate a checkpoint

python run_train.py --eval --checkpoint checkpoints/best.pt
python scripts/visualise_predictions.py --checkpoint checkpoints/best.pt

Online inference (one frame at a time)

# Single image
python -m lstm_robotics.predict \
  --checkpoint checkpoints/best.pt \
  --image frame.png

# Video file
python -m lstm_robotics.predict \
  --checkpoint checkpoints/best.pt \
  --video episode.mp4 \
  --output predicted_actions.npy

Streaming inference (deploy on robot)

from lstm_robotics.predict import Predictor
import numpy as np

predictor = Predictor.from_checkpoint("checkpoints/best.pt")

# Start of episode
predictor.reset()

# At each control tick (~25 Hz)
for frame_rgb in camera_stream:          # (H, W, 3) uint8
    action = predictor.step(frame_rgb)   # (7,) float32
    robot.send(action)

Project Structure

LSTM_Robotics/
├── lstm_robotics/
│   ├── __init__.py
│   ├── config.py          # ModelConfig + TrainConfig dataclasses
│   ├── model.py           # ConvLSTMCell, ConvLSTM, VisuoMotorLSTM
│   ├── dataset.py         # EpisodeDirectory, HDF5, Synthetic datasets
│   ├── train.py           # Trainer, RobotActionLoss, gripper_accuracy
│   ├── evaluate.py        # Per-axis metrics, prediction plots
│   └── predict.py         # Predictor (stateful online inference)
├── scripts/
│   ├── generate_demo_data.py
│   └── visualise_predictions.py
├── tests/
│   └── test_model.py      # 17 unit tests
├── run_train.py           # Training entry point
└── requirements.txt

Tests

python tests/test_model.py
# → 17 tests covering: ConvLSTMCell, ConvLSTM, backbone,
#   full model forward/backward, synthetic dataset, loss, checkpointing

Hardware Requirements

Mode Minimum Recommended
Training 8 GB RAM, CPU NVIDIA A100 / H100
Training fast Apple M2 (MPS) 4× A100 (DDP)
Inference Jetson Nano (4 GB) Jetson Orin / Coral

The lightweight backbone runs at ~30 fps on an Apple M2 CPU (224×224, seq=8).


Extending the Model

Larger backbone: Pass --backbone mobilenetv2 for pretrained ImageNet features — typically +5% success rate on real robot benchmarks at the cost of larger model size.

Longer history: Increase --seq_len to 16–32 frames for tasks requiring longer temporal context (e.g., deformable object manipulation).

Multi-camera: Stack frames from multiple cameras along the channel dim before passing to the backbone.

Language conditioning: Concatenate a sentence embedding (CLIP / T5) to the flattened LSTM output before the action head — matches the RT-2 style language-conditioned policy.

Distributional output: Replace the MSE motion head with a mixture of Gaussians (as in Diffusion Policy) for bimodal action distributions.


Regulatory / Safety Notes

For deployment on physical robots:

  • Add velocity and workspace limit clipping in the action executor
  • Log all predicted actions to a ring buffer for post-incident analysis
  • Implement a watchdog that stops the robot if prediction confidence drops (e.g., detect OOD frames via reconstruction error on a VAE side-branch)
  • Follow ISO 10218 / TS 15066 for collaborative robot safety

License

MIT License — see LICENSE for details.


Architecture inspired by Google Research spatiotemporal LSTM, MobileNetV2, and the RT-1/RT-2 robotics policy family.

About

our general LSTM model for training

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages