Jovian
⭐️
Sign In
In [2]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
In [3]:
train = pd.read_csv('C:\\Users\\vsneh\\Udemy-notebooks\\sign-language-kaggle\\sign_mnist_train.csv')
test = pd.read_csv('C:\\Users\\vsneh\\Udemy-notebooks\\sign-language-kaggle\\sign_mnist_test.csv')
In [4]:
train_labels = train['label'].values
In [5]:
test_labels = test['label'].values
In [6]:
np.unique(np.array(train_labels))
Out[6]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8, 10, 11, 12, 13, 14, 15, 16, 17,
       18, 19, 20, 21, 22, 23, 24], dtype=int64)
In [7]:
train.drop('label',axis = 1,inplace=True)
In [8]:
test.drop('label',axis = 1,inplace=True)
In [9]:
train = np.array([np.reshape(i,(28,28)).flatten() for i in train.values])
In [10]:
test = np.array([np.reshape(i,(28,28)).flatten() for i in test.values])
In [11]:
test.shape
Out[11]:
(7172, 784)
In [12]:
plt.imshow(train[0].reshape(28,28))
Out[12]:
<matplotlib.image.AxesImage at 0x2479cffe2c8>
Notebook Image
In [13]:
from sklearn.preprocessing import LabelBinarizer
l_b = LabelBinarizer()
train_labels = l_b.fit_transform(train_labels)
In [14]:
from sklearn.model_selection import train_test_split
In [15]:
x_train, x_test, y_train, y_test = train_test_split(train, train_labels, test_size = 0.3, random_state = 101)
In [16]:
import keras
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Dropout
Using TensorFlow backend.
In [17]:
batch_size = 128
num_classes = 24
epochs = 10
In [18]:
x_train = x_train / 255
x_test = x_test / 255
In [19]:
test_labels = l_b.fit_transform(test_labels)
In [20]:
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
In [ ]:
 
In [21]:
x_train.shape
Out[21]:
(19218, 28, 28, 1)
In [22]:
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
In [23]:
plt.imshow(x_train[0].reshape(28,28))
Out[23]:
<matplotlib.image.AxesImage at 0x247afc93fc8>
Notebook Image
In [24]:
model = Sequential()
model.add(Conv2D(64, kernel_size=(3,3), activation = 'relu', input_shape=(28, 28 ,1) ))
model.add(MaxPooling2D(pool_size = (2, 2)))

model.add(Conv2D(64, kernel_size = (3, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))

model.add(Conv2D(64, kernel_size = (3, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))

model.add(Flatten())
model.add(Dense(128, activation = 'relu'))
model.add(Dropout(0.20))
model.add(Dense(num_classes, activation = 'softmax'))
In [25]:
model.compile(loss = keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adam(),
              metrics=['accuracy'])
In [26]:
history = model.fit(x_train, y_train, validation_data = (x_test, y_test), epochs=epochs, batch_size=batch_size)
Train on 19218 samples, validate on 8237 samples Epoch 1/10 19218/19218 [==============================] - 15s 802us/step - loss: 2.7084 - accuracy: 0.1886 - val_loss: 1.7391 - val_accuracy: 0.4673 Epoch 2/10 19218/19218 [==============================] - 15s 801us/step - loss: 1.3122 - accuracy: 0.5696 - val_loss: 0.9336 - val_accuracy: 0.7156 Epoch 3/10 19218/19218 [==============================] - 14s 737us/step - loss: 0.7788 - accuracy: 0.7402 - val_loss: 0.5295 - val_accuracy: 0.8306 Epoch 4/10 19218/19218 [==============================] - 14s 741us/step - loss: 0.5398 - accuracy: 0.8195 - val_loss: 0.3598 - val_accuracy: 0.8843 Epoch 5/10 19218/19218 [==============================] - 15s 770us/step - loss: 0.3881 - accuracy: 0.8671 - val_loss: 0.2744 - val_accuracy: 0.9040 Epoch 6/10 19218/19218 [==============================] - 14s 735us/step - loss: 0.2714 - accuracy: 0.9088 - val_loss: 0.1620 - val_accuracy: 0.9539 Epoch 7/10 19218/19218 [==============================] - 14s 747us/step - loss: 0.2055 - accuracy: 0.9330 - val_loss: 0.1227 - val_accuracy: 0.9662 Epoch 8/10 19218/19218 [==============================] - 15s 767us/step - loss: 0.1535 - accuracy: 0.9500 - val_loss: 0.0743 - val_accuracy: 0.9829 Epoch 9/10 19218/19218 [==============================] - 15s 803us/step - loss: 0.1148 - accuracy: 0.9637 - val_loss: 0.0615 - val_accuracy: 0.9865 Epoch 10/10 19218/19218 [==============================] - 15s 777us/step - loss: 0.0971 - accuracy: 0.9703 - val_loss: 0.0469 - val_accuracy: 0.9904
In [27]:
model.evaluate(x_test,y_test)
8237/8237 [==============================] - 2s 229us/step
Out[27]:
[0.04689275687909812, 0.9904091358184814]
In [28]:
from sklearn.metrics import classification_report

predictions = model.predict_classes(x_test)
In [29]:
predictions
Out[29]:
array([ 5, 11,  4, ...,  2, 19, 14], dtype=int64)
In [30]:
predictions = keras.utils.to_categorical(predictions)
In [31]:
print(classification_report(y_test,predictions))
precision recall f1-score support 0 1.00 0.98 0.99 336 1 1.00 1.00 1.00 302 2 1.00 1.00 1.00 353 3 0.96 1.00 0.98 350 4 1.00 0.99 1.00 280 5 1.00 1.00 1.00 369 6 0.98 1.00 0.99 314 7 0.99 0.98 0.99 326 8 1.00 0.99 1.00 344 9 0.97 0.99 0.98 334 10 1.00 0.99 1.00 344 11 1.00 1.00 1.00 338 12 0.99 1.00 0.99 353 13 1.00 1.00 1.00 351 14 0.99 1.00 0.99 302 15 0.99 1.00 1.00 399 16 0.98 0.90 0.94 402 17 0.98 1.00 0.99 367 18 0.99 1.00 1.00 361 19 0.98 0.98 0.98 359 20 0.98 0.99 0.99 316 21 1.00 1.00 1.00 383 22 0.99 1.00 1.00 331 23 1.00 0.99 1.00 323 micro avg 0.99 0.99 0.99 8237 macro avg 0.99 0.99 0.99 8237 weighted avg 0.99 0.99 0.99 8237 samples avg 0.99 0.99 0.99 8237
In [32]:
test_images = test.reshape(test.shape[0], 28, 28, 1)
In [33]:
from sklearn.metrics import accuracy_score
In [34]:
accuracy_score(y_test, predictions)
Out[34]:
0.990409129537453
In [35]:
test_labels.shape
Out[35]:
(7172, 24)
In [36]:
predictions.shape
Out[36]:
(8237, 24)
In [54]:
p = model.predict(test_images)
In [55]:
p.shape
Out[55]:
(7172, 24)
In [56]:
test_labels.shape
Out[56]:
(7172, 24)
In [57]:
accuracy_score(test_labels.round(),p.round())
Out[57]:
0.8025655326268823
In [ ]:
 
In [58]:
model.save('keras_complex.h5')
In [ ]: