-
-
Save muyulin/0cbeeb57237296c71082dba416f427fb to your computer and use it in GitHub Desktop.
V-Net in Keras and tensorflow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"from os import listdir\n", | |
"import SimpleITK\n", | |
"import os.path\n", | |
"import pickle\n", | |
"import numpy" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"volSize = numpy.array((128,128,64), numpy.int32)\n", | |
"dstRes = numpy.array((1,1,1.5))\n", | |
"normDir = False\n", | |
"method = SimpleITK.sitkLinear" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def process_scan(scan):\n", | |
" ret = numpy.zeros(volSize, dtype=numpy.float32)\n", | |
" factor = numpy.asarray(scan.GetSpacing()) / dstRes\n", | |
"\n", | |
" factorSize = numpy.asarray(scan.GetSize() * factor, dtype=numpy.float)\n", | |
"\n", | |
" newSize = numpy.max([factorSize, volSize], axis=0)\n", | |
"\n", | |
" newSize = newSize.astype(dtype=numpy.int32)\n", | |
"\n", | |
" T=SimpleITK.AffineTransform(3)\n", | |
" T.SetMatrix(scan.GetDirection())\n", | |
"\n", | |
" resampler = SimpleITK.ResampleImageFilter()\n", | |
" resampler.SetReferenceImage(scan)\n", | |
" resampler.SetOutputSpacing(dstRes)\n", | |
" resampler.SetSize(newSize.tolist())\n", | |
" resampler.SetInterpolator(method)\n", | |
" if normDir:\n", | |
" resampler.SetTransform(T.GetInverse())\n", | |
"\n", | |
" imgResampled = resampler.Execute(scan)\n", | |
"\n", | |
"\n", | |
" imgCentroid = numpy.asarray(newSize, dtype=numpy.float) / 2.0\n", | |
"\n", | |
" imgStartPx = (imgCentroid - numpy.array(volSize) / 2.0).astype(dtype=int)\n", | |
"\n", | |
" regionExtractor = SimpleITK.RegionOfInterestImageFilter()\n", | |
" regionExtractor.SetSize(volSize.astype(dtype=numpy.int32).tolist())\n", | |
" regionExtractor.SetIndex(imgStartPx.tolist())\n", | |
"\n", | |
" imgResampledCropped = regionExtractor.Execute(imgResampled)\n", | |
"\n", | |
" return numpy.transpose(\n", | |
" SimpleITK.GetArrayFromImage(imgResampledCropped).astype(dtype=numpy.float),\n", | |
" [2, 1, 0]\n", | |
" )" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"def iterate_folder(folder):\n", | |
" for filename in sorted(listdir(folder)):\n", | |
" absolute_filename = os.path.join(folder, filename)\n", | |
" segmentation_absolute_filename = absolute_filename[:-4] + '_segmentation.mhd'\n", | |
" if filename.endswith('.mhd') and os.path.exists(segmentation_absolute_filename):\n", | |
" yield absolute_filename, segmentation_absolute_filename" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def load_data(folder):\n", | |
" input_filenames, label_filenames = zip(*list(iterate_folder(folder)))\n", | |
" \n", | |
" X = numpy.array([process_scan(SimpleITK.ReadImage(f)) for f in input_filenames])\n", | |
" y = numpy.array([process_scan(SimpleITK.ReadImage(f)) for f in label_filenames])\n", | |
" \n", | |
" return X, y > 0.5" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"X, y = load_data('../data/PROMISE2012/train/')\n", | |
"X.shape, y.shape, y.mean() \n", | |
"\n", | |
"with open('../data/PROMISE2012/train_data.p3', 'wb') as f:\n", | |
" pickle.dump([X, y], f)" | |
] | |
} | |
], | |
"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.5.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment