Upload README.md
Browse files
README.md
CHANGED
|
@@ -1,118 +1,214 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
-
tags:
|
| 3 |
-
- ml-intern
|
| 4 |
-
---
|
| 5 |
-
# LightweightMR β Pure-Python Mesh Reconstruction
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
---
|
| 13 |
|
| 14 |
-
## Quick Start
|
|
|
|
|
|
|
| 15 |
|
| 16 |
```bash
|
| 17 |
-
# Install
|
| 18 |
pip install torch numpy scipy
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
```
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
``
|
| 27 |
-
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
print(f"Mesh: {len(v)} vertices, {len(f)} faces")
|
| 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 |
-
sdfnet.py β SDF MLP network
|
| 96 |
-
vgnet.py β Vertex generator MLP
|
| 97 |
-
losses.py β All loss functions (Chamfer, eikonal, divergence, curvature, normals)
|
| 98 |
-
meshing.py β Delaunay + SDF labeling + surface extraction + midpoint fix
|
| 99 |
-
io_utils.py β PLY/PCD/XYZ loaders, mesh exporters, FPS, normal estimation
|
| 100 |
-
optimize.py β Two-stage Runner (SDF then VG + meshing)
|
| 101 |
-
__main__.py β CLI entry point
|
| 102 |
```
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
---
|
| 105 |
|
| 106 |
-
##
|
| 107 |
|
| 108 |
-
|
| 109 |
-
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
---
|
| 114 |
|
| 115 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
```bibtex
|
| 118 |
@inproceedings{zhang2025high,
|
|
@@ -127,23 +223,3 @@ lightweightmr/
|
|
| 127 |
---
|
| 128 |
|
| 129 |
License: MIT (reimplementation). Original paper and code Β© authors.
|
| 130 |
-
|
| 131 |
-
<!-- ml-intern-provenance -->
|
| 132 |
-
## Generated by ML Intern
|
| 133 |
-
|
| 134 |
-
This model repository was generated by [ML Intern](https://github.com/huggingface/ml-intern), an agent for machine learning research and development on the Hugging Face Hub.
|
| 135 |
-
|
| 136 |
-
- Try ML Intern: https://smolagents-ml-intern.hf.space
|
| 137 |
-
- Source code: https://github.com/huggingface/ml-intern
|
| 138 |
-
|
| 139 |
-
## Usage
|
| 140 |
-
|
| 141 |
-
```python
|
| 142 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 143 |
-
|
| 144 |
-
model_id = "bdck/lightweightmr"
|
| 145 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 146 |
-
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 147 |
-
```
|
| 148 |
-
|
| 149 |
-
For non-causal architectures, replace `AutoModelForCausalLM` with the appropriate `AutoModel` class.
|
|
|
|
| 1 |
+
# LightweightMR β Mesh from Point Cloud (Beginner Guide)
|
| 2 |
+
|
| 3 |
+
> **TL;DR** β Give it a `.ply` / `.pcd` / `.xyz` file full of 3D points, and it spits out a nice triangle mesh (`.ply` or `.obj`).
|
| 4 |
+
>
|
| 5 |
+
> Only depends on **PyTorch + NumPy + SciPy**. No CUDA compiling, no Open3D, no CGAL.
|
| 6 |
+
|
| 7 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
## π§ What does this actually do?
|
| 10 |
+
|
| 11 |
+
Imagine you have a laser scan of a statue β millions of dots floating in space. This code turns those dots into a **solid surface** made of triangles.
|
| 12 |
+
|
| 13 |
+
It does this in **two stages**:
|
| 14 |
+
|
| 15 |
+
```
|
| 16 |
+
Point Cloud Stage 1: Learn SDF Stage 2: Mesh
|
| 17 |
+
(just dots) β (learn a "distance field") β (triangles!)
|
| 18 |
+
```
|
| 19 |
+
|
| 20 |
+
### Stage 1 β Learning a Distance Field (SDF)
|
| 21 |
+
The code trains a small neural network to answer:
|
| 22 |
+
|
| 23 |
+
> *"For any random 3D point, how far is it from the surface, and which side is it on?"*
|
| 24 |
+
|
| 25 |
+
Positive = outside, negative = inside, zero = exactly on the surface.
|
| 26 |
+
|
| 27 |
+
It learns this purely from your point cloud β no camera images, no manual labels.
|
| 28 |
|
| 29 |
+
### Stage 2 β Building the Mesh
|
| 30 |
+
Now that the network knows inside vs. outside, the code:
|
| 31 |
+
|
| 32 |
+
1. Sprinkles candidate vertices near the surface
|
| 33 |
+
2. Uses another tiny network to nudge them onto high-detail areas (curvature)
|
| 34 |
+
3. Projects them exactly onto the zero-distance surface
|
| 35 |
+
4. Builds a **3D Delaunay triangulation** (like connecting dots with tetrahedra)
|
| 36 |
+
5. Labels each tetrahedron as "inside" or "outside"
|
| 37 |
+
6. The walls between inside/outside *are* your surface β extracted as triangles
|
| 38 |
+
7. Cleans up non-manifold edges by adding midpoints
|
| 39 |
|
| 40 |
---
|
| 41 |
|
| 42 |
+
## π Quick Start (5 minutes)
|
| 43 |
+
|
| 44 |
+
### 1. Install
|
| 45 |
|
| 46 |
```bash
|
|
|
|
| 47 |
pip install torch numpy scipy
|
| 48 |
+
```
|
| 49 |
+
|
| 50 |
+
That's it. No C++ compilers, no 2 GB dependencies.
|
| 51 |
|
| 52 |
+
### 2. Try on a synthetic sphere (no data needed)
|
| 53 |
+
|
| 54 |
+
We included a tiny script that makes a fake point cloud so you can see it work immediately:
|
| 55 |
+
|
| 56 |
+
```bash
|
| 57 |
+
# Download / clone the repo files, then:
|
| 58 |
+
python example/make_sphere.py # creates example/sphere.ply (3000 points)
|
| 59 |
+
python -m lightweightmr -i example/sphere.ply -o example/sphere_mesh.ply --device cpu
|
| 60 |
```
|
| 61 |
|
| 62 |
+
The second command will:
|
| 63 |
+
- Print progress bars for SDF training (~20k steps)
|
| 64 |
+
- Print progress bars for vertex generation (~8k steps)
|
| 65 |
+
- Save `example/sphere_mesh.ply`
|
| 66 |
|
| 67 |
+
**On CPU this takes ~20β40 minutes.** On a CUDA GPU (`--device cuda`) it's ~2β4 minutes.
|
| 68 |
+
|
| 69 |
+
### 3. Use your own scan
|
| 70 |
|
| 71 |
+
```bash
|
| 72 |
+
python -m lightweightmr -i myscan.ply -o mymesh.ply --device cpu
|
|
|
|
| 73 |
```
|
| 74 |
|
| 75 |
+
Supported inputs: `.ply` (ASCII or binary), `.pcd`, `.xyz`
|
| 76 |
+
|
| 77 |
---
|
| 78 |
|
| 79 |
+
## π What files do I need?
|
| 80 |
|
| 81 |
+
You only need the `lightweightmr/` folder (9 Python files). Nothing else.
|
| 82 |
+
|
| 83 |
+
```
|
| 84 |
+
lightweightmr/
|
| 85 |
+
__init__.py # package marker
|
| 86 |
+
__main__.py # CLI (the command you run)
|
| 87 |
+
optimize.py # the two-stage runner (Stage 1 + Stage 2)
|
| 88 |
+
sdfnet.py # neural network for distance field
|
| 89 |
+
vgnet.py # neural network for vertex placement
|
| 90 |
+
losses.py # math that teaches the networks
|
| 91 |
+
meshing.py # Delaunay + surface extraction
|
| 92 |
+
embedder.py # positional encoding (helps the networks)
|
| 93 |
+
io_utils.py # loading PLY/PCD/XYZ, saving meshes
|
| 94 |
+
```
|
| 95 |
|
| 96 |
---
|
| 97 |
|
| 98 |
+
## βοΈ CLI Options Explained
|
| 99 |
|
| 100 |
+
| Flag | Default | What it means |
|
| 101 |
+
|------|---------|---------------|
|
| 102 |
+
| `-i` / `--input` | **required** | Your point cloud file |
|
| 103 |
+
| `-o` / `--output` | **required** | Output mesh file (`.ply` or `.obj`) |
|
| 104 |
+
| `--device` | `cpu` | `cpu` or `cuda`. GPU is much faster. |
|
| 105 |
+
| `--sdf-iters` | `20000` | How long to train the distance field. More = better quality on noisy scans. |
|
| 106 |
+
| `--vg-iters` | `8000` | How long to train vertex placement. |
|
| 107 |
+
| `--vertices` | `3400` | Target number of vertices in final mesh. More = finer detail, slower. |
|
| 108 |
+
| `--k-samples` | `21` | Samples per tetrahedron when labeling inside/outside. Higher = cleaner mesh, slower. |
|
| 109 |
+
| `--save-freq` | `2000` | Save a checkpoint every N iterations (so you can resume). |
|
| 110 |
+
| `--resume-sdf` | β | Path to a `.pth` checkpoint to skip Stage 1. |
|
| 111 |
|
| 112 |
+
### Common recipes
|
| 113 |
|
| 114 |
+
**Fast preview (lower quality):**
|
| 115 |
+
```bash
|
| 116 |
+
python -m lightweightmr -i scan.ply -o mesh.ply --sdf-iters 5000 --vg-iters 2000 --vertices 800
|
| 117 |
+
```
|
| 118 |
|
| 119 |
+
**High quality (slower):**
|
| 120 |
+
```bash
|
| 121 |
+
python -m lightweightmr -i scan.ply -o mesh.ply --sdf-iters 40000 --vg-iters 12000 --vertices 10000
|
| 122 |
+
```
|
| 123 |
|
| 124 |
+
**Resume after Stage 1 crash:**
|
| 125 |
+
```bash
|
| 126 |
+
python -m lightweightmr -i scan.ply -o mesh.ply --resume-sdf output/sdf_checkpoints/sdf_final.pth
|
| 127 |
```
|
| 128 |
+
|
| 129 |
+
---
|
| 130 |
+
|
| 131 |
+
## π Python API (for scripts)
|
| 132 |
+
|
| 133 |
+
If you want to call it from your own code instead of the command line:
|
| 134 |
+
|
| 135 |
+
```python
|
| 136 |
+
from lightweightmr.optimize import Runner
|
| 137 |
+
|
| 138 |
+
runner = Runner(
|
| 139 |
+
pointcloud_path="myscan.ply",
|
| 140 |
+
out_dir="./output",
|
| 141 |
+
device="cpu", # or "cuda"
|
| 142 |
+
sdf_iters=20_000,
|
| 143 |
+
vg_iters=8_000,
|
| 144 |
+
vertices_size=3_400,
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
# Run both stages
|
| 148 |
+
vertices, faces = runner.run(mesh_path="mymesh.ply")
|
| 149 |
+
|
| 150 |
+
# Or run stages separately:
|
| 151 |
+
runner.train_sdf() # Stage 1
|
| 152 |
+
verts = runner.train_vg() # Stage 2
|
| 153 |
+
v, f = runner.generate_mesh(verts, save_path="mymesh.ply")
|
| 154 |
```
|
| 155 |
|
| 156 |
---
|
| 157 |
|
| 158 |
+
## π§ͺ Understanding the Output
|
| 159 |
+
|
| 160 |
+
After running, you'll see a new folder `./output/` with:
|
| 161 |
|
| 162 |
```
|
| 163 |
+
output/
|
| 164 |
+
sdf_checkpoints/
|
| 165 |
+
sdf_final.pth # trained distance field (can resume from this)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
```
|
| 167 |
|
| 168 |
+
And your chosen output file (`-o mesh.ply`) contains the mesh.
|
| 169 |
+
|
| 170 |
+
You can view `.ply` meshes with:
|
| 171 |
+
- **Blender** (free, drag & drop)
|
| 172 |
+
- **MeshLab** (free)
|
| 173 |
+
- **Windows 3D Viewer**
|
| 174 |
+
|
| 175 |
---
|
| 176 |
|
| 177 |
+
## π οΈ Troubleshooting
|
| 178 |
|
| 179 |
+
| Problem | Likely cause | Fix |
|
| 180 |
+
|---------|--------------|-----|
|
| 181 |
+
| Takes forever | CPU training | Use `--device cuda` if you have a GPU |
|
| 182 |
+
| Output mesh has holes | Not enough vertices | Increase `--vertices` |
|
| 183 |
+
| Noisy / wobbly mesh | Noisy input + too few SDF iters | Increase `--sdf-iters` to `30000+` |
|
| 184 |
+
| `ModuleNotFoundError` | Missing dependency | `pip install torch numpy scipy` |
|
| 185 |
+
| `ValueError` on `.ply` | Binary PLY variant we don't parse | Convert to ASCII PLY in MeshLab/Blender |
|
| 186 |
|
| 187 |
---
|
| 188 |
|
| 189 |
+
## π How is this different from the original paper?
|
| 190 |
+
|
| 191 |
+
The original CVPR 2025 code is **powerful but heavy** β it needs:
|
| 192 |
+
- CUDA-compiled hash encoders
|
| 193 |
+
- CGAL (C++ geometry library)
|
| 194 |
+
- Open3D, `torch_scatter`, `spconv`, `fpsample`, `mcubes`, `trimesh`
|
| 195 |
+
|
| 196 |
+
This reimplementation replaces all of that with pure Python + PyTorch + SciPy:
|
| 197 |
+
|
| 198 |
+
| Original | This version |
|
| 199 |
+
|----------|--------------|
|
| 200 |
+
| CUDA hash grid | Positional encoding (slower but no compile) |
|
| 201 |
+
| PointTransformerV3 vertex generator | Simple MLP (faster, no extra deps) |
|
| 202 |
+
| CGAL Delaunay + meshing | SciPy Delaunay + our own surface extractor |
|
| 203 |
+
| C++ KDTree | SciPy KDTree |
|
| 204 |
+
|
| 205 |
+
**Trade-off:** The SDF stage may need a few more iterations on very detailed scans, but the output quality is comparable for most shapes.
|
| 206 |
+
|
| 207 |
+
---
|
| 208 |
+
|
| 209 |
+
## π Citation
|
| 210 |
+
|
| 211 |
+
If you use this, cite the original paper:
|
| 212 |
|
| 213 |
```bibtex
|
| 214 |
@inproceedings{zhang2025high,
|
|
|
|
| 223 |
---
|
| 224 |
|
| 225 |
License: MIT (reimplementation). Original paper and code Β© authors.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|