-
Notifications
You must be signed in to change notification settings - Fork 4
/
gestures_hand.py
138 lines (93 loc) · 3.43 KB
/
gestures_hand.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# -*- coding: utf-8 -*-
"""gestures_hand.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1eJ-xBjSzbE8cA29tnMVl91jRxezIPqdR
"""
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
! git clone https://github.com/nobi1007/HandTalk.git
! ls HandTalk
train_data = pd.read_csv('./HandTalk/sign_mnist_train.csv')
test_data = pd.read_csv('./HandTalk/sign_mnist_test.csv')
train_data.head()
train_data.shape
train_labels = train_data['label'].values
unique_val = np.array(train_labels)
np.unique(unique_val)
plt.figure(figsize = (18,8))
sns.countplot(x = train_labels)
from google.colab import drive
drive.mount('/content/drive')
train_data.drop('label', axis = 1, inplace = True)
images = train_data.values
images = np.array([np.reshape(i, (28, 28)) for i in images])
images = np.array([i.flatten() for i in images])
from sklearn.preprocessing import LabelBinarizer
label_binrizer = LabelBinarizer()
labels = label_binrizer.fit_transform(train_labels)
plt.imshow(images[0].reshape(28,28))
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(images, labels, test_size = 0.3, random_state = 101)
import keras
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Dropout
batch_size = 128
num_classes = 24
epochs = 50
x_train = x_train / 255
x_test = x_test / 255
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
plt.imshow(x_train[0].reshape(28,28))
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'))
model.compile(loss = keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adam(),
metrics=['accuracy'])
history = model.fit(x_train, y_train, validation_data = (x_test, y_test), epochs=epochs, batch_size=batch_size)
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title("Accuracy")
plt.xlabel('epoch')
plt.ylabel('accuracy')
plt.legend(['train','test'])
plt.show()
test = pd.read_csv('./HandTalk/sign_mnist_test.csv')
test_labels = test['label']
test.drop('label', axis = 1, inplace = True)
test_images = test.values
test_images = np.array([np.reshape(i, (28, 28)) for i in test_images])
test_images = np.array([i.flatten() for i in test_images])
test_labels = label_binrizer.fit_transform(test_labels)
test_images = test_images.reshape(test_images.shape[0], 28, 28, 1)
test_images.shape
y_pred = model.predict(test_images)
from sklearn.metrics import accuracy_score
accuracy_score(test_labels, y_pred.round())
plt.imshow(test_images[0].reshape(28,28))
print(test_labels[0])
y_pred[0]
model.summary()
model.save('first_handTalk.h5')
new_model = keras.models.load_model('first_handTalk.h5')
new_model.summary()
x2 = test_images[:1]
x2.shape
new_model.predict(x2)
x = test_images[0]
np.shape(x)
x1 = np.reshape(x,(1,28,28,1))
print(np.shape(x1))
new_model.predict(x1)