江东的笔记

Be overcome difficulties is victory

0%

机器视觉-手势识别

使用keras搭建网络进行手势识别,并查看准确率和损失函数曲线

实验要求

实验1手势识别.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import platform
import datetime
import sys
import os


your_name, your_code, work_counter = "人工智能20本杜培博", 200507340136, 2
print(f'我是:{your_name},学号:{your_code},这是我的第 {work_counter} 次作业。')
print('现在是', datetime.datetime.now())
print('我的机器信息如下:')
print(platform.uname())

print('\n当前工作目录:')
print(os.getcwd())
print('\n当前目录的文件信息如下:')
print(os.listdir())
print('\n我安装包的情况:')
!pip list
我是:人工智能20本杜培博,学号:200507340136,这是我的第 2 次作业。
现在是 2022-10-21 16:39:19.008657
我的机器信息如下:
uname_result(system='Windows', node='LAPTOP-5PHEQ8I3', release='10', version='10.0.18362', machine='AMD64', processor='AMD64 Family 23 Model 24 Stepping 1, AuthenticAMD')

当前工作目录:
E:\jupyter\curriculum_code\da three\competition\week02

当前目录的文件信息如下:
['.ipynb_checkpoints', 'hand_gesture_dataset', 'Untitled.ipynb']

我安装包的情况:
Package                Version
---------------------- -----------

导入包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import warnings
warnings.filterwarnings('ignore')

import keras
import matplotlib.pyplot as plt # for plotting
import os # provides a way of using operating system dependent functionality
import cv2 #Image handling library
import numpy as np
import keras.backend as K
from keras.callbacks import Callback,ModelCheckpoint
from keras.models import Sequential,load_model
from keras.layers import Dense, Dropout
from keras.wrappers.scikit_learn import KerasClassifier
import keras.backend as K
from sklearn.datasets import make_multilabel_classification
# Import of keras model and hidden layers for our convolutional network
from keras.layers import Conv2D, Activation, MaxPool2D, Dense, Flatten, Dropout
Using TensorFlow backend.

读取目录,遍历文件夹

1
2
3
4
5
6
7
# paths for dataset
data_path = "./hand_gesture_dataset"
IMG_SIZE = 40
CATEGORIES = []
for i in os.listdir(data_path):
CATEGORIES.append(i)
CATEGORIES
['EIGHT',
 'FIVE',
 'FOUR',
 'GOOD',
 'NINE',
 'OK',
 'ONE',
 'SEVEN',
 'SIX',
 'TEN',
 'THREE',
 'TWO']

将label进行编码

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
def revice(name):
if name=='ONE':
return 1
elif name=='TWO':
return 2
elif name=='THREE':
return 3
elif name=='FOUR':
return 4
elif name=='FIVE':
return 5
elif name=='SIX':
return 6
elif name=='SEVEN':
return 7
elif name=='EIGHT':
return 8
elif name=='NINE':
return 9
elif name=='TEN':
return 10
elif name=='GOOD':
return 11
elif name=='OK':
return 12

遍历读取图片data

1
2
3
4
5
6
7
8
9
10
11
12
13
image_data = []

data = {}
for category in CATEGORIES:
path = os.path.join(data_path, category)
# print(os.listdir(path))
for img in os.listdir(path):
img_arr = cv2.imread(os.path.join(path,img)) # ,cv2.IMREAD_GRAYSCALE
image_data.append([img_arr,revice(category)])
# data[category] =image_data
# print(data.get(categorys,img_arr))

plt.imshow(img_arr)
<matplotlib.image.AxesImage at 0x19a7e675e80>

Snipaste_2022-11-26_20-50-10.png

打乱数据

1
2
3
# shuffle the input data
import random
random.shuffle(image_data)

将数据和label分离

1
2
3
4
5
6
input_data = []
label = []
for X, y in image_data:
input_data.append(X)
label.append(y)
# input_data[:5]
1
# label
1
2
3
4
5
6
7
8
plt.figure(1, figsize=(10,10))
for i in range(1,17):
plt.subplot(4,4,i)
plt.imshow(image_data[i][0], cmap='hot')
plt.xticks([])
plt.yticks([])
# plt.title(CATEGORIES[label[i]][3:])
#plt.show()

Snipaste_2022-11-26_20-50-10.png

归一化数值

1
2
3
4
5
# Normalizing the data
input_data = np.array(input_data)
label = np.array(label)
input_data = input_data/255.0
input_data.shape
(96252, 40, 40, 3)

数据分割

1
2
3
from sklearn.datasets import make_multilabel_classification
X,y=make_multilabel_classification(n_samples=500,n_features=4,n_classes=2,n_labels=3,random_state=1)
X
array([[ 9., 12.,  6., 12.],
       [ 5.,  2., 12., 22.],
       [15.,  5., 12., 11.],
       ...,
       [ 5., 10., 15., 28.],
       [ 0.,  8., 16., 27.],
       [ 6.,  9., 13., 22.]])

独热编码

1
2
3
4

from tensorflow.keras.utils import to_categorical
label = to_categorical(label, num_classes=13,dtype='i1')
label.shape
(96252, 13)

训练集和测试集9:1分割

1
2
3
4
from sklearn.model_selection import train_test_split
#X_train, X_test, y_train, y_test = train_test_split(input_data, label, test_size = 0.3, random_state=0)
X_train, X_test, y_train, y_test = train_test_split(input_data, label, test_size = 0.10, random_state=0)
X_train.shape,y_train.shape,X_test.shape,y_test.shape
((86626, 40, 40, 3), (86626, 13), (9626, 40, 40, 3), (9626, 13))

搭建模型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
model = keras.models.Sequential()

model.add(Conv2D(filters = 32, kernel_size = (3,3), input_shape = (IMG_SIZE, IMG_SIZE, 3)))
model.add(Activation('relu'))

model.add(Conv2D(filters = 32, kernel_size = (3,3)))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(0.3))

model.add(Conv2D(filters = 64, kernel_size = (3,3)))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(0.3))

model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dense(13, activation='softmax'))

model.compile(loss='categorical_crossentropy',
optimizer = 'rmsprop',
metrics = ['accuracy'])

训练模型:32batch_size,7epoch

1
model.fit(X_train, y_train, epochs = 7, batch_size=32, validation_data=(X_test, y_test))
Train on 86626 samples, validate on 9626 samples
Epoch 1/7
86626/86626 [==============================] - 224s 3ms/step - loss: 0.5109 - accuracy: 0.8228 - val_loss: 0.2274 - val_accuracy: 0.9259
Epoch 2/7
86626/86626 [==============================] - 234s 3ms/step - loss: 0.1164 - accuracy: 0.9630 - val_loss: 0.0445 - val_accuracy: 0.9876
Epoch 3/7
86626/86626 [==============================] - 230s 3ms/step - loss: 0.0731 - accuracy: 0.9773 - val_loss: 0.0430 - val_accuracy: 0.9889
Epoch 4/7
86626/86626 [==============================] - 230s 3ms/step - loss: 0.0574 - accuracy: 0.9825 - val_loss: 0.0309 - val_accuracy: 0.9913
Epoch 5/7
86626/86626 [==============================] - 236s 3ms/step - loss: 0.0508 - accuracy: 0.9847 - val_loss: 0.0329 - val_accuracy: 0.9909
Epoch 6/7
86626/86626 [==============================] - 238s 3ms/step - loss: 0.0468 - accuracy: 0.9863 - val_loss: 0.0197 - val_accuracy: 0.9947
Epoch 7/7
86626/86626 [==============================] - 239s 3ms/step - loss: 0.0464 - accuracy: 0.9870 - val_loss: 0.0219 - val_accuracy: 0.9936





<keras.callbacks.callbacks.History at 0x19a1ead3b38>

查看模型的网络结构

1
model.summary()
Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 38, 38, 32)        896       
_________________________________________________________________
activation_1 (Activation)    (None, 38, 38, 32)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 36, 36, 32)        9248      
_________________________________________________________________
activation_2 (Activation)    (None, 36, 36, 32)        0         
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 18, 18, 32)        0         
_________________________________________________________________
dropout_1 (Dropout)          (None, 18, 18, 32)        0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 16, 16, 64)        18496     
_________________________________________________________________
activation_3 (Activation)    (None, 16, 16, 64)        0         
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 8, 8, 64)          0         
_________________________________________________________________
dropout_2 (Dropout)          (None, 8, 8, 64)          0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 4096)              0         
_________________________________________________________________
dense_1 (Dense)              (None, 256)               1048832   
_________________________________________________________________
dense_2 (Dense)              (None, 13)                3341      
=================================================================
Total params: 1,080,813
Trainable params: 1,080,813
Non-trainable params: 0
_________________________________________________________________

查看损失

1
2
3
4
5
6
7
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()

output_30_0.png

查看准确率

1
2
3
4
5
6
7
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()

output_32_0.png