{ "cells": [ { "cell_type": "markdown", "id": "5141c4a3-4305-41f7-8e58-cbdcd5d28201", "metadata": {}, "source": [ "# Spleen 3D segmentation with MONAI\n", "\n", "This tutorial uses cached data generated by the Spleen 3D Segmentation tutorial from MONAI to demonstrate how to use the TensorBoardPlugin3D.\n", "\n", "The original Spleen dataset can be downloaded from http://medicaldecathlon.com/.\n", "\n", "![spleen](http://medicaldecathlon.com/img/spleen0.png)\n", "\n", "Target: Spleen \n", "Modality: CT \n", "Size: 61 3D volumes (41 Training + 20 Testing) \n", "Source: Memorial Sloan Kettering Cancer Center \n", "Challenge: Large ranging foreground size\n", "\n", "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/KitwareMedical/tensorboard-plugin-3d/blob/main/demo/notebook/cached_spleen_segmentation_3d.ipynb)" ] }, { "cell_type": "markdown", "id": "e962c9f6-135f-4034-a008-f2b54f70db45", "metadata": {}, "source": [ "## Setup environment" ] }, { "cell_type": "code", "execution_count": null, "id": "4be79f1e-e133-40d8-8c60-a534af4d43b8", "metadata": {}, "outputs": [], "source": [ "!python -c \"import monai\" || pip install -q \"monai-weekly[gdown, nibabel, tqdm, ignite]\"\n", "!python -c \"import matplotlib\" || pip install -q matplotlib\n", "%matplotlib inline\n", "!pip install -q tensorboard-plugin-3d\n", "!python -c \"import gdown\" || pip install -q gdown" ] }, { "cell_type": "code", "execution_count": null, "id": "aec333d5-f256-4449-8433-6330e648e91c", "metadata": {}, "outputs": [], "source": [ "import tempfile\n", "import shutil\n", "import os\n", "import gdown" ] }, { "cell_type": "markdown", "id": "d56b8c5e-57b9-4255-ba51-f0fe77a4336e", "metadata": {}, "source": [ "## Setup data directory\n", "\n", "You can specify a directory to download the data to with the `MONAI_DATA_DIRECTORY` environment variable. \n", "This allows you to save results and reuse downloads. If not specified a temporary directory will be used." ] }, { "cell_type": "code", "execution_count": null, "id": "ad5c984b-c932-4408-bde8-3b6e9cf725af", "metadata": {}, "outputs": [], "source": [ "directory = os.environ.get(\"MONAI_DATA_DIRECTORY\")\n", "root_dir = tempfile.mkdtemp() if directory is None else directory\n", "url = 'https://drive.google.com/drive/folders/1zCicae9Tk_ChYK7JEnxJwQ0nUyfmHKHr?usp=sharing'\n", "gdown.download_folder(url, output=root_dir, quiet=True)\n", "print(root_dir)" ] }, { "cell_type": "markdown", "id": "245450b2-24c9-4f2c-bc5b-bbc03343293b", "metadata": {}, "source": [ "## Check best model output with the input image and label\n", "\n", "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.\n", "- `output_only` contains the output model only.\n", "- `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.\n", "- `input_with_label` contains the original input image and label so that you can view the original data with the transforms applied\n", "- `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." ] }, { "cell_type": "code", "execution_count": null, "id": "c77bfb5c-e9d0-4c20-ba75-a34dfa3aa4cc", "metadata": {}, "outputs": [], "source": [ "output_only = os.path.join(root_dir, \"output_only\")\n", "output_with_label = os.path.join(root_dir, \"output_with_label\")\n", "input_with_label = os.path.join(root_dir, \"input_with_label\")\n", "all_data = os.path.join(root_dir, \"all_data\")" ] }, { "cell_type": "markdown", "id": "19c31968-7e25-425b-b764-26ea3bc68fc8", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": null, "id": "3127ff2b-c1d5-4c3c-9bb6-1cd74cfa40b0", "metadata": {}, "outputs": [], "source": [ "%load_ext tensorboard\n", "%tensorboard --logdir=$all_data" ] }, { "cell_type": "markdown", "id": "b900195e-9600-4fc7-b57d-a3dcb7a2a263", "metadata": {}, "source": [ "Feel free to try out any of the other directories by changing up the `logdir` flag." ] }, { "cell_type": "code", "execution_count": null, "id": "132cfc75-8386-48c1-a707-ef9ae559cb18", "metadata": {}, "outputs": [], "source": [ "%tensorboard --logdir=$output_with_label" ] }, { "cell_type": "code", "execution_count": null, "id": "728f2c3a-d4ea-467f-a2dd-bd3dad9ab9f0", "metadata": {}, "outputs": [], "source": [ "%tensorboard --logdir=$output_only" ] }, { "cell_type": "code", "execution_count": null, "id": "8e793a7d-7fbf-4c1b-bac2-91c932a2b146", "metadata": {}, "outputs": [], "source": [ "%tensorboard --logdir=$input_with_label" ] }, { "cell_type": "markdown", "id": "41024e25-d98e-4970-a84a-244968a0bb27", "metadata": {}, "source": [ "## Cleanup data directory\n", "\n", "Remove directory if a temporary was used." ] }, { "cell_type": "code", "execution_count": null, "id": "5c8f0f72-6166-41f6-a0b7-d8b88fc4e6a3", "metadata": {}, "outputs": [], "source": [ "if directory is None:\n", " shutil.rmtree(root_dir)" ] }, { "cell_type": "code", "execution_count": null, "id": "289ad9e3-2a64-489b-8ef0-6769b8a040ea", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }