<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 <a id="Top"></a>
                               ___                                     ___           ___     
    _____                     /\__\                                   /\  \         /\__\    
   /
Step1:  <a id="TF"></a>
Build prediction model with tflearn and tensorflow
<a id="CNN"></a>
Multilayer convolutional neural network
Code based on this tflearn example, with CNN architecture modeled after TensorFlow's tutorial Deep MNIST for experts.
Step2:  <a id="Digitre"></a>
Classify digit examples from Digitre
<a id="Prep"></a>
Example step-by-step preprocessing
Take example base64-encoded handwritten digit images (generated from html canvas element) and preprocess step-by-step to a format ready for classification model. Compare with MNIST example.
Step3:  <a id="Class"></a>
Classify preprocessed images
<ASSISTANT_TASK:>
Python Code:
# Standard library
import datetime
import time
# Third party libraries
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# Digitre code
import digitre_preprocessing as prep
import digitre_model
import digitre_classifier
# Reload digitre code in the same session (during development)
import imp
imp.reload(prep)
imp.reload(digitre_model)
imp.reload(digitre_classifier)
# Latest update
str(datetime.datetime.now())
Explanation: <a id="Top"></a>
                               ___                                     ___           ___     
    _____                     /\__\                                   /\  \         /\__\    
   /::\  \       ___         /:/ _/_       ___           ___         /::\  \       /:/ _/_   
  /:/\:\  \     /\__\       /:/ /\  \     /\__\         /\__\       /:/\:\__\     /:/ /\__\  
 /:/  \:\__\   /:/__/      /:/ /::\  \   /:/__/        /:/  /      /:/ /:/  /    /:/ /:/ _/_ 
/:/__/ \:|__| /::\  \     /:/__\/\:\__\ /::\  \       /:/__/      /:/_/:/__/___ /:/_/:/ /\__\
\:\  \ /:/  / \/\:\  \__  \:\  \ /:/  / \/\:\  \__   /::\  \      \:\/:::::/  / \:\/:/ /:/  /
 \:\  /:/  /   ~~\:\/\__\  \:\  /:/  /   ~~\:\/\__\ /:/\:\  \      \::/~~/~~~~   \::/_/:/  / 
  \:\/:/  /       \::/  /   \:\/:/  /       \::/  / \/__\:\  \      \:\~~\        \:\/:/  /  
   \::/  /        /:/  /     \::/  /        /:/  /       \:\__\      \:\__\        \::/  /   
    \/__/         \/__/       \/__/         \/__/         \/__/       \/__/         \/__/
December 2016
Table of contents
Build prediction model with tflearn and tensorflow
Multilayer convolutional neural network
Serialize trained CNN model for serving
Classify digit examples from Digitre
Example step-by-step preprocessing
Classify preprocessed images
End of explanation
# Data loading and preprocessing
X, Y, testX, testY = digitre_model.load_data()
#X = X.reshape([-1, 28, 28, 1])
#testX = testX.reshape([-1, 28, 28, 1])
# Plot functions
def plot_digit(digit, show=True, file_name=None):
    plt.imshow(digit, cmap = 'Greys', interpolation = 'none')
    plt.tick_params(axis='both', which='both', bottom='off', top='off',
                    labelbottom='off', right='off', left='off', labelleft='off')
    if file_name is not None:
        plt.savefig(file_name)
    if show:
        plt.show()
    
def plot_digits(digits, rows, columns):
    for i, digit in enumerate(digits):
        plt.subplot(rows, columns, i+1)
        plot_digit(digit, show=False)
    plt.show()
# Plot a few training examples
X_eg = X[10:20,:,:,:]
X_eg = [digit.reshape(28, 28) for digit in X_eg]
plot_digits(X_eg, 2, 5)
# Visualization
# Used "tensorboard_verbose=0", meaning Loss & Metric
# Run "$ tensorboard --logdir='/tmp/tflearn_logs'"
### Fit model using all data (merge training and test data)
# Done from command line:
# $ python digitre_model.py -f 'cnn_alldata.tflearn' -a -e 20
# Training Step: 20320  | total loss: 0.642990.9401 | val_loss: 0.052
# | Adam | epoch: 020 | loss: 0.64299 - acc: 0.9401 | val_loss: 0.05263 - val_acc: 0.9866 -- iter: 65000/65000
# --
# -----
# Completed training in
# 3.5 hr.
# -----
# ... Saving trained model as " cnn_alldata.tflearn "
Explanation: <a id="TF"></a>
Build prediction model with tflearn and tensorflow
<a id="CNN"></a>
Multilayer convolutional neural network
Code based on this tflearn example, with CNN architecture modeled after TensorFlow's tutorial Deep MNIST for experts.
End of explanation
with open('b64_2_preprocessing.txt', 'r') as f:
    eg_2 = f.read()
# Preview base64 encoded image
print(eg_2[:500])
eg_2 = prep.b64_str_to_np(eg_2)
eg_2.shape
# Plot the example handwritten digit
plot_digit(eg_2, file_name='b64_2_preprocessing_1.png')
eg_2 = prep.crop_img(eg_2)
plot_digit(eg_2, file_name='b64_2_preprocessing_2.png')
eg_2 = prep.center_img(eg_2)
plot_digit(eg_2, file_name='b64_2_preprocessing_3.png')
eg_2 = prep.resize_img(eg_2)
eg_2.shape
plot_digit(eg_2, file_name='b64_2_preprocessing_4.png')
eg_2 = prep.min_max_scaler(eg_2, final_range=(0, 1))
plot_digit(eg_2)
# Plot processed Digitre image together with MNIST example
plot_digits([eg_2, X_eg[6]], 1, 2)
# Save MNIST example too
plot_digit(X_eg[6], file_name='MNIST_2.png')
eg_2.max()
eg_2.shape
Explanation: <a id="Digitre"></a>
Classify digit examples from Digitre
<a id="Prep"></a>
Example step-by-step preprocessing
Take example base64-encoded handwritten digit images (generated from html canvas element) and preprocess step-by-step to a format ready for classification model. Compare with MNIST example.
End of explanation
# Instantiate Classifier (loads the tflearn pre-trained model)
model = digitre_classifier.Classifier(file_name='cnn.tflearn')
# Classify same example digit
with open('b64_2_preprocessing.txt', 'r') as f:
    eg_2 = f.read()
eg_2 = model.preprocess(eg_2)
pred = np.around(model.classify(eg_2)[0], 2)
pred
from altair import Chart, Data, X, Y, Axis, Scale
# Plot prediction
def prob_distribution_plot(pred):
    prediction = pred.reshape([10])
    data = Data(values=[{'x': i, 'y': value} for i, value in enumerate(pred)])
    plot = Chart(data).mark_bar(color='#f6755e').encode(
        x=X('x:O', axis=Axis(title='Digit', labelAngle=0.5,
                             tickLabelFontSize=15, titleFontSize=15)),
        y=Y('y:Q', axis=Axis(format='%', title='Probability',
                             tickLabelFontSize=15, titleFontSize=15),
            scale=Scale(domain=(0, 1))))
    
    return plot
prob_distribution_plot(pred)
from altair import Chart, Data, X, Y, Axis
# Plot prediction
def prob_distribution_plot(pred):
    prediction = pred.reshape([10])
    data = Data(values=[{'x': i, 'y': value} for i, value in enumerate(prediction)])
    plot = Chart(data).mark_bar(color='#f6755e').encode(
        x=X('x:O', axis=Axis(title='Digit', labelAngle=0.5, tickLabelFontSize=15, titleFontSize=15)),
        y=Y('y:Q', axis=Axis(format='%', title='Probability', tickLabelFontSize=15, titleFontSize=15)))
    
    return plot.to_json(indent=2)
prob_distribution_plot(pred)
Explanation: <a id="Class"></a>
Classify preprocessed images
End of explanation
<END_TASK>