This notebook shows how one traing a deep learning model to classify a subset of the ISBI 2012 data set. This assumes you have access to the ISBI 2012 data, which is available as a download from the ISBI challenge website or via an ndparse database call (see example below).
Note that this example provides reasonable but not state-of-the-art results. You will also need to install Keras (this script was tested with version 1.1.0) along with a suitable backend - (we use Theano).
In [20]:
%load_ext autoreload
%autoreload 2
%matplotlib inline
import sys, os, copy, logging, socket, time
import numpy as np
import pylab as plt
#from ndparse.algorithms import nddl as nddl
#import ndparse as ndp
sys.path.append('..'); import ndparse as ndp
try:
logger
except:
# do this precisely once
logger = logging.getLogger("train_model")
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setFormatter(logging.Formatter('[%(asctime)s:%(name)s:%(levelname)s] %(message)s'))
logger.addHandler(ch)
In [21]:
print("Running on system: %s" % socket.gethostname())
if True:
# Using a local copy of data volume
#inDir = '/Users/graywr1/code/bio-segmentation/data/ISBI2012/'
inDir = '/home/pekalmj1/Data/EM_2012'
X = ndp.nddl.load_cube(os.path.join(inDir, 'train-volume.tif'))
Y = ndp.nddl.load_cube(os.path.join(inDir, 'train-labels.tif'))
# show some details. Note that data tensors are assumed to have dimensions:
# (#slices, #channels, #rows, #columns)
#
print('Train data shape is: %s %s' % (str(X.shape), str(Y.shape)))
plt.imshow(X[0,0,...], interpolation='none', cmap='bone')
plt.title('train volume, slice 0')
plt.gca().axes.get_xaxis().set_ticks([])
plt.gca().axes.get_yaxis().set_ticks([])
plt.show()
In [ ]:
# Note that for demonstration purposes we use an artifically low
# number of training slices and epochs. For actualy training,
# you would use more data and train for longer.
train_slices = np.arange(2) # e.g. change to np.arange(25)
valid_slices = np.arange(25,30)
n_epochs = 1
tic = time.time()
model = ndp.nddl.train_model(X[train_slices,...], np.squeeze(Y[train_slices, ...]),
X[valid_slices,...], np.squeeze(Y[valid_slices, ...]),
nEpochs=n_epochs, log=logger)
print("Time to train: %0.2f sec" % (time.time() - tic))