text_plain_output_2.png text_plain_output_1.png image_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 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])

x_train1, x_valid1, x_test1, y_train1, y_valid1, y_test1 = tts(gray_images, clabels)
x_train2, x_valid2, x_test2, y_train2, y_valid2, y_test2 = tts(gray_images, ctargets)
y_train2_list = [y_train2[:, :33], y_train2[:, 33:]]
y_test2_list = [y_test2[:, :33], y_test2[:, 33:]]
y_valid2_list = [y_valid2[:, :33], y_valid2[:, 33:]]

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])

def gray_multi_model():
    model_input = Input(shape=(32, 32, 1))
    x = BatchNormalization()(model_input)
    x = Conv2D(32, (5, 5), padding='same')(model_input)
    x = LeakyReLU(alpha=0.02)(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)
    x = Dropout(0.25)(x)
    x = Conv2D(256, (5, 5), padding='same')(x)
    x = LeakyReLU(alpha=0.02)(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)
    x = Dropout(0.25)(x)
    x = GlobalMaxPooling2D()(x)
    x = Dense(1024)(x)
    x = LeakyReLU(alpha=0.02)(x)
    x = Dropout(0.25)(x)
    x = Dense(256)(x)
    x = LeakyReLU(alpha=0.02)(x)
    x = Dropout(0.025)(x)
    y1 = Dense(33, activation='softmax')(x)
    y2 = Dense(4, activation='softmax')(x)
    model = Model(inputs=model_input, outputs=[y1, y2])
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=[categorical_accuracy, top_3_categorical_accuracy])
    return model
gray_multi_model = gray_multi_model()

checkpointer = ModelCheckpoint(filepath=fw, verbose=2, save_best_only=True)
lr_reduction = ReduceLROnPlateau(monitor='val_loss', patience=5, verbose=2, factor=0.75)
estopping = EarlyStopping(monitor='val_loss', patience=20, verbose=2)
history = gray_multi_model.fit(x_train2, y_train2_list, epochs=200, batch_size=128, verbose=2, validation_data=(x_valid2, y_valid2_list), callbacks=[checkpointer, lr_reduction, estopping])

gray_multi_model.evaluate(x_test2, y_test2_list)