In Codice Ratio Convolutional Neural Network - Test (with 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.preprocessing.image import ImageDataGenerator
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 = 800
# checkpoint path
checkpoints_filepath = "checkpoints/06_ICR_A_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)

Image preprocessing


In [11]:
datagen = ImageDataGenerator(
    rotation_range=30,
    width_shift_range=0.05,
    height_shift_range=0.05,
    zoom_range=0.1,
    horizontal_flip=False)

# compute quantities required for featurewise normalization
# (std, mean, and principal components if ZCA whitening is applied)
datagen.fit(X_train)

Model definition


In [12]:
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 [13]:
# checkpoint
checkpoint = ModelCheckpoint(checkpoints_filepath, monitor='val_precision', verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint]

In [14]:
# fits the model on batches with real-time data augmentation, for nb_epoch-100 epochs
history = model.fit_generator(datagen.flow(X_train, Y_train, 
                                           batch_size=batch_size, 
                                          # save_to_dir='distorted_data', 
                                          # save_format='png'
                                           seed=1337),
                              samples_per_epoch=len(X_train), nb_epoch=nb_epoch-50, 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)


Epoch 00000: val_precision improved from -inf to 0.94017, saving model to checkpoints/05_ICR_A_dist_weights.best.hdf5
Epoch 00001: val_precision improved from 0.94017 to 0.94658, saving model to checkpoints/05_ICR_A_dist_weights.best.hdf5
Epoch 00002: val_precision did not improve
Epoch 00003: val_precision improved from 0.94658 to 0.95085, saving model to checkpoints/05_ICR_A_dist_weights.best.hdf5
Epoch 00004: val_precision did not improve
Epoch 00005: val_precision did not improve
Epoch 00006: val_precision did not improve
Epoch 00007: val_precision did not improve
Epoch 00008: val_precision did not improve
Epoch 00009: val_precision did not improve
Epoch 00010: val_precision did not improve
Epoch 00011: val_precision did not improve
Epoch 00012: val_precision did not improve
Epoch 00013: val_precision did not improve
Epoch 00014: val_precision did not improve
Epoch 00015: val_precision did not improve
Epoch 00016: val_precision did not improve
Epoch 00017: val_precision did not improve
Epoch 00018: val_precision did not improve
Epoch 00019: val_precision did not improve
Epoch 00020: val_precision did not improve
Epoch 00021: val_precision did not improve
Epoch 00022: val_precision did not improve
Epoch 00023: val_precision did not improve
Epoch 00024: val_precision did not improve
Epoch 00025: val_precision improved from 0.95085 to 0.95513, saving model to checkpoints/05_ICR_A_dist_weights.best.hdf5
Epoch 00026: val_precision did not improve
Epoch 00027: val_precision did not improve
Epoch 00028: val_precision did not improve
Epoch 00029: val_precision did not improve
Epoch 00030: val_precision did not improve
Epoch 00031: val_precision did not improve
Epoch 00032: val_precision did not improve
Epoch 00033: val_precision did not improve
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 did not improve
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 did not improve
Epoch 00044: val_precision did not improve
Epoch 00045: val_precision did not improve
Epoch 00046: val_precision did not improve
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 did not improve
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 did not improve
Epoch 00065: val_precision did not improve
Epoch 00066: val_precision did not improve
Epoch 00067: val_precision did not improve
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 improved from 0.95513 to 0.95513, saving model to checkpoints/05_ICR_A_dist_weights.best.hdf5
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 improved from 0.95513 to 0.95940, saving model to checkpoints/05_ICR_A_dist_weights.best.hdf5
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 did not improve
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 did not improve
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 improved from 0.95940 to 0.96154, saving model to checkpoints/05_ICR_A_dist_weights.best.hdf5
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 improved from 0.96154 to 0.96368, saving model to checkpoints/05_ICR_A_dist_weights.best.hdf5
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
Epoch 00500: val_precision did not improve
Epoch 00501: val_precision did not improve
Epoch 00502: val_precision did not improve
Epoch 00503: val_precision did not improve
Epoch 00504: val_precision did not improve
Epoch 00505: val_precision did not improve
Epoch 00506: val_precision did not improve
Epoch 00507: val_precision did not improve
Epoch 00508: val_precision did not improve
Epoch 00509: val_precision did not improve
Epoch 00510: val_precision did not improve
Epoch 00511: val_precision did not improve
Epoch 00512: val_precision did not improve
Epoch 00513: val_precision did not improve
Epoch 00514: val_precision did not improve
Epoch 00515: val_precision did not improve
Epoch 00516: val_precision did not improve
Epoch 00517: val_precision did not improve
Epoch 00518: val_precision did not improve
Epoch 00519: val_precision did not improve
Epoch 00520: val_precision did not improve
Epoch 00521: val_precision did not improve
Epoch 00522: val_precision did not improve
Epoch 00523: val_precision did not improve
Epoch 00524: val_precision did not improve
Epoch 00525: val_precision did not improve
Epoch 00526: val_precision did not improve
Epoch 00527: val_precision did not improve
Epoch 00528: val_precision did not improve
Epoch 00529: val_precision did not improve
Epoch 00530: val_precision did not improve
Epoch 00531: val_precision did not improve
Epoch 00532: val_precision did not improve
Epoch 00533: val_precision did not improve
Epoch 00534: val_precision did not improve
Epoch 00535: val_precision did not improve
Epoch 00536: val_precision did not improve
Epoch 00537: val_precision did not improve
Epoch 00538: val_precision did not improve
Epoch 00539: val_precision did not improve
Epoch 00540: val_precision did not improve
Epoch 00541: val_precision did not improve
Epoch 00542: val_precision did not improve
Epoch 00543: val_precision did not improve
Epoch 00544: val_precision did not improve
Epoch 00545: val_precision did not improve
Epoch 00546: val_precision did not improve
Epoch 00547: val_precision did not improve
Epoch 00548: val_precision did not improve
Epoch 00549: val_precision did not improve
Epoch 00550: val_precision did not improve
Epoch 00551: val_precision did not improve
Epoch 00552: val_precision did not improve
Epoch 00553: val_precision did not improve
Epoch 00554: val_precision did not improve
Epoch 00555: val_precision did not improve
Epoch 00556: val_precision did not improve
Epoch 00557: val_precision did not improve
Epoch 00558: val_precision did not improve
Epoch 00559: val_precision did not improve
Epoch 00560: val_precision did not improve
Epoch 00561: val_precision did not improve
Epoch 00562: val_precision did not improve
Epoch 00563: val_precision did not improve
Epoch 00564: val_precision did not improve
Epoch 00565: val_precision did not improve
Epoch 00566: val_precision did not improve
Epoch 00567: val_precision did not improve
Epoch 00568: val_precision did not improve
Epoch 00569: val_precision did not improve
Epoch 00570: val_precision did not improve
Epoch 00571: val_precision did not improve
Epoch 00572: val_precision did not improve
Epoch 00573: val_precision did not improve
Epoch 00574: val_precision did not improve
Epoch 00575: val_precision did not improve
Epoch 00576: val_precision did not improve
Epoch 00577: val_precision did not improve
Epoch 00578: val_precision did not improve
Epoch 00579: val_precision did not improve
Epoch 00580: val_precision did not improve
Epoch 00581: val_precision did not improve
Epoch 00582: val_precision did not improve
Epoch 00583: val_precision did not improve
Epoch 00584: val_precision did not improve
Epoch 00585: val_precision did not improve
Epoch 00586: val_precision did not improve
Epoch 00587: val_precision did not improve
Epoch 00588: val_precision did not improve
Epoch 00589: val_precision did not improve
Epoch 00590: val_precision did not improve
Epoch 00591: val_precision did not improve
Epoch 00592: val_precision did not improve
Epoch 00593: val_precision did not improve
Epoch 00594: val_precision did not improve
Epoch 00595: val_precision did not improve
Epoch 00596: val_precision did not improve
Epoch 00597: val_precision did not improve
Epoch 00598: val_precision did not improve
Epoch 00599: val_precision did not improve
Epoch 00600: val_precision did not improve
Epoch 00601: val_precision did not improve
Epoch 00602: val_precision did not improve
Epoch 00603: val_precision did not improve
Epoch 00604: val_precision did not improve
Epoch 00605: val_precision did not improve
Epoch 00606: val_precision did not improve
Epoch 00607: val_precision did not improve
Epoch 00608: val_precision did not improve
Epoch 00609: val_precision did not improve
Epoch 00610: val_precision did not improve
Epoch 00611: val_precision did not improve
Epoch 00612: val_precision did not improve
Epoch 00613: val_precision did not improve
Epoch 00614: val_precision did not improve
Epoch 00615: val_precision did not improve
Epoch 00616: val_precision did not improve
Epoch 00617: val_precision did not improve
Epoch 00618: val_precision did not improve
Epoch 00619: val_precision did not improve
Epoch 00620: val_precision did not improve
Epoch 00621: val_precision did not improve
Epoch 00622: val_precision did not improve
Epoch 00623: val_precision did not improve
Epoch 00624: val_precision did not improve
Epoch 00625: val_precision did not improve
Epoch 00626: val_precision did not improve
Epoch 00627: val_precision did not improve
Epoch 00628: val_precision did not improve
Epoch 00629: val_precision did not improve
Epoch 00630: val_precision did not improve
Epoch 00631: val_precision did not improve
Epoch 00632: val_precision did not improve
Epoch 00633: val_precision did not improve
Epoch 00634: val_precision did not improve
Epoch 00635: val_precision did not improve
Epoch 00636: val_precision did not improve
Epoch 00637: val_precision did not improve
Epoch 00638: val_precision did not improve
Epoch 00639: val_precision did not improve
Epoch 00640: val_precision did not improve
Epoch 00641: val_precision did not improve
Epoch 00642: val_precision did not improve
Epoch 00643: val_precision did not improve
Epoch 00644: val_precision did not improve
Epoch 00645: val_precision did not improve
Epoch 00646: val_precision did not improve
Epoch 00647: val_precision did not improve
Epoch 00648: val_precision did not improve
Epoch 00649: val_precision did not improve
Epoch 00650: val_precision did not improve
Epoch 00651: val_precision did not improve
Epoch 00652: val_precision did not improve
Epoch 00653: val_precision did not improve
Epoch 00654: val_precision did not improve
Epoch 00655: val_precision did not improve
Epoch 00656: val_precision did not improve
Epoch 00657: val_precision did not improve
Epoch 00658: val_precision did not improve
Epoch 00659: val_precision did not improve
Epoch 00660: val_precision did not improve
Epoch 00661: val_precision did not improve
Epoch 00662: val_precision did not improve
Epoch 00663: val_precision did not improve
Epoch 00664: val_precision improved from 0.96368 to 0.96581, saving model to checkpoints/05_ICR_A_dist_weights.best.hdf5
Epoch 00665: val_precision did not improve
Epoch 00666: val_precision did not improve
Epoch 00667: val_precision did not improve
Epoch 00668: val_precision did not improve
Epoch 00669: val_precision did not improve
Epoch 00670: val_precision did not improve
Epoch 00671: val_precision did not improve
Epoch 00672: val_precision did not improve
Epoch 00673: val_precision did not improve
Epoch 00674: val_precision did not improve
Epoch 00675: val_precision did not improve
Epoch 00676: val_precision did not improve
Epoch 00677: val_precision did not improve
Epoch 00678: val_precision did not improve
Epoch 00679: val_precision did not improve
Epoch 00680: val_precision did not improve
Epoch 00681: val_precision did not improve
Epoch 00682: val_precision did not improve
Epoch 00683: val_precision did not improve
Epoch 00684: val_precision did not improve
Epoch 00685: val_precision did not improve
Epoch 00686: val_precision did not improve
Epoch 00687: val_precision did not improve
Epoch 00688: val_precision did not improve
Epoch 00689: val_precision did not improve
Epoch 00690: val_precision did not improve
Epoch 00691: val_precision did not improve
Epoch 00692: val_precision did not improve
Epoch 00693: val_precision did not improve
Epoch 00694: val_precision did not improve
Epoch 00695: val_precision did not improve
Epoch 00696: val_precision did not improve
Epoch 00697: val_precision did not improve
Epoch 00698: val_precision did not improve
Epoch 00699: val_precision did not improve
Epoch 00700: val_precision did not improve
Epoch 00701: val_precision did not improve
Epoch 00702: val_precision did not improve
Epoch 00703: val_precision did not improve
Epoch 00704: val_precision did not improve
Epoch 00705: val_precision did not improve
Epoch 00706: val_precision did not improve
Epoch 00707: val_precision did not improve
Epoch 00708: val_precision did not improve
Epoch 00709: val_precision did not improve
Epoch 00710: val_precision did not improve
Epoch 00711: val_precision did not improve
Epoch 00712: val_precision did not improve
Epoch 00713: val_precision did not improve
Epoch 00714: val_precision did not improve
Epoch 00715: val_precision did not improve
Epoch 00716: val_precision did not improve
Epoch 00717: val_precision did not improve
Epoch 00718: val_precision did not improve
Epoch 00719: val_precision did not improve
Epoch 00720: val_precision did not improve
Epoch 00721: val_precision did not improve
Epoch 00722: val_precision did not improve
Epoch 00723: val_precision did not improve
Epoch 00724: val_precision did not improve
Epoch 00725: val_precision did not improve
Epoch 00726: val_precision did not improve
Epoch 00727: val_precision did not improve
Epoch 00728: val_precision did not improve
Epoch 00729: val_precision did not improve
Epoch 00730: val_precision did not improve
Epoch 00731: val_precision did not improve
Epoch 00732: val_precision did not improve
Epoch 00733: val_precision did not improve
Epoch 00734: val_precision did not improve
Epoch 00735: val_precision did not improve
Epoch 00736: val_precision did not improve
Epoch 00737: val_precision did not improve
Epoch 00738: val_precision did not improve
Epoch 00739: val_precision did not improve
Epoch 00740: val_precision did not improve
Epoch 00741: val_precision did not improve
Epoch 00742: val_precision did not improve
Epoch 00743: val_precision did not improve
Epoch 00744: val_precision did not improve
Epoch 00745: val_precision did not improve
Epoch 00746: val_precision did not improve
Epoch 00747: val_precision did not improve
Epoch 00748: val_precision did not improve
Epoch 00749: val_precision did not improve

In [15]:
# training
history_cont = model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=50,
          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)


Epoch 00000: val_precision did not improve
Epoch 00001: val_precision did not improve
Epoch 00002: val_precision did not improve
Epoch 00003: val_precision did not improve
Epoch 00004: val_precision did not improve
Epoch 00005: val_precision did not improve
Epoch 00006: val_precision improved from 0.96581 to 0.96795, saving model to checkpoints/05_ICR_A_dist_weights.best.hdf5
Epoch 00007: val_precision did not improve
Epoch 00008: val_precision did not improve
Epoch 00009: val_precision did not improve
Epoch 00010: val_precision did not improve
Epoch 00011: val_precision did not improve
Epoch 00012: val_precision did not improve
Epoch 00013: val_precision did not improve
Epoch 00014: val_precision did not improve
Epoch 00015: val_precision did not improve
Epoch 00016: val_precision did not improve
Epoch 00017: val_precision did not improve
Epoch 00018: val_precision did not improve
Epoch 00019: val_precision did not improve
Epoch 00020: val_precision did not improve
Epoch 00021: val_precision did not improve
Epoch 00022: val_precision did not improve
Epoch 00023: val_precision did not improve
Epoch 00024: val_precision did not improve
Epoch 00025: val_precision did not improve
Epoch 00026: val_precision did not improve
Epoch 00027: val_precision did not improve
Epoch 00028: val_precision did not improve
Epoch 00029: val_precision did not improve
Epoch 00030: val_precision did not improve
Epoch 00031: val_precision did not improve
Epoch 00032: val_precision did not improve
Epoch 00033: val_precision did not improve
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 did not improve
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 did not improve
Epoch 00044: val_precision did not improve
Epoch 00045: val_precision did not improve
Epoch 00046: val_precision did not improve
Epoch 00047: val_precision did not improve
Epoch 00048: val_precision did not improve
Epoch 00049: val_precision did not improve

In [16]:
# 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.144882577822
Test accuracy: 96.7948717949 %
Test error: 3.20512820513 %

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

u.plot_history(history_cont)
u.plot_history(history_cont, metric='loss', loc='upper left')


Inspecting the result


In [18]:
# 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 [19]:
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 [20]:
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 [21]:
u.plot_confusion_matrix(y_test, nb_classes, predicted_classes)


[[224  10]
 [  5 229]]