celldetection

repository·main·Indexed 41 days ago

https://github.com/fzj-inm1-bda/celldetection

A 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.

Tokens
13K
Snippets
39
Records
53
Agent score
99%

What's inside celldetection

  1. Overview of Cell Detection

    main
    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.
  2. Available model architectures in celldetection.models

    main

    The celldetection.models module 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 Encoder and Smp Encoder to integrate pre-trained weights.
  3. Access datasets in celldetection.data

    main

    The celldetection.data.datasets module 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.

  4. Use PyTorch Image Models (timm) with celldetection

    main

    You can leverage the timm (PyTorch Image Models) library to use high-quality pretrained encoders within celldetection. This allows you to build models like CpnTimmMaNet or TimmUNet. You can also use cd.models.TimmEncoder to wrap a timm model 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.TimmMaNet
  5. Use CPN and Eval operations for cell detection

    main

    For 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.
  6. Configure tile size and stride for cpn_inference.py

    main

    The --tile_size and --stride arguments 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=1024 and stride=768).

  7. Perform data and segmentation operations

    main

    The celldetection.data package 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.
  8. Use Segmentation Models PyTorch (smp) with celldetection

    main

    The project integrates with segmentation_models_pytorch (smp). You can use cd.models.SmpEncoder to create an encoder from an smp backbone (e.g., 'mit_b5') with pretrained weights. This encoder can then be used in architectures like CpnSmpMaNet, CpnSmpUNet, SmpMaNet, or SmpUNet.

    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.SmpUNet
  9. Optimize cpn_inference.py for memory-constrained environments

    main

    If you encounter memory issues while running cpn_inference.py, adjust the following parameters to reduce the memory footprint:

    1. --tile_size: Defines the size of the windows used to process large images. Reducing this (e.g., to 512) consumes less memory but may increase processing time or affect segmentation quality for large objects. (Default: 1024)
    2. --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)
    3. --batch_size: Use a smaller batch size to reduce the number of samples processed simultaneously.
    4. --precision: Lower the precision (e.g., from '32-true' to '16-mixed') to reduce the model's memory footprint, though this may impact accuracy.
    5. Resource Allocation: On multi-GPU systems, use --strategy and --devices settings to distribute the workload more effectively.
  10. Run cpn_inference.py using Apptainer and Slurm

    main

    For High-Performance Computing (HPC) environments using Slurm and Apptainer, use srun to 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]
  11. Install celldetection via PyPI or GitHub

    main

    You can install the celldetection package using pip. 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.git