bdck commited on
Commit
91a403a
Β·
verified Β·
1 Parent(s): 3196e80

Upload README.md

Browse files
Files changed (1) hide show
  1. README.md +169 -93
README.md CHANGED
@@ -1,118 +1,214 @@
 
 
 
 
 
 
1
  ---
2
- tags:
3
- - ml-intern
4
- ---
5
- # LightweightMR β€” Pure-Python Mesh Reconstruction
6
 
7
- Pure-Python reimplementation of **["High-Fidelity Lightweight Mesh Reconstruction from Point Clouds"](https://openaccess.thecvf.com/content/CVPR2025/papers/Zhang_High-Fidelity_Lightweight_Mesh_Reconstruction_from_Point_Clouds_CVPR_2025_paper.pdf)** (CVPR 2025 Highlight, Zhang et al.)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
- Input: **PLY / PCD / XYZ point cloud**
10
- Output: **Triangle mesh (PLY / OBJ)**
 
 
 
 
 
 
 
 
11
 
12
  ---
13
 
14
- ## Quick Start
 
 
15
 
16
  ```bash
17
- # Install
18
  pip install torch numpy scipy
 
 
 
19
 
20
- # Run
21
- python -m lightweightmr -i myscan.ply -o mesh.ply
 
 
 
 
 
 
22
  ```
23
 
24
- Or use the Python API:
 
 
 
25
 
26
- ```python
27
- from lightweightmr.optimize import Runner
 
28
 
29
- runner = Runner("myscan.ply", out_dir="./output", device="cpu")
30
- v, f = runner.run(mesh_path="mesh.ply")
31
- print(f"Mesh: {len(v)} vertices, {len(f)} faces")
32
  ```
33
 
 
 
34
  ---
35
 
36
- ## Two-Stage Pipeline
37
 
38
- 1. **SDF Learning** β€” Train a coordinate MLP with positional encoding to fit an implicit signed distance field to the point cloud.
39
- 2. **Vertex Generation + Delaunay Meshing** β€”
40
- - Sample surface queries using SDF gradient projection.
41
- - Train a vertex generator (MLP-only, no PointTransformerV3) to displace initial FPS samples.
42
- - Move vertices to the learned SDF surface.
43
- - Build a 3D Delaunay triangulation (`scipy.spatial.Delaunay`).
44
- - Label tetrahedra as inside/outside by sampling the SDF.
45
- - Extract the surface as facets between differently-labeled cells.
46
- - Post-process with midpoint-vertex insertion to fix non-manifold edges.
 
 
 
 
 
47
 
48
  ---
49
 
50
- ## Differences from Original
51
 
52
- | Feature | Original | This Reimplementation |
53
- |---------|----------|------------------------|
54
- | Hash encoding | CUDA hash grid + triplane | Positional encoding only (no CUDA compilation) |
55
- | Vertex generator | PointTransformerV3 + MLP | MLP-only (faster, no `spconv`/`torch_scatter`) |
56
- | KDTree | C++ libkdtree | `scipy.spatial.KDTree` |
57
- | Delaunay meshing | CGAL C++ binary | `scipy.spatial.Delaunay` |
58
- | Mesh extraction | CGAL `create_mesh` | Pure Python facet extraction |
59
- | Dependencies | Open3D, CGAL, boost, `fpsample`, `mcubes`, `trimesh`, `torch_scatter`, `spconv` | **Only torch, numpy, scipy** |
 
 
 
60
 
61
- **Trade-off**: Without the original hash encoding, the SDF stage converges slightly slower and may need a few more iterations on highly detailed scans. The meshing quality is comparable for typical genus-0/1 shapes.
62
 
63
- ---
 
 
 
64
 
65
- ## CLI Reference
 
 
 
66
 
 
 
 
67
  ```
68
- python -m lightweightmr -i INPUT.ply -o OUTPUT.ply [options]
69
-
70
- Options:
71
- --device cpu | cuda (default: cpu)
72
- --sdf-iters 20000 SDF training iterations
73
- --vg-iters 8000 Vertex generator iterations
74
- --sdf-lr 0.001
75
- --vg-lr 0.001
76
- --sdf-batch 5000 Batch size for SDF queries
77
- --vertices 3400 Target vertex count
78
- --update-size 5 Curriculum update steps
79
- --update-ratio 1.2 Vertex count growth ratio
80
- --k-samples 21 Interior samples per tetrahedron
81
- --multires 8 Positional encoding frequencies
82
- --project-sdf-level 0.0 Surface SDF level
83
- --save-freq 2000 Checkpoint frequency
84
- --resume-sdf PATH.pth Resume from SDF checkpoint
 
 
 
 
 
 
 
 
 
85
  ```
86
 
87
  ---
88
 
89
- ## Package Structure
 
 
90
 
91
  ```
92
- lightweightmr/
93
- __init__.py
94
- embedder.py β€” Positional encoding (NeRF-style)
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
- ## Tips
107
 
108
- - **CPU-only is slow** β€” SDF training on CPU with 20k iterations takes ~30–60 min depending on your machine. If you have a GPU, use `--device cuda`.
109
- - **Vertex count** β€” Increase `--vertices` for finer detail (slower meshing). Decrease for faster/cleaner low-poly results.
110
- - **Noise** β€” If your point cloud is noisy, increase `--sdf-iters` to 30k+ and use a small `--project-sdf-level` (e.g. `0.001`) to pull slightly inward.
111
- - **Large clouds** β€” The code automatically subsamples to ~1/60th of input points for the SDF training set. For very large scans, reduce `--queries-size`.
 
 
 
112
 
113
  ---
114
 
115
- ## Citation
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.