Skip to content
Snippets Groups Projects
Commit d2d3c17e authored by abivarma.kandiah's avatar abivarma.kandiah
Browse files

Sign language translator model work on PC

parent 78bbdd7e
Branches
No related tags found
No related merge requests found
numpy
tensorflow
matplotlib
keras
pandas
\ No newline at end of file
%% Cell type:markdown id: tags:
# SIGN LANGUAGE TRANSLATOR
%% Cell type:code id: tags:
``` python
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import pandas as pd
import random
%matplotlib inline
```
%% Cell type:markdown id: tags:
Get data and label from train and test dataset.
%% Cell type:code id: tags:
``` python
train_path = "dataset/sign_mnist_train.csv"
test_path = "dataset/sign_mnist_test.csv"
train = pd.read_csv(train_path)
test = pd.read_csv(test_path)
train_data = np.array(train)[:, 1:]
train_label = np.array(train)[:, 0]
test_data = np.array(test)[:, 1:]
test_label = np.array(test)[:, 0]
train_data = np.reshape(train_data, (-1, 28, 28, 1))
test_data = np.reshape(test_data, (-1, 28, 28, 1))
```
%% Cell type:markdown id: tags:
Show random image from train dataset.
%% Cell type:code id: tags:
``` python
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
rand_i = random.randint(1, train_data.shape[0])
img = train_data[rand_i]
print(f"Letter : {alphabet[train_label[rand_i]]}")
plt.imshow(img, cmap='gray')
```
%% Cell type:markdown id: tags:
# CNN
Entrainement et evaluation du modèle.
%% Cell type:code id: tags:
``` python
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, Dense, MaxPool2D, Flatten, Rescaling, Dropout
from tensorflow.keras.losses import SparseCategoricalCrossentropy
CLASS_COUNT = int(np.max(train_label) + 1)
INPUT_SHAPE = train_data.shape[1]
BATCH_SIZE = 32#train_data.shape[0] // 4
EPOCHS = 15
train_label = tf.keras.utils.to_categorical(train_label, CLASS_COUNT)
test_label = tf.keras.utils.to_categorical(test_label, CLASS_COUNT)
model = Sequential()
model.add(Rescaling(1./255, input_shape=(INPUT_SHAPE, INPUT_SHAPE, 1)))
model.add(Conv2D(filters=32, kernel_size=3, padding="same", activation='relu'))
model.add(MaxPool2D())
model.add(Conv2D(filters=64, kernel_size=3, padding="same", activation='relu'))
model.add(MaxPool2D())
model.add(Flatten())
model.add(Dropout(0.5))
model.add(Dense(CLASS_COUNT, activation="softmax"))
model.summary()
model.compile(optimizer='adam', loss="categorical_crossentropy", metrics=['accuracy'])
history = model.fit(train_data, train_label, batch_size=BATCH_SIZE, epochs=EPOCHS)
plt.plot(history.history["loss"])
plt.xlabel("Epochs")
plt.ylabel("Loss value")
plt.title("Evolution of Loss")
plt.show()
plt.plot(history.history["accuracy"])
plt.xlabel("Epochs")
plt.ylabel("Accuracy")
plt.title("Evolution of Accuracy")
plt.show()
evaluate = model.evaluate(test_data, test_label, verbose=2) # ~58%
```
%% Cell type:markdown id: tags:
# Export as Keras model
%% Cell type:code id: tags:
``` python
model.save('sign_model.keras')
```
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment