Upload README.md
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171188 unmodified lines127128129130131132133134135136137138139140141142143144145146147148149---tags:- ml-intern---# LightweightMR β Pure-Python Mesh ReconstructionPure-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.)Input: **PLY / PCD / XYZ point cloud**Output: **Triangle mesh (PLY / OBJ)**---## Quick Start```bash# Installpip install torch numpy scipy# Runpython -m lightweightmr -i myscan.ply -o mesh.ply```Or use the Python API:```pythonfrom lightweightmr.optimize import Runnerrunner = Runner("myscan.ply", out_dir="./output", device="cpu")v, f = runner.run(mesh_path="mesh.ply")print(f"Mesh: {len(v)} vertices, {len(f)} faces")```---## Two-Stage Pipeline1. **SDF Learning** β Train a coordinate MLP with positional encoding to fit an implicit signed distance field to the point cloud.2. **Vertex Generation + Delaunay Meshing** β- Sample surface queries using SDF gradient projection.- Train a vertex generator (MLP-only, no PointTransformerV3) to displace initial FPS samples.- Move vertices to the learned SDF surface.- Build a 3D Delaunay triangulation (`scipy.spatial.Delaunay`).- Label tetrahedra as inside/outside by sampling the SDF.- Extract the surface as facets between differently-labeled cells.- Post-process with midpoint-vertex insertion to fix non-manifold edges.---## Differences from Original| Feature | Original | This Reimplementation ||---------|----------|------------------------|| Hash encoding | CUDA hash grid + triplane | Positional encoding only (no CUDA compilation) || Vertex generator | PointTransformerV3 + MLP | MLP-only (faster, no `spconv`/`torch_scatter`) || KDTree | C++ libkdtree | `scipy.spatial.KDTree` || Delaunay meshing | CGAL C++ binary | `scipy.spatial.Delaunay` || Mesh extraction | CGAL `create_mesh` | Pure Python facet extraction || Dependencies | Open3D, CGAL, boost, `fpsample`, `mcubes`, `trimesh`, `torch_scatter`, `spconv` | **Only torch, numpy, scipy** |**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.---## CLI Reference```python -m lightweightmr -i INPUT.ply -o OUTPUT.ply [options]Options:--device cpu | cuda (default: cpu)--sdf-iters 20000 SDF training iterations--vg-iters 8000 Vertex generator iterations--sdf-lr 0.001--vg-lr 0.001--sdf-batch 5000 Batch size for SDF queries--vertices 3400 Target vertex count--update-size 5 Curriculum update steps--update-ratio 1.2 Vertex count growth ratio--k-samples 21 Interior samples per tetrahedron--multires 8 Positional encoding frequencies--project-sdf-level 0.0 Surface SDF level--save-freq 2000 Checkpoint frequency--resume-sdf PATH.pth Resume from SDF checkpoint```---## Package Structure```lightweightmr/__init__.pyembedder.py β Positional encoding (NeRF-style)sdfnet.py β SDF MLP networkvgnet.py β Vertex generator MLPlosses.py β All loss functions (Chamfer, eikonal, divergence, curvature, normals)meshing.py β Delaunay + SDF labeling + surface extraction + midpoint fixio_utils.py β PLY/PCD/XYZ loaders, mesh exporters, FPS, normal estimationoptimize.py β Two-stage Runner (SDF then VG + meshing)__main__.py β CLI entry point```---## Tips- **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`.- **Vertex count** β Increase `--vertices` for finer detail (slower meshing). Decrease for faster/cleaner low-poly results.- **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.- **Large clouds** β The code automatically subsamples to ~1/60th of input points for the SDF training set. For very large scans, reduce `--queries-size`.---## Citation```bibtex@inproceedings{zhang2025high,8 unmodified lines---License: MIT (reimplementation). Original paper and code Β© authors.<!-- ml-intern-provenance -->## Generated by ML InternThis 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.- Try ML Intern: https://smolagents-ml-intern.hf.space- Source code: https://github.com/huggingface/ml-intern## Usage```pythonfrom transformers import AutoModelForCausalLM, AutoTokenizermodel_id = "bdck/lightweightmr"tokenizer = AutoTokenizer.from_pretrained(model_id)model = AutoModelForCausalLM.from_pretrained(model_id)```For non-causal architectures, replace `AutoModelForCausalLM` with the appropriate `AutoModel` class.1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132148 unmodified lines223224225# LightweightMR β Mesh from Point Cloud (Beginner Guide)> **TL;DR** β Give it a `.ply` / `.pcd` / `.xyz` file full of 3D points, and it spits out a nice triangle mesh (`.ply` or `.obj`).>> Only depends on **PyTorch + NumPy + SciPy**. No CUDA compiling, no Open3D, no CGAL.---## π§ What does this actually do?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.It does this in **two stages**:```Point Cloud Stage 1: Learn SDF Stage 2: Mesh(just dots) β (learn a "distance field") β (triangles!)```### Stage 1 β Learning a Distance Field (SDF)The code trains a small neural network to answer:> *"For any random 3D point, how far is it from the surface, and which side is it on?"*Positive = outside, negative = inside, zero = exactly on the surface.It learns this purely from your point cloud β no camera images, no manual labels.### Stage 2 β Building the MeshNow that the network knows inside vs. outside, the code:1. Sprinkles candidate vertices near the surface2. Uses another tiny network to nudge them onto high-detail areas (curvature)3. Projects them exactly onto the zero-distance surface4. Builds a **3D Delaunay triangulation** (like connecting dots with tetrahedra)5. Labels each tetrahedron as "inside" or "outside"6. The walls between inside/outside *are* your surface β extracted as triangles7. Cleans up non-manifold edges by adding midpoints---## π Quick Start (5 minutes)### 1. Install```bashpip install torch numpy scipy```That's it. No C++ compilers, no 2 GB dependencies.### 2. Try on a synthetic sphere (no data needed)We included a tiny script that makes a fake point cloud so you can see it work immediately:```bash# Download / clone the repo files, then:python example/make_sphere.py # creates example/sphere.ply (3000 points)python -m lightweightmr -i example/sphere.ply -o example/sphere_mesh.ply --device cpu```The second command will:- Print progress bars for SDF training (~20k steps)- Print progress bars for vertex generation (~8k steps)- Save `example/sphere_mesh.ply`**On CPU this takes ~20β40 minutes.** On a CUDA GPU (`--device cuda`) it's ~2β4 minutes.### 3. Use your own scan```bashpython -m lightweightmr -i myscan.ply -o mymesh.ply --device cpu```Supported inputs: `.ply` (ASCII or binary), `.pcd`, `.xyz`---## π What files do I need?You only need the `lightweightmr/` folder (9 Python files). Nothing else.```lightweightmr/__init__.py # package marker__main__.py # CLI (the command you run)optimize.py # the two-stage runner (Stage 1 + Stage 2)sdfnet.py # neural network for distance fieldvgnet.py # neural network for vertex placementlosses.py # math that teaches the networksmeshing.py # Delaunay + surface extractionembedder.py # positional encoding (helps the networks)io_utils.py # loading PLY/PCD/XYZ, saving meshes```---## βοΈ CLI Options Explained| Flag | Default | What it means ||------|---------|---------------|| `-i` / `--input` | **required** | Your point cloud file || `-o` / `--output` | **required** | Output mesh file (`.ply` or `.obj`) || `--device` | `cpu` | `cpu` or `cuda`. GPU is much faster. || `--sdf-iters` | `20000` | How long to train the distance field. More = better quality on noisy scans. || `--vg-iters` | `8000` | How long to train vertex placement. || `--vertices` | `3400` | Target number of vertices in final mesh. More = finer detail, slower. || `--k-samples` | `21` | Samples per tetrahedron when labeling inside/outside. Higher = cleaner mesh, slower. || `--save-freq` | `2000` | Save a checkpoint every N iterations (so you can resume). || `--resume-sdf` | β | Path to a `.pth` checkpoint to skip Stage 1. |### Common recipes**Fast preview (lower quality):**```bashpython -m lightweightmr -i scan.ply -o mesh.ply --sdf-iters 5000 --vg-iters 2000 --vertices 800```**High quality (slower):**```bashpython -m lightweightmr -i scan.ply -o mesh.ply --sdf-iters 40000 --vg-iters 12000 --vertices 10000```**Resume after Stage 1 crash:**```bashpython -m lightweightmr -i scan.ply -o mesh.ply --resume-sdf output/sdf_checkpoints/sdf_final.pth```---## π Python API (for scripts)If you want to call it from your own code instead of the command line:```pythonfrom lightweightmr.optimize import Runnerrunner = Runner(pointcloud_path="myscan.ply",out_dir="./output",device="cpu", # or "cuda"sdf_iters=20_000,vg_iters=8_000,vertices_size=3_400,)# Run both stagesvertices, faces = runner.run(mesh_path="mymesh.ply")# Or run stages separately:runner.train_sdf() # Stage 1verts = runner.train_vg() # Stage 2v, f = runner.generate_mesh(verts, save_path="mymesh.ply")```---## π§ͺ Understanding the OutputAfter running, you'll see a new folder `./output/` with:```output/sdf_checkpoints/sdf_final.pth # trained distance field (can resume from this)```And your chosen output file (`-o mesh.ply`) contains the mesh.You can view `.ply` meshes with:- **Blender** (free, drag & drop)- **MeshLab** (free)- **Windows 3D Viewer**---## π οΈ Troubleshooting| Problem | Likely cause | Fix ||---------|--------------|-----|| Takes forever | CPU training | Use `--device cuda` if you have a GPU || Output mesh has holes | Not enough vertices | Increase `--vertices` || Noisy / wobbly mesh | Noisy input + too few SDF iters | Increase `--sdf-iters` to `30000+` || `ModuleNotFoundError` | Missing dependency | `pip install torch numpy scipy` || `ValueError` on `.ply` | Binary PLY variant we don't parse | Convert to ASCII PLY in MeshLab/Blender |---## π How is this different from the original paper?The original CVPR 2025 code is **powerful but heavy** β it needs:- CUDA-compiled hash encoders- CGAL (C++ geometry library)- Open3D, `torch_scatter`, `spconv`, `fpsample`, `mcubes`, `trimesh`This reimplementation replaces all of that with pure Python + PyTorch + SciPy:| Original | This version ||----------|--------------|| CUDA hash grid | Positional encoding (slower but no compile) || PointTransformerV3 vertex generator | Simple MLP (faster, no extra deps) || CGAL Delaunay + meshing | SciPy Delaunay + our own surface extractor || C++ KDTree | SciPy KDTree |**Trade-off:** The SDF stage may need a few more iterations on very detailed scans, but the output quality is comparable for most shapes.---## π CitationIf you use this, cite the original paper:```bibtex@inproceedings{zhang2025high,8 unmodified lines---License: MIT (reimplementation). Original paper and code Β© authors.
README.md+169-93