--- a +++ b/.ipynb_checkpoints/demo-checkpoint.ipynb @@ -0,0 +1,370 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's load some sample dasets to test the code. " + ] + }, + { + "cell_type": "code", + "execution_count": 102, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Downloading...\n", + "From: https://drive.google.com/uc?id=1QZNgRojYpYBLzUQJntWAmw1QwQMh4H50\n", + "To: /tf/Dropbox (Partners HealthCare)/ubuntu/docker/repos/DeepStrain/datasets/sample_nifti_3D/patient101_frame14.nii.gz\n", + "100%|████████████████████████████████████████| 667k/667k [00:00<00:00, 7.39MB/s]\n", + "Downloading...\n", + "From: https://drive.google.com/uc?id=1zFJM_qQKwz85xiYpX3XBRqhL0SQwy-Iw\n", + "To: /tf/Dropbox (Partners HealthCare)/ubuntu/docker/repos/DeepStrain/datasets/sample_nifti_3D/patient101_frame01.nii.gz\n", + "100%|████████████████████████████████████████| 664k/664k [00:00<00:00, 3.10MB/s]\n", + "Downloading...\n", + "From: https://drive.google.com/uc?id=1FqTquCYhLD2-EKxmCR9A5zt5265AEPdQ\n", + "To: /tf/Dropbox (Partners HealthCare)/ubuntu/docker/repos/DeepStrain/datasets/sample_nifti_4D/patient101_4d.nii.gz\n", + "20.0MB [00:01, 17.2MB/s]\n" + ] + } + ], + "source": [ + "!bash ./datasets/download_sample_dataset.sh" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Load the pre-trained models that attached to the publication. This will download the cardiac segmentation and motion estimation trained parameters: " + ] + }, + { + "cell_type": "code", + "execution_count": 103, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Note: available models are carson_Jan2021, carmen_Jan2021\n", + "Downloading models ...\n", + "Downloading...\n", + "From: https://drive.google.com/uc?id=1rINpNPZ4_lT9XuFB6Q7gyna_L4O3AIY9\n", + "To: /tf/Dropbox (Partners HealthCare)/ubuntu/docker/repos/DeepStrain/pretrained_models/carson_Jan2021.h5\n", + "229MB [00:12, 18.6MB/s] \n", + "Downloading...\n", + "From: https://drive.google.com/uc?id=10eMGoYYa4xFdwFuiwC7bwVSJ6b-bx7Ni\n", + "To: /tf/Dropbox (Partners HealthCare)/ubuntu/docker/repos/DeepStrain/pretrained_models/carmen_Jan2021.h5\n", + "449MB [00:23, 18.9MB/s] \n" + ] + } + ], + "source": [ + "!bash ./pretrained_models/download_model.sh" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Test segmentation on 3D data in NIFTI format. " + ] + }, + { + "cell_type": "code", + "execution_count": 104, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "+ DATAROOT=./datasets/sample_nifti_3D\n", + "+ DATAFORMAT=NIFTI\n", + "+ RESULTS_DIR=./results/sample_nifti_3D\n", + "+ CARSON_PATH=../private_models/main_carson_model.h5\n", + "+ CARMEN_PATH=./pretrained_models/carmen_Jan2021.h5\n", + "+ PIPELINE=segmentation\n", + "+ CUDA_VISIBLE_DEVICES=\n", + "+ python ./test.py --dataroot ./datasets/sample_nifti_3D --dataformat NIFTI --results_dir ./results/sample_nifti_3D --pretrained_models_netS ../private_models/main_carson_model.h5 --pretrained_models_netME ./pretrained_models/carmen_Jan2021.h5 --pipeline segmentation\n", + "2021-02-14 18:02:27.114286: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudart.so.10.1\n", + "2021-02-14 18:02:29.315607: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcuda.so.1\n", + "2021-02-14 18:02:29.340591: E tensorflow/stream_executor/cuda/cuda_driver.cc:314] failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected\n", + "2021-02-14 18:02:29.340649: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: e990b504c5b4\n", + "2021-02-14 18:02:29.340666: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: e990b504c5b4\n", + "2021-02-14 18:02:29.340797: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:200] libcuda reported version is: 450.102.4\n", + "2021-02-14 18:02:29.340843: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:204] kernel reported version is: 450.102.4\n", + "2021-02-14 18:02:29.340860: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:310] kernel version seems to match DSO: 450.102.4\n", + "2021-02-14 18:02:29.341267: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations: AVX2 FMA\n", + "To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n", + "2021-02-14 18:02:29.352981: I tensorflow/core/platform/profile_utils/cpu_utils.cc:104] CPU Frequency: 1696155000 Hz\n", + "2021-02-14 18:02:29.353521: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x4ad3bc0 initialized for platform Host (this does not guarantee that XLA will be used). Devices:\n", + "2021-02-14 18:02:29.353563: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version\n", + "/tf/Dropbox (Partners HealthCare)/ubuntu/docker/repos/DeepStrain/data/nifti_dataset.py:77: UserWarning: Affine in nifti might be set incorrectly. Setting to affine=affine*zooms\n", + " warnings.warn(\"Affine in nifti might be set incorrectly. Setting to affine=affine*zooms\")\n", + "/tf/Dropbox (Partners HealthCare)/ubuntu/docker/repos/DeepStrain/data/nifti_dataset.py:77: UserWarning: Affine in nifti might be set incorrectly. Setting to affine=affine*zooms\n", + " warnings.warn(\"Affine in nifti might be set incorrectly. Setting to affine=affine*zooms\")\n" + ] + } + ], + "source": [ + "!bash ./scripts/test_segmentation.sh ./datasets/sample_nifti_3D NIFTI ./results/sample_nifti_3D" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Test segmentation on 4D (3D + time) data in NIFTI format. " + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "+ DATAROOT=./datasets/sample_nifti_4D\n", + "+ DATAFORMAT=NIFTI\n", + "+ RESULTS_DIR=./results/sample_nifti_4D\n", + "+ CARSON_PATH=../private_models/main_carson_model.h5\n", + "+ CARMEN_PATH=./pretrained_models/carmen_Jan2021.h5\n", + "+ PIPELINE=segmentation\n", + "+ CUDA_VISIBLE_DEVICES=\n", + "+ python ./test.py --dataroot ./datasets/sample_nifti_4D --dataformat NIFTI --results_dir ./results/sample_nifti_4D --pretrained_models_netS ../private_models/main_carson_model.h5 --pretrained_models_netME ./pretrained_models/carmen_Jan2021.h5 --pipeline segmentation\n", + "2021-02-14 18:02:36.541501: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudart.so.10.1\n", + "2021-02-14 18:02:38.748517: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcuda.so.1\n", + "2021-02-14 18:02:38.772471: E tensorflow/stream_executor/cuda/cuda_driver.cc:314] failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected\n", + "2021-02-14 18:02:38.772523: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: e990b504c5b4\n", + "2021-02-14 18:02:38.772544: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: e990b504c5b4\n", + "2021-02-14 18:02:38.772684: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:200] libcuda reported version is: 450.102.4\n", + "2021-02-14 18:02:38.772733: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:204] kernel reported version is: 450.102.4\n", + "2021-02-14 18:02:38.772749: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:310] kernel version seems to match DSO: 450.102.4\n", + "2021-02-14 18:02:38.773126: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations: AVX2 FMA\n", + "To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n", + "2021-02-14 18:02:38.784659: I tensorflow/core/platform/profile_utils/cpu_utils.cc:104] CPU Frequency: 1696155000 Hz\n", + "2021-02-14 18:02:38.785149: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x6260db0 initialized for platform Host (this does not guarantee that XLA will be used). Devices:\n", + "2021-02-14 18:02:38.785202: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version\n", + "/tf/Dropbox (Partners HealthCare)/ubuntu/docker/repos/DeepStrain/data/nifti_dataset.py:77: UserWarning: Affine in nifti might be set incorrectly. Setting to affine=affine*zooms\n", + " warnings.warn(\"Affine in nifti might be set incorrectly. Setting to affine=affine*zooms\")\n" + ] + } + ], + "source": [ + "!bash ./scripts/test_segmentation.sh ./datasets/sample_nifti_4D NIFTI ./results/sample_nifti_4D" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Test motion on 4D (3D + time) data in NIFTI format. Motion is only avilable for 4D data. " + ] + }, + { + "cell_type": "code", + "execution_count": 106, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "+ DATAROOT=./datasets/sample_nifti_4D\n", + "+ DATAFORMAT=NIFTI\n", + "+ RESULTS_DIR=./results/sample_nifti_4D\n", + "+ CARSON_PATH=./pretrained_models/carson_Jan2021.h5\n", + "+ CARMEN_PATH=./pretrained_models/carmen_Jan2021.h5\n", + "+ PIPELINE=motion\n", + "+ CUDA_VISIBLE_DEVICES=\n", + "+ python ./test.py --dataroot ./datasets/sample_nifti_4D --dataformat NIFTI --results_dir ./results/sample_nifti_4D --pretrained_models_netS ./pretrained_models/carson_Jan2021.h5 --pretrained_models_netME ./pretrained_models/carmen_Jan2021.h5 --pipeline motion\n", + "2021-02-14 18:03:25.301883: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudart.so.10.1\n", + "2021-02-14 18:03:27.512196: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcuda.so.1\n", + "2021-02-14 18:03:27.536612: E tensorflow/stream_executor/cuda/cuda_driver.cc:314] failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected\n", + "2021-02-14 18:03:27.536661: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: e990b504c5b4\n", + "2021-02-14 18:03:27.536678: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: e990b504c5b4\n", + "2021-02-14 18:03:27.536807: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:200] libcuda reported version is: 450.102.4\n", + "2021-02-14 18:03:27.536855: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:204] kernel reported version is: 450.102.4\n", + "2021-02-14 18:03:27.536872: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:310] kernel version seems to match DSO: 450.102.4\n", + "2021-02-14 18:03:27.537213: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations: AVX2 FMA\n", + "To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n", + "2021-02-14 18:03:27.549271: I tensorflow/core/platform/profile_utils/cpu_utils.cc:104] CPU Frequency: 1696155000 Hz\n", + "2021-02-14 18:03:27.549656: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x4af9c90 initialized for platform Host (this does not guarantee that XLA will be used). Devices:\n", + "2021-02-14 18:03:27.549686: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version\n", + "/tf/Dropbox (Partners HealthCare)/ubuntu/docker/repos/DeepStrain/data/nifti_dataset.py:77: UserWarning: Affine in nifti might be set incorrectly. Setting to affine=affine*zooms\n", + " warnings.warn(\"Affine in nifti might be set incorrectly. Setting to affine=affine*zooms\")\n", + "2021-02-14 18:03:38.911740: W tensorflow/core/framework/cpu_allocator_impl.cc:81] Allocation of 1946157056 exceeds 10% of free system memory.\n", + "2021-02-14 18:03:39.743555: W tensorflow/core/framework/cpu_allocator_impl.cc:81] Allocation of 1946157056 exceeds 10% of free system memory.\n", + "2021-02-14 18:03:40.195599: W tensorflow/core/framework/cpu_allocator_impl.cc:81] Allocation of 1946157056 exceeds 10% of free system memory.\n", + "2021-02-14 18:03:40.499049: W tensorflow/core/framework/cpu_allocator_impl.cc:81] Allocation of 1946157056 exceeds 10% of free system memory.\n", + "2021-02-14 18:03:41.084644: W tensorflow/core/framework/cpu_allocator_impl.cc:81] Allocation of 1946157056 exceeds 10% of free system memory.\n" + ] + } + ], + "source": [ + "!bash ./scripts/test_motion.sh ./datasets/sample_nifti_4D NIFTI ./results/sample_nifti_4D" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Test both segmentation and motion on 4D niftis. " + ] + }, + { + "cell_type": "code", + "execution_count": 107, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "+ DATAROOT=./datasets/sample_nifti_4D\n", + "+ DATAFORMAT=NIFTI\n", + "+ RESULTS_DIR=./results/sample_nifti_4D\n", + "+ CARSON_PATH=./pretrained_models/carson_Jan2021.h5\n", + "+ CARMEN_PATH=./pretrained_models/carmen_Jan2021.h5\n", + "+ PIPELINE=segmentation_motion\n", + "+ CUDA_VISIBLE_DEVICES=\n", + "+ python ./test.py --dataroot ./datasets/sample_nifti_4D --dataformat NIFTI --results_dir ./results/sample_nifti_4D --pretrained_models_netS ./pretrained_models/carson_Jan2021.h5 --pretrained_models_netME ./pretrained_models/carmen_Jan2021.h5 --pipeline segmentation_motion\n", + "2021-02-14 18:04:19.796322: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudart.so.10.1\n", + "2021-02-14 18:04:21.998095: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcuda.so.1\n", + "2021-02-14 18:04:22.024630: E tensorflow/stream_executor/cuda/cuda_driver.cc:314] failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected\n", + "2021-02-14 18:04:22.024683: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: e990b504c5b4\n", + "2021-02-14 18:04:22.024704: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: e990b504c5b4\n", + "2021-02-14 18:04:22.024818: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:200] libcuda reported version is: 450.102.4\n", + "2021-02-14 18:04:22.024863: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:204] kernel reported version is: 450.102.4\n", + "2021-02-14 18:04:22.024878: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:310] kernel version seems to match DSO: 450.102.4\n", + "2021-02-14 18:04:22.025239: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations: AVX2 FMA\n", + "To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n", + "2021-02-14 18:04:22.036833: I tensorflow/core/platform/profile_utils/cpu_utils.cc:104] CPU Frequency: 1696155000 Hz\n", + "2021-02-14 18:04:22.037295: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x5482b60 initialized for platform Host (this does not guarantee that XLA will be used). Devices:\n", + "2021-02-14 18:04:22.037323: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version\n", + "/tf/Dropbox (Partners HealthCare)/ubuntu/docker/repos/DeepStrain/data/nifti_dataset.py:77: UserWarning: Affine in nifti might be set incorrectly. Setting to affine=affine*zooms\n", + " warnings.warn(\"Affine in nifti might be set incorrectly. Setting to affine=affine*zooms\")\n", + "/tf/Dropbox (Partners HealthCare)/ubuntu/docker/repos/DeepStrain/data/nifti_dataset.py:77: UserWarning: Affine in nifti might be set incorrectly. Setting to affine=affine*zooms\n", + " warnings.warn(\"Affine in nifti might be set incorrectly. Setting to affine=affine*zooms\")\n", + "2021-02-14 18:05:17.144698: W tensorflow/core/framework/cpu_allocator_impl.cc:81] Allocation of 1946157056 exceeds 10% of free system memory.\n", + "2021-02-14 18:05:17.945462: W tensorflow/core/framework/cpu_allocator_impl.cc:81] Allocation of 1946157056 exceeds 10% of free system memory.\n", + "2021-02-14 18:05:18.392550: W tensorflow/core/framework/cpu_allocator_impl.cc:81] Allocation of 1946157056 exceeds 10% of free system memory.\n", + "2021-02-14 18:05:18.698553: W tensorflow/core/framework/cpu_allocator_impl.cc:81] Allocation of 1946157056 exceeds 10% of free system memory.\n", + "2021-02-14 18:05:19.246800: W tensorflow/core/framework/cpu_allocator_impl.cc:81] Allocation of 1946157056 exceeds 10% of free system memory.\n" + ] + } + ], + "source": [ + "!bash ./scripts/test_segmentation_motion.sh ./datasets/sample_nifti_4D NIFTI ./results/sample_nifti_4D" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "After the segmentations and motion estimates have been generated, we can use both calculate myocardial strain. Note that we're passing the output folder from the previous runs. " + ] + }, + { + "cell_type": "code", + "execution_count": 108, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "+ RESULTS_DIR=./results/sample_nifti_4D\n", + "+ PIPELINE=strain\n", + "+ CUDA_VISIBLE_DEVICES=\n", + "+ python ./test.py --dataroot ./results/sample_nifti_4D --results_dir ./results/sample_nifti_4D --pipeline strain\n", + "2021-02-14 18:05:58.124863: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudart.so.10.1\n", + "0.0 0.0\n", + "-0.0028643845930383114 1.082031278527137e-05\n", + "0.050226276271155106 -0.08250821851016393\n", + "0.0565662083407661 -0.08721526046380865\n", + "0.06341052114161663 -0.09039004858265032\n", + "0.062187569151276205 -0.09092932158690682\n", + "0.06273986208445685 -0.09008545707566279\n", + "0.06704142900754055 -0.09048872862980738\n", + "0.06900019280843682 -0.09202953300260222\n", + "0.06752667505148324 -0.09357088160323081\n", + "0.062176541303209334 -0.09396584141478774\n", + "0.05509482533956014 -0.09227094988109329\n", + "-0.011429068055575722 -0.006970679116830844\n", + "0.04503282824559175 -0.08830198004612085\n", + "0.03627042423655744 -0.08262235528522073\n", + "0.029786156419924492 -0.07606680680357371\n", + "0.026712081007800637 -0.06975290381524307\n", + "0.019263847211321777 -0.06391941641474\n", + "0.007899134163335668 -0.055720237241491964\n", + "-0.005480475496803074 -0.03917932457221275\n", + "-0.00592674874319504 -0.010661710877623127\n", + "-0.0024683947363135297 -0.0014360990600069954\n", + "-0.0021524594363897293 5.030914597078318e-05\n", + "-0.026725685733460826 -0.02099679369525766\n", + "-0.02529592552751031 -0.03299636940750714\n", + "-0.010376969736070649 -0.04378397382622917\n", + "0.002002523134231003 -0.05443799860918108\n", + "0.015893347546116793 -0.06210413845861033\n", + "0.02648765196612738 -0.06909838143488618\n", + "0.03835757750338634 -0.0751729741905356\n" + ] + } + ], + "source": [ + "!bash ./scripts/test_strain.sh ./results/sample_nifti_4D" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.6.9" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}