简介
本文基于LSTM来完成用户行为识别。数据集来源:https://archive.ics.uci.edu/ml/machine-learning-databases/00240/

此数据集一共有6种行为状态:

行走;
站立;
躺下;
坐下;
上楼;
下楼;
以上6种行为数据是通过传感器进行采集的。

.\data\UCI HAR Dataset\train\Inertial Signals

实现
本次实验实现的是6分类任务。

pip install -i https://pypi.douban.com/simple/ --trusted-host=pypi.douban.com/simple tensorflow==1.13.1

import tensorflow as tf
import numpy as np# # 模型好坏主要由数据决定,数据决定模型上限,模型决定逼近这个上限,记录仪上的数据
def load_X(X_signals_paths):X_signals = []for signal_type_path in X_signals_paths:file = open(signal_type_path, 'r')X_signals.append([np.array(serie, dtype=np.float32) for serie in[row.replace('  ', ' ').strip().split(' ') for row in file]])file.close()return np.transpose(np.array(X_signals), (1, 2, 0))def load_y(y_path):file = open(y_path, 'r')y_ = np.array([elem for elem in [row.replace('  ', ' ').strip().split(' ') for row in file]], dtype=np.int32)file.close()return y_ - 1class Config(object):def __init__(self, X_train, X_test):self.train_count = len(X_train)  # 训练记录self.test_data_count = len(X_test)self.n_steps = len(X_train[0])  # 步长,128步self.learning_rate = 0.0025self.lambda_loss_amount = 0.0015  # 正则化惩罚粒度self.training_epochs = 300self.batch_size = 1500self.n_inputs = len(X_train[0][0])  # 每个step收集9个,数据收集维度self.n_hidden = 32  # 隐层神经元个数self.n_classes = 6  # 输出6个类别self.W = {'hidden': tf.Variable(tf.random_normal([self.n_inputs, self.n_hidden])),  # 输入到隐层'output': tf.Variable(tf.random_normal([self.n_hidden, self.n_classes]))}  # 隐层到输出self.biases = {'hidden': tf.Variable(tf.random_normal([self.n_hidden], mean=1.0)),'output': tf.Variable(tf.random_normal([self.n_classes]))}# 构造LSTM网络
def LSTM_Network(_X, config):# 数据转换,使其满足LSTM网络要求_X = tf.transpose(_X, [1, 0, 2])  # 把0 1 2调换成1 0 2,调换第一维度和第二维度_X = tf.reshape(_X, [-1, config.n_inputs])_X = tf.nn.relu(tf.matmul(_X, config.W['hidden']) + config.biases['hidden'])  # 9个神经元变为32_X = tf.split(_X, config.n_steps, 0)  # 把每一步放到RNN对应的位置# 两层LSTM堆叠在一起lstm_cell_1 = tf.contrib.rnn.BasicLSTMCell(config.n_hidden, forget_bias=1.0, state_is_tuple=True)lstm_cell_2 = tf.contrib.rnn.BasicLSTMCell(config.n_hidden, forget_bias=1.0, state_is_tuple=True)lstm_cells = tf.contrib.rnn.MultiRNNCell([lstm_cell_1, lstm_cell_2], state_is_tuple=True)outputs, states = tf.contrib.rnn.static_rnn(lstm_cells, _X, dtype=tf.float32)  # outputs:最终结果; states:中间结果print(np.array(outputs).shape)  # 查看outputs,128个输出结果lstm_last_output = outputs[-1]  # 取最终结果return tf.matmul(lstm_last_output, config.W['output']) + config.biases['output']  # 分类def one_hot(y_):y_ = y_.reshape(len(y_))n_values = int(np.max(y_)) + 1return np.eye(n_values)[np.array(y_, dtype=np.int32)]if __name__ == '__main__':# 指定九种不同输入信号,即9个文件的文件名前缀INPUT_SIGNAL_TYPES = ['body_acc_x_','body_acc_y_','body_acc_z_','body_gyro_x_','body_gyro_y_','body_gyro_z_','total_acc_x_','total_acc_y_','total_acc_z_']# 六种行为标签,行走 站立 躺下 坐下 上楼 下楼LABELS = ['WALKING','WALKING_UPSTAIRS','WALKING_DOWNSTAIRS','SITTING','STANDING','LAYING']# 指定数据路径DATA_PATH = 'data/'DATASET_PATH = DATA_PATH + 'UCI HAR Dataset/'print('\n' + 'Dataset is now located at:' + DATASET_PATH)TRAIN = 'train/'TEST = 'test/'X_train_signals_paths = [DATASET_PATH + TRAIN + 'Inertial Signals/' + signal + 'train.txt' for signal in INPUT_SIGNAL_TYPES]X_test_signals_paths = [DATASET_PATH + TEST + 'Inertial Signals/' + signal + 'test.txt' for signal inINPUT_SIGNAL_TYPES]X_train = load_X(X_train_signals_paths)X_test = load_X(X_test_signals_paths)print('X_train:', X_train.shape)  # 7352条数据,每个数据128窗口序列,每个序列记录9个不同指标print('X_test:', X_test.shape)y_train_path = DATASET_PATH + TRAIN + 'y_train.txt'y_test_path = DATASET_PATH + TEST + 'y_test.txt'y_train = one_hot(load_y(y_train_path))y_test = one_hot(load_y(y_test_path))print('y_train:', y_train.shape)  # 7352条数据,6个类别print('y_test:', y_test.shape)config = Config(X_train, X_test)print("Some useful info to get an insight on dataset's shape and normalisation:")print("features shape, labels shape, each features mean, each features standard deviation")print(X_test.shape, y_test.shape,np.mean(X_test), np.std(X_test))print('the dataset is therefore properly normalised, as expected.')X = tf.placeholder(tf.float32, [None, config.n_steps, config.n_inputs])Y = tf.placeholder(tf.float32, [None, config.n_classes])pred_Y = LSTM_Network(X, config)  # 最终预测结果# l2正则惩罚,tf.trainable_variables()(仅可以查看可训练的变量)l2 = config.lambda_loss_amount * \sum(tf.nn.l2_loss(tf_var) for tf_var in tf.trainable_variables())# 损失值cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=Y, logits=pred_Y)) + l2optimizer = tf.train.AdamOptimizer(learning_rate=config.learning_rate).minimize(cost)correct_pred = tf.equal(tf.argmax(pred_Y, 1), tf.argmax(Y, 1))accuracy = tf.reduce_mean(tf.cast(correct_pred, dtype=tf.float32))# tf.InteractiveSession():可以先构建一个session然后再定义操作(operation)# tf.Session():需要在会话构建之前定义好全部的操作(operation)然后再构建会话# tf.ConfigProto():获取到 operations 和 Tensor 被指派到哪个设备(几号CPU或几号GPU)上运行# log_device_placement=False:不会在终端打印出各项操作是在哪个设备上运行sess = tf.InteractiveSession(config=tf.ConfigProto(log_device_placement=False))init = tf.global_variables_initializer()sess.run(init)best_accuracy = 0.0for i in range(config.training_epochs):# zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表for start, end in zip(range(0, config.train_count, config.batch_size),range(config.batch_size, config.train_count + 1, config.batch_size)):sess.run(optimizer, feed_dict={X: X_train[start:end],Y: y_train[start:end]})# 也可对迭代过程进行可视化展示pred_out, accuracy_out, loss_out = sess.run([pred_Y, accuracy, cost], feed_dict={X: X_test, Y: y_test})print('traing iter: {},'.format(i) + 'test accuracy: {},'.format(accuracy_out) + 'loss:{}'.format(loss_out))best_accuracy = max(best_accuracy, accuracy_out)print('')print('final test accuracy: {}'.format(accuracy_out))print("best epoch's test accuracy: {}".format(best_accuracy))print('')

运行结果:

Dataset is now located at:data/UCI HAR Dataset/
X_train: (7352, 128, 9)
X_test: (2947, 128, 9)
y_train: (7352, 6)
y_test: (2947, 6)
Some useful info to get an insight on dataset's shape and normalisation:
features shape, labels shape, each features mean, each features standard deviation
(2947, 128, 9) (2947, 6) 0.09913992 0.39567086
the dataset is therefore properly normalised, as expected.
WARNING:tensorflow:From D:/WorkSpace/ai/csdn/lab-lstm-activity-recognition/lstm.py:58: BasicLSTMCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.
Instructions for updating:
This class is deprecated, please use tf.nn.rnn_cell.LSTMCell, which supports all the feature this cell currently has. Please replace the existing code with tf.nn.rnn_cell.LSTMCell(name='basic_lstm_cell').
(128,)
WARNING:tensorflow:From D:/WorkSpace/ai/csdn/lab-lstm-activity-recognition/lstm.py:141: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Future major versions of TensorFlow will allow gradients to flow
into the labels input on backprop by default.
See `tf.nn.softmax_cross_entropy_with_logits_v2`.
2019-12-16 19:45:10.908801: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
traing iter: 0,test accuracy: 0.4801492989063263,loss:1.9456521272659302
final test accuracy: 0.4801492989063263
best epoch's test accuracy: 0.4801492989063263traing iter: 1,test accuracy: 0.5334238409996033,loss:1.6313532590866089final test accuracy: 0.5334238409996033
best epoch's test accuracy: 0.5334238409996033
traing iter: 2,test accuracy: 0.6128265857696533,loss:1.4844205379486084
final test accuracy: 0.6128265857696533
best epoch's test accuracy: 0.6128265857696533

【TensorFlow】 基于视频时序LSTM的行为动作识别相关推荐

  1. 基于Detectron2和LSTM的人体动作识别

    人体动作识别通过分析视频来预测或分类视频中人物的各种动作.它被广泛应用于监测.体育.健身.防御等各个领域. 假设你想创建一个在线教授瑜伽的应用程序.它应该提供一个预先录制的瑜伽视频列表供用户观看.用户 ...

  2. ECCV 2022 | 浙大提出:基于骨骼点的少样本动作识别

    点击下方卡片,关注"CVer"公众号 AI/CV重磅干货,第一时间送达 作者:Dropooict |  已授权转载(源:知乎)编辑:CVer https://zhuanlan.zh ...

  3. 综述:基于骨骼(skeleton)的动作识别方法

    Deep learning‐based action recognition with 3D skeleton: Asurvey 1. 简介 1.1 3D Skeleton‐based Action ...

  4. 基于骨骼关键点的动作识别(OpenMMlab学习笔记,附PYSKL相关代码演示)

    一.骨骼动作识别 骨骼动作识别是视频理解领域的一项任务 1.1 视频数据的多种模态 RGB:使用最广,包含信息最多,从RGB可以得到Flow.Skeleton.但是处理需要较大的计算量 Flow:光流 ...

  5. 基于动态骨骼的动作识别方法ST-GCN

    解读:基于动态骨骼的动作识别方法ST-GCN(时空图卷积网络模型) 2018年04月09日 01:14:14 我是婉君的 阅读数 16076更多 分类专栏: 计算机视觉 论文 版权声明:本文为博主原创 ...

  6. Temporal Segment Networks for Action Recognition in Videos 用于动作识别的时序分割网络

    Temporal Segment Networks for Action Recognition in Videos 用于动作识别的时序分割网络 本文原创,欢迎转载 https://blog.csdn ...

  7. 解密体育背后AI黑科技:花样滑冰动作识别、多模视频分类和精彩片段剪辑

    最近,各大视频平台实时更新着冬奥赛场上的精彩瞬间集锦,谷爱凌.武大靖.苏翊鸣等运动健儿们勇闯佳绩,可喜可贺!在为中国体育的强大实力感动.欣喜的同时,我们也关注到了体育竞技背后的一些AI产业应用,比如通 ...

  8. 足球、篮球、花样滑冰、乒乓球四大运动的动作识别通用方案开源了

    北京冬奥会即将开幕,全民健身如火如荼.2020年夏季奥运会有46项体育项目,2022年冬奥会有15项体育项目,丰富的项目涉及的姿势标准也各有区别.运动员如何科学地进行体育锻炼.准确矫正健身动作?教练员 ...

  9. 基于C3D网络的视频分析与动作识别

    卷积神经网络(CNN)被广泛应用于计算机视觉中,包括分类.检测.分割等任务.这些任务一般都是针对图像进行的,使用的是二维卷积(即卷积核的维度为二维).而对于基于视频分析的问题,2D convoluti ...

最新文章

  1. nginx配置websocket负载均衡
  2. HAProxy http和https都使用mode tcp模式
  3. P2617 Dynamic Rankings(带修主席树)
  4. python--字符/文本编码解码笔记
  5. AEAP的完整形式是什么?
  6. C# 序列化与反序列化json
  7. 【英语】舞动奇迹--荡漾我心
  8. fsLayui联动表格使用(二)
  9. 如何将kux格式的视频转换成我们常用的MP4格式
  10. 华为透露成长秘诀:信息化建设铸就发展奇迹
  11. cjson构建_cJSON的构造和解析
  12. 2000份简历模板 唯美时尚简约个人简历模板 英文简历模板 简历封面 自荐信下载
  13. 阿里云DataV数据可视化简介和购买流程
  14. 应届生应聘软件开发岗位推荐书籍
  15. 如何利用COOC生成动态排名变化利器可识别的数据格式
  16. ux设计_UX设计趋势回顾展2019
  17. 时间序列--残差分析
  18. [导入]干掉Google Base? 微软欲推Fremont服务
  19. 如何区别劳动合同和劳务派遣合同
  20. 关于win弹出cmd命令行问题

热门文章

  1. Unable to resolve dependency问题解决
  2. 洛谷P4133 [BJOI2012]最多的方案(记忆化搜索)
  3. OpenBSD基金会收到锤子科技约140万捐赠款
  4. 趣说游戏AI开发:对状态机的褒扬和批判
  5. git reset, git checkout, git revert 区别 (译)
  6. 在Tomcat中配配置数据源汇总
  7. 【学习生活杂谈】学习记录
  8. 利用SQL查找表中的质数(prime number)和完全数(perfect number)以及几个有趣的SQL语句...
  9. 设计模式的功力长了!
  10. php 自定义打印模板下载,PHP – 创建自定义模板系统?