【TensorFlow】 基于视频时序LSTM的行为动作识别
生活随笔
收集整理的這篇文章主要介紹了
【TensorFlow】 基于视频时序LSTM的行为动作识别
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
簡介
本文基于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('')?
運行結果:
?
總結
以上是生活随笔為你收集整理的【TensorFlow】 基于视频时序LSTM的行为动作识别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Stacked Hourglass Ne
- 下一篇: 第十章触发器的创建与管理