AxonDeepSeg is a software that allows you to segment both axon and myelin sheaths from various microscopy modalities. The algorithm is based on convolutional neural networks.
In this Jupyter notebook, the following steps are listed:
(0) - Import the necessary librairies.
(1) - Define the inputs and parameters for the segmentation (test image, model).
(2) - Launch the segmentation on the test image.
(3) - Display the result (axon/myelin mask).
(4) - Validate the segmentation output against the gold standard using the Dice similarity coefficient.
First of all, we are going to import some standard libraries.
%matplotlib inline
from pathlib import Path
import json
import os
import matplotlib.pyplot as plt
from AxonDeepSeg.testing.segmentation_scoring import pw_dice
import numpy as np
import AxonDeepSeg.ads_utils as ads
from time import time
from config import axonmyelin_suffix
Here, we are going to specify the input parameters to use for the segmentation.
1.1. Set the path of the test image to segment.
We first need to specify the path to the directory containing the image that we want to segment. The standard directory structure for AxonDeepSeg contains: (i) the image file to segment ('image.png'), (ii) the gold standard mask of the segmentation to validate our result ('mask.png') and (iii) a txt file that contains the pixel size of the image in micrometers ('pixel_size_in_micrometer.txt').
ads_path = Path(os.path.abspath('')).resolve().parent
path_testing = Path(os.path.join(ads_path,'AxonDeepSeg','models','model_seg_rat_axon-myelin_sem','data_test'))
1.2. Select the trained model you want to use for the segmentation.
Here, we specify the deep learning model we want to use in order to segment our sample. We currently propose 3 models: one for scanning electron microscopy (SEM) samples, a second for transmission electron microscopy (TEM) samples, and a third for bright field optical microscopy (BF) samples. The current versions of the models are 'model_seg_rat_axon-myelin_sem', 'model_seg_mouse_axon-myelin_tem"', and 'model_seg_rat_axon-myelin_bf', respectively. In this case, our test sample is a SEM spinal cord sample of the rat, so we select the SEM model available.
model_name = 'model_seg_rat_axon-myelin_sem'
1.3. Set the path of the segmentation model.
Here, we specify the path of the directory containing our models.
path_model = Path(os.path.join(ads_path,'AxonDeepSeg','models',model_name))
2.1. Import the function that performs the segmentation from AxonDeepSeg.
The function segment_image in the 'segment.py' script computes the axon/myelin segmentation of a sample.
from AxonDeepSeg.segment import segment_image
2.2. Launch the segmentation of the image.
Here, we launch the segmentation. Here, we specify the following inputs in the segment_image function: (i) the path of the image, (ii) the path of the model, (iii) the overlap value between patches and (iv) the resolution of the image (pixel size in micrometers).
The output here will be the predicted image, which consists of a 3-label mask (background=0, myelin=127, axon=255). By default, the output prediction will be saved in the same directory as the input image, and named 'image_seg-axonmyelin.png'.
segment_image(
path_testing_image=path_testing / 'image.png',
path_model=path_model,
overlap_value=[48, 48],
acquired_resolution=0.13
)
3.1. Import sample image and segmentation result.
imorg = ads.imread(os.path.join(path_testing,'image.png'))
imads = ads.imread(os.path.join(path_testing,'image' + str(axonmyelin_suffix)))
3.2. Display sample image.
Here, we display the original microscopy image.
plt.figure(figsize=(13,10))
plt.title('Original image')
plt.imshow(imorg, cmap='gray')
plt.show();
3.3. Display segmentation result.
Here, we display the predicted segmentation. Axons are displayed in yellow, while myelin sheaths are displayed in green.
plt.figure(figsize=(13,10))
plt.title('Segmented image')
plt.imshow(imads)
plt.show();
3.4. Display the segmentation result overlaid on the sample image.
Here, we overlay the segmentation mask on the original microscopy image to qualitatively evaluate the performance of the segmentation.
plt.figure(figsize=(13,10))
plt.title('Superposed images')
plt.imshow(imorg, cmap='gray', alpha=0.8)
plt.imshow(imads, cmap='viridis', alpha=0.2)
4.1. Import the segmentation result and the gold standard mask.
mask = ads.imread(os.path.join(path_testing,'mask.png'))
pred = ads.imread(os.path.join(path_testing,'image' + str(axonmyelin_suffix)))
4.2. Get the axon and myelin masks from the 3-label masks.
Here, we extract the binary axon and myelin masks from the 3-class predicted mask.
gt_axon = mask > 200
gt_myelin = np.logical_and(mask >= 50, mask <= 200)
pred_axon = pred > 200
pred_myelin = np.logical_and(pred >= 50, pred <= 200)
4.3. Compute the axon and myelin Dice similarity metrics and display the results.
We are using the Dice similarity coefficient to evaluate the segmentation. The obtained axon mask is compared against the axon gold standard. Similarly, we compare the myelin segmentation mask against the myelin gold standard. A Dice value closer to 1 indicates a higher similarity between the segmentation and the gold standard.
dice_axon = pw_dice(pred_axon, gt_axon)
dice_myelin = pw_dice(pred_myelin, gt_myelin)
print('Dice for Axon : ' + str(dice_axon))
print('Dice for myelin : ' + str(dice_myelin))