LRCN(4) solver之train_test_lstm_RGB.prototxt

数据层

该网络的数据层为python层,从sequence_input_layer.py中导入。

name: "lstm_joints"
layer {
  name: "data"
  type: "Python"
  top: "data"
  top: "label"
  top: "clip_markers" #标志是否为连续帧
  python_param {
    module: "sequence_input_layer" 
    layer: "videoReadTrain_RGB" #训练阶段输入层,为.py中的一个类
  }
  include: { phase: TRAIN }
}

网络结构可视化
这里写图片描述
top:表示output
bottom:表示input
data 与 label: 在数据层中,至少有一个命名为data的top(输出)。如果有第二个top,一般命名为label。 这种(data,label)配对是分类模型所必需的。

type:网络类型,有的数据层是ImageData,这里输入是一个python文件作为数据层的类型。

fc6

这里写图片描述
fc6有三个处理步骤
- 1.做内积 innerproduct
- 2.relu
- 3.dropout
输出的数据送给reshape-data层

layer {
  name: "fc6"
  type: "InnerProduct"
  bottom: "pool5"
  top: "fc6"  #名字是 fc6,输出也是fc6,目的是进行relu和dropout
  param {
    lr_mult: 1
    decay_mult: 1
  }
  param {
    lr_mult: 2
    decay_mult: 0
  }
  inner_product_param {
    num_output: 4096
    weight_filler {
      type: "gaussian"
      std: 0.01
    }
    bias_filler {
      type: "constant"
      value: 0.1
    }
  }
}
layer {
  name: "relu6"
  type: "ReLU"
  bottom: "fc6"  #输入fc6
  top: "fc6"     #输出fc6
}
layer {
  name: "drop6"
  type: "Dropout"
  bottom: "fc6"    #输入fc6
  top: "fc6"       #输出fc6
  dropout_param {
    dropout_ratio: 0.9
  }
}

reshape-data

fc6的输出维度是4096维,通过这一层变成16*3*4096


  bottom: "fc6"
  top: "fc6-reshape"
  reshape_param{
    shape{
      dim: 16
      dim: 3
      dim: 4096
    }
  }

fc6-reshape是reshape-data的输出

reshape-cm

将clip-markers变成输出维度16*3,reshape-data和reshape-cm是要配套使用的

  bottom: "clip_markers"
  top: "reshape-cm"
  reshape_param{
    shape{
      dim: 16
      dim: 3
    }

LSTM层

这里写图片描述

  • N 为LSTM同时处理的独立流的个数,在该实验中为输入LSTM相互独立的视频的个数,以该实验测试网络为例,本文取T=3。

  • T 为LSTM网络层处理的时间步总数,在该实验中为输入LSTM的任意一独立视频的视频帧个数,以该实验测试网络为例,本文取T=16。

  • 4096为AlexNet中全连接层的维度,即CNN特征的维度。

  • reshape-cm的输出维度为 T×N,即每一个帧有一个是否连续帧的标志。

  • reshape-label的维度同样为 T×N
    参考caffe中lstm的实现以及lstmlayer的理解

Caffe中一个LSTMLayer即为一个LSTM网络。CNN特征维度为4096,LSTM特征维度为256。

layer {
  name: "lstm1"
  type: "LSTM"
  bottom: "fc6-reshape"
  bottom: "reshape-cm"
  top: "lstm1"
  recurrent_param {
    num_output: 256
    weight_filler {
      type: "uniform"
      min: -0.01
      max: 0.01
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}

lstm1-drop

layer {
  name: "lstm1-drop"
  type: "Dropout"
  bottom: "lstm1"
  top: "lstm1-drop"
  dropout_param {
    dropout_ratio: 0.5
  }
}

附录

卷积层

layer {
  name: "conv1"
  type: "Convolution"
  bottom: "data"
  top: "conv1"
  param {
    lr_mult: 1
    decay_mult: 1
  }
  param {
    lr_mult: 2
    decay_mult: 0
  }
  convolution_param {
    num_output: 96
    kernel_size: 7
    stride: 2
    weight_filler {
      type: "gaussian"
      std: 0.01
    }
    bias_filler {
      type: "constant"
      value: 0.1
    }
  }
}

lr_mult
decay_mult
num_output
type: “constant”

附录

# python draw_net.py prototxt文件名 保存图片文件名 --rankdir=方向,比如:BT是从上到下
python draw_net.py lenet_train_test.prototxt mnist\lenet_train_test.png --rankdir=LR

stage: “test-on-test” include

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一份可能的代码实现(仅供参考): ```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`是粒子群算法的超参数,可以根据具体需求进行修改。最后,使用最优超参数训练模型并预测身体寿命。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值