Spleen 3D segmentation with MONAI

This tutorial uses cached data generated by the Spleen 3D Segmentation tutorial from MONAI to demonstrate how to use the TensorBoardPlugin3D.

The original Spleen dataset can be downloaded from http://medicaldecathlon.com/.

spleen

Target: Spleen
Modality: CT
Size: 61 3D volumes (41 Training + 20 Testing)
Source: Memorial Sloan Kettering Cancer Center
Challenge: Large ranging foreground size

Open In Colab

Setup environment

In [ ]:
!python -c "import monai" || pip install -q "monai-weekly[gdown, nibabel, tqdm, ignite]"
!python -c "import matplotlib" || pip install -q matplotlib
%matplotlib inline
!pip install -q tensorboard-plugin-3d
!python -c "import gdown" || pip install -q gdown
In [ ]:
import tempfile
import shutil
import os
import gdown

Setup data directory

You can specify a directory to download the data to with the MONAI_DATA_DIRECTORY environment variable.
This allows you to save results and reuse downloads. If not specified a temporary directory will be used.

In [ ]:
directory = os.environ.get("MONAI_DATA_DIRECTORY")
root_dir = tempfile.mkdtemp() if directory is None else directory
url = 'https://drive.google.com/drive/folders/1zCicae9Tk_ChYK7JEnxJwQ0nUyfmHKHr?usp=sharing'
gdown.download_folder(url, output=root_dir, quiet=True)
print(root_dir)

Check best model output with the input image and label

In our sample data we have four folders to select from: all_data, input_with_label, output_with_label, and output_only. All options contain data for the first three items in out DataLoader.

  • output_only contains the output model only.
  • output_with_label contains the output model with the input label so that input and output can be viewed at the same time and easily compared.
  • input_with_label contains the original input image and label so that you can view the original data with the transforms applied
  • all_data contains the data from all three other folders: input_with_label, output_with_label, and output_only. This allows us to view the transformed input image and label followed by the output model by itself, then finally the output model with the input label for each of the three options.
In [ ]:
output_only = os.path.join(root_dir, "output_only")
output_with_label = os.path.join(root_dir, "output_with_label")
input_with_label = os.path.join(root_dir, "input_with_label")
all_data = os.path.join(root_dir, "all_data")

Lets look at all_data to get an idea of what each sample output looks like in the TensorBoard plugin. Setting the --logdir flag will tell TensorBoard which directory to look at for data.

In [ ]:
%load_ext tensorboard
%tensorboard --logdir=$all_data

Feel free to try out any of the other directories by changing up the logdir flag.

In [ ]:
%tensorboard --logdir=$output_with_label
In [ ]:
%tensorboard --logdir=$output_only
In [ ]:
%tensorboard --logdir=$input_with_label

Cleanup data directory

Remove directory if a temporary was used.

In [ ]:
if directory is None:
    shutil.rmtree(root_dir)
In [ ]: