人工智能-人体动作识别算法编程

Xsens动作捕捉 2023-05-01 11277

人体动作识别(Human Action Recognition)是计算机视觉领域的重要研究方向,它的目的是根据输入的视频序列,自动识别出其中所包含的人体动作类别。下面是一个简单的Python代码示例,用于实现基于深度学习的人体动作识别:

人工智能-人体动作识别算法编程  第1张


首先,我们需要导入必要的库:

pip install keras

pip install tensorflow

conda install opencv

import keras

from keras.models import Sequential

from keras.layers import Dense, Dropout, Flatten

from keras.layers import Conv2D, MaxPooling2D

from keras.optimizers import SGD

from keras.utils import np_utils

from keras.datasets import cifar10

import numpy as np

import cv2

出现的提示:

2023-02-27 20:40:44.626322: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA

To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.

这段代码是 TensorFlow 输出的一条信息,它告诉我们当前使用的 TensorFlow 二进制文件是经过优化的,并使用了 oneAPI Deep Neural Network Library (oneDNN) 来在性能关键操作中使用 AVX2 和 FMA 等 CPU 指令集。它还提示我们,如果希望在其他操作中启用这些指令集,可以使用适当的编译器标志重新构建 TensorFlow。


2023-02-27 21:14:50.308899: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.

这段消息是由TensorFlow库输出的,它表明您的TensorFlow二进制文件已经针对具有特定CPU指令集的处理器进行了优化,以提高性能。在性能关键操作中,TensorFlow将使用具有这些CPU指令集的操作。如果您想在其他操作中启用它们,您需要使用适当的编译器标志重新构建TensorFlow。

这条消息通常可以忽略,因为它只是告诉您TensorFlow如何使用您的CPU。

然后,我们需要定义数据集的输入形状和输出类别数:


img_rows, img_cols = 32, 32

nb_classes = 10


接下来,我们加载数据集并进行预处理:


(X_train, y_train), (X_test, y_test) = cifar10.load_data()

# 调整图像尺寸

X_train = np.array([cv2.resize(img, (img_rows, img_cols)) for img in X_train[:,:,:,:]])

X_test = np.array([cv2.resize(img, (img_rows, img_cols)) for img in X_test[:,:,:,:]])

# 转换成浮点数并归一化

X_train = X_train.astype(float32) / 255

X_test = X_test.astype(float32) / 255

# 转换类别标签为独热编码

Y_train = np_utils.to_categorical(y_train, nb_classes)

Y_test = np_utils.to_categorical(y_test, nb_classes)


接下来,我们构建深度学习模型:


model = Sequential()

# 添加卷积层和池化层

model.add(Conv2D(32, (3, 3), activation=relu, input_shape=(img_rows, img_cols, 3)))

model.add(Conv2D(32, (3, 3), activation=relu))

model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Dropout(0.25))

# 添加全连接层和输出层

model.add(Flatten())

人工智能-人体动作识别算法编程  第2张

model.add(Dense(128, activation=relu))

model.add(Dropout(0.5))

model.add(Dense(nb_classes, activation=softmax))

2023-02-27 21:35:54.566192: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA

To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.

这段消息是由TensorFlow库输出的,它表明您的TensorFlow二进制文件已经针对具有特定CPU指令集的处理器进行了优化,以提高性能。在性能关键操作中,TensorFlow将使用具有这些CPU指令集的操作。如果您想在其他操作中启用它们,您需要使用适当的编译器标志重新构建TensorFlow。

这条消息通常可以忽略,因为它只是告诉您TensorFlow如何使用您的CPU。如果您在使用TensorFlow时遇到任何问题,请告诉我。

然后,我们定义模型的优化器和损失函数:


sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)

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

/Users/zhouli/opt/anaconda3/lib/python3.9/site-packages/keras/optimizers/optimizer_v2/gradient_descent.py:114: UserWarning: The `lr` argument is deprecated, use `learning_rate` instead.

super().__init__(name, **kwargs)

这个警告来自于Keras优化器库,它表明在使用GradientDescentOptimizer时使用lr参数已被弃用。相反,应使用learning_rate参数。这是因为在Keras的版本2.4.0中,lr参数已被弃用,因此会发出此警告。

您可以使用以下代码将lr参数更改为learning_rate参数:

sgd = SGD(learning_rate=0.01, decay=1e-6, momentum=0.9, nesterov=True)

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

最后,我们训练模型并评估其性能:


model.fit(X_train, Y_train, batch_size=32, epochs=10, validation_data=(X_test, Y_test))

score = model.evaluate(X_test, Y_test, batch_size=32)

print(Test score:, score[0])

print(Test accuracy:, score[1])


Epoch 1/10

1563/1563 [==============================] - 62s 40ms/step - loss: 1.6995 - accuracy: 0.3781 - val_loss: 1.3538 - val_accuracy: 0.5137

Epoch 2/10

1563/1563 [==============================] - 65s 42ms/step - loss: 1.3914 - accuracy: 0.4968 - val_loss: 1.1925 - val_accuracy: 0.5831

Epoch 3/10

1563/1563 [==============================] - 62s 40ms/step - loss: 1.2720 - accuracy: 0.5480 - val_loss: 1.0891 - val_accuracy: 0.6153

Epoch 4/10

1563/1563 [==============================] - 62s 40ms/step - loss: 1.1796 - accuracy: 0.5792 - val_loss: 1.0727 - val_accuracy: 0.6230

Epoch 5/10

1563/1563 [==============================] - 60s 38ms/step - loss: 1.1069 - accuracy: 0.6063 - val_loss: 1.0193 - val_accuracy: 0.6432

Epoch 6/10

1563/1563 [==============================] - 61s 39ms/step - loss: 1.0544 - accuracy: 0.6283 - val_loss: 1.0035 - val_accuracy: 0.6450

Epoch 7/10

1563/1563 [==============================] - 65s 42ms/step - loss: 1.0094 - accuracy: 0.6442 - val_loss: 1.0054 - val_accuracy: 0.6478

Epoch 8/10

1563/1563 [==============================] - 65s 42ms/step - loss: 0.9621 - accuracy: 0.6570 - val_loss: 1.0045 - val_accuracy: 0.6479

Epoch 9/10

1563/1563 [==============================] - 63s 40ms/step - loss: 0.9304 - accuracy: 0.6690 - val_loss: 0.9626 - val_accuracy: 0.6668

Epoch 10/10

1563/1563 [==============================] - 62s 40ms/step - loss: 0.8959 - accuracy: 0.6822 - val_loss: 0.9390 - val_accuracy: 0.6726

313/313 [==============================] - 2s 6ms/step - loss: 0.9390 - accuracy: 0.6726

Test score: 0.9390373826026917

Test accuracy: 0.6725999712944031

这是您训练神经网络的训练日志。在这个例子中,您训练了一个卷积神经网络来对CIFAR-10图像数据集进行分类,其中包含10个类别(例如飞机、汽车、鸟、猫等)。在这个日志中,您可以看到训练和验证的损失和准确度的变化情况,以及最后在测试集上的损失和准确度。在这个例子中,您训练了10个epoch,每个epoch包含1563个步骤(每个步骤对应一个batch),并且在测试集上达到了大约67%的准确率。


The End