This project offer a full framework for the 3D Cell Surface Reconstruction from Fluorescence Microscopy
Before starting, we recommend creating a new conda environment or a virtual environment with Python 3.10+.
conda create -y -n im2mesh -c conda-forge python=3.11
conda activate im2meshThis project is implemented within our Im2Im Transformation framework for training/inferece (only in the case of the Unet models).
pip install "mmv_im2im[all] @ git+https://github.com/MMV-Lab/mmv_im2im.git"Clone the actuall repo and install the requirements
git clone https://github.com/MMV-Lab/im2mesh.git
cd im2mesh
pip install -r requirements.txt For Training proposes we use the nucleus morphological dataset provided by the Allen Institute
In aim to get the data we provide the scrip based on the provided for the Allen Institute in the official page.
python core/get_allancell_data.py This dataset contain the multi cell images and the corresponding sementation generated by watershed algorithm
The original nucleus morphological dataset also provide some model segmentation but there are just 18 examples with smoother segmentations in comparation with the watershed, we inlcude the training option for use the cellpose model.
To get and parse the data set ready for training with cellpose just run:
python core/get_cellpose_training_dataset.py --path_files /path/to/the/raw/imagesTo run your own cellpose training, set your desired parameters and run:
python core/cellpose_training.py --train_dir /path/to/dataset --n_epochs 100 --learning_rate 1e-5 --weight_decay 0.1 --batch_size 4We also provide the scrip that take the raw cell images and use cellpose to generate segmentations, you can try the pre-trained models.
python core/cellpose_segmentation.py --input_path /path/to/raw/images/ --trained_model path/to/trained/modelWe use a Unet like architecture (Attention/nn/Probabilistic) in order to learn the spherical harmonics decopisition for the mesh generation, it's called explicit due the posibility of obtain the coefficents of the decomposition, this models imposes no constraint on the spatial resolution of input volumes (variable shape training/inference).
This model is designed to compress complex 3D spatial data into set of continuous numerical coefficients, this models imposes no constraint on the spatial resolution of input volumes (variable shape training/inference). It does this in three distinct phases:
3D Feature Extraction, the network takes a 3D volume input and passes it through a series of 3D convolutional and residual blocks. Each step downsamples the spatial size while increasing the number of feature channels. Finally, global pooling and a fully connected layer condense the entire 3D space into a compact 1D latent vector.
Harmonic Transformation Instead of passing the vector straight to the output, it enters a sequence of three HarmonicOscillatorLayers. This block remaps the latent features into a periodic, frequency-based representation space.
Final Regression, A standard linear layer maps these refined harmonic features directly to the final output vector.
An harmonic oscillator describes a system that moves periodically (like a pendulum or a vibrating string), which is mathematically represented by sine and cosine waves.This architecture translates that physical concept into deep learning by replacing traditional activation functions with a sinusoidal activation function.
This offer a Physics-Informed model capable of learn the spheric decomposition in a implicit way that means that we don't have the explicit acces to the spherical harmonics coefficents, this allow us to predict directly a 3D mesh representation.
HarmoMeshNet reconstructs the 3D triangular mesh surface of biological cells directly from a fluorescence microscopy volume and imposes no constraint on the spatial resolution of input volumes (variable shape training/inference).
Since batch_size=1 is mandatory, the --accumulate_grad_batches N flag is
the only way to approximate a larger effective batch.
Both input volumes and output meshes operate in a shared [-1, 1] coordinate
space. Ground-truth mesh vertices are normalised at load time:
This ensures that grid_sample coordinates computed from predicted vertex
positions remain within the valid sampling range.
Any smooth, star-shaped closed surface can be expressed over the unit sphere
In matrix form for
Initialisation via least-squares projection of the unit sphere:
The Laplace–Beltrami operator on
This eigenvalue is the spring constant in the harmonic oscillator analogy: low-degree modes (global shape) are soft; high-degree modes (wrinkles) are stiff. The mode energy regulariser:
is the
The Willmore functional
since
| Term | Default weight | Description |
|---|---|---|
| 100 | Bidirectional point-cloud distance | |
| 50 | Normal consistency across adjacent faces | |
| 50 | Edge-length variance | |
| 50 | Global Laplacian fairness | |
| 0.01 | Discrete |
|
| 0.001 |
|
To generate the dataset used for train the explicit architectures the raw/segmentation multicell files are required and the dataset it's generated running:
Note: On the case of generate custome segmentations provide the corresponding paths to them.
python core/generate_mesh_dataset.py --image_files /path/to/raw/images/ --segentation_files path/to/segmentation/files --only_full_cell --save_seg --save_mesh --correct_seg --p_testset 0.2This generate the dataset and also the dataset_creation_log.txt file that contains some spatial information about the generated files and the global_info_dataset.txt with the global spatial information, useful for hyperparameter election:
📂im2mesh_dataset/
├── 📄 dataset_creation_log.txt
├── 📄 global_info_dataset.text
├── 📂training_set/
├── 📂training_set/
├── 🖼️ img1_IM.tiff # 3D volumes .tif / .tiff (any spatial size)
├── 📄 img1_GT.npy #spherical represemtation gt vector
├── 📂test_set/
├── 🖼️ img1_IM.tiff # 3D volumes .tif / .tiff (any spatial size)
├── 📄 img1_GT.npy #spherical represemtation gt vector
├── 📂meshes/
├── 📄 img1.obj # GT surfaces(meshes) .obj / .stl / .ply / .vtk
With the previous dataset generated
We can generate the harmmesh_dataset for implicit model training, running:
python core/generate_harmmesh_dataset.py --dataset_file /path/to/generated_dataset This code will generate the following file system structure:
📂 harmmesh_dataset/
├── 📂train/
├── 📂images/
├── 🖼️ img1_IM.tiff # 3D volumes .tif / .tiff (any spatial size)
├── 📂meshes/
├── 📄 img1.obj # GT surfaces(meshes) .obj / .stl / .ply / .vtk
├── 📂test/
├── 📂images/
├── 🖼️ img1_IM.tiff # 3D volumes .tif / .tiff (any spatial size)
├── 📂meshes/
├── 📄 img1.obj # GT surfaces(meshes) .obj / .stl / .ply / .vtk
Three different architectures are currently ready for be trained (Attention_Unet/nn_Unet/Probabilistic_Unet) in order to train the corresponding train yaml template files are provided, you can edit the desired training configuration, save the changes and run:
run_im2im --config /path/to/the/yamlThe output structure will produce a version_x folder (one for each training):
📂 lightning_logs/
├── 📂version_x/
├── 📂checkpoints/ # weights of the model during training
├── 📄 weights_1.ckpt
├── 📄 weights_n.ckpt
├── 📄 data_config.yaml # yaml files with the configuration used for training
├── 📄 model_config.yaml
├── 📄 train_config.yaml
├── 📄 hparams.yaml
├── 📄 events.out.tfevents # tf file with the logs for training plot visualization
For training the model run the comand:
python core/train_HarmonicRegressionNet.py --data_path path/to/dataset --val_split 0.2 --optional_id initial_training --n_start_filters 32 --latent_dim 512 --target_dim 2503 --lr 0.0001 --warmup_ratio 0.05 --warmup_lr_start 1e-06 --n_epoch 3000 --accumulate_grad_batches 6 --loss_type weighted_mse --ckpts_interval 10 --patience 90The output structure :
📂 HarmonicRegressionNet_trainings/
├── 📂training_id/
├── 📂ckpts/ # weights of the model during training
├── 📂 model/
├── 📄 best_model.ckpt
├── 📄 model_epochn.ckpt
├── 📂logs/ # txt files with logs for training plot visualization
├── 📄 train_epoch.txt
├── 📄 train_step.txt
├── 📄 val_epoch.txt
├── 📄 inference_args.txt # model training configuration
├── 📄 training_execution_config.txt # training configuration used
For training the model run:
python core/train_HarmoMeshNet.py --data_path path/to/dataset --val_split 0.2 --optional_id initial_training --n_start_filters 32 --latent_dim 512 --max_sh_degree 8 --sphere_subdivisions 4 --refiner_steps 3 --refiner_layers 3 --refiner_hidden 64 --n_smooth 1 --lambd 0.5 --lr 0.0002 --n_epoch 3000 --accumulate_grad_batches 6 --ckpts_interval 10 --patience 90 --chamfer_scale 1.0 --normal_consistency_scale 0.05 --edge_length_scale 0.3 --laplacian_smoothing_scale 0.3 --willmore_scale 5e-05 --mode_energy_scale 0.0001 --num_workers 4The output structure :
📂 HarmoMeshNet_trainings/
├── 📂training_id/
├── 📂ckpts/ # weights of the model during training
├── 📂 model/
├── 📄 best_model.ckpt
├── 📄 model_epochn.ckpt
├── 📂logs/ # txt files with logs for training plot visualization
├── 📄 train_epoch.txt
├── 📄 train_step.txt
├── 📄 val_epoch.txt
├── 📄 inference_args.txt # model training configuration
├── 📄 training_execution_config.txt # training configuration used
Having the model weights of your trained model, configure the inference yaml and run:
python core/Unet_inference.py --yaml_inference /path/to/yaml --mode_prediction multicell2multicell --save_detection --only_full_cells The training script generate the inference_args.txt file with the parameters need for the model configuration (same used for training).
For inference with the model run:
python core/HarmonicRegressionNet_inference.py --input_dir path/to/test/images/folder --weights_path path/to/trained/model/.pt --inference_mode multicell2multicell --n_start_filters 32 --latent_dim 512 --target_dim 2503The training script generate the inference_args.txt file with the parameters need for the model configuration (same used for training).
For inference with the model run:
python core/HarmoMeshNet_inference.py --input_dir path/to/test/images/folder --weights_path path/to/trained/model/.pt --inference_mode singlecell2singlecell/multicell2singlecell --n_start_filters 32 --latent_dim 512 --max_sh_degree 8 --sphere_subdivisions 4 --refiner_steps 3 --refiner_layers 3 --refiner_hidden 64 --n_smooth 1 --lambd 0.5In all the cases we allow three different behaviours during inference throught the --mode_prediction parameter:
multicell2singlecell: You have volumes (Z,Y,X) with multiple cells but you want to generate the individual mesh for each individual cell.
singlecell2singlecell: You have volumes (Z,Y,X) with a single cell and you want to generate the mesh of this cells.
multicell2multicell: You have volumes (Z,Y,X) with multiple cells and you want to generate the mesh of this volumes.
Change your desired training version folder and run the command:
tensorboard --logdir=./lightning_logs/version_xA Serving TensorBoard link will be provided copy and paste the url on the webpage and the plots will be deployed:
When the model is trained, the folder logs is generated with the .txt files containing the track of the metrics during the execusion for plot generation, run:
python core/plot_HarmonicRegressionNet_losses.py --input_dir path/to/the/log/folderWhen the model is trained, the folder logs is generated with the .txt files containing the track of the metrics during the execusion for plot generation, run:
python core/plot_HarmoMeshNet_losses.py --input_dir path/to/the/log/folderTo rendering the spheric harmonic descomposition and the correspondig mesh given the .npy file just run
python core/spherical_descomposition_visualization.py --path_file /path/to/file/.npyThis will show the corresponding rendering within Vedo
Given a folder with .npy files you can get the corresponding .obj files running:
python core/vector2Mesh.py --input_files path/to/.npy/folderThe generated .obj can be visualized with help of napari vedo bridge just running napari whithin the im2mesh enviroment and drawing the file to the visualizer.
(im2mesh) $user: napariFor a simple visualization of a single .obj mesh you can use the ViewSTL tool throught the web, just draw the desired file.







