-
-
Save ravnoor/a8d26c485cd39c1d9dd21af7c27ac232 to your computer and use it in GitHub Desktop.
| { | |
| "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 | |
| } |
It's the PROMISE12 dataset.
Hello,
Thanks for your implementation.
With;
model.fit(X, y, batch_size=4, epochs=5, verbose=1)
I have following exception;
InvalidArgumentError (see above for traceback): Incompatible shapes: [8388608] vs. [1048576]
- I am using same dataset with yours.
- Load data.ipynb works fine.
- I have tried with different batch_size, i.e. 4,5,10,50
- The model's summary is exactly same with yours.
I just wonder, do you have any idea, why I am getting this exception?
Thank you, regards.
- Hakan
hello,
I run U-Net using dice loss, but the predicted images are all white. Do you know what's wrong?
def dice_coef(y_true, y_pred):
smooth = 1
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
intersection = K.sum(y_true_f * y_pred_f)
return (2. * intersection +smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) +smooth)
def dice_coef_loss(y_true, y_pred):
print("dice loss")
return 1-dice_coef(y_true, y_pred)
....
model.compile(optimizer = Adam(lr = 1e-5), loss = dice_coef_loss, metrics = ['accuracy'])
What dataset was your model used on?