LRCN_activity_recognition

这篇博客详细介绍了如何使用Caffe框架重新训练LRCN(Long Short-Term Memory)活动识别模型。步骤包括从UCF-101视频中提取RGB帧和计算光流,然后训练单帧模型和LRCN模型。提供的脚本包括run_singleFrame_RGB.sh、run_singleFrame_flow.sh、run_lstm_RGB.sh和run_lstm_flow.sh。
摘要由CSDN通过智能技术生成

http://www.eecs.berkeley.edu/~lisa_anne/LRCN_video

作者用的是caffe。。

Steps to retrain the LRCN activity recognition models:

  1. Extract RGB frames: The script “extract_frames.sh” will convert UCF-101 .avi files to .jpg images. I extracted frames at 30 frames/second.
    提取视频帧。
  2. Compute flow frames: After downloading the code from [1], you can use “create_flow_images_LRCN.m” to compute flow frames. Example flow images for the video “YoYo_g25_c03” are here.
    计算光流。
  3. Train single frame models: Finetune the hybrid model (found here) with video frames to train a single frame model. Use “run_singleFrame_RGB.sh” and “run_singleFrame_flow.sh” to train the RGB and flow models respectively. Make sure to change the “root_folder” param in “train_test_singleFrame_RGB.prototxt” and “train_test_singleFrame_flow.prototxt” as needed. The single frame models I trained can be found here.
    对RGB和flow分别训练baseline模型。
  4. Train LRCN models: Using the single frame models as a starting point, train the LRCN models by running “run_lstm_RGB.sh” and “run_lstm_flow.sh“. The data layer for the LRCN model is a python layer (“sequence_input_layer.py”). Make sure to set “WITH_PYTHON_LAYER := 1” in Makefile.config. Change the paths “flow_frames” and “RGB_frames” in “sequence_input_layer.py” as needed. The models I trained can be found here.
    分别训练LRCN模型。

run_singleFrame_RGB.sh

#!/bin/sh
TOOLS=../../build/tools

GLOG_logtostderr=1 $TOOLS/caffe train -solver singleFrame_solver_RGB.prototxt -weights caffe_imagenet_hyb2_wr_rc_solver_sqrt_iter_310000 
echo 'Done.'

GLOG_logtostderr=1 设置glog日志。glog是google 出的一个C++轻量级日志库,介绍请看

好的,以下是一份可能的代码实现(仅供参考): ```python import numpy as np import tensorflow as tf from sklearn.preprocessing import MinMaxScaler from pyswarm import pso from lrcn import LRCN # LRCN是自己实现的LSTM+CNN模型 # 读取数据 data = np.loadtxt('data.txt', delimiter=',') X = data[:, :-1] y = data[:, -1] # 归一化 scaler = MinMaxScaler() X_scaled = scaler.fit_transform(X) # 划分数据集 split_ratio = 0.8 split_index = int(split_ratio * len(X)) X_train, X_test = X_scaled[:split_index], X_scaled[split_index:] y_train, y_test = y[:split_index], y[split_index:] # 定义LSTM+CNN模型 model = LRCN(input_shape=(X.shape[1], 1)) model.compile(optimizer='adam', loss='mse') # 定义适应度函数 def fitness_function(params, X_train, y_train, X_test, y_test): # 解压参数 learning_rate, batch_size, num_epochs = params # 构建模型 model = LRCN(input_shape=(X_train.shape[1], 1)) optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate) model.compile(optimizer=optimizer, loss='mse') # 训练模型 model.fit(X_train, y_train, batch_size=batch_size, epochs=num_epochs) # 在测试集上计算预测误差 y_pred = model.predict(X_test) mse = np.mean((y_test - y_pred)**2) return mse # 定义参数空间 lb = [0.0001, 10, 10] # 最小学习率,最小批大小,最小迭代次数 ub = [0.01, 100, 100] # 最大学习率,最大批大小,最大迭代次数 # 使用粒子群算法寻找最优超参数 best_mse, best_params = pso(fitness_function, lb, ub, args=(X_train.reshape(-1, X_train.shape[1], 1), y_train, X_test.reshape(-1, X_test.shape[1], 1), y_test), swarmsize=10, maxiter=100) # 输出最优超参数 print("Best MSE:", best_mse) print("Best Params:", best_params) # 用最优超参数训练模型并预测身体寿命 learning_rate, batch_size, num_epochs = best_params optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate) model.compile(optimizer=optimizer, loss='mse') model.fit(X_scaled.reshape(-1, X.shape[1], 1), y, batch_size=batch_size, epochs=num_epochs) predicted_lifetimes = model.predict(X_scaled.reshape(-1, X.shape[1], 1)) ``` 其中,`data.txt`是包含电涌保护器数据的文本文件,每行是一个样本,最后一列是对应的身体寿命。`LRCN`是自己实现的LSTM+CNN模型,可以根据具体需求进行修改。`fitness_function`是适应度函数,用于计算在给定超参数下模型在测试集上的预测误差。`pso`是使用粒子群算法搜索最优超参数的函数,其中`swarmsize`和`maxiter`是粒子群算法的超参数,可以根据具体需求进行修改。最后,使用最优超参数训练模型并预测身体寿命。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值