SpineDL-Structure: Anatomical structures of the mouse spinal cord

This tutorial shows how to run SpineDL-Structure, the semantic segmentation model developed in the manuscript [9]. The goal of this workflow is the automatic segmentation of several anatomical structures of the mouse spinal cord from fluorescence images stained with DAPI and NeuN. Specifically, the model segments the following structures:

  • Background (0 value)

  • White matter (1 value)

  • Ependyma (2 value)

  • Gray matter (3 value)

  • Lesioned / damaged tissue (4 value)

../../_images/0009ATF_raw.png

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

../../_images/0009ATF_label.png

Its corresponding semantic mask depicting four anatomical structures: background (black), white matter (yellow), gray matter (teal) and ependyma (pink).

Note

This tutorial covers only the semantic segmentation module (SpineDL-Structure). A separate tutorial will cover neuron instance segmentation with SpineDL-Neuron. See: SpineDL-Neuron: Neuron segmentation in 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 former. The directory structure should look like this:

Expand to see the directory structure
SpineDL/
β”œβ”€β”€ Structure identification
β”‚   β”œβ”€β”€ train
β”‚   β”‚   β”œβ”€β”€ raw
β”‚   β”‚   β”‚   β”œβ”€ 0001 ATF.tif
β”‚   β”‚   β”‚   β”œβ”€ 0002 ATF.tif
β”‚   β”‚   β”‚   └─ ...
β”‚   β”‚   └── label
β”‚   β”‚       β”œβ”€ 0001 ATF.tif
β”‚   β”‚       β”œβ”€ 0002 ATF.tif
β”‚   β”‚       └─ ...
β”‚   β”œβ”€β”€ validation
β”‚   β”‚   β”œβ”€β”€ raw
β”‚   β”‚   β”‚   β”œβ”€ 0007 ATF.tif
β”‚   β”‚   β”‚   β”œβ”€ 0062 ATF.tif
β”‚   β”‚   β”‚   └─ ...
β”‚   β”‚   └── label
β”‚   β”‚       β”œβ”€ 0007 ATF.tif
β”‚   β”‚       β”œβ”€ 0062 ATF.tif
β”‚   β”‚       └─ ...
β”‚   └── test
β”‚       β”œβ”€β”€ raw
β”‚       β”‚   β”œβ”€ 0011 ATF.tif
β”‚       β”‚   β”œβ”€ 0016 ATF.tif
β”‚       β”‚   └─ ...
β”‚       β”œβ”€β”€ Manual_annotation_1
β”‚       β”‚   β”œβ”€ 0011 ATF.tif
β”‚       β”‚   β”œβ”€ 0016 ATF.tif
β”‚       β”‚   └─ ...
β”‚       β”œβ”€β”€ Manual_annotation_2
β”‚       β”‚   β”œβ”€ 0011 ATF.tif
β”‚       β”‚   β”œβ”€ 0016 ATF.tif
β”‚       β”‚   └─ ...
β”‚       └── Manual_annotation_3
β”‚           β”œβ”€ 0011 ATF.tif
β”‚           β”œβ”€ 0016 ATF.tif
β”‚           └─ ...
β”œβ”€β”€ Neuron identification
β”‚   β”œβ”€β”€ ...
└── Image Information.xlsx

How to use SpineDL-Structure

You can use the pretrained SpineDL-Structure 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-Structure from here, which is already prepared to load the pretrained model, called β€˜greedy-deer’, 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-Structure from scratch

If you want to reproduce the results of SpineDL-Structure 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-Structure 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/Structure identification/train/raw/"
          GT_PATH: "/home/user/SpineDL/Structure identification/train/label/"
       VAL:
          PATH: "/home/user/SpineDL/Structure identification/validation/raw/"
          GT_PATH: "/home/user/SpineDL/Structure identification/validation/label/"
       TEST:
          PATH: "/home/user/SpineDL/Structure identification/test/raw/"
    
  4. Run BiaPy using the CLI as described below:

    # Configuration file
    job_cfg_file=/home/user/spinedl-structure-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_structure_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_structure_from_scratch/my_spinedl_structure_from_scratch_1/results. Specifically, in that folder you will find the predicted masks for the test images under per_image_binarized.

  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 semantic_seg_stats.py \
       --pred_folder "/home/user/exp_results/my_spinedl_structure_from_scratch/my_spinedl_structure_from_scratch_1/results/per_image_binarized/" \
       --experts_root "/home/user/Structure identification/test/label" \
       --output_dir "/home/user/output_dir"
    

    This script will generate several evaluation metrics comparing the predicted masks with the expert annotations, including IoU scores and agreement maps under /home/user/output_dir. This script was used to generate the results reported in Figure 4 and 5 of the manuscript [9].