text_plain_output_1.png image_output_2.png image_output_1.png
from keras import optimizers

from keras.layers import Activation, Convolution2D, Dropout, Conv2D,MaxPool2D

from keras.layers import Dense, Conv2D , MaxPooling2D , Flatten , Dropout

from keras.layers.core import Dense, Dropout, Activation, Flatten

from keras.models import Sequential

from keras.models import Sequential

from sklearn.metrics import classification_report, confusion_matrix

from sklearn.preprocessing import LabelBinarizer

import keras

import matplotlib.pyplot as plt

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import seaborn as sns

train = pd.read_csv('/kaggle/input/sign-language-mnist/sign_mnist_train.csv')
test = pd.read_csv('/kaggle/input/sign-language-mnist/sign_mnist_test.csv')
X_train = train.iloc[:, 1:]
Y_train = train.iloc[:, 0]
X_test = test.iloc[:, 1:]
Y_test = test.iloc[:, 0]

(X_train.shape, Y_train.shape)

X_train = np.array(X_train)
X_test = np.array(X_test)

X_train = X_train / 255
X_test = X_test / 255

X_train = X_train.reshape(-1, 28, 28, 1)
X_test = X_test.reshape(-1, 28, 28, 1)

X_train.shape

from sklearn.preprocessing import LabelBinarizer
encoder = LabelBinarizer()
Y_train = encoder.fit_transform(Y_train)
Y_test = encoder.fit_transform(Y_test)

model = Sequential()
model.add(Conv2D(input_shape=(28, 28, 1), filters=20, kernel_size=(3, 3), padding='same', activation='relu'))
model.add(keras.layers.Dropout(0.1))
model.add(MaxPool2D(2, 2))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPool2D(2, 2))
model.add(keras.layers.Dropout(0.1))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPool2D((2, 2)))
model.add(keras.layers.Dropout(0.1))
model.add(Conv2D(128, (1, 1), activation='relu'))
model.add(MaxPool2D((2, 2)))
model.add(keras.layers.Dropout(0.1))
model.add(Flatten())
model.add(Dense(units=64, activation='relu'))
model.add(keras.layers.Dropout(0.1))
model.add(Dense(units=24, activation='softmax'))

learning_rate = 0.001
lr_decay = 1e-06
sgd = optimizers.SGD(lr=learning_rate, decay=lr_decay, momentum=0.9, nesterov=True)
adam = optimizers.Adam(lr=0.002)
model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'])
history = model.fit(X_train, Y_train, epochs=60, batch_size=524, verbose=1, validation_data=(X_valid, Y_valid))

testModel = model.evaluate(X_test, Y_test)

predicted_classes = model.predict_classes(X_test)
rounded_labels = np.argmax(Y_test, axis=1)
confusionMatrix = confusion_matrix(rounded_labels, predicted_classes)
confusionMatrix = pd.DataFrame(confusionMatrix, index=[i for i in range(25) if i != 9], columns=[i for i in range(25) if i != 9])

classes = ['Class ' + str(i) for i in range(25) if i != 9]
print(classification_report(rounded_labels, predicted_classes, target_names=classes))