0% found this document useful (0 votes)
67 views9 pages

Signlanguage - Project - Jupyter Notebook

hy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views9 pages

Signlanguage - Project - Jupyter Notebook

hy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

In [1]:

import mnist
import pandas as pd
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense,Flatten,Conv2D,MaxPool2D,Dropout
import matplotlib.pyplot as plt
import seaborn as sns
from tensorflow.keras.preprocessing.image import ImageDataGenerator

In [4]:

train_df=pd.read_csv("sign_mnist_train.csv")
train_df.head()

Out[4]:

label pixel1 pixel2 pixel3 pixel4 pixel5 pixel6 pixel7 pixel8 pixel9 ... pixel775 pixel

0 3 107 118 127 134 139 143 146 150 153 ... 207

1 6 155 157 156 156 156 157 156 158 158 ... 69

2 2 187 188 188 187 187 186 187 188 187 ... 202

3 2 211 211 212 212 211 210 211 210 210 ... 235

4 13 164 167 170 172 176 179 180 184 185 ... 92

5 rows × 785 columns

In [5]:

test_df=pd.read_csv("sign_mnist_test.csv")
test_df.head()

Out[5]:

label pixel1 pixel2 pixel3 pixel4 pixel5 pixel6 pixel7 pixel8 pixel9 ... pixel775 pixel

0 6 149 149 150 150 150 151 151 150 151 ... 138

1 5 126 128 131 132 133 134 135 135 136 ... 47

2 10 85 88 92 96 105 123 135 143 147 ... 68

3 0 203 205 207 206 207 209 210 209 210 ... 154

4 3 188 191 193 195 199 201 202 203 203 ... 26

5 rows × 785 columns


In [6]:

train_df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 27455 entries, 0 to 27454
Columns: 785 entries, label to pixel784
dtypes: int64(785)
memory usage: 164.4 MB

In [7]:

test_df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 7172 entries, 0 to 7171
Columns: 785 entries, label to pixel784
dtypes: int64(785)
memory usage: 43.0 MB

In [8]:

train_df.describe()

Out[8]:

label pixel1 pixel2 pixel3 pixel4 pixel5

count 27455.000000 27455.000000 27455.000000 27455.000000 27455.000000 27455.000000 27

mean 12.318813 145.419377 148.500273 151.247714 153.546531 156.210891

std 7.287552 41.358555 39.942152 39.056286 38.595247 37.111165

min 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000

25% 6.000000 121.000000 126.000000 130.000000 133.000000 137.000000

50% 13.000000 150.000000 153.000000 156.000000 158.000000 160.000000

75% 19.000000 174.000000 176.000000 178.000000 179.000000 181.000000

max 24.000000 255.000000 255.000000 255.000000 255.000000 255.000000

8 rows × 785 columns


In [9]:

train_df.head(6)

Out[9]:

label pixel1 pixel2 pixel3 pixel4 pixel5 pixel6 pixel7 pixel8 pixel9 ... pixel775 pixel

0 3 107 118 127 134 139 143 146 150 153 ... 207

1 6 155 157 156 156 156 157 156 158 158 ... 69

2 2 187 188 188 187 187 186 187 188 187 ... 202

3 2 211 211 212 212 211 210 211 210 210 ... 235

4 13 164 167 170 172 176 179 180 184 185 ... 92

5 16 161 168 172 173 178 184 189 193 196 ... 76

6 rows × 785 columns

In [10]:

train_label=train_df['label']
train_label.head()
trainset=train_df.drop(['label'],axis=1)
trainset.head()

Out[10]:

pixel1 pixel2 pixel3 pixel4 pixel5 pixel6 pixel7 pixel8 pixel9 pixel10 ... pixel775 pix

0 107 118 127 134 139 143 146 150 153 156 ... 207

1 155 157 156 156 156 157 156 158 158 157 ... 69

2 187 188 188 187 187 186 187 188 187 186 ... 202

3 211 211 212 212 211 210 211 210 210 211 ... 235

4 164 167 170 172 176 179 180 184 185 186 ... 92

5 rows × 784 columns

In [11]:

X_train = trainset.values
X_train = trainset.values.reshape(-1,28,28,1)
print(X_train.shape)

(27455, 28, 28, 1)


In [13]:

test_label=test_df['label']
X_test=test_df.drop(['label'],axis=1)
print(X_test.shape)
X_test.head()

(7172, 784)

Out[13]:

pixel1 pixel2 pixel3 pixel4 pixel5 pixel6 pixel7 pixel8 pixel9 pixel10 ... pixel775 pix

0 149 149 150 150 150 151 151 150 151 152 ... 138

1 126 128 131 132 133 134 135 135 136 138 ... 47

2 85 88 92 96 105 123 135 143 147 152 ... 68

3 203 205 207 206 207 209 210 209 210 209 ... 154

4 188 191 193 195 199 201 202 203 203 203 ... 26

5 rows × 784 columns

In [14]:

from sklearn.preprocessing import LabelBinarizer


lb=LabelBinarizer()
y_train=lb.fit_transform(train_label)
y_test=lb.fit_transform(test_label)

In [15]:

y_train

Out[15]:

array([[0, 0, 0, ..., 0, 0, 0],


[0, 0, 0, ..., 0, 0, 0],
[0, 0, 1, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 1, 0]])

In [16]:

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

In [17]:

print(X_train.shape,y_train.shape,X_test.shape,y_test.shape)

(27455, 28, 28, 1) (27455, 24) (7172, 28, 28, 1) (7172, 24)
In [18]:

train_datagen = ImageDataGenerator(rescale = 1./255,


rotation_range = 0,
height_shift_range=0.2,
width_shift_range=0.2,
shear_range=0,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest')

X_test=X_test/255

In [19]:

fig,axe=plt.subplots(2,2)
fig.suptitle('Preview of dataset')
axe[0,0].imshow(X_train[0].reshape(28,28),cmap='gray')
axe[0,0].set_title('label: 3 letter: C')
axe[0,1].imshow(X_train[1].reshape(28,28),cmap='gray')
axe[0,1].set_title('label: 6 letter: F')
axe[1,0].imshow(X_train[2].reshape(28,28),cmap='gray')
axe[1,0].set_title('label: 2 letter: B')
axe[1,1].imshow(X_train[4].reshape(28,28),cmap='gray')
axe[1,1].set_title('label: 13 letter: M')

Out[19]:

Text(0.5, 1.0, 'label: 13 letter: M')


In [20]:

sns.countplot(train_label)
plt.title("Frequency of each label")

C:\Users\Lenovo\anaconda3\lib\site-packages\seaborn\_decorators.py:36: Futur
eWarning: Pass the following variable as a keyword arg: x. From version 0.1
2, the only valid positional argument will be `data`, and passing other argu
ments without an explicit keyword will result in an error or misinterpretati
on.
warnings.warn(

Out[20]:

Text(0.5, 1.0, 'Frequency of each label')

In [21]:

model=Sequential()
model.add(Conv2D(128,kernel_size=(5,5),
strides=1,padding='same',activation='relu',input_shape=(28,28,1)))
model.add(MaxPool2D(pool_size=(3,3),strides=2,padding='same'))
model.add(Conv2D(64,kernel_size=(2,2),
strides=1,activation='relu',padding='same'))
model.add(MaxPool2D((2,2),2,padding='same'))
model.add(Conv2D(32,kernel_size=(2,2),
strides=1,activation='relu',padding='same'))
model.add(MaxPool2D((2,2),2,padding='same'))

model.add(Flatten())
In [22]:

model.add(Dense(units=512,activation='relu'))
model.add(Dropout(rate=0.25))
model.add(Dense(units=24,activation='softmax'))
model.summary()

Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 28, 28, 128) 3328

max_pooling2d (MaxPooling2D (None, 14, 14, 128) 0


)

conv2d_1 (Conv2D) (None, 14, 14, 64) 32832

max_pooling2d_1 (MaxPooling (None, 7, 7, 64) 0


2D)

conv2d_2 (Conv2D) (None, 7, 7, 32) 8224

max_pooling2d_2 (MaxPooling (None, 4, 4, 32) 0


2D)

flatten (Flatten) (None, 512) 0

dense (Dense) (None, 512) 262656

dropout (Dropout) (None, 512) 0

dense_1 (Dense) (None, 24) 12312

=================================================================
Total params: 319,352
Trainable params: 319,352
Non-trainable params: 0
_________________________________________________________________

In [23]:

model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
In [25]:

model.fit(train_datagen.flow(X_train,y_train,batch_size=200),
epochs = 10,
validation_data=(X_test,y_test),
shuffle=1
)

Epoch 1/10
138/138 [==============================] - 63s 455ms/step - loss: 2.8499 - a
ccuracy: 0.1407 - val_loss: 2.1006 - val_accuracy: 0.3420
Epoch 2/10
138/138 [==============================] - 60s 438ms/step - loss: 2.1736 - a
ccuracy: 0.3164 - val_loss: 1.4515 - val_accuracy: 0.4858
Epoch 3/10
138/138 [==============================] - 63s 457ms/step - loss: 1.6268 - a
ccuracy: 0.4687 - val_loss: 1.0478 - val_accuracy: 0.6471
Epoch 4/10
138/138 [==============================] - 62s 452ms/step - loss: 1.2565 - a
ccuracy: 0.5830 - val_loss: 0.7785 - val_accuracy: 0.7462
Epoch 5/10
138/138 [==============================] - 60s 436ms/step - loss: 1.0422 - a
ccuracy: 0.6530 - val_loss: 0.6935 - val_accuracy: 0.7510
Epoch 6/10
138/138 [==============================] - 60s 434ms/step - loss: 0.8913 - a
ccuracy: 0.6970 - val_loss: 0.5678 - val_accuracy: 0.8113
Epoch 7/10
138/138 [==============================] - 60s 435ms/step - loss: 0.7566 - a
ccuracy: 0.7468 - val_loss: 0.4137 - val_accuracy: 0.8783
Epoch 8/10
138/138 [==============================] - 60s 438ms/step - loss: 0.6505 - a
ccuracy: 0.7800 - val_loss: 0.3338 - val_accuracy: 0.8813
Epoch 9/10
138/138 [==============================] - 60s 438ms/step - loss: 0.5672 - a
ccuracy: 0.8090 - val_loss: 0.2608 - val_accuracy: 0.9265
Epoch 10/10
138/138 [==============================] - 60s 435ms/step - loss: 0.5077 - a
ccuracy: 0.8273 - val_loss: 0.2717 - val_accuracy: 0.9113

Out[25]:

<keras.callbacks.History at 0x1ba7f17a6d0>

In [26]:

(ls,acc)=model.evaluate(x=X_test,y=y_test)

225/225 [==============================] - 4s 19ms/step - loss: 0.2717 - acc


uracy: 0.9113

In [27]:

print('MODEL ACCURACY = {}%'.format(acc*100))

MODEL ACCURACY = 91.13218188285828%

In [ ]:

You might also like