SpineDL-Neuron: Neuron segmentation in the mouse spinal cord

This tutorial shows how to run SpineDL-Neuron, the instance segmentation model developed in the manuscript [9]. The goal of this workflow is the automatic neuron segmentation of the mouse spinal cord from fluorescence images stained with DAPI and NeuN.

../../_images/0032ATF_raw.png

SpineDL-Neuron image sample with two channels: DAPI (channel 1) and NeuN (channel 2).

../../_images/0032ATF_label.png

Its corresponding instance mask, where each neuron is represented with a different color.

Note

This tutorial covers only the instance segmentation module (SpineDL-Neuron). A separate tutorial will cover instance segmentation with SpineDL-Structure. See: SpineDL-Structure: Anatomical structures of the mouse spinal cord.

Dataset

The dataset consists of more than 160 confocal images of mouse spinal cord tissue stained with NeuN (neuronal somas) and DAPI (nuclei). Images were acquired as mosaics of Z-stacks and converted to 2-channel TIFF files (channel 1: DAPI, channel 2: NeuN). Find a further description of the dataset, as well as the link to download it, in Zenodo.

The dataset will contain two directories called Structure identification and Neuron identification. This tutorial focuses on the latter. The directory neuron should look like this:

Expand to see the directory structure
SpineDL/
β”œβ”€β”€ Neuron identification
β”‚   β”œβ”€β”€ train
β”‚   β”‚   β”œβ”€β”€ raw
β”‚   β”‚   β”‚   β”œβ”€ 0002 ATF.tif
β”‚   β”‚   β”‚   β”œβ”€ 0024 ATF.tif
β”‚   β”‚   β”‚   └─ ...
β”‚   β”‚   └── label
β”‚   β”‚       β”œβ”€ 0002 ATF.tif
β”‚   β”‚       β”œβ”€ 0024 ATF.tif
β”‚   β”‚       └─ ...
β”‚   β”œβ”€β”€ validation
β”‚   β”‚   β”œβ”€β”€ raw
β”‚   β”‚   β”‚   β”œβ”€ 0037 ATF.tif
β”‚   β”‚   β”‚   β”œβ”€ 0141 UCF Crop2.tif
β”‚   β”‚   β”‚   └─ ...
β”‚   β”‚   └── label
β”‚   β”‚       β”œβ”€ 0037 ATF.tif
β”‚   β”‚       β”œβ”€ 0141 UCF Crop2.tif
β”‚   β”‚       └─ ...
β”‚   └── test
β”‚       β”œβ”€β”€ raw
β”‚       β”‚   β”œβ”€ 0004 ATF.tif
β”‚       β”‚   β”œβ”€ 0076 ATF.tif
β”‚       β”‚   └─ ...
β”‚       β”œβ”€β”€ Manual_annotation_1
β”‚       β”‚   β”œβ”€ 0004 ATF.xml
β”‚       β”‚   β”œβ”€ 0076 ATF.xml
β”‚       β”‚   └─ ...
β”‚       β”œβ”€β”€ Manual_annotation_2
β”‚       β”‚   β”œβ”€ 0004 ATF.xml
β”‚       β”‚   β”œβ”€ 0076 ATF.xml
β”‚       β”‚   └─ ...
β”‚       └── ...
β”œβ”€β”€ Structure identification
β”‚   β”œβ”€β”€ ...
└── Image Information.xlsx

How to use SpineDL-Neuron

You can use the pretrained SpineDL-Neuron model to predict new images using several methods supported by BiaPy. You will need to follow the following steps:

  1. Prepare your images according to the model requirements:

    • 2D images with 2 channels (channel 1: DAPI, channel 2: NeuN). The order of the channels is important!

    • The images are expected to be in uint8 format. If your images are in a different format (e.g., uint16 or float), you should rescale them to the range 0-255 and convert them to uint8 before running inference with the model to ensure optimal performance.

    • The expected image resolution is from 0.445 to 0.891 Β΅m/pixel. If your images have a different pixel size, you should rescale them accordingly before running inference with the model.

    If you have doubts you can check the dataset provided to see examples of images used in our work. If you have any questions regarding image preparation, please contact us through the Image.sc Forum , using biapy tag, or through our GitHub issues page.

    Note

    Here you can also use the images included in the test/raw folder of the provided dataset to test the model.

  2. Decide which method you want to use to run BiaPy: GUI, Jupyter/Colab, Galaxy, Docker, CLI, or API. Based on that you will need to follow the installation instructions described in the Installation section.

  3. Download the yaml configuration file for SpineDL-Neuron from here, which is already prepared to load the pretrained model, called β€˜proactive-snail’, from the BioImage Model Zoo.

  4. Then follow the instructions corresponding to your selected method to run BiaPy with the downloaded configuration file.

To begin, please follow these steps within the Graphical User Interface (GUI) (version 1.2.2 or later):

  1. Click on "Load and modify workflow" button in BiaPy GUI and load the downloaded YAML configuration file.

  2. If some of the paths in the YAML are not valid, you will be redirected to this window to modify them.

  3. Once that all the paths are valid, you can change the configuration filename if you want and click on "Save file".

  4. You will be redirected to the "Run Workflow" window. The recently loaded YAML configuration will be selected automatically, and a job name will be pre-filled based on the configuration filename. Next, select an output folder where all results will be saved.

  5. Then, you can optionally check the configuration file consistency before running BiaPy by clicking on the "Check file" button.

  6. Start the workflow by clicking the "Run" button.

Find a depiction of these steps below:

Reproducing SpineDL-Neuron from scratch

If you want to reproduce the results of SpineDL-Neuron using the provided dataset, you can follow the steps described below. We assume here some level of expertise so we describe it how to do it using the CLI and also assume you have already installed BiaPy.

  1. Download the dataset from Zenodo and extract it to a desired location.

  2. Download the yaml configuration file for SpineDL-Neuron from here. Notice that this configuration file is slightly different from the one used for inference, as it is prepared to train the model from scratch.

  3. Modify the configuration file to set the correct paths to the training, validation, and test data included in the dataset. For example, if you extracted the dataset to /home/user/SpineDL/, you will need to set the following paths in the configuration file:

    DATA:
       TRAIN:
          PATH: "/home/user/SpineDL/Neuron identification/train/raw/"
          GT_PATH: "/home/user/SpineDL/Neuron identification/train/label/"
       VAL:
          PATH: "/home/user/SpineDL/Neuron identification/validation/raw/"
          GT_PATH: "/home/user/SpineDL/Neuron identification/validation/label/"
       TEST:
          PATH: "/home/user/SpineDL/Neuron identification/test/raw/"
    
  4. Run BiaPy using the CLI as described below:

    # Configuration file
    job_cfg_file=/home/user/spinedl-neuron-from-scratch.yaml
    # Where the experiment output directory should be created
    result_dir=/home/user/exp_results
    # Just a name for the job
    job_name=my_spinedl_neuron_from_scratch
    # Number that should be increased when one need to run the same job multiple times (reproducibility)
    job_counter=1
    # Number of the GPU to run the job in (according to 'nvidia-smi' command)
    gpu_number=0
    
    # Load the environment
    conda activate BiaPy_env
    
    biapy \
          --config $job_cfg_file \
          --result_dir $result_dir  \
          --name $job_name    \
          --run_id $job_counter  \
          --gpu "$gpu_number"
    

    This will generate the results in /home/user/exp_results/my_spinedl_neuron_from_scratch/my_spinedl_neuron_from_scratch_1/results. Specifically, in that folder you will find the predicted masks for the test images under per_image_instances and the post-processed images under per_image_post_processing.

  5. Finally, you can evaluate the predictions for test images using this script. You will need to provide the path to the predicted masks and the root folder containing the expert annotations for the test set. For example:

    python -u agreement_manual_points_vs_SpineDL-neuron_instances.py \
       --pred_folder "/home/user/exp_results/my_spinedl_neuron_from_scratch/my_spinedl_neuron_from_scratch_1/results/per_image_post_processing/" \
       --manual_annotation_dir "/home/user/Neuron identification/test/label" \
    

    This script will generate the Figure 6 of [9] showing the agreement between manual and SpineDL-Neuron identifications with tolerance-aware matching.

  6. If you want to compare the performance of SpineDL-Neuron with CellSense, you can download the CellSense predictions from here and run the following script.:

    python -u agreement_manual_points_vs_SpineDL-neuron_vs_CellSense_instances.py \
       --spinedl_pred_folder "/home/user/exp_results/my_spinedl_neuron_from_scratch/my_spinedl_neuron_from_scratch_1/results/per_image_post_processing/" \
       --cellsense_pred_folder "/home/user/cellSense" \
       --manual_annotation_dir "/home/user/Neuron identification/test/label"