In Codice Ratio Convolutional Neural Network - Test (no distortions)

In notebooks from 5 to 7 we are going to apply everything we experimented so far on the new dataset to see what best fits it.

Imports


In [1]:
import os.path
from IPython.display import Image

from util import Util
u = Util()

import numpy as np
# Explicit random seed for reproducibility
np.random.seed(1337)


Using TensorFlow backend.

In [2]:
from keras.callbacks import ModelCheckpoint
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.utils import np_utils
from keras import backend as K

In [3]:
import dataset_generator as dataset

char = 'a'
char_type = 'centrato'

Definitions


In [4]:
batch_size = 512
nb_classes = 2
nb_epoch = 500
# checkpoint path
checkpoints_filepath = "checkpoints/05_ICR_A_no-dist_weights.best.hdf5"

In [5]:
# input image dimensions
img_rows, img_cols = 34, 56
# number of convolutional filters to use
nb_filters1 = 30
nb_filters2 = 50
# size of pooling area for max pooling
pool_size1 = (2, 2)
pool_size2 = (3, 3)
# convolution kernel size
kernel_size1 = (4, 4)
kernel_size2 = (5, 5)
# dense layer size
dense_layer_size1 = 250
# dropout rate
dropout = 0.15
# activation
activation = 'relu'

Data load


In [6]:
(X_train, y_train, X_test, y_test) = dataset.generate_half_labeled_half_height(char, char_type)


Trovati 1638 esempi positivi per il carattere A .
Campioni di training: 1404 	Campioni di test: 234
Richiesti 1638 esempi negativi: trovati 819 generici e 819 per altezza.
Samples negativi generici in training: 702 	in test: 117
Samples negativi per altezza in training: 702 	in test: 117
Campioni di training: 1404 	Campioni di test: 234
Numero totale di campioni di training: 2808
Numero totale di campioni di test: 468

In [7]:
u.plot_images(X_train[0:9], y_train[0:9], img_shape=(56,34))



In [8]:
if K.image_dim_ordering() == 'th':
    X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
    X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)
    input_shape = (1, img_rows, img_cols)
else:
    X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 1)
    X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 1)
    input_shape = (img_rows, img_cols, 1)

In [9]:
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
print('X_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')


X_train shape: (2808, 34, 56, 1)
2808 train samples
468 test samples

In [10]:
# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)

Model definition


In [11]:
model = Sequential()

def initialize_network(model, dropout1=dropout, dropout2=dropout):
    model.add(Convolution2D(nb_filters1, kernel_size1[0], kernel_size1[1],
                            border_mode='valid',
                            input_shape=input_shape, name='covolution_1_' + str(nb_filters1) + '_filters'))
    model.add(Activation(activation, name='activation_1_' + activation))
    model.add(MaxPooling2D(pool_size=pool_size1, name='max_pooling_1_' + str(pool_size1) + '_pool_size'))
    model.add(Convolution2D(nb_filters2, kernel_size2[0], kernel_size2[1]))
    model.add(Activation(activation, name='activation_2_' + activation))
    model.add(MaxPooling2D(pool_size=pool_size2, name='max_pooling_1_' + str(pool_size2) + '_pool_size'))
    model.add(Dropout(dropout))

    model.add(Flatten())
    model.add(Dense(dense_layer_size1, name='fully_connected_1_' + str(dense_layer_size1) + '_neurons'))
    model.add(Activation(activation, name='activation_3_' + activation))
    model.add(Dropout(dropout))
    model.add(Dense(nb_classes, name='output_' + str(nb_classes) + '_neurons'))
    model.add(Activation('softmax', name='softmax'))

    model.compile(loss='categorical_crossentropy',
                  optimizer='adadelta',
                  metrics=['accuracy', 'precision', 'recall'])
    
    # loading weights from checkpoints 
    if os.path.exists(checkpoints_filepath):
        model.load_weights(checkpoints_filepath)
    
initialize_network(model)

Training and evaluation

Using non verbose output for training, since we already get some informations from the callback.


In [12]:
# checkpoint
checkpoint = ModelCheckpoint(checkpoints_filepath, monitor='val_precision', verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint]

# training
print('training model')
history = model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
          verbose=0, validation_data=(X_test, Y_test), callbacks=callbacks_list)

# ensures that the weights with the best evaluation are loaded
if os.path.exists(checkpoints_filepath):
    model.load_weights(checkpoints_filepath)


training model
Epoch 00000: val_precision improved from -inf to 0.50214, saving model to checkpoints/05_ICR_A_no-dist_weights.best.hdf5
Epoch 00001: val_precision improved from 0.50214 to 0.67521, saving model to checkpoints/05_ICR_A_no-dist_weights.best.hdf5
Epoch 00002: val_precision improved from 0.67521 to 0.68590, saving model to checkpoints/05_ICR_A_no-dist_weights.best.hdf5
Epoch 00003: val_precision did not improve
Epoch 00004: val_precision improved from 0.68590 to 0.73504, saving model to checkpoints/05_ICR_A_no-dist_weights.best.hdf5
Epoch 00005: val_precision did not improve
Epoch 00006: val_precision did not improve
Epoch 00007: val_precision improved from 0.73504 to 0.76282, saving model to checkpoints/05_ICR_A_no-dist_weights.best.hdf5
Epoch 00008: val_precision did not improve
Epoch 00009: val_precision improved from 0.76282 to 0.81838, saving model to checkpoints/05_ICR_A_no-dist_weights.best.hdf5
Epoch 00010: val_precision improved from 0.81838 to 0.83333, saving model to checkpoints/05_ICR_A_no-dist_weights.best.hdf5
Epoch 00011: val_precision did not improve
Epoch 00012: val_precision did not improve
Epoch 00013: val_precision improved from 0.83333 to 0.84829, saving model to checkpoints/05_ICR_A_no-dist_weights.best.hdf5
Epoch 00014: val_precision improved from 0.84829 to 0.86538, saving model to checkpoints/05_ICR_A_no-dist_weights.best.hdf5
Epoch 00015: val_precision improved from 0.86538 to 0.88675, saving model to checkpoints/05_ICR_A_no-dist_weights.best.hdf5
Epoch 00016: val_precision did not improve
Epoch 00017: val_precision improved from 0.88675 to 0.89103, saving model to checkpoints/05_ICR_A_no-dist_weights.best.hdf5
Epoch 00018: val_precision improved from 0.89103 to 0.89316, saving model to checkpoints/05_ICR_A_no-dist_weights.best.hdf5
Epoch 00019: val_precision did not improve
Epoch 00020: val_precision improved from 0.89316 to 0.89744, saving model to checkpoints/05_ICR_A_no-dist_weights.best.hdf5
Epoch 00021: val_precision improved from 0.89744 to 0.90171, saving model to checkpoints/05_ICR_A_no-dist_weights.best.hdf5
Epoch 00022: val_precision did not improve
Epoch 00023: val_precision did not improve
Epoch 00024: val_precision improved from 0.90171 to 0.90385, saving model to checkpoints/05_ICR_A_no-dist_weights.best.hdf5
Epoch 00025: val_precision did not improve
Epoch 00026: val_precision improved from 0.90385 to 0.91667, saving model to checkpoints/05_ICR_A_no-dist_weights.best.hdf5
Epoch 00027: val_precision did not improve
Epoch 00028: val_precision improved from 0.91667 to 0.92735, saving model to checkpoints/05_ICR_A_no-dist_weights.best.hdf5
Epoch 00029: val_precision did not improve
Epoch 00030: val_precision did not improve
Epoch 00031: val_precision improved from 0.92735 to 0.93376, saving model to checkpoints/05_ICR_A_no-dist_weights.best.hdf5
Epoch 00032: val_precision improved from 0.93376 to 0.93590, saving model to checkpoints/05_ICR_A_no-dist_weights.best.hdf5
Epoch 00033: val_precision improved from 0.93590 to 0.93803, saving model to checkpoints/05_ICR_A_no-dist_weights.best.hdf5
Epoch 00034: val_precision did not improve
Epoch 00035: val_precision did not improve
Epoch 00036: val_precision did not improve
Epoch 00037: val_precision did not improve
Epoch 00038: val_precision improved from 0.93803 to 0.94017, saving model to checkpoints/05_ICR_A_no-dist_weights.best.hdf5
Epoch 00039: val_precision did not improve
Epoch 00040: val_precision did not improve
Epoch 00041: val_precision did not improve
Epoch 00042: val_precision did not improve
Epoch 00043: val_precision improved from 0.94017 to 0.94444, saving model to checkpoints/05_ICR_A_no-dist_weights.best.hdf5
Epoch 00044: val_precision improved from 0.94444 to 0.94658, saving model to checkpoints/05_ICR_A_no-dist_weights.best.hdf5
Epoch 00045: val_precision did not improve
Epoch 00046: val_precision improved from 0.94658 to 0.94872, saving model to checkpoints/05_ICR_A_no-dist_weights.best.hdf5
Epoch 00047: val_precision did not improve
Epoch 00048: val_precision did not improve
Epoch 00049: val_precision did not improve
Epoch 00050: val_precision did not improve
Epoch 00051: val_precision improved from 0.94872 to 0.95085, saving model to checkpoints/05_ICR_A_no-dist_weights.best.hdf5
Epoch 00052: val_precision did not improve
Epoch 00053: val_precision did not improve
Epoch 00054: val_precision did not improve
Epoch 00055: val_precision did not improve
Epoch 00056: val_precision did not improve
Epoch 00057: val_precision did not improve
Epoch 00058: val_precision did not improve
Epoch 00059: val_precision did not improve
Epoch 00060: val_precision did not improve
Epoch 00061: val_precision did not improve
Epoch 00062: val_precision did not improve
Epoch 00063: val_precision did not improve
Epoch 00064: val_precision improved from 0.95085 to 0.95299, saving model to checkpoints/05_ICR_A_no-dist_weights.best.hdf5
Epoch 00065: val_precision did not improve
Epoch 00066: val_precision did not improve
Epoch 00067: val_precision improved from 0.95299 to 0.95726, saving model to checkpoints/05_ICR_A_no-dist_weights.best.hdf5
Epoch 00068: val_precision did not improve
Epoch 00069: val_precision did not improve
Epoch 00070: val_precision did not improve
Epoch 00071: val_precision did not improve
Epoch 00072: val_precision did not improve
Epoch 00073: val_precision did not improve
Epoch 00074: val_precision did not improve
Epoch 00075: val_precision did not improve
Epoch 00076: val_precision did not improve
Epoch 00077: val_precision did not improve
Epoch 00078: val_precision did not improve
Epoch 00079: val_precision did not improve
Epoch 00080: val_precision did not improve
Epoch 00081: val_precision did not improve
Epoch 00082: val_precision did not improve
Epoch 00083: val_precision did not improve
Epoch 00084: val_precision did not improve
Epoch 00085: val_precision did not improve
Epoch 00086: val_precision did not improve
Epoch 00087: val_precision did not improve
Epoch 00088: val_precision did not improve
Epoch 00089: val_precision did not improve
Epoch 00090: val_precision did not improve
Epoch 00091: val_precision did not improve
Epoch 00092: val_precision did not improve
Epoch 00093: val_precision did not improve
Epoch 00094: val_precision did not improve
Epoch 00095: val_precision did not improve
Epoch 00096: val_precision did not improve
Epoch 00097: val_precision did not improve
Epoch 00098: val_precision did not improve
Epoch 00099: val_precision did not improve
Epoch 00100: val_precision did not improve
Epoch 00101: val_precision did not improve
Epoch 00102: val_precision did not improve
Epoch 00103: val_precision did not improve
Epoch 00104: val_precision did not improve
Epoch 00105: val_precision did not improve
Epoch 00106: val_precision did not improve
Epoch 00107: val_precision did not improve
Epoch 00108: val_precision did not improve
Epoch 00109: val_precision did not improve
Epoch 00110: val_precision did not improve
Epoch 00111: val_precision did not improve
Epoch 00112: val_precision did not improve
Epoch 00113: val_precision did not improve
Epoch 00114: val_precision did not improve
Epoch 00115: val_precision did not improve
Epoch 00116: val_precision did not improve
Epoch 00117: val_precision did not improve
Epoch 00118: val_precision did not improve
Epoch 00119: val_precision did not improve
Epoch 00120: val_precision did not improve
Epoch 00121: val_precision did not improve
Epoch 00122: val_precision did not improve
Epoch 00123: val_precision did not improve
Epoch 00124: val_precision did not improve
Epoch 00125: val_precision did not improve
Epoch 00126: val_precision did not improve
Epoch 00127: val_precision improved from 0.95726 to 0.95940, saving model to checkpoints/05_ICR_A_no-dist_weights.best.hdf5
Epoch 00128: val_precision did not improve
Epoch 00129: val_precision did not improve
Epoch 00130: val_precision did not improve
Epoch 00131: val_precision did not improve
Epoch 00132: val_precision did not improve
Epoch 00133: val_precision did not improve
Epoch 00134: val_precision did not improve
Epoch 00135: val_precision did not improve
Epoch 00136: val_precision did not improve
Epoch 00137: val_precision did not improve
Epoch 00138: val_precision did not improve
Epoch 00139: val_precision did not improve
Epoch 00140: val_precision did not improve
Epoch 00141: val_precision did not improve
Epoch 00142: val_precision did not improve
Epoch 00143: val_precision did not improve
Epoch 00144: val_precision did not improve
Epoch 00145: val_precision did not improve
Epoch 00146: val_precision did not improve
Epoch 00147: val_precision did not improve
Epoch 00148: val_precision did not improve
Epoch 00149: val_precision did not improve
Epoch 00150: val_precision did not improve
Epoch 00151: val_precision did not improve
Epoch 00152: val_precision did not improve
Epoch 00153: val_precision did not improve
Epoch 00154: val_precision did not improve
Epoch 00155: val_precision did not improve
Epoch 00156: val_precision did not improve
Epoch 00157: val_precision did not improve
Epoch 00158: val_precision did not improve
Epoch 00159: val_precision did not improve
Epoch 00160: val_precision did not improve
Epoch 00161: val_precision did not improve
Epoch 00162: val_precision did not improve
Epoch 00163: val_precision did not improve
Epoch 00164: val_precision did not improve
Epoch 00165: val_precision did not improve
Epoch 00166: val_precision did not improve
Epoch 00167: val_precision did not improve
Epoch 00168: val_precision did not improve
Epoch 00169: val_precision did not improve
Epoch 00170: val_precision did not improve
Epoch 00171: val_precision did not improve
Epoch 00172: val_precision did not improve
Epoch 00173: val_precision did not improve
Epoch 00174: val_precision did not improve
Epoch 00175: val_precision did not improve
Epoch 00176: val_precision did not improve
Epoch 00177: val_precision did not improve
Epoch 00178: val_precision did not improve
Epoch 00179: val_precision did not improve
Epoch 00180: val_precision did not improve
Epoch 00181: val_precision did not improve
Epoch 00182: val_precision did not improve
Epoch 00183: val_precision did not improve
Epoch 00184: val_precision did not improve
Epoch 00185: val_precision did not improve
Epoch 00186: val_precision did not improve
Epoch 00187: val_precision improved from 0.95940 to 0.96154, saving model to checkpoints/05_ICR_A_no-dist_weights.best.hdf5
Epoch 00188: val_precision did not improve
Epoch 00189: val_precision did not improve
Epoch 00190: val_precision did not improve
Epoch 00191: val_precision did not improve
Epoch 00192: val_precision did not improve
Epoch 00193: val_precision did not improve
Epoch 00194: val_precision did not improve
Epoch 00195: val_precision did not improve
Epoch 00196: val_precision did not improve
Epoch 00197: val_precision did not improve
Epoch 00198: val_precision did not improve
Epoch 00199: val_precision did not improve
Epoch 00200: val_precision did not improve
Epoch 00201: val_precision did not improve
Epoch 00202: val_precision did not improve
Epoch 00203: val_precision did not improve
Epoch 00204: val_precision did not improve
Epoch 00205: val_precision did not improve
Epoch 00206: val_precision did not improve
Epoch 00207: val_precision did not improve
Epoch 00208: val_precision did not improve
Epoch 00209: val_precision did not improve
Epoch 00210: val_precision did not improve
Epoch 00211: val_precision did not improve
Epoch 00212: val_precision did not improve
Epoch 00213: val_precision did not improve
Epoch 00214: val_precision did not improve
Epoch 00215: val_precision did not improve
Epoch 00216: val_precision did not improve
Epoch 00217: val_precision did not improve
Epoch 00218: val_precision did not improve
Epoch 00219: val_precision did not improve
Epoch 00220: val_precision did not improve
Epoch 00221: val_precision did not improve
Epoch 00222: val_precision did not improve
Epoch 00223: val_precision did not improve
Epoch 00224: val_precision did not improve
Epoch 00225: val_precision did not improve
Epoch 00226: val_precision did not improve
Epoch 00227: val_precision did not improve
Epoch 00228: val_precision did not improve
Epoch 00229: val_precision did not improve
Epoch 00230: val_precision did not improve
Epoch 00231: val_precision did not improve
Epoch 00232: val_precision did not improve
Epoch 00233: val_precision did not improve
Epoch 00234: val_precision did not improve
Epoch 00235: val_precision did not improve
Epoch 00236: val_precision did not improve
Epoch 00237: val_precision did not improve
Epoch 00238: val_precision did not improve
Epoch 00239: val_precision did not improve
Epoch 00240: val_precision did not improve
Epoch 00241: val_precision did not improve
Epoch 00242: val_precision did not improve
Epoch 00243: val_precision did not improve
Epoch 00244: val_precision did not improve
Epoch 00245: val_precision did not improve
Epoch 00246: val_precision did not improve
Epoch 00247: val_precision did not improve
Epoch 00248: val_precision did not improve
Epoch 00249: val_precision did not improve
Epoch 00250: val_precision did not improve
Epoch 00251: val_precision did not improve
Epoch 00252: val_precision did not improve
Epoch 00253: val_precision did not improve
Epoch 00254: val_precision did not improve
Epoch 00255: val_precision did not improve
Epoch 00256: val_precision did not improve
Epoch 00257: val_precision did not improve
Epoch 00258: val_precision did not improve
Epoch 00259: val_precision did not improve
Epoch 00260: val_precision did not improve
Epoch 00261: val_precision did not improve
Epoch 00262: val_precision did not improve
Epoch 00263: val_precision did not improve
Epoch 00264: val_precision did not improve
Epoch 00265: val_precision did not improve
Epoch 00266: val_precision did not improve
Epoch 00267: val_precision did not improve
Epoch 00268: val_precision did not improve
Epoch 00269: val_precision did not improve
Epoch 00270: val_precision did not improve
Epoch 00271: val_precision did not improve
Epoch 00272: val_precision did not improve
Epoch 00273: val_precision did not improve
Epoch 00274: val_precision did not improve
Epoch 00275: val_precision did not improve
Epoch 00276: val_precision did not improve
Epoch 00277: val_precision did not improve
Epoch 00278: val_precision did not improve
Epoch 00279: val_precision did not improve
Epoch 00280: val_precision did not improve
Epoch 00281: val_precision did not improve
Epoch 00282: val_precision did not improve
Epoch 00283: val_precision did not improve
Epoch 00284: val_precision did not improve
Epoch 00285: val_precision did not improve
Epoch 00286: val_precision did not improve
Epoch 00287: val_precision did not improve
Epoch 00288: val_precision did not improve
Epoch 00289: val_precision did not improve
Epoch 00290: val_precision did not improve
Epoch 00291: val_precision did not improve
Epoch 00292: val_precision did not improve
Epoch 00293: val_precision did not improve
Epoch 00294: val_precision did not improve
Epoch 00295: val_precision did not improve
Epoch 00296: val_precision did not improve
Epoch 00297: val_precision did not improve
Epoch 00298: val_precision did not improve
Epoch 00299: val_precision did not improve
Epoch 00300: val_precision did not improve
Epoch 00301: val_precision did not improve
Epoch 00302: val_precision did not improve
Epoch 00303: val_precision did not improve
Epoch 00304: val_precision did not improve
Epoch 00305: val_precision did not improve
Epoch 00306: val_precision did not improve
Epoch 00307: val_precision did not improve
Epoch 00308: val_precision did not improve
Epoch 00309: val_precision did not improve
Epoch 00310: val_precision did not improve
Epoch 00311: val_precision did not improve
Epoch 00312: val_precision did not improve
Epoch 00313: val_precision did not improve
Epoch 00314: val_precision did not improve
Epoch 00315: val_precision did not improve
Epoch 00316: val_precision did not improve
Epoch 00317: val_precision did not improve
Epoch 00318: val_precision did not improve
Epoch 00319: val_precision did not improve
Epoch 00320: val_precision did not improve
Epoch 00321: val_precision did not improve
Epoch 00322: val_precision did not improve
Epoch 00323: val_precision did not improve
Epoch 00324: val_precision did not improve
Epoch 00325: val_precision did not improve
Epoch 00326: val_precision did not improve
Epoch 00327: val_precision did not improve
Epoch 00328: val_precision did not improve
Epoch 00329: val_precision did not improve
Epoch 00330: val_precision did not improve
Epoch 00331: val_precision did not improve
Epoch 00332: val_precision did not improve
Epoch 00333: val_precision did not improve
Epoch 00334: val_precision did not improve
Epoch 00335: val_precision did not improve
Epoch 00336: val_precision did not improve
Epoch 00337: val_precision did not improve
Epoch 00338: val_precision did not improve
Epoch 00339: val_precision did not improve
Epoch 00340: val_precision did not improve
Epoch 00341: val_precision did not improve
Epoch 00342: val_precision did not improve
Epoch 00343: val_precision did not improve
Epoch 00344: val_precision did not improve
Epoch 00345: val_precision did not improve
Epoch 00346: val_precision did not improve
Epoch 00347: val_precision did not improve
Epoch 00348: val_precision did not improve
Epoch 00349: val_precision did not improve
Epoch 00350: val_precision did not improve
Epoch 00351: val_precision did not improve
Epoch 00352: val_precision did not improve
Epoch 00353: val_precision did not improve
Epoch 00354: val_precision did not improve
Epoch 00355: val_precision did not improve
Epoch 00356: val_precision did not improve
Epoch 00357: val_precision did not improve
Epoch 00358: val_precision did not improve
Epoch 00359: val_precision did not improve
Epoch 00360: val_precision did not improve
Epoch 00361: val_precision did not improve
Epoch 00362: val_precision did not improve
Epoch 00363: val_precision did not improve
Epoch 00364: val_precision did not improve
Epoch 00365: val_precision did not improve
Epoch 00366: val_precision did not improve
Epoch 00367: val_precision did not improve
Epoch 00368: val_precision did not improve
Epoch 00369: val_precision did not improve
Epoch 00370: val_precision did not improve
Epoch 00371: val_precision did not improve
Epoch 00372: val_precision did not improve
Epoch 00373: val_precision did not improve
Epoch 00374: val_precision did not improve
Epoch 00375: val_precision did not improve
Epoch 00376: val_precision did not improve
Epoch 00377: val_precision did not improve
Epoch 00378: val_precision did not improve
Epoch 00379: val_precision did not improve
Epoch 00380: val_precision did not improve
Epoch 00381: val_precision did not improve
Epoch 00382: val_precision did not improve
Epoch 00383: val_precision did not improve
Epoch 00384: val_precision did not improve
Epoch 00385: val_precision did not improve
Epoch 00386: val_precision did not improve
Epoch 00387: val_precision did not improve
Epoch 00388: val_precision did not improve
Epoch 00389: val_precision did not improve
Epoch 00390: val_precision did not improve
Epoch 00391: val_precision did not improve
Epoch 00392: val_precision did not improve
Epoch 00393: val_precision did not improve
Epoch 00394: val_precision did not improve
Epoch 00395: val_precision did not improve
Epoch 00396: val_precision did not improve
Epoch 00397: val_precision did not improve
Epoch 00398: val_precision did not improve
Epoch 00399: val_precision did not improve
Epoch 00400: val_precision did not improve
Epoch 00401: val_precision did not improve
Epoch 00402: val_precision did not improve
Epoch 00403: val_precision did not improve
Epoch 00404: val_precision did not improve
Epoch 00405: val_precision did not improve
Epoch 00406: val_precision did not improve
Epoch 00407: val_precision did not improve
Epoch 00408: val_precision did not improve
Epoch 00409: val_precision did not improve
Epoch 00410: val_precision did not improve
Epoch 00411: val_precision did not improve
Epoch 00412: val_precision did not improve
Epoch 00413: val_precision did not improve
Epoch 00414: val_precision did not improve
Epoch 00415: val_precision did not improve
Epoch 00416: val_precision did not improve
Epoch 00417: val_precision did not improve
Epoch 00418: val_precision did not improve
Epoch 00419: val_precision did not improve
Epoch 00420: val_precision did not improve
Epoch 00421: val_precision did not improve
Epoch 00422: val_precision did not improve
Epoch 00423: val_precision did not improve
Epoch 00424: val_precision did not improve
Epoch 00425: val_precision did not improve
Epoch 00426: val_precision did not improve
Epoch 00427: val_precision did not improve
Epoch 00428: val_precision did not improve
Epoch 00429: val_precision did not improve
Epoch 00430: val_precision did not improve
Epoch 00431: val_precision did not improve
Epoch 00432: val_precision did not improve
Epoch 00433: val_precision did not improve
Epoch 00434: val_precision did not improve
Epoch 00435: val_precision did not improve
Epoch 00436: val_precision did not improve
Epoch 00437: val_precision did not improve
Epoch 00438: val_precision did not improve
Epoch 00439: val_precision did not improve
Epoch 00440: val_precision did not improve
Epoch 00441: val_precision did not improve
Epoch 00442: val_precision did not improve
Epoch 00443: val_precision did not improve
Epoch 00444: val_precision did not improve
Epoch 00445: val_precision did not improve
Epoch 00446: val_precision did not improve
Epoch 00447: val_precision did not improve
Epoch 00448: val_precision did not improve
Epoch 00449: val_precision did not improve
Epoch 00450: val_precision did not improve
Epoch 00451: val_precision did not improve
Epoch 00452: val_precision did not improve
Epoch 00453: val_precision did not improve
Epoch 00454: val_precision did not improve
Epoch 00455: val_precision did not improve
Epoch 00456: val_precision did not improve
Epoch 00457: val_precision did not improve
Epoch 00458: val_precision did not improve
Epoch 00459: val_precision did not improve
Epoch 00460: val_precision did not improve
Epoch 00461: val_precision did not improve
Epoch 00462: val_precision did not improve
Epoch 00463: val_precision did not improve
Epoch 00464: val_precision did not improve
Epoch 00465: val_precision did not improve
Epoch 00466: val_precision did not improve
Epoch 00467: val_precision did not improve
Epoch 00468: val_precision did not improve
Epoch 00469: val_precision did not improve
Epoch 00470: val_precision did not improve
Epoch 00471: val_precision did not improve
Epoch 00472: val_precision did not improve
Epoch 00473: val_precision did not improve
Epoch 00474: val_precision did not improve
Epoch 00475: val_precision did not improve
Epoch 00476: val_precision did not improve
Epoch 00477: val_precision did not improve
Epoch 00478: val_precision did not improve
Epoch 00479: val_precision did not improve
Epoch 00480: val_precision did not improve
Epoch 00481: val_precision did not improve
Epoch 00482: val_precision did not improve
Epoch 00483: val_precision did not improve
Epoch 00484: val_precision did not improve
Epoch 00485: val_precision did not improve
Epoch 00486: val_precision did not improve
Epoch 00487: val_precision did not improve
Epoch 00488: val_precision did not improve
Epoch 00489: val_precision did not improve
Epoch 00490: val_precision did not improve
Epoch 00491: val_precision did not improve
Epoch 00492: val_precision did not improve
Epoch 00493: val_precision did not improve
Epoch 00494: val_precision did not improve
Epoch 00495: val_precision did not improve
Epoch 00496: val_precision did not improve
Epoch 00497: val_precision did not improve
Epoch 00498: val_precision did not improve
Epoch 00499: val_precision did not improve

In [13]:
# evaluation
print('evaluating model')
score = model.evaluate(X_test, Y_test, verbose=1)
print('Test score:', score[0])
print('Test accuracy:', score[1]*100, '%')
print('Test error:', (1-score[2])*100, '%')


evaluating model
468/468 [==============================] - 0s     
Test score: 0.175480633164
Test accuracy: 96.1538461538 %
Test error: 3.84615384615 %

In [14]:
u.plot_history(history)
u.plot_history(history, metric='loss', loc='upper left')


Inspecting the result


In [15]:
# The predict_classes function outputs the highest probability class
# according to the trained classifier for each input example.
predicted_classes = model.predict_classes(X_test)

# Check which items we got right / wrong
correct_indices = np.nonzero(predicted_classes == y_test)[0]
incorrect_indices = np.nonzero(predicted_classes != y_test)[0]


416/468 [=========================>....] - ETA: 0s

Examples of correct predictions


In [16]:
u.plot_images(X_test[correct_indices[:9]], y_test[correct_indices[:9]], 
              predicted_classes[correct_indices[:9]], img_shape=(56,34))


Examples of incorrect predictions


In [17]:
u.plot_images(X_test[incorrect_indices[:9]], y_test[incorrect_indices[:9]], 
              predicted_classes[incorrect_indices[:9]], img_shape=(56,34))


Confusion matrix


In [18]:
u.plot_confusion_matrix(y_test, nb_classes, predicted_classes)


[[221  13]
 [  5 229]]