celldetection
repository·main·Indexed 41 days ago
https://github.com/fzj-inm1-bda/celldetectionA Python library for cell segmentation and detection in biological imaging. It provides pretrained U-Net architectures, Contour Proposal Networks (CPN), and utility functions for image processing and visualization. The library supports 2D and 3D architectures including MA-Nets, ConvNeXt, and Residual Networks, with integrations for timm and segmentation_models_pytorch (smp). It includes a Napari plugin for interactive visualization and supports deployment via Docker, Apptainer, and Hugging Face API.
What's inside celldetection
- Cell Detection is a Python package built with PyTorch designed for detecting cells in biological imagery. It supports various architectures, including 2D and 3D U-Nets, and is capable of handling diverse datasets such as nuclei in chemical screens or malaria-infected human blood cells.
Available model architectures in celldetection.models
mainThe
celldetection.modelsmodule provides a variety of neural network architectures designed for cell detection tasks. These include specialized segmentation networks and various encoder backbones.Available architectures include:
- U-Net: Available in both 2D and 3D variants.
- Multi-Scale Attention Network (MANet)
- Contour Proposal Network (CPN)
- ResNet
- Feature Pyramid Network (FPN)
- ConvNeXt
- MobileNetV3
- Encoder Backbones: Support for
Timm EncoderandSmp Encoderto integrate pre-trained weights.
Access datasets in celldetection.data
mainThe
celldetection.data.datasetsmodule provides access to various datasets used for cell detection tasks. Supported datasets include:- BBBC039: A specific biological dataset.
- BBBC041: A specific biological dataset.
- Synth: Synthetic datasets generated for testing and training.
Use these modules to load standardized data formats for training or evaluation.
Use PyTorch Image Models (timm) with celldetection
mainYou can leverage the
timm(PyTorch Image Models) library to use high-quality pretrained encoders withincelldetection. This allows you to build models likeCpnTimmMaNetorTimmUNet. You can also usecd.models.TimmEncoderto wrap atimmmodel as an encoder for other architectures.import timm import celldetection as cd # Explore available timm models timm.list_models(filter='*') # Use a timm-based model # Examples: cd.models.CpnTimmMaNet, cd.models.TimmUNet, cd.models.TimmMaNetUse CPN and Eval operations for cell detection
mainFor specialized analysis and performance measurement, use the following modules:
- CPN Operations (
celldetection.data.cpn): Operations related to CPN (likely Cell/Point/Nuclei) processing. - Eval Operations (
celldetection.data.instance_eval): Tools for instance-level evaluation, used to calculate metrics for cell detection accuracy.
- CPN Operations (
Configure tile size and stride for cpn_inference.py
mainThe
--tile_sizeand--stridearguments control how large images are partitioned for processing.--tile_size: The dimension of the processing window.--stride: The step size between windows.
A common strategy for balancing computational efficiency and stitching quality is to set the stride to a value slightly less than the tile size (e.g.,
tile_size=1024andstride=768).Perform data and segmentation operations
mainThe
celldetection.datapackage includes modules for common data processing tasks:- Data Operations (
celldetection.data.misc): General utility functions for data handling. - Segmentation Operations (
celldetection.data.segmentation): Tools specifically designed for processing segmentation masks and related data structures.
- Data Operations (
Use Segmentation Models PyTorch (smp) with celldetection
mainThe project integrates with
segmentation_models_pytorch(smp). You can usecd.models.SmpEncoderto create an encoder from ansmpbackbone (e.g., 'mit_b5') with pretrained weights. This encoder can then be used in architectures likeCpnSmpMaNet,CpnSmpUNet,SmpMaNet, orSmpUNet.import segmentation_models_pytorch as smp import celldetection as cd # Explore available smp encoders smp.encoders.get_encoder_names() # Create an smp encoder for use in celldetection encoder = cd.models.SmpEncoder(encoder_name='mit_b5', pretrained='imagenet') # Use smp-based models like cd.models.CpnSmpUNet or cd.models.SmpUNetOptimize cpn_inference.py for memory-constrained environments
mainIf you encounter memory issues while running
cpn_inference.py, adjust the following parameters to reduce the memory footprint:--tile_size: Defines the size of the windows used to process large images. Reducing this (e.g., to512) consumes less memory but may increase processing time or affect segmentation quality for large objects. (Default:1024)--stride: Determines the overlap between consecutive tiles. A smaller stride increases overlap (better stitching) but increases computation. A larger stride reduces computation but may miss objects at tile borders. (Default:768)--batch_size: Use a smaller batch size to reduce the number of samples processed simultaneously.--precision: Lower the precision (e.g., from'32-true'to'16-mixed') to reduce the model's memory footprint, though this may impact accuracy.- Resource Allocation: On multi-GPU systems, use
--strategyand--devicessettings to distribute the workload more effectively.
Run cpn_inference.py using Apptainer and Slurm
mainFor High-Performance Computing (HPC) environments using Slurm and Apptainer, use
srunto execute the script within a Singularity/Apptainer image.srun [srun-arguments] apptainer exec --nv celldetection_latest.sif python cpn_inference.py [arguments]srun [srun-arguments] apptainer exec --nv celldetection_latest.sif python cpn_inference.py [arguments]Install celldetection via PyPI or GitHub
mainYou can install the
celldetectionpackage usingpip. Ensure that PyTorch is already installed on your system before proceeding.# Install from PyPI pip install -U celldetection # Install directly from GitHub pip install git+https://github.com/FZJ-INM1-BDA/celldetection.gitUse toy data for testing
mainThecelldetection.data.toydatamodule provides small, simplified datasets. These are intended for rapid prototyping, debugging, and verifying that your data pipelines or model integrations are working correctly without the overhead of large biological datasets.