<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Example
Step1:  Step 2
Step2:  Step 3
Step3:  Step 4
<ASSISTANT_TASK:>
Python Code:
%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("deploy_model")
    logger.setLevel(logging.DEBUG)
    ch = logging.StreamHandler()
    ch.setFormatter(logging.Formatter('[%(asctime)s:%(name)s:%(levelname)s]  %(message)s'))
    logger.addHandler(ch)
Explanation: Example: Deploying a Classifier
This notebook shows how one might use a previously trained 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).  It also assumes you have a local copy of trained weights for a Keras deep learning model; one example weights file is checked into this repository which will provide reasonable (but not state-of-the-art) results.  You will also need to have Keras (along with a suitable backend - we use Theano) installed.
Step 1: setup python environment
End of explanation
print("Running on system: %s" % socket.gethostname())
# Load previously trained CNN weights
weightsFile = './isbi2012_weights_e025.h5'
if True:
    # Using a local copy of data volume
    #inDir = '/Users/graywr1/code/bio-segmentation/data/ISBI2012/'
    inDir = '/home/pekalmj1/Data/EM_2012'
    Xtrain = ndp.nddl.load_cube(os.path.join(inDir, 'train-volume.tif'))
    Ytrain = ndp.nddl.load_cube(os.path.join(inDir, 'train-labels.tif'))
    Xtest = ndp.nddl.load_cube(os.path.join(inDir, 'test-volume.tif'))
    
else:
    # example of using ndio database call
    import ndio.remote.neurodata as ND
    tic = time.time()
    
    nd = ND()
    
    token = 'kasthuri11cc'
    channel = 'image'
    xstart, xstop = 5472, 6496
    ystart, ystop = 8712, 9736
    zstart, zstop = 1000, 1100
    res = 1
    
    Xtest = nd.get_cutout(token, channel, xstart, xstop, ystart, ystop, zstart, zstop, resolution=res)
    Xtest = np.transpose(Xtest, [2, 0, 1])
    Xtest = Xtest[:, np.newaxis, :, :]  # add a channel dimension
    print 'time elapsed is: {} seconds'.format(time.time()-tic)
    
# show some details.  Note that data tensors are assumed to have dimensions:
#   (#slices, #channels, #rows, #columns)
#
print('Test data shape is: %s' % str(Xtest.shape))
plt.imshow(Xtest[0,0,...], interpolation='none', cmap='bone')
plt.title('test volume, slice 0')
plt.gca().axes.get_xaxis().set_ticks([])
plt.gca().axes.get_yaxis().set_ticks([])
plt.show()
Explanation: Step 2: Load data and model weights
End of explanation
# In the interest of time, only deploy on one slice (z-dimension) of the test volume 
# *and* only evaluate a subset of the pixels in that slice.
#
# Note: depending upon your system (e.g. CPU vs GPU) this may take a few minutes...
#
tic = time.time()
P0 = ndp.nddl.fit(Xtest, weightsFile, slices=[0,], evalPct=.1, log=logger)
print("Time to deploy: %0.2f sec" % (time.time() - tic))
# The shape of the probability estimate tensor is:
#    (#slices, #classes, #rows, #cols)
print('Class probabilities shape: %s' % str(P0.shape))
Explanation: Step 3: Deploy the model
End of explanation
# Use a simple interpolation scheme to fill in "missing" values 
# (i.e. those pixels we did not evaluate using the CNN).
# 
Pint = ndp.nddl.interpolate_nn(P0)
# visualize
plt.imshow(P0[0,0,...]); plt.colorbar()
plt.gca().axes.get_xaxis().set_ticks([])
plt.gca().axes.get_yaxis().set_ticks([])
plt.title('Class Estimates (slice 0, subsampled)')
plt.show()
plt.imshow(Pint[0,0,...]); plt.colorbar()
plt.title('Class Estimates: (slice 0, interpolated)')
plt.gca().axes.get_xaxis().set_ticks([])
plt.gca().axes.get_yaxis().set_ticks([])
plt.show()
Explanation: Step 4: Postprocessing
Note: in order to do actual science, one would use more sophisticated postprocessing (and also put more effort into the CNN design).
End of explanation
<END_TASK>