主要内容

Code Generation for LSTM Network on Raspberry Pi

This example shows how to generate code for a pretrained long short-term memory (LSTM) network that uses the ARM® Compute Library and deploy the code on a Raspberry Pi™ target. In this example, the LSTM network predicts the Remaining Useful Life (RUL) of a machine. The network takes as input time series data sets that represent various sensors in the engine. The network returns the Remaining Useful Life of an engine, measured in cycles, as its output.

This example uses the Turbofan Engine Degradation Simulation Data Set as described in [1]. This data set contains 100 training observations and 100 test observations. The training data contains simulated time series data for 100 engines. Each sequence has 17 features, varies in length, and corresponds to a full run to failure (RTF) instance. The test data contains 100 partial sequences and corresponding values of the Remaining Useful Life at the end of each sequence.

This example uses a pretrained LSTM network. For more information on how to train an LSTM network, see the exampleSequence Classification Using Deep Learning

This example demonstrates two different approaches for performing prediction by using an LSTM network:

  • 第一种方法使用标准LSTM网络,并在一组时间序列数据上运行推断。

  • The second approach leverages the stateful behavior of the same LSTM network. In this method, you pass a single timestep of data at a time, and have the network update its state at each time step.

此示例使用基于PIL基础的工作流来生成MEX函数,又调用来自Matlab的目标硬件中生成的可执行文件。

Notes:

  • The code lines in this example are commented out. Uncomment them before you run the example.

  • The ARM Compute library version that this example uses might not be the latest version that code generation supports. For information on the supported versions of the compilers and libraries, see第三方硬件和软件(MATLAB编码器)

  • This example is not supported in MATLAB Online.

Prerequisites

  • MATLAB®Coder™

  • 嵌入式编码器®

  • 深度学习工具箱™

  • MATLAB编码器界面用于深度学习库。要安装此支持包,请使用万博1manbetxAdd-On Explorer

  • MATLAB Support Package for Raspberry Pi Hardware. To install this support package, use theAdd-On Explorer

  • 覆盆子PI硬件

  • ARM Compute库(目标臂硬件上)

  • 编译器和库的环境变量。设置环境变量,请参阅环境变量(MATLAB编码器)

设置静态库的代码生成配置对象

要生成指定的入口点函数的PIL MEX函数,请为静态库创建代码配置对象,并将验证模式设置为“PIL”。将目标语言设置为C ++。

% cfg = coder.config('lib', 'ecoder', true);%cfg.verificationmode ='pil';% cfg.TargetLang = 'C++';

设置深度学习代码生成的配置对象

Create acoder.ARMNEONConfig目的。指定Compute库版本。对于此示例,假设Raspberry PI硬件中的ARM计算库为19.05版。

% dlcfg = coder.DeepLearningConfig('arm-compute');% dlcfg.ArmComputeVersion = '19.05';

Set theDeeplearningConfigproperty of the code generation configuration object to the deep learning configuration object.

% cfg.DeepLearningConfig = dlcfg;

创建与覆盆子PI的连接

Use the MATLAB Support Package for Raspberry Pi Support Package function,raspi.,创建与覆盆子PI的连接。在以下代码中,替换:

  • raspi.namewith the name of your Raspberry Pi

  • usernamewith your user name

  • passwordwith your password

%r = raspi('raspiname','用户名','password');

Configure Code Generation Hardware Parameters for Raspberry Pi

Create acoder.Hardwareobject for Raspberry Pi and attach it to the code generation configuration object.

%hw =编码器。硬件('raspberry pi');%cfg.hardware = hw;

First Approach: Generate PIL MEX Function for LSTM Network

In this approach, you generate code for the entry-point functionrul_lstmnet_predict

Therul_lstmnet_predict.mentry-point function takes an entire time series data set as an input and passes it to the network for prediction. Specifically, the function uses the LSTM network that is trained in the exampleSequence Classification Using Deep Learning。The function loads the network object from theRUL_LSTMNET.MAT.file into a persistent variable and reuses this persistent object in subsequent prediction calls. A sequence-to-sequence LSTM network enables you to make different predictions for each individual time step of a data sequence.

To display an interactive visualization of the network architecture and information about the network layers, use theanalyzeNetworkfunction.

type('rul_lstmnet_predict.m')
function out = rul_lstmnet_predict(in) %#codegen % Copyright 2019 The MathWorks, Inc. persistent mynet; if isempty(mynet) mynet = coder.loadDeepLearningNetwork('rul_lstmnet.mat'); end out = mynet.predict(in);

通过使用使用代码codegen(MATLAB编码器)command, use theCoder.typeof.(MATLAB编码器)function to specify the type and size of the input argument to the entry-point function. In this example, the input is of double data type with a feature dimension value of 17 and a variable sequence length. Specify the sequence length as variable-size to perform prediction on an input sequence of any length.

% matrixInput = coder.typeof(double(0),[17 Inf],[false true]);

运行codegen命令来生成一个基于公益诉讼的我x functionRUL_LSTMNET_PREDICT_PIL.在主机平台上。

%codegen -config cfg rul_lstmnet_predict --args {matrixInput} -report

Run Generated PIL MEX Function on Test Data

Load the MAT-filerultestdata.。此MAT文件存储变量XTest.ytest.that contain sample timeseries of sensor readings on which you can test the generated code. This test data is taken from the exampleSequence Classification Using Deep Learning在数据预处理之后。

loadrultestdata.;

TheXTest.variable contains 100 input observations. Each observation has 17 features with varying sequence length.

XTest.(1:5)
ANS =.5×1个单元阵列{17×31 double} {17×49 double} {17×126 double} {17×106 double} {17×98 double}

Theytest.变量包含100个对应的输出观察XTest.input variable. Each output observation is a Remaining Useful Life (RUI) value, measured in cycles, for each time step data in entire sequence.

ytest(1:5)
ANS =.5×1个单元阵列{[ 142 141 140 139 138 137 136 135 134 133 132 131 130 129 128 127 126 125 124 123 122 121 120 119 118 117 116 115 114 113 112]} {[ 146 145 144 143 142 141 140 139 138 137 136 135 134 133 132 131 130 129 128 127 126 125 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98]} {[150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 149 148 147 146 145 144 143 142 141 140 139 138 137 136 135 134 133 132 131 130 129 128 127 126 125 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69]} {[ 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 149 148 147 146 145 144 143 142 141 140 139 138 137 136 135 134 133 132 131 130 129 128 127 126 125 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82]} {[ 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 149 148 147 146 145 144 143 142 141 140 139 138 137 136 135 134 133 132 131 130 129 128 127 126 125 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 96 95 94 93 92 91]}

运行生成的mex函数RUL_LSTMNET_PREDICT_PIL.on a random test data set.

% idx = randperm(numel(XTest), 1);%inputdata = xtest {idx};%ypred1 = rul_lstmnet_predict_pil(inputdata);

Compare Predictions with Test Data

Use a plot to compare the MEX output data with the test data.

% figure('Name', 'Standard LSTM', 'NumberTitle', 'off');%% plot(YTest{idx},'--')% hold on% plot(YPred1,'.-')% hold off%% ylim([0 175])%标题(“测试观察”+ IDX)%xlabel(“时间步骤”)%Ylabel(“rul测量在循环中”)

Clear PIL

% clear rul_lstmnet_predict_pil;

Second Approach: Generate PIL MEX Function for Stateful LSTM Network

Instead of passing the entire timeseries data all at once to预测, you can run prediction by streaming the input data segment-wise by using the预测AndUpdateStatefunction.

的入口点函数rul_lstmnet_predict_and_update.m使用单倍时间输入并通过使用它来处理它预测AndUpdateStatefunction.预测AndUpdateStatereturns a prediction for the input timestep and updates the network so that subsequent parts of the input are treated as subsequent timesteps of the same sample.

type('rul_lstmnet_predict_and_update.m')
function out = rul_lstmnet_predict_and_update(in) %#codegen % Copyright 2019 The MathWorks, Inc. persistent mynet; if isempty(mynet) mynet = coder.loadDeepLearningNetwork('rul_lstmnet.mat'); end [mynet, out] = predictAndUpdateState(mynet, in); end

Create the input type for thecodegencommand. Becauserul_lstmnet_predict_and_updateaccepts a single timestep data in each call, specify the input type矩阵扣上to have a fixed sequence length of 1 instead of a variable sequence length.

%matrixinput = coder.typeof(double(0),[17 1]);

Run thecodegen命令生成基于PI的MEX功能rul_lstmnet_predict_and_update_pil在主机平台上。

% codegen -config cfg rul_lstmnet_predict_and_update -args {matrixInput} -report

Run Generated PIL MEX Function on Test Data

每次运行生成的MEX函数(| RUL_LSTMNET_PREDICT_AND_UPDATE_PIL |)% time step data in the inputData sequence.% sequenceLength = size(inputData,2);%Ypred2 =零(1,序列);% for i=1:sequenceLength% inTimeStep = inputData(:,i);% YPred2(:, i) = rul_lstmnet_predict_and_update_pil(inTimeStep);% end

在你传递所有时间的时间后,一次一个rul_lstmnet_predict_and_updatefunction, the resulting output is the same as that in the first approach in which you passed all inputs at once.

Compare Predictions with Test Data

Use a plot to compare the MEX output data with the test data.

%图('name','sengyfull lstm','numbertitle','关闭');%%% plot(YTest{idx},'--')% hold on% plot(YPred2,'.-')% hold off%% ylim([0 175])%标题(“测试观察”+ IDX)%xlabel(“时间步骤”)%Ylabel(“rul测量在循环中”)

Clear PIL

% clear rul_lstmnet_predict_and_update_pil;

参考

[1] Saxena,Abhinav,Kai Goebel,Don Simon和Neil Eklund。“飞机发动机碰到故障模拟的损伤传播建模。”在预后和健康管理中,2008年.PHM 2008.国际会议,第1-9页。IEEE,2008。

See Also

|(MATLAB编码器)|(MATLAB编码器)|(MATLAB编码器)

相关话题