Upload folder using huggingface_hub
Browse files- .gitattributes +1 -0
- README.md +254 -0
- demo_data.pkl +3 -0
- replay_results.png +3 -0
- vla_checkpoint.pt +3 -0
- vla_checkpoint_best.pt +3 -0
- vla_flow_matching.py +965 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
replay_results.png filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,254 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
| 5 |
+
tags:
|
| 6 |
+
- robotics
|
| 7 |
+
- vision-language-action
|
| 8 |
+
- vla
|
| 9 |
+
- flow-matching
|
| 10 |
+
- clip
|
| 11 |
+
- manipulation
|
| 12 |
+
- pick-and-place
|
| 13 |
+
- educational
|
| 14 |
+
- lightweight
|
| 15 |
+
- beginner-friendly
|
| 16 |
+
library_name: transformers
|
| 17 |
+
pipeline_tag: robotics
|
| 18 |
+
datasets:
|
| 19 |
+
- synthetic-pick-place-1k
|
| 20 |
+
metrics:
|
| 21 |
+
- mae
|
| 22 |
+
- position_error
|
| 23 |
+
- quaternion_error
|
| 24 |
+
- accuracy
|
| 25 |
+
---
|
| 26 |
+
|
| 27 |
+
# 🎓 Minimal VLA: The Simplest Vision-Language-Action Model
|
| 28 |
+
|
| 29 |
+
> **The lightest VLA implementation for learning and experimentation — only ~20MB!**
|
| 30 |
+
|
| 31 |
+
A beginner-friendly, minimal Vision-Language-Action (VLA) model designed for **educational purposes** and **rapid prototyping**. This project demonstrates the core concepts of VLA systems using CLIP + Flow Matching in the simplest possible setup.
|
| 32 |
+
|
| 33 |
+
## ✨ Why This Project?
|
| 34 |
+
|
| 35 |
+
| Feature | This Model | Typical VLAs |
|
| 36 |
+
|---------|-----------|--------------|
|
| 37 |
+
| **Model Size** | **~20MB** | 1-7GB+ |
|
| 38 |
+
| **Training Time** | **~20 min** | Hours to days |
|
| 39 |
+
| **Hardware** | Any GPU / CPU | High-end GPUs |
|
| 40 |
+
| **Simulation** | 2D rendering | Physics engines |
|
| 41 |
+
| **Complexity** | ~1000 lines | 10,000+ lines |
|
| 42 |
+
| **Dependencies** | PyTorch + CLIP | Complex stacks |
|
| 43 |
+
|
| 44 |
+
**Perfect for:**
|
| 45 |
+
- 🎓 **Students** learning VLA fundamentals
|
| 46 |
+
- 🔬 **Researchers** prototyping new ideas quickly
|
| 47 |
+
- 👨🏫 **Educators** teaching robot learning concepts
|
| 48 |
+
- 🚀 **Developers** building their first VLA system
|
| 49 |
+
|
| 50 |
+
## 🏗️ Model Overview
|
| 51 |
+
|
| 52 |
+
This minimal VLA predicts 8-DOF robotic actions from RGB images and natural language:
|
| 53 |
+
|
| 54 |
+
```
|
| 55 |
+
Input: Image (224×224) + Text ("pick up the red cube")
|
| 56 |
+
↓
|
| 57 |
+
CLIP ViT-B/32 (frozen, vision + language encoding)
|
| 58 |
+
↓
|
| 59 |
+
Flow Matching Policy (~2MB trainable parameters)
|
| 60 |
+
↓
|
| 61 |
+
Output: [x, y, z, qx, qy, qz, qw, gripper]
|
| 62 |
+
```
|
| 63 |
+
|
| 64 |
+
### Key Design Choices for Simplicity
|
| 65 |
+
|
| 66 |
+
1. **Frozen CLIP Backbone** — No need to train vision-language understanding
|
| 67 |
+
2. **2D Synthetic Environment** — No physics engine required
|
| 68 |
+
3. **Flow Matching** — Elegant generative approach for continuous actions
|
| 69 |
+
4. **Separate Gripper Classifier** — Binary decision for open/close
|
| 70 |
+
|
| 71 |
+
## 📊 Performance
|
| 72 |
+
|
| 73 |
+
Evaluated on 10 test samples from 1000 synthetic demonstrations:
|
| 74 |
+
|
| 75 |
+
| Metric | Value | Notes |
|
| 76 |
+
|--------|-------|-------|
|
| 77 |
+
| Position Error | **8.60cm** | Suitable for ~5cm cube picking |
|
| 78 |
+
| Gripper Accuracy | **75%** | Reliable grasp planning |
|
| 79 |
+
| Overall MAE | **0.1217** | Across all 8 action dimensions |
|
| 80 |
+
| Quaternion Error | 19.36° | Best for top-down grasps |
|
| 81 |
+
|
| 82 |
+
> ⚠️ **Note**: This is an educational model trained on simplified 2D projections. Real-world deployment requires fine-tuning on actual robot data.
|
| 83 |
+
|
| 84 |
+
## 🚀 Quick Start
|
| 85 |
+
|
| 86 |
+
### Installation
|
| 87 |
+
|
| 88 |
+
```bash
|
| 89 |
+
pip install torch transformers pillow numpy matplotlib
|
| 90 |
+
```
|
| 91 |
+
|
| 92 |
+
### Inference (3 lines!)
|
| 93 |
+
|
| 94 |
+
```python
|
| 95 |
+
from vla_flow_matching import VLM_Encoder, ImprovedFlowMatchingPolicy
|
| 96 |
+
import torch
|
| 97 |
+
|
| 98 |
+
# Load model
|
| 99 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 100 |
+
checkpoint = torch.load('pytorch_model.bin', map_location=device)
|
| 101 |
+
|
| 102 |
+
vlm_encoder = VLM_Encoder().to(device)
|
| 103 |
+
policy = ImprovedFlowMatchingPolicy(action_dim=8, context_dim=1024, hidden_dim=512).to(device)
|
| 104 |
+
policy.load_state_dict(checkpoint['policy'])
|
| 105 |
+
policy.eval()
|
| 106 |
+
|
| 107 |
+
# Predict!
|
| 108 |
+
from PIL import Image
|
| 109 |
+
image = Image.open('workspace.jpg').resize((224, 224))
|
| 110 |
+
context = vlm_encoder.encode([image], ["pick up the red cube"])
|
| 111 |
+
action = policy.sample(context, num_samples=1, device=device)
|
| 112 |
+
|
| 113 |
+
print(f"Position: {action[0, :3].cpu().numpy()}")
|
| 114 |
+
print(f"Gripper: {'CLOSE' if action[0, 7] > 0 else 'OPEN'}")
|
| 115 |
+
```
|
| 116 |
+
|
| 117 |
+
### Train from Scratch (~20 minutes)
|
| 118 |
+
|
| 119 |
+
```bash
|
| 120 |
+
# Step 1: Generate synthetic data
|
| 121 |
+
python vla_flow_matching.py --mode generate_data --num_demos 1000
|
| 122 |
+
|
| 123 |
+
# Step 2: Train (takes ~20 min on consumer GPU)
|
| 124 |
+
python vla_flow_matching.py --mode train --epochs 200 --batch_size 32
|
| 125 |
+
|
| 126 |
+
# Step 3: Evaluate
|
| 127 |
+
python vla_flow_matching.py --mode replay --checkpoint vla_checkpoint_best.pt
|
| 128 |
+
```
|
| 129 |
+
|
| 130 |
+
## 📁 Repository Structure
|
| 131 |
+
|
| 132 |
+
```
|
| 133 |
+
├── vla_flow_matching.py # Complete implementation (~1000 lines)
|
| 134 |
+
├── pytorch_model.bin # Trained weights (~20MB)
|
| 135 |
+
├── demo_data.pkl # Training data (1000 demos)
|
| 136 |
+
├── replay_results.png # Evaluation visualization
|
| 137 |
+
└── README.md # This file
|
| 138 |
+
```
|
| 139 |
+
|
| 140 |
+
## 🎯 What You'll Learn
|
| 141 |
+
|
| 142 |
+
This codebase teaches core VLA concepts:
|
| 143 |
+
|
| 144 |
+
1. **Vision-Language Encoding**: Using CLIP for joint image-text understanding
|
| 145 |
+
2. **Flow Matching**: A modern generative approach for action prediction
|
| 146 |
+
3. **Action Representation**: 8-DOF with quaternion rotations
|
| 147 |
+
4. **Synthetic Data Generation**: Creating training environments without physics
|
| 148 |
+
5. **Model Architecture**: Combining frozen backbones with trainable policies
|
| 149 |
+
|
| 150 |
+
## 🔧 Architecture Details
|
| 151 |
+
|
| 152 |
+
### VLM Encoder (Frozen CLIP)
|
| 153 |
+
- Vision: ViT-B/32 → 512-dim features
|
| 154 |
+
- Text: Transformer → 512-dim features
|
| 155 |
+
- Combined: 1024-dim context vector
|
| 156 |
+
|
| 157 |
+
### Flow Matching Policy (~2MB)
|
| 158 |
+
```
|
| 159 |
+
Context Encoder: 1024 → 512 → 128 (with LayerNorm, GELU, Dropout)
|
| 160 |
+
Time Embedding: Sinusoidal 128-dim
|
| 161 |
+
Action Encoder: 7D → 128
|
| 162 |
+
Velocity Network: 384 → 512 → 256 → 7
|
| 163 |
+
```
|
| 164 |
+
|
| 165 |
+
### Gripper Classifier
|
| 166 |
+
```
|
| 167 |
+
Context → 512 → 256 → 2 (softmax)
|
| 168 |
+
```
|
| 169 |
+
|
| 170 |
+
### Training Configuration
|
| 171 |
+
```yaml
|
| 172 |
+
Epochs: 200
|
| 173 |
+
Batch Size: 32
|
| 174 |
+
Learning Rate: 1e-4 (cosine decay to 1e-5)
|
| 175 |
+
Optimizer: AdamW (weight_decay=1e-4)
|
| 176 |
+
Flow Steps: 200 (Euler integration)
|
| 177 |
+
```
|
| 178 |
+
|
| 179 |
+
## 🌈 Training Data
|
| 180 |
+
|
| 181 |
+
The synthetic environment generates pick-and-place demonstrations:
|
| 182 |
+
|
| 183 |
+
- **1000 demonstrations** with diverse object positions
|
| 184 |
+
- **6 cube colors**: red, blue, green, yellow, purple, orange
|
| 185 |
+
- **24 instruction templates**: "pick up the [color] cube", "grasp the [color] block", etc.
|
| 186 |
+
- **40cm × 40cm workspace** with position and orientation variations
|
| 187 |
+
- **2D projection with 3D visual effects** (shadows, shading)
|
| 188 |
+
|
| 189 |
+
## ⚡ Extending This Work
|
| 190 |
+
|
| 191 |
+
### Ideas for Students/Researchers
|
| 192 |
+
|
| 193 |
+
1. **Add more objects**: Extend beyond cubes to spheres, cylinders
|
| 194 |
+
2. **Multi-step tasks**: Chain pick → place actions
|
| 195 |
+
3. **Real images**: Fine-tune on real robot data
|
| 196 |
+
4. **Better orientation**: Improve quaternion prediction accuracy
|
| 197 |
+
5. **Action chunking**: Predict action sequences instead of single steps
|
| 198 |
+
6. **Physics simulation**: Replace 2D rendering with PyBullet/MuJoCo
|
| 199 |
+
|
| 200 |
+
### Fine-tuning for Real Robots
|
| 201 |
+
|
| 202 |
+
```bash
|
| 203 |
+
# Collect 10-50 real demonstrations, then:
|
| 204 |
+
python vla_flow_matching.py --mode finetune \
|
| 205 |
+
--checkpoint pytorch_model.bin \
|
| 206 |
+
--data_path real_robot_demos.pkl \
|
| 207 |
+
--epochs 30 --lr 1e-5
|
| 208 |
+
```
|
| 209 |
+
|
| 210 |
+
## ⚠️ Limitations
|
| 211 |
+
|
| 212 |
+
This is an **educational model** with intentional simplifications:
|
| 213 |
+
|
| 214 |
+
- ❌ 2D synthetic environment (no physics)
|
| 215 |
+
- ❌ Single-object scenes only
|
| 216 |
+
- ❌ Limited orientation precision
|
| 217 |
+
- ❌ Not suitable for direct real-world deployment
|
| 218 |
+
- ❌ No temporal/sequential reasoning
|
| 219 |
+
|
| 220 |
+
**Do NOT use for**: Safety-critical applications, precision assembly, or autonomous operation without extensive testing.
|
| 221 |
+
|
| 222 |
+
## 🙏 Acknowledgments
|
| 223 |
+
|
| 224 |
+
Built with:
|
| 225 |
+
- 🤗 Transformers (CLIP)
|
| 226 |
+
- 🔥 PyTorch
|
| 227 |
+
- 📊 NumPy & Matplotlib
|
| 228 |
+
|
| 229 |
+
Inspired by:
|
| 230 |
+
- [Flow Matching](https://arxiv.org/abs/2210.02747) (Lipman et al., 2023)
|
| 231 |
+
- [CLIP](https://arxiv.org/abs/2103.00020) (Radford et al., 2021)
|
| 232 |
+
- [RT-1](https://arxiv.org/abs/2212.06817) (Brohan et al., 2022)
|
| 233 |
+
- [OpenVLA](https://openvla.github.io/) (Kim et al., 2024)
|
| 234 |
+
|
| 235 |
+
## 📚 Citation
|
| 236 |
+
|
| 237 |
+
```bibtex
|
| 238 |
+
@misc{minimal-vla-2025,
|
| 239 |
+
title={Minimal VLA: A Lightweight Vision-Language-Action Model for Education},
|
| 240 |
+
author={LeTau},
|
| 241 |
+
year={2025},
|
| 242 |
+
publisher={Hugging Face},
|
| 243 |
+
url={https://huggingface.co/your-username/minimal-vla}
|
| 244 |
+
}
|
| 245 |
+
```
|
| 246 |
+
|
| 247 |
+
## 📄 License
|
| 248 |
+
|
| 249 |
+
MIT License — Feel free to use, modify, and share!
|
| 250 |
+
|
| 251 |
+
---
|
| 252 |
+
|
| 253 |
+
**Questions?** Open an issue or reach out. Happy learning! 🤖
|
| 254 |
+
|
demo_data.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2e758339350195bd3b127b11a81b652e437708a7b990ccb90441a2e0c928a094
|
| 3 |
+
size 150768410
|
replay_results.png
ADDED
|
Git LFS Details
|
vla_checkpoint.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8ad14f8bf0908267f2a9b27ec79769653869d68c5d832315833f6b866548a21a
|
| 3 |
+
size 22344945
|
vla_checkpoint_best.pt
ADDED
|
@@ -0,0 +1,3 @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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Improved VLM + Flow Matching VLA with Simplified Simulator
|
| 3 |
+
Optimizations:
|
| 4 |
+
- Quaternion normalization
|
| 5 |
+
- Separate gripper classification
|
| 6 |
+
- Better data generation with diverse scenarios
|
| 7 |
+
- Enhanced training stability
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import torch
|
| 11 |
+
import torch.nn as nn
|
| 12 |
+
import torch.nn.functional as F
|
| 13 |
+
from torch.utils.data import Dataset, DataLoader
|
| 14 |
+
import numpy as np
|
| 15 |
+
from PIL import Image, ImageDraw
|
| 16 |
+
import pickle
|
| 17 |
+
from pathlib import Path
|
| 18 |
+
import argparse
|
| 19 |
+
from tqdm import tqdm
|
| 20 |
+
import matplotlib.pyplot as plt
|
| 21 |
+
from transformers import CLIPProcessor, CLIPModel
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# ============================================================================
|
| 25 |
+
# Simplified Simulator
|
| 26 |
+
# ============================================================================
|
| 27 |
+
|
| 28 |
+
class ImprovedSimulator:
|
| 29 |
+
"""Enhanced simulator with more realistic rendering and diverse scenarios"""
|
| 30 |
+
|
| 31 |
+
def __init__(self, gui=False):
|
| 32 |
+
self.gui = gui
|
| 33 |
+
self.colors = {
|
| 34 |
+
'red': [255, 0, 0],
|
| 35 |
+
'blue': [0, 0, 255],
|
| 36 |
+
'green': [0, 255, 0],
|
| 37 |
+
'yellow': [255, 255, 0],
|
| 38 |
+
'purple': [128, 0, 128],
|
| 39 |
+
'orange': [255, 165, 0]
|
| 40 |
+
}
|
| 41 |
+
self.obj_pos = None
|
| 42 |
+
self.obj_color_name = None
|
| 43 |
+
|
| 44 |
+
def reset(self, object_color='red'):
|
| 45 |
+
"""Reset environment with a new object"""
|
| 46 |
+
# Random object position on table
|
| 47 |
+
pos_x = np.random.uniform(0.3, 0.7)
|
| 48 |
+
pos_y = np.random.uniform(-0.2, 0.2)
|
| 49 |
+
pos_z = 0.65 # Table height + object height
|
| 50 |
+
|
| 51 |
+
self.obj_pos = [pos_x, pos_y, pos_z]
|
| 52 |
+
self.obj_color_name = object_color
|
| 53 |
+
|
| 54 |
+
# Random orientation (top-down grasp with small variations)
|
| 55 |
+
# Quaternion: mostly upright with small perturbations
|
| 56 |
+
angle_z = np.random.uniform(-np.pi/6, np.pi/6) # ±30 degrees rotation
|
| 57 |
+
qw = np.cos(angle_z / 2)
|
| 58 |
+
qx = 0.0
|
| 59 |
+
qy = 0.0
|
| 60 |
+
qz = np.sin(angle_z / 2)
|
| 61 |
+
|
| 62 |
+
obj_orn = [qx, qy, qz, qw]
|
| 63 |
+
|
| 64 |
+
return self.obj_pos, obj_orn, object_color
|
| 65 |
+
|
| 66 |
+
def get_camera_image(self, width=224, height=224):
|
| 67 |
+
"""Render enhanced RGB image with better visual quality"""
|
| 68 |
+
# Create base image
|
| 69 |
+
img = Image.new('RGB', (width, height), color=(200, 200, 200))
|
| 70 |
+
draw = ImageDraw.Draw(img)
|
| 71 |
+
|
| 72 |
+
# Draw table (brown gradient for depth)
|
| 73 |
+
table_start = int(height * 0.6)
|
| 74 |
+
for y in range(table_start, height):
|
| 75 |
+
darkness = (y - table_start) / (height - table_start)
|
| 76 |
+
color = int(139 * (1 - darkness * 0.3))
|
| 77 |
+
draw.rectangle([(0, y), (width, y+1)], fill=(color, int(90*(1-darkness*0.3)), int(43*(1-darkness*0.3))))
|
| 78 |
+
|
| 79 |
+
# Project 3D position to 2D image (orthographic projection)
|
| 80 |
+
obj_x_img = int((self.obj_pos[0] - 0.3) / 0.4 * width * 0.6 + width * 0.2)
|
| 81 |
+
obj_y_img = int((self.obj_pos[1] + 0.2) / 0.4 * height * 0.4 + height * 0.3)
|
| 82 |
+
|
| 83 |
+
# Clip to valid range
|
| 84 |
+
obj_x_img = np.clip(obj_x_img, 20, width - 50)
|
| 85 |
+
obj_y_img = np.clip(obj_y_img, 20, table_start - 20)
|
| 86 |
+
|
| 87 |
+
# Draw object with 3D effect
|
| 88 |
+
cube_size = 35
|
| 89 |
+
|
| 90 |
+
# Shadow
|
| 91 |
+
shadow_offset = 5
|
| 92 |
+
draw.ellipse(
|
| 93 |
+
[(obj_x_img - cube_size//2, table_start - shadow_offset),
|
| 94 |
+
(obj_x_img + cube_size//2, table_start + shadow_offset)],
|
| 95 |
+
fill=(100, 100, 100, 128)
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
# Main cube face
|
| 99 |
+
obj_color = tuple(self.colors[self.obj_color_name])
|
| 100 |
+
draw.rectangle(
|
| 101 |
+
[(obj_x_img - cube_size//2, obj_y_img - cube_size//2),
|
| 102 |
+
(obj_x_img + cube_size//2, obj_y_img + cube_size//2)],
|
| 103 |
+
fill=obj_color,
|
| 104 |
+
outline=(0, 0, 0),
|
| 105 |
+
width=2
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
# Add shading for 3D effect (top face)
|
| 109 |
+
lighter_color = tuple(min(255, int(c * 1.3)) for c in self.colors[self.obj_color_name])
|
| 110 |
+
draw.polygon(
|
| 111 |
+
[(obj_x_img - cube_size//2, obj_y_img - cube_size//2),
|
| 112 |
+
(obj_x_img + cube_size//2, obj_y_img - cube_size//2),
|
| 113 |
+
(obj_x_img + cube_size//2 - 8, obj_y_img - cube_size//2 - 8),
|
| 114 |
+
(obj_x_img - cube_size//2 - 8, obj_y_img - cube_size//2 - 8)],
|
| 115 |
+
fill=lighter_color,
|
| 116 |
+
outline=(0, 0, 0)
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
# Add shading for 3D effect (side face)
|
| 120 |
+
darker_color = tuple(int(c * 0.6) for c in self.colors[self.obj_color_name])
|
| 121 |
+
draw.polygon(
|
| 122 |
+
[(obj_x_img + cube_size//2, obj_y_img - cube_size//2),
|
| 123 |
+
(obj_x_img + cube_size//2, obj_y_img + cube_size//2),
|
| 124 |
+
(obj_x_img + cube_size//2 - 8, obj_y_img + cube_size//2 - 8),
|
| 125 |
+
(obj_x_img + cube_size//2 - 8, obj_y_img - cube_size//2 - 8)],
|
| 126 |
+
fill=darker_color,
|
| 127 |
+
outline=(0, 0, 0)
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
return img
|
| 131 |
+
|
| 132 |
+
def close(self):
|
| 133 |
+
"""Close simulator"""
|
| 134 |
+
pass
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
def generate_improved_data(num_demos=500, save_path='demo_data.pkl', gui=False):
|
| 138 |
+
"""Generate diverse demonstrations with improved simulator"""
|
| 139 |
+
|
| 140 |
+
print(f"Generating {num_demos} demonstrations using improved simulator...")
|
| 141 |
+
|
| 142 |
+
sim = ImprovedSimulator(gui=gui)
|
| 143 |
+
data = []
|
| 144 |
+
|
| 145 |
+
# Expanded task templates with variations
|
| 146 |
+
task_templates = {
|
| 147 |
+
'red': [
|
| 148 |
+
"pick up the red cube",
|
| 149 |
+
"grasp the red block",
|
| 150 |
+
"grab the red object",
|
| 151 |
+
"reach for the red cube"
|
| 152 |
+
],
|
| 153 |
+
'blue': [
|
| 154 |
+
"pick up the blue cube",
|
| 155 |
+
"grasp the blue block",
|
| 156 |
+
"grab the blue object",
|
| 157 |
+
"reach for the blue cube"
|
| 158 |
+
],
|
| 159 |
+
'green': [
|
| 160 |
+
"pick up the green cube",
|
| 161 |
+
"grasp the green block",
|
| 162 |
+
"grab the green object",
|
| 163 |
+
"reach for the green cube"
|
| 164 |
+
],
|
| 165 |
+
'yellow': [
|
| 166 |
+
"pick up the yellow cube",
|
| 167 |
+
"grasp the yellow block",
|
| 168 |
+
"grab the yellow object",
|
| 169 |
+
"reach for the yellow cube"
|
| 170 |
+
],
|
| 171 |
+
'purple': [
|
| 172 |
+
"pick up the purple cube",
|
| 173 |
+
"grasp the purple block",
|
| 174 |
+
"grab the purple object",
|
| 175 |
+
"reach for the purple cube"
|
| 176 |
+
],
|
| 177 |
+
'orange': [
|
| 178 |
+
"pick up the orange cube",
|
| 179 |
+
"grasp the orange block",
|
| 180 |
+
"grab the orange object",
|
| 181 |
+
"reach for the orange cube"
|
| 182 |
+
]
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
try:
|
| 186 |
+
for i in tqdm(range(num_demos)):
|
| 187 |
+
# Random object color with balanced distribution
|
| 188 |
+
color = np.random.choice(list(task_templates.keys()))
|
| 189 |
+
|
| 190 |
+
# Reset environment
|
| 191 |
+
obj_pos, obj_orn, obj_color = sim.reset(object_color=color)
|
| 192 |
+
|
| 193 |
+
# Get camera image
|
| 194 |
+
image = sim.get_camera_image()
|
| 195 |
+
|
| 196 |
+
# Random task instruction
|
| 197 |
+
instruction = np.random.choice(task_templates[obj_color])
|
| 198 |
+
|
| 199 |
+
# Generate action: pre-grasp position above object
|
| 200 |
+
x, y, z_obj = obj_pos
|
| 201 |
+
|
| 202 |
+
# Add variation in approach height
|
| 203 |
+
z = z_obj + np.random.uniform(0.10, 0.20) # 10-20cm above object
|
| 204 |
+
|
| 205 |
+
# Add small noise to xy position (for robustness)
|
| 206 |
+
x += np.random.normal(0, 0.01)
|
| 207 |
+
y += np.random.normal(0, 0.01)
|
| 208 |
+
|
| 209 |
+
# Orientation: use the object's orientation with small noise
|
| 210 |
+
qx, qy, qz, qw = obj_orn
|
| 211 |
+
|
| 212 |
+
# Add small orientation noise for diversity
|
| 213 |
+
noise = np.random.randn(4) * 0.05
|
| 214 |
+
qx += noise[0]
|
| 215 |
+
qy += noise[1]
|
| 216 |
+
qz += noise[2]
|
| 217 |
+
qw += noise[3]
|
| 218 |
+
|
| 219 |
+
# Normalize quaternion
|
| 220 |
+
q_norm = np.sqrt(qx**2 + qy**2 + qz**2 + qw**2)
|
| 221 |
+
qx, qy, qz, qw = qx/q_norm, qy/q_norm, qz/q_norm, qw/q_norm
|
| 222 |
+
|
| 223 |
+
# Gripper state: open for approach (70%), closed for grasp (30%)
|
| 224 |
+
gripper_open = np.random.random() < 0.7
|
| 225 |
+
gripper = -1.0 if gripper_open else 1.0
|
| 226 |
+
|
| 227 |
+
action = np.array([x, y, z, qx, qy, qz, qw, gripper], dtype=np.float32)
|
| 228 |
+
|
| 229 |
+
data.append({
|
| 230 |
+
'image': image,
|
| 231 |
+
'instruction': instruction,
|
| 232 |
+
'action': action
|
| 233 |
+
})
|
| 234 |
+
|
| 235 |
+
finally:
|
| 236 |
+
sim.close()
|
| 237 |
+
|
| 238 |
+
# Save data
|
| 239 |
+
with open(save_path, 'wb') as f:
|
| 240 |
+
pickle.dump(data, f)
|
| 241 |
+
|
| 242 |
+
print(f"Saved {num_demos} demonstrations to {save_path}")
|
| 243 |
+
print(f"Action statistics:")
|
| 244 |
+
actions = np.array([d['action'] for d in data])
|
| 245 |
+
print(f" Position range: x=[{actions[:, 0].min():.2f}, {actions[:, 0].max():.2f}], "
|
| 246 |
+
f"y=[{actions[:, 1].min():.2f}, {actions[:, 1].max():.2f}], "
|
| 247 |
+
f"z=[{actions[:, 2].min():.2f}, {actions[:, 2].max():.2f}]")
|
| 248 |
+
print(f" Gripper open: {(actions[:, 7] < 0).sum()}/{len(actions)} ({(actions[:, 7] < 0).sum()/len(actions)*100:.1f}%)")
|
| 249 |
+
|
| 250 |
+
return data
|
| 251 |
+
|
| 252 |
+
|
| 253 |
+
# ============================================================================
|
| 254 |
+
# VLM Encoder (CLIP-based)
|
| 255 |
+
# ============================================================================
|
| 256 |
+
|
| 257 |
+
class VLM_Encoder(nn.Module):
|
| 258 |
+
"""Vision-Language Model encoder using CLIP"""
|
| 259 |
+
|
| 260 |
+
def __init__(self, model_name='openai/clip-vit-base-patch32', freeze=True):
|
| 261 |
+
super().__init__()
|
| 262 |
+
self.clip_model = CLIPModel.from_pretrained(model_name)
|
| 263 |
+
self.processor = CLIPProcessor.from_pretrained(model_name)
|
| 264 |
+
|
| 265 |
+
if freeze:
|
| 266 |
+
for param in self.clip_model.parameters():
|
| 267 |
+
param.requires_grad = False
|
| 268 |
+
|
| 269 |
+
self.vision_dim = self.clip_model.config.vision_config.hidden_size
|
| 270 |
+
self.text_dim = self.clip_model.config.text_config.hidden_size
|
| 271 |
+
self.output_dim = self.vision_dim + self.text_dim
|
| 272 |
+
|
| 273 |
+
print(f"VLM Encoder initialized: vision={self.vision_dim}, text={self.text_dim}, total={self.output_dim}")
|
| 274 |
+
|
| 275 |
+
def encode_image(self, images):
|
| 276 |
+
"""Encode PIL images to visual features"""
|
| 277 |
+
inputs = self.processor(images=images, return_tensors="pt", padding=True)
|
| 278 |
+
inputs = {k: v.to(next(self.clip_model.parameters()).device) for k, v in inputs.items()}
|
| 279 |
+
|
| 280 |
+
with torch.no_grad():
|
| 281 |
+
vision_outputs = self.clip_model.vision_model(**inputs)
|
| 282 |
+
image_features = vision_outputs.pooler_output
|
| 283 |
+
|
| 284 |
+
return image_features
|
| 285 |
+
|
| 286 |
+
def encode_text(self, texts):
|
| 287 |
+
"""Encode text instructions to language features"""
|
| 288 |
+
inputs = self.processor(text=texts, return_tensors="pt", padding=True, truncation=True)
|
| 289 |
+
inputs = {k: v.to(next(self.clip_model.parameters()).device) for k, v in inputs.items()}
|
| 290 |
+
|
| 291 |
+
with torch.no_grad():
|
| 292 |
+
text_outputs = self.clip_model.text_model(**inputs)
|
| 293 |
+
text_features = text_outputs.pooler_output
|
| 294 |
+
|
| 295 |
+
return text_features
|
| 296 |
+
|
| 297 |
+
def encode(self, images, texts):
|
| 298 |
+
"""Encode both image and text, return concatenated features"""
|
| 299 |
+
image_feats = self.encode_image(images)
|
| 300 |
+
text_feats = self.encode_text(texts)
|
| 301 |
+
combined = torch.cat([image_feats, text_feats], dim=-1)
|
| 302 |
+
return combined
|
| 303 |
+
|
| 304 |
+
|
| 305 |
+
# ============================================================================
|
| 306 |
+
# Improved Flow Matching Policy with Quaternion Normalization
|
| 307 |
+
# ============================================================================
|
| 308 |
+
|
| 309 |
+
class SinusoidalPosEmb(nn.Module):
|
| 310 |
+
"""Sinusoidal positional embeddings for time"""
|
| 311 |
+
|
| 312 |
+
def __init__(self, dim):
|
| 313 |
+
super().__init__()
|
| 314 |
+
self.dim = dim
|
| 315 |
+
|
| 316 |
+
def forward(self, t):
|
| 317 |
+
device = t.device
|
| 318 |
+
half_dim = self.dim // 2
|
| 319 |
+
emb = np.log(10000) / (half_dim - 1)
|
| 320 |
+
emb = torch.exp(torch.arange(half_dim, device=device) * -emb)
|
| 321 |
+
emb = t[:, None] * emb[None, :]
|
| 322 |
+
emb = torch.cat([torch.sin(emb), torch.cos(emb)], dim=-1)
|
| 323 |
+
return emb
|
| 324 |
+
|
| 325 |
+
|
| 326 |
+
class ImprovedFlowMatchingPolicy(nn.Module):
|
| 327 |
+
"""
|
| 328 |
+
Improved Flow Matching policy with:
|
| 329 |
+
- Quaternion normalization
|
| 330 |
+
- Separate gripper classification
|
| 331 |
+
- Better numerical stability
|
| 332 |
+
"""
|
| 333 |
+
|
| 334 |
+
def __init__(self, action_dim=8, context_dim=1024, hidden_dim=512,
|
| 335 |
+
time_dim=128, num_flow_steps=200):
|
| 336 |
+
super().__init__()
|
| 337 |
+
self.action_dim = action_dim
|
| 338 |
+
self.continuous_dim = 7 # xyz + quaternion
|
| 339 |
+
self.num_flow_steps = num_flow_steps
|
| 340 |
+
|
| 341 |
+
# Time embedding
|
| 342 |
+
self.time_mlp = nn.Sequential(
|
| 343 |
+
SinusoidalPosEmb(time_dim),
|
| 344 |
+
nn.Linear(time_dim, time_dim),
|
| 345 |
+
nn.GELU(),
|
| 346 |
+
)
|
| 347 |
+
|
| 348 |
+
# Context encoder (deeper for better representation)
|
| 349 |
+
self.context_encoder = nn.Sequential(
|
| 350 |
+
nn.Linear(context_dim, hidden_dim),
|
| 351 |
+
nn.LayerNorm(hidden_dim),
|
| 352 |
+
nn.GELU(),
|
| 353 |
+
nn.Dropout(0.1),
|
| 354 |
+
nn.Linear(hidden_dim, time_dim),
|
| 355 |
+
nn.LayerNorm(time_dim),
|
| 356 |
+
)
|
| 357 |
+
|
| 358 |
+
# Continuous action encoder (xyz + quaternion)
|
| 359 |
+
self.action_encoder = nn.Sequential(
|
| 360 |
+
nn.Linear(self.continuous_dim, time_dim),
|
| 361 |
+
nn.GELU(),
|
| 362 |
+
)
|
| 363 |
+
|
| 364 |
+
# Velocity network for continuous actions
|
| 365 |
+
self.velocity_net = nn.Sequential(
|
| 366 |
+
nn.Linear(time_dim * 3, hidden_dim),
|
| 367 |
+
nn.LayerNorm(hidden_dim),
|
| 368 |
+
nn.GELU(),
|
| 369 |
+
nn.Dropout(0.1),
|
| 370 |
+
nn.Linear(hidden_dim, hidden_dim // 2),
|
| 371 |
+
nn.GELU(),
|
| 372 |
+
nn.Linear(hidden_dim // 2, self.continuous_dim),
|
| 373 |
+
)
|
| 374 |
+
|
| 375 |
+
# Separate gripper classifier
|
| 376 |
+
self.gripper_classifier = nn.Sequential(
|
| 377 |
+
nn.Linear(context_dim, hidden_dim),
|
| 378 |
+
nn.GELU(),
|
| 379 |
+
nn.Dropout(0.1),
|
| 380 |
+
nn.Linear(hidden_dim, hidden_dim // 2),
|
| 381 |
+
nn.GELU(),
|
| 382 |
+
nn.Linear(hidden_dim // 2, 2), # Binary: open/close
|
| 383 |
+
)
|
| 384 |
+
|
| 385 |
+
def normalize_quaternion(self, quat):
|
| 386 |
+
"""Normalize quaternion to unit length"""
|
| 387 |
+
quat_norm = torch.norm(quat, dim=-1, keepdim=True)
|
| 388 |
+
return quat / (quat_norm + 1e-8)
|
| 389 |
+
|
| 390 |
+
def forward(self, x_t, t, context):
|
| 391 |
+
"""
|
| 392 |
+
Predict velocity field at time t for continuous actions
|
| 393 |
+
|
| 394 |
+
Args:
|
| 395 |
+
x_t: Current continuous action state [B, 7] (xyz + quat)
|
| 396 |
+
t: Time in [0, 1] [B]
|
| 397 |
+
context: Visual-language features [B, context_dim]
|
| 398 |
+
|
| 399 |
+
Returns:
|
| 400 |
+
velocity: dx/dt [B, 7]
|
| 401 |
+
"""
|
| 402 |
+
# Encode inputs
|
| 403 |
+
t_emb = self.time_mlp(t)
|
| 404 |
+
context_emb = self.context_encoder(context)
|
| 405 |
+
x_emb = self.action_encoder(x_t)
|
| 406 |
+
|
| 407 |
+
# Concatenate
|
| 408 |
+
combined = torch.cat([x_emb, t_emb, context_emb], dim=-1)
|
| 409 |
+
|
| 410 |
+
# Predict velocity
|
| 411 |
+
velocity = self.velocity_net(combined)
|
| 412 |
+
return velocity
|
| 413 |
+
|
| 414 |
+
def predict_gripper(self, context):
|
| 415 |
+
"""Predict gripper state (binary classification)"""
|
| 416 |
+
logits = self.gripper_classifier(context) # [B, 2]
|
| 417 |
+
return logits
|
| 418 |
+
|
| 419 |
+
def sample(self, context, num_samples=1, device='cuda'):
|
| 420 |
+
"""
|
| 421 |
+
Sample actions by integrating the flow from t=0 to t=1
|
| 422 |
+
|
| 423 |
+
Returns:
|
| 424 |
+
actions: [B*num_samples, 8] (7 continuous + 1 gripper)
|
| 425 |
+
"""
|
| 426 |
+
batch_size = context.shape[0]
|
| 427 |
+
|
| 428 |
+
# Start from Gaussian noise for continuous actions
|
| 429 |
+
x_t = torch.randn(batch_size * num_samples, self.continuous_dim).to(device)
|
| 430 |
+
|
| 431 |
+
# Repeat context for multiple samples
|
| 432 |
+
context_repeated = context.repeat_interleave(num_samples, dim=0)
|
| 433 |
+
|
| 434 |
+
# Integrate flow with Euler method
|
| 435 |
+
dt = 1.0 / self.num_flow_steps
|
| 436 |
+
|
| 437 |
+
for step in range(self.num_flow_steps):
|
| 438 |
+
t = torch.ones(batch_size * num_samples).to(device) * (step * dt)
|
| 439 |
+
|
| 440 |
+
with torch.no_grad():
|
| 441 |
+
velocity = self.forward(x_t, t, context_repeated)
|
| 442 |
+
x_t = x_t + velocity * dt
|
| 443 |
+
|
| 444 |
+
# Normalize quaternion every few steps for stability
|
| 445 |
+
if step % 10 == 0:
|
| 446 |
+
x_t[:, 3:7] = self.normalize_quaternion(x_t[:, 3:7])
|
| 447 |
+
|
| 448 |
+
# Final quaternion normalization
|
| 449 |
+
x_t[:, 3:7] = self.normalize_quaternion(x_t[:, 3:7])
|
| 450 |
+
|
| 451 |
+
# Predict gripper (use original context, not repeated)
|
| 452 |
+
with torch.no_grad():
|
| 453 |
+
gripper_logits = self.predict_gripper(context)
|
| 454 |
+
gripper_probs = F.softmax(gripper_logits, dim=-1)
|
| 455 |
+
|
| 456 |
+
# Sample gripper state: 0=open (-1), 1=close (+1)
|
| 457 |
+
gripper_pred = torch.argmax(gripper_probs, dim=-1).float() # [B]
|
| 458 |
+
gripper_pred = gripper_pred * 2 - 1 # Map 0,1 to -1,+1
|
| 459 |
+
|
| 460 |
+
# Repeat for multiple samples
|
| 461 |
+
gripper_pred = gripper_pred.repeat_interleave(num_samples)[:, None]
|
| 462 |
+
|
| 463 |
+
# Combine continuous and gripper
|
| 464 |
+
actions = torch.cat([x_t, gripper_pred], dim=-1)
|
| 465 |
+
|
| 466 |
+
return actions
|
| 467 |
+
|
| 468 |
+
def compute_loss(self, actions, context):
|
| 469 |
+
"""
|
| 470 |
+
Compute combined loss: flow matching + gripper classification
|
| 471 |
+
|
| 472 |
+
Args:
|
| 473 |
+
actions: [B, 8] (7 continuous + 1 gripper)
|
| 474 |
+
context: [B, context_dim]
|
| 475 |
+
"""
|
| 476 |
+
batch_size = actions.shape[0]
|
| 477 |
+
device = actions.device
|
| 478 |
+
|
| 479 |
+
# Split actions
|
| 480 |
+
continuous_actions = actions[:, :7] # xyz + quaternion
|
| 481 |
+
gripper_labels = actions[:, 7] # -1 or +1
|
| 482 |
+
|
| 483 |
+
# === Flow Matching Loss for Continuous Actions ===
|
| 484 |
+
|
| 485 |
+
# Sample random time
|
| 486 |
+
t = torch.rand(batch_size).to(device)
|
| 487 |
+
|
| 488 |
+
# Sample noise
|
| 489 |
+
x_0 = torch.randn_like(continuous_actions)
|
| 490 |
+
|
| 491 |
+
# Ensure quaternion is normalized in target
|
| 492 |
+
continuous_actions[:, 3:7] = self.normalize_quaternion(continuous_actions[:, 3:7])
|
| 493 |
+
|
| 494 |
+
# Linear interpolation
|
| 495 |
+
x_t = t[:, None] * continuous_actions + (1 - t[:, None]) * x_0
|
| 496 |
+
|
| 497 |
+
# Normalize quaternion in interpolated state
|
| 498 |
+
x_t[:, 3:7] = self.normalize_quaternion(x_t[:, 3:7])
|
| 499 |
+
|
| 500 |
+
# Target velocity
|
| 501 |
+
target_velocity = continuous_actions - x_0
|
| 502 |
+
|
| 503 |
+
# Predict velocity
|
| 504 |
+
pred_velocity = self.forward(x_t, t, context)
|
| 505 |
+
|
| 506 |
+
# Flow matching loss (MSE)
|
| 507 |
+
flow_loss = F.mse_loss(pred_velocity, target_velocity)
|
| 508 |
+
|
| 509 |
+
# === Gripper Classification Loss ===
|
| 510 |
+
|
| 511 |
+
# Convert gripper labels: -1 → 0 (open), +1 → 1 (close)
|
| 512 |
+
gripper_labels_binary = ((gripper_labels + 1) / 2).long() # Map to 0,1
|
| 513 |
+
|
| 514 |
+
# Predict gripper
|
| 515 |
+
gripper_logits = self.predict_gripper(context)
|
| 516 |
+
|
| 517 |
+
# Cross entropy loss
|
| 518 |
+
gripper_loss = F.cross_entropy(gripper_logits, gripper_labels_binary)
|
| 519 |
+
|
| 520 |
+
# Compute gripper accuracy for monitoring
|
| 521 |
+
gripper_pred = torch.argmax(gripper_logits, dim=-1)
|
| 522 |
+
gripper_acc = (gripper_pred == gripper_labels_binary).float().mean()
|
| 523 |
+
|
| 524 |
+
# Combined loss
|
| 525 |
+
total_loss = flow_loss + 0.5 * gripper_loss # Weight gripper loss
|
| 526 |
+
|
| 527 |
+
return total_loss, {
|
| 528 |
+
'flow_loss': flow_loss.item(),
|
| 529 |
+
'gripper_loss': gripper_loss.item(),
|
| 530 |
+
'gripper_acc': gripper_acc.item(),
|
| 531 |
+
'total_loss': total_loss.item()
|
| 532 |
+
}
|
| 533 |
+
|
| 534 |
+
|
| 535 |
+
# ============================================================================
|
| 536 |
+
# Dataset
|
| 537 |
+
# ============================================================================
|
| 538 |
+
|
| 539 |
+
class VLADataset(Dataset):
|
| 540 |
+
"""Dataset for VLA training"""
|
| 541 |
+
|
| 542 |
+
def __init__(self, data_path):
|
| 543 |
+
with open(data_path, 'rb') as f:
|
| 544 |
+
self.data = pickle.load(f)
|
| 545 |
+
|
| 546 |
+
def __len__(self):
|
| 547 |
+
return len(self.data)
|
| 548 |
+
|
| 549 |
+
def __getitem__(self, idx):
|
| 550 |
+
sample = self.data[idx]
|
| 551 |
+
return {
|
| 552 |
+
'image': sample['image'],
|
| 553 |
+
'instruction': sample['instruction'],
|
| 554 |
+
'action': torch.FloatTensor(sample['action'])
|
| 555 |
+
}
|
| 556 |
+
|
| 557 |
+
|
| 558 |
+
def collate_fn(batch):
|
| 559 |
+
"""Custom collate function to handle PIL images"""
|
| 560 |
+
images = [item['image'] for item in batch]
|
| 561 |
+
instructions = [item['instruction'] for item in batch]
|
| 562 |
+
actions = torch.stack([item['action'] for item in batch])
|
| 563 |
+
|
| 564 |
+
return {
|
| 565 |
+
'images': images,
|
| 566 |
+
'instructions': instructions,
|
| 567 |
+
'actions': actions
|
| 568 |
+
}
|
| 569 |
+
|
| 570 |
+
|
| 571 |
+
# ============================================================================
|
| 572 |
+
# Training
|
| 573 |
+
# ============================================================================
|
| 574 |
+
|
| 575 |
+
def train(args):
|
| 576 |
+
"""Train VLA model from scratch"""
|
| 577 |
+
|
| 578 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 579 |
+
print(f"Using device: {device}")
|
| 580 |
+
|
| 581 |
+
# Load data
|
| 582 |
+
if not Path(args.data_path).exists():
|
| 583 |
+
print(f"Data file {args.data_path} not found. Generating data...")
|
| 584 |
+
generate_improved_data(num_demos=args.num_demos, save_path=args.data_path, gui=args.gui)
|
| 585 |
+
|
| 586 |
+
dataset = VLADataset(args.data_path)
|
| 587 |
+
dataloader = DataLoader(dataset, batch_size=args.batch_size,
|
| 588 |
+
shuffle=True, collate_fn=collate_fn, num_workers=0)
|
| 589 |
+
|
| 590 |
+
# Initialize models
|
| 591 |
+
print("Initializing models...")
|
| 592 |
+
vlm_encoder = VLM_Encoder().to(device)
|
| 593 |
+
context_dim = vlm_encoder.output_dim
|
| 594 |
+
|
| 595 |
+
policy = ImprovedFlowMatchingPolicy(
|
| 596 |
+
action_dim=args.action_dim,
|
| 597 |
+
context_dim=context_dim,
|
| 598 |
+
hidden_dim=args.hidden_dim,
|
| 599 |
+
num_flow_steps=args.num_flow_steps
|
| 600 |
+
).to(device)
|
| 601 |
+
|
| 602 |
+
# Optimizer with warmup
|
| 603 |
+
optimizer = torch.optim.AdamW(policy.parameters(), lr=args.lr, weight_decay=1e-4)
|
| 604 |
+
|
| 605 |
+
# Learning rate scheduler
|
| 606 |
+
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
|
| 607 |
+
optimizer, T_max=args.epochs, eta_min=args.lr * 0.1
|
| 608 |
+
)
|
| 609 |
+
|
| 610 |
+
# Training loop
|
| 611 |
+
print(f"Training for {args.epochs} epochs...")
|
| 612 |
+
policy.train()
|
| 613 |
+
|
| 614 |
+
best_loss = float('inf')
|
| 615 |
+
|
| 616 |
+
for epoch in range(args.epochs):
|
| 617 |
+
total_metrics = {'total_loss': 0, 'flow_loss': 0, 'gripper_loss': 0, 'gripper_acc': 0}
|
| 618 |
+
pbar = tqdm(dataloader, desc=f"Epoch {epoch+1}/{args.epochs}")
|
| 619 |
+
|
| 620 |
+
for batch in pbar:
|
| 621 |
+
images = batch['images']
|
| 622 |
+
instructions = batch['instructions']
|
| 623 |
+
actions = batch['actions'].to(device)
|
| 624 |
+
|
| 625 |
+
# Encode visual-language context
|
| 626 |
+
context = vlm_encoder.encode(images, instructions)
|
| 627 |
+
|
| 628 |
+
# Compute loss
|
| 629 |
+
loss, metrics = policy.compute_loss(actions, context)
|
| 630 |
+
|
| 631 |
+
# Backward
|
| 632 |
+
optimizer.zero_grad()
|
| 633 |
+
loss.backward()
|
| 634 |
+
|
| 635 |
+
# Gradient clipping for stability
|
| 636 |
+
torch.nn.utils.clip_grad_norm_(policy.parameters(), max_norm=1.0)
|
| 637 |
+
|
| 638 |
+
optimizer.step()
|
| 639 |
+
|
| 640 |
+
# Update metrics
|
| 641 |
+
for k, v in metrics.items():
|
| 642 |
+
total_metrics[k] += v
|
| 643 |
+
|
| 644 |
+
pbar.set_postfix({
|
| 645 |
+
'loss': f'{metrics["total_loss"]:.4f}',
|
| 646 |
+
'flow': f'{metrics["flow_loss"]:.4f}',
|
| 647 |
+
'grip_acc': f'{metrics["gripper_acc"]:.2%}'
|
| 648 |
+
})
|
| 649 |
+
|
| 650 |
+
# Epoch summary
|
| 651 |
+
for k in total_metrics:
|
| 652 |
+
total_metrics[k] /= len(dataloader)
|
| 653 |
+
|
| 654 |
+
print(f"Epoch {epoch+1} - Loss: {total_metrics['total_loss']:.4f}, "
|
| 655 |
+
f"Flow: {total_metrics['flow_loss']:.4f}, "
|
| 656 |
+
f"Gripper Loss: {total_metrics['gripper_loss']:.4f}, "
|
| 657 |
+
f"Gripper Acc: {total_metrics['gripper_acc']:.2%}, "
|
| 658 |
+
f"LR: {scheduler.get_last_lr()[0]:.6f}")
|
| 659 |
+
|
| 660 |
+
# Update learning rate
|
| 661 |
+
scheduler.step()
|
| 662 |
+
|
| 663 |
+
# Save best model
|
| 664 |
+
if total_metrics['total_loss'] < best_loss:
|
| 665 |
+
best_loss = total_metrics['total_loss']
|
| 666 |
+
best_path = args.save_path.replace('.pt', '_best.pt')
|
| 667 |
+
saved_args = vars(args).copy()
|
| 668 |
+
saved_args['context_dim'] = context_dim
|
| 669 |
+
checkpoint = {
|
| 670 |
+
'epoch': epoch,
|
| 671 |
+
'policy': policy.state_dict(),
|
| 672 |
+
'optimizer': optimizer.state_dict(),
|
| 673 |
+
'args': saved_args,
|
| 674 |
+
'best_loss': best_loss
|
| 675 |
+
}
|
| 676 |
+
torch.save(checkpoint, best_path)
|
| 677 |
+
print(f" → Saved best model (loss={best_loss:.4f})")
|
| 678 |
+
|
| 679 |
+
# Save final checkpoint
|
| 680 |
+
saved_args = vars(args).copy()
|
| 681 |
+
saved_args['context_dim'] = context_dim
|
| 682 |
+
checkpoint = {
|
| 683 |
+
'epoch': args.epochs,
|
| 684 |
+
'policy': policy.state_dict(),
|
| 685 |
+
'optimizer': optimizer.state_dict(),
|
| 686 |
+
'args': saved_args
|
| 687 |
+
}
|
| 688 |
+
torch.save(checkpoint, args.save_path)
|
| 689 |
+
print(f"\nFinal model saved to {args.save_path}")
|
| 690 |
+
print(f"Best model saved to {best_path} (loss={best_loss:.4f})")
|
| 691 |
+
|
| 692 |
+
|
| 693 |
+
def finetune(args):
|
| 694 |
+
"""Fine-tune pre-trained VLA model"""
|
| 695 |
+
|
| 696 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 697 |
+
print(f"Using device: {device}")
|
| 698 |
+
|
| 699 |
+
# Load checkpoint
|
| 700 |
+
print(f"Loading checkpoint from {args.checkpoint}")
|
| 701 |
+
checkpoint = torch.load(args.checkpoint, map_location=device)
|
| 702 |
+
|
| 703 |
+
# Load data
|
| 704 |
+
dataset = VLADataset(args.data_path)
|
| 705 |
+
dataloader = DataLoader(dataset, batch_size=args.batch_size,
|
| 706 |
+
shuffle=True, collate_fn=collate_fn, num_workers=0)
|
| 707 |
+
|
| 708 |
+
# Initialize models
|
| 709 |
+
vlm_encoder = VLM_Encoder().to(device)
|
| 710 |
+
|
| 711 |
+
if 'context_dim' in checkpoint['args']:
|
| 712 |
+
context_dim = checkpoint['args']['context_dim']
|
| 713 |
+
else:
|
| 714 |
+
context_dim = vlm_encoder.output_dim
|
| 715 |
+
|
| 716 |
+
policy = ImprovedFlowMatchingPolicy(
|
| 717 |
+
action_dim=checkpoint['args']['action_dim'],
|
| 718 |
+
context_dim=context_dim,
|
| 719 |
+
hidden_dim=checkpoint['args']['hidden_dim'],
|
| 720 |
+
num_flow_steps=checkpoint['args']['num_flow_steps']
|
| 721 |
+
).to(device)
|
| 722 |
+
|
| 723 |
+
policy.load_state_dict(checkpoint['policy'])
|
| 724 |
+
|
| 725 |
+
# Optimizer with lower learning rate
|
| 726 |
+
optimizer = torch.optim.AdamW(policy.parameters(), lr=args.lr * 0.1, weight_decay=1e-4)
|
| 727 |
+
|
| 728 |
+
# Fine-tuning loop
|
| 729 |
+
print(f"Fine-tuning for {args.epochs} epochs...")
|
| 730 |
+
policy.train()
|
| 731 |
+
|
| 732 |
+
for epoch in range(args.epochs):
|
| 733 |
+
total_metrics = {'total_loss': 0, 'flow_loss': 0, 'gripper_loss': 0, 'gripper_acc': 0}
|
| 734 |
+
pbar = tqdm(dataloader, desc=f"Epoch {epoch+1}/{args.epochs}")
|
| 735 |
+
|
| 736 |
+
for batch in pbar:
|
| 737 |
+
images = batch['images']
|
| 738 |
+
instructions = batch['instructions']
|
| 739 |
+
actions = batch['actions'].to(device)
|
| 740 |
+
|
| 741 |
+
context = vlm_encoder.encode(images, instructions)
|
| 742 |
+
loss, metrics = policy.compute_loss(actions, context)
|
| 743 |
+
|
| 744 |
+
optimizer.zero_grad()
|
| 745 |
+
loss.backward()
|
| 746 |
+
torch.nn.utils.clip_grad_norm_(policy.parameters(), max_norm=1.0)
|
| 747 |
+
optimizer.step()
|
| 748 |
+
|
| 749 |
+
for k, v in metrics.items():
|
| 750 |
+
total_metrics[k] += v
|
| 751 |
+
|
| 752 |
+
pbar.set_postfix({'loss': f'{metrics["total_loss"]:.4f}'})
|
| 753 |
+
|
| 754 |
+
for k in total_metrics:
|
| 755 |
+
total_metrics[k] /= len(dataloader)
|
| 756 |
+
|
| 757 |
+
print(f"Epoch {epoch+1} - Loss: {total_metrics['total_loss']:.4f}")
|
| 758 |
+
|
| 759 |
+
# Save fine-tuned model
|
| 760 |
+
finetuned_path = args.save_path.replace('.pt', '_finetuned.pt')
|
| 761 |
+
saved_args = vars(args).copy()
|
| 762 |
+
saved_args['context_dim'] = context_dim
|
| 763 |
+
checkpoint = {
|
| 764 |
+
'policy': policy.state_dict(),
|
| 765 |
+
'args': saved_args
|
| 766 |
+
}
|
| 767 |
+
torch.save(checkpoint, finetuned_path)
|
| 768 |
+
print(f"Fine-tuned model saved to {finetuned_path}")
|
| 769 |
+
|
| 770 |
+
|
| 771 |
+
# ============================================================================
|
| 772 |
+
# Replay and Visualization
|
| 773 |
+
# ============================================================================
|
| 774 |
+
|
| 775 |
+
def replay(args):
|
| 776 |
+
"""Replay demonstrations and visualize predictions"""
|
| 777 |
+
|
| 778 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 779 |
+
print(f"Using device: {device}")
|
| 780 |
+
|
| 781 |
+
# Load checkpoint
|
| 782 |
+
print(f"Loading checkpoint from {args.checkpoint}")
|
| 783 |
+
checkpoint = torch.load(args.checkpoint, map_location=device)
|
| 784 |
+
|
| 785 |
+
# Load data
|
| 786 |
+
dataset = VLADataset(args.data_path)
|
| 787 |
+
|
| 788 |
+
# Initialize models
|
| 789 |
+
vlm_encoder = VLM_Encoder().to(device)
|
| 790 |
+
|
| 791 |
+
if 'context_dim' in checkpoint['args']:
|
| 792 |
+
context_dim = checkpoint['args']['context_dim']
|
| 793 |
+
else:
|
| 794 |
+
context_dim = vlm_encoder.output_dim
|
| 795 |
+
|
| 796 |
+
policy = ImprovedFlowMatchingPolicy(
|
| 797 |
+
action_dim=checkpoint['args']['action_dim'],
|
| 798 |
+
context_dim=context_dim,
|
| 799 |
+
hidden_dim=checkpoint['args']['hidden_dim'],
|
| 800 |
+
num_flow_steps=checkpoint['args']['num_flow_steps']
|
| 801 |
+
).to(device)
|
| 802 |
+
|
| 803 |
+
policy.load_state_dict(checkpoint['policy'])
|
| 804 |
+
policy.eval()
|
| 805 |
+
|
| 806 |
+
# Replay samples
|
| 807 |
+
num_samples = min(args.num_replay, len(dataset))
|
| 808 |
+
print(f"Replaying {num_samples} demonstrations...")
|
| 809 |
+
|
| 810 |
+
fig, axes = plt.subplots(num_samples, 2, figsize=(14, 3.5*num_samples))
|
| 811 |
+
if num_samples == 1:
|
| 812 |
+
axes = axes.reshape(1, -1)
|
| 813 |
+
|
| 814 |
+
total_mae = 0
|
| 815 |
+
total_pos_error = 0
|
| 816 |
+
total_quat_error = 0
|
| 817 |
+
total_gripper_acc = 0
|
| 818 |
+
|
| 819 |
+
for i in range(num_samples):
|
| 820 |
+
sample = dataset[i]
|
| 821 |
+
image = sample['image']
|
| 822 |
+
instruction = sample['instruction']
|
| 823 |
+
gt_action = sample['action'].numpy()
|
| 824 |
+
|
| 825 |
+
# Predict action
|
| 826 |
+
context = vlm_encoder.encode([image], [instruction])
|
| 827 |
+
with torch.no_grad():
|
| 828 |
+
pred_action = policy.sample(context, num_samples=1, device=device)
|
| 829 |
+
pred_action = pred_action.cpu().numpy()[0]
|
| 830 |
+
|
| 831 |
+
# Compute errors
|
| 832 |
+
mae = np.abs(gt_action - pred_action).mean()
|
| 833 |
+
pos_error = np.linalg.norm(gt_action[:3] - pred_action[:3])
|
| 834 |
+
|
| 835 |
+
# Quaternion error (geodesic distance)
|
| 836 |
+
q_gt = gt_action[3:7]
|
| 837 |
+
q_pred = pred_action[3:7]
|
| 838 |
+
quat_dot = np.abs(np.dot(q_gt, q_pred))
|
| 839 |
+
quat_error = 2 * np.arccos(np.clip(quat_dot, 0, 1)) * 180 / np.pi
|
| 840 |
+
|
| 841 |
+
# Gripper accuracy
|
| 842 |
+
gripper_correct = np.sign(gt_action[7]) == np.sign(pred_action[7])
|
| 843 |
+
|
| 844 |
+
total_mae += mae
|
| 845 |
+
total_pos_error += pos_error
|
| 846 |
+
total_quat_error += quat_error
|
| 847 |
+
total_gripper_acc += gripper_correct
|
| 848 |
+
|
| 849 |
+
# Visualize
|
| 850 |
+
axes[i, 0].imshow(image)
|
| 851 |
+
axes[i, 0].set_title(
|
| 852 |
+
f"Instruction: {instruction}\n"
|
| 853 |
+
f"MAE: {mae:.4f}, Pos: {pos_error:.4f}m, "
|
| 854 |
+
f"Quat: {quat_error:.1f}°, Grip: {'✓' if gripper_correct else '✗'}",
|
| 855 |
+
fontsize=9
|
| 856 |
+
)
|
| 857 |
+
axes[i, 0].axis('off')
|
| 858 |
+
|
| 859 |
+
# Plot actions
|
| 860 |
+
action_names = ['x', 'y', 'z', 'qx', 'qy', 'qz', 'qw', 'grip']
|
| 861 |
+
x_pos = np.arange(len(action_names))
|
| 862 |
+
width = 0.35
|
| 863 |
+
|
| 864 |
+
axes[i, 1].bar(x_pos - width/2, gt_action, width, label='Ground Truth', alpha=0.7)
|
| 865 |
+
axes[i, 1].bar(x_pos + width/2, pred_action, width, label='Predicted', alpha=0.7)
|
| 866 |
+
axes[i, 1].set_xticks(x_pos)
|
| 867 |
+
axes[i, 1].set_xticklabels(action_names, rotation=45)
|
| 868 |
+
axes[i, 1].set_ylabel('Action Value')
|
| 869 |
+
axes[i, 1].set_title(f'Action Comparison (Sample {i+1})')
|
| 870 |
+
axes[i, 1].legend()
|
| 871 |
+
axes[i, 1].grid(True, alpha=0.3)
|
| 872 |
+
axes[i, 1].axhline(y=0, color='k', linestyle='-', linewidth=0.5)
|
| 873 |
+
|
| 874 |
+
# Print comparison
|
| 875 |
+
print(f"\nSample {i+1}:")
|
| 876 |
+
print(f" Instruction: {instruction}")
|
| 877 |
+
print(f" GT Action: {gt_action}")
|
| 878 |
+
print(f" Pred Action: {pred_action}")
|
| 879 |
+
print(f" MAE: {mae:.4f}, Pos Error: {pos_error:.4f}m, "
|
| 880 |
+
f"Quat Error: {quat_error:.1f}°, Gripper: {'✓' if gripper_correct else '✗'}")
|
| 881 |
+
|
| 882 |
+
# Summary statistics
|
| 883 |
+
avg_mae = total_mae / num_samples
|
| 884 |
+
avg_pos_error = total_pos_error / num_samples
|
| 885 |
+
avg_quat_error = total_quat_error / num_samples
|
| 886 |
+
avg_gripper_acc = total_gripper_acc / num_samples
|
| 887 |
+
|
| 888 |
+
print(f"\n{'='*70}")
|
| 889 |
+
print(f"Performance Summary (n={num_samples}):")
|
| 890 |
+
print(f" Average MAE: {avg_mae:.4f}")
|
| 891 |
+
print(f" Average Position Error: {avg_pos_error:.4f}m ({avg_pos_error*100:.2f}cm)")
|
| 892 |
+
print(f" Average Quaternion Error: {avg_quat_error:.2f}°")
|
| 893 |
+
print(f" Gripper Accuracy: {avg_gripper_acc:.2%}")
|
| 894 |
+
print(f"{'='*70}")
|
| 895 |
+
|
| 896 |
+
plt.tight_layout()
|
| 897 |
+
save_path = 'replay_results.png'
|
| 898 |
+
plt.savefig(save_path, dpi=150, bbox_inches='tight')
|
| 899 |
+
print(f"\nVisualization saved to {save_path}")
|
| 900 |
+
plt.close()
|
| 901 |
+
|
| 902 |
+
|
| 903 |
+
# ============================================================================
|
| 904 |
+
# Main
|
| 905 |
+
# ============================================================================
|
| 906 |
+
|
| 907 |
+
def main():
|
| 908 |
+
parser = argparse.ArgumentParser(description='Improved VLM + Flow Matching VLA')
|
| 909 |
+
|
| 910 |
+
# Mode
|
| 911 |
+
parser.add_argument('--mode', type=str, default='train',
|
| 912 |
+
choices=['generate_data', 'train', 'finetune', 'replay'],
|
| 913 |
+
help='Operation mode')
|
| 914 |
+
|
| 915 |
+
# Data
|
| 916 |
+
parser.add_argument('--data_path', type=str, default='demo_data.pkl',
|
| 917 |
+
help='Path to demonstration data')
|
| 918 |
+
parser.add_argument('--num_demos', type=int, default=500,
|
| 919 |
+
help='Number of demonstrations to generate')
|
| 920 |
+
parser.add_argument('--gui', action='store_true',
|
| 921 |
+
help='Show GUI during data generation')
|
| 922 |
+
|
| 923 |
+
# Model
|
| 924 |
+
parser.add_argument('--action_dim', type=int, default=8,
|
| 925 |
+
help='Action dimension')
|
| 926 |
+
parser.add_argument('--hidden_dim', type=int, default=512,
|
| 927 |
+
help='Hidden dimension')
|
| 928 |
+
parser.add_argument('--num_flow_steps', type=int, default=200,
|
| 929 |
+
help='Number of flow discretization steps')
|
| 930 |
+
|
| 931 |
+
# Training
|
| 932 |
+
parser.add_argument('--epochs', type=int, default=100,
|
| 933 |
+
help='Number of training epochs')
|
| 934 |
+
parser.add_argument('--batch_size', type=int, default=32,
|
| 935 |
+
help='Batch size')
|
| 936 |
+
parser.add_argument('--lr', type=float, default=1e-4,
|
| 937 |
+
help='Learning rate')
|
| 938 |
+
|
| 939 |
+
# Checkpoint
|
| 940 |
+
parser.add_argument('--checkpoint', type=str, default='vla_checkpoint.pt',
|
| 941 |
+
help='Path to checkpoint')
|
| 942 |
+
parser.add_argument('--save_path', type=str, default='vla_checkpoint.pt',
|
| 943 |
+
help='Path to save trained model')
|
| 944 |
+
|
| 945 |
+
# Replay
|
| 946 |
+
parser.add_argument('--num_replay', type=int, default=10,
|
| 947 |
+
help='Number of samples to replay')
|
| 948 |
+
|
| 949 |
+
args = parser.parse_args()
|
| 950 |
+
|
| 951 |
+
# Execute based on mode
|
| 952 |
+
if args.mode == 'generate_data':
|
| 953 |
+
generate_improved_data(args.num_demos, args.data_path, args.gui)
|
| 954 |
+
elif args.mode == 'train':
|
| 955 |
+
train(args)
|
| 956 |
+
elif args.mode == 'finetune':
|
| 957 |
+
finetune(args)
|
| 958 |
+
elif args.mode == 'replay':
|
| 959 |
+
replay(args)
|
| 960 |
+
else:
|
| 961 |
+
raise ValueError(f"Unknown mode: {args.mode}")
|
| 962 |
+
|
| 963 |
+
|
| 964 |
+
if __name__ == '__main__':
|
| 965 |
+
main()
|