Skip to content

TorchIO download data to MONAI_DATA_DIRECTORY #686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,13 @@ tests/testing_data/*Hippocampus*
# Ignore torch saves
*/torch/runs
logs
*/runs
lightning_logs

# ignore automatically created files
*.ts
nohup.out
deepgrow/ignite/_image.nii.gz
*.zip
deployment/bentoml/mednist_classifier_bentoml.py
deployment/ray/mednist_classifier_start.py
82 changes: 50 additions & 32 deletions modules/TorchIO_MONAI_PyTorch_Lightning.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -69,7 +69,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -78,7 +78,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {
"id": "KvbbZuhmquRR"
},
Expand All @@ -92,14 +92,16 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {
"id": "gduPdIturUIB"
},
"outputs": [],
"source": [
"from pathlib import Path\n",
"from datetime import datetime\n",
"import os\n",
"import tempfile\n",
"from glob import glob\n",
"\n",
"import torch\n",
"from torch.utils.data import random_split, DataLoader\n",
Expand All @@ -117,6 +119,36 @@
"%load_ext tensorboard"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup data directory\n",
"\n",
"You can specify a directory with the `MONAI_DATA_DIRECTORY` environment variable. \n",
"This allows you to save results and reuse downloads. \n",
"If not specified a temporary directory will be used."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/mnt/data/rbrown/Documents/Data/MONAI\n"
]
}
],
"source": [
"directory = os.environ.get(\"MONAI_DATA_DIRECTORY\")\n",
"root_dir = tempfile.mkdtemp() if directory is None else directory\n",
"print(root_dir)"
]
},
{
"cell_type": "markdown",
"metadata": {
Expand Down Expand Up @@ -145,20 +177,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {
"id": "KuhTaRl3vf37"
},
"outputs": [],
"source": [
"\n",
"\n",
"class MedicalDecathlonDataModule(pl.LightningDataModule):\n",
" def __init__(self, task, batch_size, train_val_ratio):\n",
" super().__init__()\n",
" self.task = task\n",
" self.batch_size = batch_size\n",
" self.dataset_dir = Path(task)\n",
" self.base_dir = root_dir\n",
" self.dataset_dir = os.path.join(root_dir, task)\n",
" self.train_val_ratio = train_val_ratio\n",
" self.subjects = None\n",
" self.test_subjects = None\n",
Expand All @@ -175,16 +206,13 @@
" return shapes.max(axis=0)\n",
"\n",
" def download_data(self):\n",
" if not self.dataset_dir.is_dir():\n",
" url = 'https://msd-for-monai.s3-us-west-2.amazonaws.com/Task04_Hippocampus.tar'\n",
" monai.apps.download_and_extract(url=url, output_dir=\".\")\n",
" if not os.path.isdir(self.dataset_dir):\n",
" url = f'https://msd-for-monai.s3-us-west-2.amazonaws.com/{self.task}.tar'\n",
" monai.apps.download_and_extract(url=url, output_dir=self.base_dir)\n",
"\n",
" def get_niis(d):\n",
" return sorted(p for p in d.glob('*.nii*') if not p.name.startswith('.'))\n",
"\n",
" image_training_paths = get_niis(self.dataset_dir / 'imagesTr')\n",
" label_training_paths = get_niis(self.dataset_dir / 'labelsTr')\n",
" image_test_paths = get_niis(self.dataset_dir / 'imagesTs')\n",
" image_training_paths = sorted(glob(os.path.join(self.dataset_dir, 'imagesTr', \"*.nii*\")))\n",
" label_training_paths = sorted(glob(os.path.join(self.dataset_dir, 'labelsTr', \"*.nii*\")))\n",
" image_test_paths = sorted(glob(os.path.join(self.dataset_dir, 'imagesTs', \"*.nii*\")))\n",
" return image_training_paths, label_training_paths, image_test_paths\n",
"\n",
" def prepare_data(self):\n",
Expand Down Expand Up @@ -260,7 +288,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {
"id": "hcHf9w2nLfyC"
},
Expand All @@ -284,7 +312,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
Expand All @@ -293,16 +321,6 @@
"outputId": "7cb39051-4c26-4811-b838-8a5e938e53a3"
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Downloading...\n",
"From: https://drive.google.com/uc?id=1RzPB1_bqzQhlWvU-YGvZzhx2omcDh38C\n",
"To: /content/Task04_Hippocampus.tar\n",
"28.4MB [00:00, 82.8MB/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
Expand Down Expand Up @@ -341,7 +359,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {
"id": "1Ov3H12p6Qx1"
},
Expand Down Expand Up @@ -395,7 +413,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
Expand Down