text_plain_output_1.png
from IPython import display

from keras.callbacks import ModelCheckpoint,ReduceLROnPlateau,EarlyStopping

from keras.layers import Conv2D,MaxPooling2D,GlobalMaxPooling2D

from keras.layers import Dense,LSTM,GlobalAveragePooling1D,GlobalAveragePooling2D

from keras.layers import Input,Activation,Flatten,Dropout,BatchNormalization

from keras.layers.advanced_activations import PReLU,LeakyReLU

from keras.metrics import top_k_categorical_accuracy,categorical_accuracy

from keras.models import Sequential,load_model,Model

from keras.preprocessing.image import ImageDataGenerator

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import OneHotEncoder

import warnings
warnings.filterwarnings('ignore')
import numpy as np, pandas as pd, pylab as pl, h5py
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
from IPython import display
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping
from keras.metrics import top_k_categorical_accuracy, categorical_accuracy
from keras.models import Sequential, load_model, Model
from keras.layers import Dense, LSTM, GlobalAveragePooling1D, GlobalAveragePooling2D
from keras.layers.advanced_activations import PReLU, LeakyReLU
from keras.layers import Input, Activation, Flatten, Dropout, BatchNormalization
from keras.layers import Conv2D, MaxPooling2D, GlobalMaxPooling2D
fw = 'weights.best.letters.hdf5'

def history_plot(fit_history):
    keys = list(fit_history.history.keys())[0:4]
def ohe(x):
    return OneHotEncoder(categories='auto').fit(x.reshape(-1, 1)).transform(x.reshape(-1, 1)).toarray().astype('int64')
def tts(X, y):
    x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)
    n = int(len(x_test) / 2)
    x_valid, y_valid = (x_test[:n], y_test[:n])
    x_test, y_test = (x_test[n:], y_test[n:])
    return (x_train, x_valid, x_test, y_train, y_valid, y_test)

f = h5py.File('../input/LetterColorImages_123.h5', 'r')
keys = list(f.keys())
keys

letters = u'абвгдеёжзийклмнопрстуфхцчшщъыьэюя'
backgrounds = np.array(f[keys[0]])
labels = np.array(f[keys[2]])
images = np.array(f[keys[1]]) / 255

gray_images = np.dot(images[..., :3], [0.299, 0.587, 0.114])
rn = np.random.randint(10000)
pl.xticks([])
pl.yticks([])
gray_images = gray_images.reshape(-1, 32, 32, 1)

cbackgrounds, clabels = (ohe(backgrounds), ohe(labels))
ctargets = np.concatenate((clabels, cbackgrounds), axis=1)
pd.DataFrame([clabels.shape, cbackgrounds.shape, ctargets.shape])

def top_3_categorical_accuracy(y_true, y_pred):
    return top_k_categorical_accuracy(y_true, y_pred, k=3)
def gray_model():
    model = Sequential()
    model.add(Conv2D(32, (5, 5), padding='same', input_shape=x_train1.shape[1:]))
    model.add(LeakyReLU(alpha=0.02))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))
    model.add(Conv2D(128, (5, 5)))
    model.add(LeakyReLU(alpha=0.02))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))
    model.add(GlobalMaxPooling2D())
    model.add(Dense(1024))
    model.add(LeakyReLU(alpha=0.02))
    model.add(Dropout(0.25))
    model.add(Dense(128))
    model.add(LeakyReLU(alpha=0.02))
    model.add(Dropout(0.25))
    model.add(Dense(33))
    model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=[categorical_accuracy, top_3_categorical_accuracy])
    return model
gray_model = gray_model()

checkpointer = ModelCheckpoint(filepath=fw, verbose=2, save_best_only=True)
lr_reduction = ReduceLROnPlateau(monitor='val_loss', patience=10, verbose=2, factor=0.5)
estopping = EarlyStopping(monitor='val_loss', patience=20, verbose=2)
history = gray_model.fit(x_train1, y_train1, epochs=200, batch_size=64, verbose=2, validation_data=(x_valid1, y_valid1), callbacks=[checkpointer, lr_reduction, estopping])

gray_model.load_weights(fw)
gray_model.evaluate(x_test1, y_test1)

steps, epochs = (1000, 10)
igen = ImageDataGenerator(zoom_range=0.3, shear_range=0.3, rotation_range=30)
generator = gray_model.fit_generator(igen.flow(x_train1, y_train1, batch_size=64), steps_per_epoch=steps, epochs=epochs, verbose=2, validation_data=(x_valid1, y_valid1), callbacks=[checkpointer, lr_reduction])

gray_model.load_weights(fw)
gray_model.evaluate(x_test1, y_test1)

py_test1 = gray_model.predict_classes(x_test1)
fig = pl.figure(figsize=(12, 12))
for i, idx in enumerate(np.random.choice(x_test1.shape[0], size=16, replace=False)):
    ax = fig.add_subplot(4, 4, i + 1, xticks=[], yticks=[])
    ax.imshow(np.squeeze(x_test1[idx]), cmap=pl.cm.bone)
    pred_idx = py_test1[idx]
    true_idx = np.argmax(y_test1[idx])
    ax.set_title('{} ({})'.format(letters[pred_idx], letters[true_idx]), color='darkblue' if pred_idx == true_idx else 'darkred')