File size: 10,518 Bytes
24857f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# FoundationPose Deployment - Current Status

**Last Updated:** 2026-01-28
**Repository:** `/Users/georgpuschel/repos/robot-ml/foundationpose/`
**Hugging Face Space:** https://huggingface.co/spaces/gpue/foundationpose

---

## 📦 What's Been Completed

### Core Files Created

1. **app.py** (570 lines)
   - Complete Gradio application with ZeroGPU integration
   - Dual mode: Placeholder (default) and Real FoundationPose
   - REST API endpoints: `/api/initialize` and `/api/estimate`
   - Web UI with tabs for initialization, estimation, and API docs
   - Environment variable `USE_REAL_MODEL` controls mode

2. **estimator.py** (350+ lines)
   - FoundationPoseEstimator class wrapping the real FoundationPose API
   - Methods: `register_object()`, `estimate_pose()`, `reset_tracking()`
   - Handles camera intrinsics, depth images, and segmentation masks
   - Quaternion and rotation matrix conversions
   - Mesh loading and reconstruction (placeholder for BundleSDF)

3. **client.py** (200+ lines)
   - Python client for calling the API from robot-ml
   - FoundationPoseClient class with initialize() and estimate_pose()
   - Image encoding/decoding utilities
   - Example usage code

4. **requirements.txt**
   - Core dependencies: gradio, spaces, torch, opencv-python
   - 3D vision: trimesh, pyrender, scikit-image
   - Placeholder for FoundationPose installation

5. **Dockerfile**
   - CUDA 12.1 base image
   - System dependencies (eigen3, OpenGL, etc.)
   - FoundationPose repository clone and build
   - NVDiffRast, Kaolin, PyTorch3D installation

6. **download_weights.py**
   - Script to check for and download model weights
   - Instructions for manual weight setup
   - Git-LFS integration guide

7. **deploy.sh**
   - Interactive deployment script
   - Checks for weights and git status
   - Offers placeholder vs real mode deployment
   - Guides through git-lfs setup

8. **Documentation**
   - README.md (updated with full details)
   - DEPLOYMENT.md (step-by-step deployment guide)
   - QUICKSTART.md (quick start for both modes)
   - STATUS.md (this file)

9. **.gitignore**
   - Python cache files
   - Virtual environments
   - Model weights (for git-lfs)
   - Test images

---

## 🎯 How It Works

### Placeholder Mode (Default)

- **Purpose**: API testing without GPU requirements
- **Behavior**: Returns empty pose results with success=true
- **Use Cases**:
  - Developing client integrations
  - Testing API structure
  - Demos without GPU costs

### Real Mode (Requires Setup)

- **Purpose**: Actual 6D pose estimation
- **Requirements**:
  - Model weights in `weights/` directory
  - FoundationPose repository cloned
  - Environment variable `USE_REAL_MODEL=true`
- **Behavior**: Uses actual FoundationPose inference
- **Use Cases**:
  - Production pose estimation
  - Validation and testing with real data
  - Integration with robot-ml training

---

## 🚀 Deployment Options

### Option 1: Test Locally (Placeholder)

```bash
cd foundationpose
pip install -r requirements.txt
python app.py
# Visit http://localhost:7860
```

### Option 2: Deploy to HF (Placeholder)

```bash
./deploy.sh
# Or manually:
git add .
git commit -m "Deploy FoundationPose Space"
git push origin main
```

### Option 3: Deploy to HF (Real Mode)

**Requirements:**
1. Download weights from Google Drive
2. Set up git-lfs
3. Enable USE_REAL_MODEL=true

**Steps:**
```bash
# 1. Download weights (manual step)
# Visit: https://drive.google.com/drive/folders/1GCyGE-LbFGgRC-FuGsF3a1zeBuzsQ1Da
# Extract to: weights/2023-10-28-18-33-37/ and weights/2024-01-11-20-02-45/

# 2. Set up git-lfs
git lfs install
git lfs track "weights/**"
git add .gitattributes

# 3. Add weights
git add weights/
git commit -m "Add model weights"

# 4. Deploy
git push origin main

# 5. Set Space secret: USE_REAL_MODEL=true
```

---

## 🔗 Integration with robot-ml

### Update FoundationPose Wrapper

Edit `/training/nova_sim_trainer/perception/foundation_pose_wrapper.py`:

```python
from foundationpose.client import FoundationPoseClient
from pathlib import Path
import cv2

class FoundationPoseWrapper(PoseEstimator):
    def __init__(self, api_url: str, tracked_objects: List[Dict], **kwargs):
        super().__init__()
        self.client = FoundationPoseClient(api_url)
        self.object_ids = []

        # Initialize each tracked object
        for obj_config in tracked_objects:
            if not obj_config.get("enabled", True):
                continue

            object_id = obj_config["object_id"]
            ref_dir = Path(obj_config["reference_images_dir"])

            # Load reference images
            ref_images = []
            for img_path in sorted(ref_dir.glob("*.jpg")):
                img = cv2.imread(str(img_path))
                img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
                ref_images.append(img)

            # Register object with API
            logger.info(f"Registering {object_id} with {len(ref_images)} images...")
            self.client.initialize(object_id, ref_images)
            self.object_ids.append(object_id)

    def estimate_poses(self, frame, camera_intrinsics, scene_objects):
        detected_poses = []

        for object_id in self.object_ids:
            poses = self.client.estimate_pose(object_id, frame, camera_intrinsics)

            for pose in poses:
                detected_poses.append(DetectedPose(
                    object_id=pose["object_id"],
                    position=pose["position"],
                    orientation=pose["orientation"],
                    confidence=pose["confidence"],
                    timestamp=0.0,
                    dimensions=tuple(pose.get("dimensions", [0.1, 0.1, 0.1]))
                ))

        return detected_poses
```

### Update Configuration

Edit `/training/observations.yaml`:

```yaml
perception:
  enabled: true
  model: foundation_pose
  api_url: https://gpue-foundationpose.hf.space  # Your deployed Space URL
  camera: aux_top
  inference_fps: 5

  tracked_objects:
    - object_id: target_cube
      scene_object_name: t_object
      enabled: true
      reference_images_dir: ./perception/reference/target_cube
      dimensions: [0.1, 0.1, 0.1]
```

---

## ⚠️ What's NOT Done Yet

### Missing Pieces

1. **Model Weights**
   - Not included in repo (too large)
   - Must be downloaded manually
   - Requires git-lfs setup

2. **FoundationPose Repository**
   - Not included (git submodule or clone needed)
   - C++ extensions need to be built
   - Tested only with placeholder code

3. **BundleSDF Integration**
   - Mesh reconstruction not implemented
   - Currently uses placeholder cube mesh
   - Needed for model-free mode

4. **Segmentation**
   - Object segmentation uses placeholder mask
   - Should integrate SAM (Segment Anything Model)
   - Optional but improves accuracy

5. **Pose Visualization**
   - Estimation results don't show overlays yet
   - TODO: Render detected pose on query image
   - Would improve debugging

### Testing Status

- ✅ Placeholder mode tested locally
- ⚠️ Real mode NOT tested (no weights)
- ⚠️ API integration NOT tested end-to-end
- ⚠️ ZeroGPU behavior unknown (not deployed yet)

---

## 📊 Performance Expectations

### ZeroGPU Characteristics

- **Cold Start**: 15-30 seconds (GPU allocation + model loading)
- **Warm Inference**: 0.5-2 seconds per query
- **Free Tier**: Limited monthly usage
- **Timeout**: GPU allocation lasts for duration specified in decorator

### robot-ml Training Integration

**Not suitable for real-time training loop (30 Hz):**
- 5 Hz perception requires 200ms per frame
- ZeroGPU latency: 500ms-2s warm, 15-30s cold
- ❌ Too slow for synchronous training

**Suitable for:**
- ✅ Batch processing recorded episodes
- ✅ Validation and testing
- ✅ Demos and visualization
- ✅ Reference data collection

**Recommendation:**
- Use dummy estimator during training (reads ground truth from sim)
- Use FoundationPose API for validation/testing only
- Consider local GPU deployment for production

---

## 📝 Next Steps

### Immediate (Before Deployment)

1. **Test Locally**
   ```bash
   cd foundationpose
   python app.py
   # Test UI at http://localhost:7860
   ```

2. **Deploy Placeholder Mode**
   ```bash
   ./deploy.sh
   # Choose "N" for real mode
   ```

3. **Verify Space Works**
   - Visit https://huggingface.co/spaces/gpue/foundationpose
   - Test initialization with test images
   - Check logs for errors

### Short Term (With Weights)

1. **Download Model Weights**
   - Get from Google Drive (see DEPLOYMENT.md)
   - Extract to `weights/` directory

2. **Test Real Mode Locally**
   ```bash
   export USE_REAL_MODEL=true
   python app.py
   # Upload real reference images
   # Test pose estimation
   ```

3. **Deploy Real Mode**
   ```bash
   # Set up git-lfs
   git lfs track "weights/**"
   git add .gitattributes weights/
   git commit -m "Add model weights"
   git push

   # Set Space secret: USE_REAL_MODEL=true
   ```

### Long Term (Production)

1. **Optimize Performance**
   - Implement batch inference
   - Add caching for frequently used objects
   - Tune GPU duration parameters

2. **Improve Accuracy**
   - Integrate SAM for segmentation
   - Add depth image support
   - Implement BundleSDF reconstruction

3. **Production Deployment**
   - Consider dedicated GPU (RunPod, Modal, etc.)
   - Set up monitoring and logging
   - Implement retry logic and error handling

---

## 📚 Reference Links

- **FoundationPose GitHub**: https://github.com/NVlabs/FoundationPose
- **Research Paper**: https://arxiv.org/abs/2312.08344
- **Model Weights**: https://drive.google.com/drive/folders/1GCyGE-LbFGgRC-FuGsF3a1zeBuzsQ1Da
- **HF Spaces Docs**: https://huggingface.co/docs/hub/spaces
- **ZeroGPU Docs**: https://huggingface.co/docs/hub/spaces-gpus-zerogpu
- **Gradio Docs**: https://www.gradio.app/docs

---

## 🤝 Citation

If you use this in your work:

```bibtex
@inproceedings{wen2023foundationpose,
  title={FoundationPose: Unified 6D Pose Estimation and Tracking of Novel Objects},
  author={Wen, Bowen and Yang, Wei and Kautz, Jan and Birchfield, Stan},
  booktitle={CVPR},
  year={2024}
}
```

---

**Summary:** The FoundationPose Space is fully set up and ready to deploy. It defaults to placeholder mode (no GPU needed) for testing the API structure. To enable real pose estimation, you need to manually download the model weights and set USE_REAL_MODEL=true. The integration code for robot-ml is ready but untested without the actual weights.