Professional Documents
Culture Documents
[19]: import os
import cv2
from skimage import io, color
import numpy as np
import matplotlib.pyplot as plt
import keras
from keras.layers import Conv2D, Activation, Dense, Dropout
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
import warnings
warnings.filterwarnings('ignore')
print(dir)
data_00_dir= []
data_00_dir.extend(data_00_folder)
print(data_00_dir[:10])
Categories= data_00_dir[:10]
['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', 'leapGestRecog']
['01_palm', '02_l', '03_fist', '04_fist_moved', '05_thumb', '06_index', '07_ok',
'08_palm_moved', '09_c', '10_down']
1
[107]: image_data = []
for Y in image_data:
label.append(Y)
[109]: image_data[:10]
2
[5, 4, 4, …, 3, 5, 4],
[6, 5, 5, …, 3, 4, 5]], dtype=uint8),
8],
[array([[5, 7, 6, …, 7, 7, 5],
[6, 6, 6, …, 6, 6, 6],
[7, 6, 7, …, 6, 6, 5],
…,
[6, 5, 5, …, 6, 5, 5],
[5, 5, 6, …, 6, 5, 5],
[3, 5, 5, …, 5, 5, 5]], dtype=uint8),
4],
[array([[5, 4, 5, …, 4, 4, 4],
[5, 5, 5, …, 5, 4, 3],
[4, 5, 6, …, 4, 4, 4],
…,
[4, 4, 4, …, 5, 4, 4],
[4, 5, 5, …, 4, 5, 4],
[5, 5, 5, …, 4, 5, 4]], dtype=uint8),
6],
[array([[ 4, 5, 5, …, 4, 4, 5],
[ 4, 5, 4, …, 4, 3, 3],
[ 5, 6, 5, …, 4, 4, 3],
…,
[ 7, 10, 12, …, 5, 4, 4],
[ 7, 9, 12, …, 4, 3, 3],
[ 8, 9, 10, …, 3, 4, 5]], dtype=uint8),
2],
[array([[4, 5, 7, …, 4, 4, 5],
[5, 4, 4, …, 4, 4, 3],
[6, 5, 5, …, 5, 5, 4],
…,
[4, 5, 4, …, 4, 4, 4],
[3, 4, 5, …, 4, 3, 4],
[4, 4, 5, …, 3, 4, 2]], dtype=uint8),
3],
[array([[5, 5, 4, …, 3, 4, 4],
[5, 5, 5, …, 4, 5, 4],
[5, 4, 5, …, 4, 4, 4],
…,
[4, 4, 5, …, 5, 5, 4],
[4, 4, 4, …, 4, 4, 4],
[3, 5, 5, …, 5, 4, 4]], dtype=uint8),
3],
[array([[4, 3, 3, …, 5, 5, 6],
[2, 3, 3, …, 5, 4, 5],
[4, 4, 4, …, 5, 5, 5],
…,
3
[3, 3, 4, …, 7, 5, 4],
[3, 4, 4, …, 5, 6, 5],
[1, 4, 4, …, 6, 4, 5]], dtype=uint8),
6],
[array([[3, 4, 5, …, 3, 4, 4],
[4, 3, 4, …, 2, 3, 3],
[4, 4, 6, …, 3, 4, 2],
…,
[4, 4, 5, …, 4, 4, 4],
[4, 4, 4, …, 3, 4, 4],
[6, 4, 4, …, 3, 3, 4]], dtype=uint8),
9],
[array([[4, 4, 3, …, 4, 6, 5],
[4, 4, 3, …, 4, 4, 4],
[4, 5, 4, …, 5, 5, 5],
…,
[4, 5, 4, …, 5, 5, 5],
[3, 4, 4, …, 5, 5, 5],
[4, 4, 5, …, 5, 5, 5]], dtype=uint8),
5]]
[110]: label[:10]
4
[array([[5, 4, 5, …, 4, 4, 4],
[5, 5, 5, …, 5, 4, 3],
[4, 5, 6, …, 4, 4, 4],
…,
[4, 4, 4, …, 5, 4, 4],
[4, 5, 5, …, 4, 5, 4],
[5, 5, 5, …, 4, 5, 4]], dtype=uint8),
6],
[array([[ 4, 5, 5, …, 4, 4, 5],
[ 4, 5, 4, …, 4, 3, 3],
[ 5, 6, 5, …, 4, 4, 3],
…,
[ 7, 10, 12, …, 5, 4, 4],
[ 7, 9, 12, …, 4, 3, 3],
[ 8, 9, 10, …, 3, 4, 5]], dtype=uint8),
2],
[array([[4, 5, 7, …, 4, 4, 5],
[5, 4, 4, …, 4, 4, 3],
[6, 5, 5, …, 5, 5, 4],
…,
[4, 5, 4, …, 4, 4, 4],
[3, 4, 5, …, 4, 3, 4],
[4, 4, 5, …, 3, 4, 2]], dtype=uint8),
3],
[array([[5, 5, 4, …, 3, 4, 4],
[5, 5, 5, …, 4, 5, 4],
[5, 4, 5, …, 4, 4, 4],
…,
[4, 4, 5, …, 5, 5, 4],
[4, 4, 4, …, 4, 4, 4],
[3, 5, 5, …, 5, 4, 4]], dtype=uint8),
3],
[array([[4, 3, 3, …, 5, 5, 6],
[2, 3, 3, …, 5, 4, 5],
[4, 4, 4, …, 5, 5, 5],
…,
[3, 3, 4, …, 7, 5, 4],
[3, 4, 4, …, 5, 6, 5],
[1, 4, 4, …, 6, 4, 5]], dtype=uint8),
6],
[array([[3, 4, 5, …, 3, 4, 4],
[4, 3, 4, …, 2, 3, 3],
[4, 4, 6, …, 3, 4, 2],
…,
[4, 4, 5, …, 4, 4, 4],
[4, 4, 4, …, 3, 4, 4],
[6, 4, 4, …, 3, 3, 4]], dtype=uint8),
5
9],
[array([[4, 4, 3, …, 4, 6, 5],
[4, 4, 3, …, 4, 4, 4],
[4, 5, 4, …, 5, 5, 5],
…,
[4, 5, 4, …, 5, 5, 5],
[3, 4, 4, …, 5, 5, 5],
[4, 4, 5, …, 5, 5, 5]], dtype=uint8),
5]]
img_array = image_data[i][0]
print(img_array.shape)
if len(img_array.shape) == 2:
plt.imshow(img_array, cmap='hot')
plt.xticks([])
plt.yticks([])
index = image_data[i][1]
plt.title(Categories[index][3:])
print(index, type(index))
else:
print(f"Skipping subplot {i} due to invalid image shape: {img_array.
↪shape}")
plt.show()
(50, 50)
8 <class 'int'>
(50, 50)
4 <class 'int'>
(50, 50)
6 <class 'int'>
(50, 50)
2 <class 'int'>
(50, 50)
3 <class 'int'>
(50, 50)
3 <class 'int'>
(50, 50)
6 <class 'int'>
(50, 50)
6
9 <class 'int'>
(50, 50)
5 <class 'int'>
print(input_data.shape)
print(label.shape)
7
(20000,)
↪3,stratify=label)
model = Sequential()
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
WARNING:tensorflow:From
c:\Users\vinma\AppData\Local\Programs\Python\Python311\Lib\site-
packages\keras\src\layers\pooling\max_pooling2d.py:161: The name tf.nn.max_pool
is deprecated. Please use tf.nn.max_pool2d instead.
WARNING:tensorflow:From
c:\Users\vinma\AppData\Local\Programs\Python\Python311\Lib\site-
packages\keras\src\optimizers\__init__.py:309: The name tf.train.Optimizer is
deprecated. Please use tf.compat.v1.train.Optimizer instead.
8
[125]: import tensorflow as tf
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
WARNING:tensorflow:From
C:\Users\vinma\AppData\Local\Temp\ipykernel_2400\1503859844.py:2: The name
tf.logging.set_verbosity is deprecated. Please use
tf.compat.v1.logging.set_verbosity instead.
Epoch 1/7
438/438 [==============================] - 60s 137ms/step - loss: 0.0020 -
accuracy: 0.9993 - val_loss: 0.0043 - val_accuracy: 0.9997
Epoch 2/7
438/438 [==============================] - 57s 129ms/step - loss: 4.1735e-05 -
accuracy: 1.0000 - val_loss: 0.0027 - val_accuracy: 0.9997
Epoch 3/7
438/438 [==============================] - 74s 169ms/step - loss: 0.0015 -
accuracy: 0.9994 - val_loss: 0.0020 - val_accuracy: 0.9998
Epoch 4/7
438/438 [==============================] - 62s 141ms/step - loss: 0.0033 -
accuracy: 0.9991 - val_loss: 0.0162 - val_accuracy: 0.9970
Epoch 5/7
438/438 [==============================] - 73s 168ms/step - loss: 0.0043 -
accuracy: 0.9987 - val_loss: 0.0049 - val_accuracy: 0.9995
Epoch 6/7
438/438 [==============================] - 59s 136ms/step - loss: 0.0019 -
accuracy: 0.9994 - val_loss: 0.0059 - val_accuracy: 0.9995
Epoch 7/7
438/438 [==============================] - 56s 128ms/step - loss: 6.2474e-04 -
accuracy: 0.9999 - val_loss: 0.0080 - val_accuracy: 0.9995
[126]: model.summary()
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_2 (Conv2D) (None, 48, 48, 32) 320
9
activation_3 (Activation) (None, 46, 46, 32) 0
=================================================================
Total params: 1669290 (6.37 MB)
Trainable params: 1669290 (6.37 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
[127]: plt.plot(model.history.history['loss'])
plt.plot(model.history.history['val_loss'])
plt.title('Model Loss')
plt.ylabel('Loss')
plt.xlabel('Epochs')
plt.legend(['train', 'test'])
plt.show()
10
[128]: plt.plot(model.history.history['accuracy'])
plt.plot(model.history.history['val_accuracy'])
plt.title('Model Accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epochs')
plt.legend(['train', 'test'])
plt.show()
11
[129]: test_loss, test_accuracy = model.evaluate(X_test, Y_test)
[132]: []
12
[ ]:
13