主要内容

Code Generation for a Deep Learning Simulink Model to Classify ECG Signals

This example demonstrates how you can use powerful signal processing techniques and Convolutional Neural Networks together to classify ECG signals. We will also showcase how CUDA® code can be generated from the Simulink® model. This example uses the pretrained CNN network from theClassify Time Series Using Wavelet Analysis and Deep Learningexample of the Wavelet Toolbox™ to classify ECG signals based on images from the CWT of the time series data. For information on training, seeClassify Time Series Using Wavelet Analysis and Deep Learning(Wavelet Toolbox).

For a video demonstration on how to perform software-in-the-loop (SIL), processor-in-the-loop (PIL) simulation, and deploying this example to NVIDIA Jetson® board, see//www.tatmou.com/videos/deep-learning-in-金宝appsimulink-for-nvidia-gpus-classification-of-ecg-signals-1621401016961.html.

This example illustrates the following concepts:

  • 模拟Simulink中的分类应用程序。金宝app采用MATLAB功能执行ECG数据的预处理和小波变换的块。使用Image Classifier从深度学习Toolbox™中封锁,以加载预处理的网络并执行ECG数据的分类。

  • Configure the model for code generation.

  • Generate a CUDA executable for the Simulink model.

第三方先决条件

Verify GPU Environment

要验证正确设置此示例所需的编译器和库是否正确设置Coder.CheckgPuinstallfunction.

envcfg = coder.gpuenvconfig('主持人');envCfg.DeepLibTarget ='cudnn';envcfg.deepcodegen = 1;envcfg.quiet = 1;coder.checkgpuinstall(envcfg);

ECG数据描述

此示例使用来自ECG数据Physionetdatabase. It contains data from three groups of people:

  1. Persons with cardiac arrhythmia (ARR)

  2. Persons with congestive heart failure (CHF)

  3. 人与正常窦性节律(NSR)

它包括来自ARR的人的96张录音,有30张CHF的人的录音以及NSR的人的36张录音。这ecg_signalsMAT-file contains the test ECG data in time series format. The image classifier in this example distinguishes between ARR, CHF, and NSR.

算法工作流程

显示了Simulink模型的算法工作流程的框图。金宝app

ECG Deep Learning Simulink Model

显示了用金宝app于分类ECG信号的Simulink模型。当模型运行时Video Viewer块显示分类的心电图信号。

open_system('ecg_dl_cwt');

ECG Preprocessing Subsystem

ECG Preprocessingsubsystem contains aMATLAB功能block that performs CWT to obtain scalogram of the ECG signal and then processes the scalogram to obtain an image and anImage Classifierblock that loads the pretrained network fromtrainedNet.mat和performs prediction for image classification based on SqueezeNet deep learning CNN.

open_system('ecg_dl_cwt/ECG Preprocessing');

ScalogramFromECGfunction block defines a function calledecg_to_scalogram那:

  • 采用s 65536 samples of double-precision ECG data as input.

  • Create time frequency representation from the ECG data by applying Wavelet transform.

  • 从小波系数中获取缩放图。

  • 将比例图转换为大小的图像(227x227x3)。

这function signature ofecg_to_scalogram显示。

typeecg_to_scalogram
function ecg_image = ecg_to_scalogram(ecg_signal) % Copyright 2020 The MathWorks, Inc. persistent jetdata; if(isempty(jetdata)) jetdata = colourmap(128,'single'); end % Obtain wavelet coefficients from ECG signal cfs = cwt_ecg(ecg_signal); % Obtain scalogram from wavelet coefficients image = ind2rgb(im2uint8(rescale(cfs)),jetdata); ecg_image = im2uint8(imresize(image,[227,227])); end

ECG Postprocessing

ECG PostprocessingMATLAB function block defines thelabel_prob_image基于图像分类器输出的分数的最高分数找到缩放图图像的标签。它以标签和置信度在其上输出缩放图图像。

typelabel_prob_image
函数final_image = label_prob_image(ecg_image,分数,标签)%版权2020-2021 The MathWorks,Inc。Scores = double(分数);%获得最大置信度[prob,index] = max(得分);置信度= Prob*100;%获得对应于最大置信度标签= erase(char(标签(索引))的标签,'_ label');text =单元格(2,1);text {1} = ['分类:'label];text {2} = ['profors:'sprintf('%0.2f',profess)'%'];位置= [135 20 0 0;130 40 0 0];final_image = insertObjectAnnotation(ecg_image,'Rectangle',位置,...文本,'textboxopacity',0.9,'fontsize',9); end

Run the Simulation

打开配置参数对话框。

模拟目标pane, selectGPU acceleration. In the深度学习组,选择目标库作为cuDNN.

To verify the algorithm and display the labels and confidence score of the test ECG signal loaded in the workspace, run the simulation.

set_param('ecg_dl_cwt',“SimulationMode','Normal');sim('ecg_dl_cwt');

Generate and Build the Simulink Model

Code Generationpane, select theLanguageasC++并启用生成GPU代码.

Open代码生成> GPU代码pane. In the subcategory, 使能够cuBLAS,cuSOLVERcuFFT.

Generate and build the Simulink model on the host GPU by using theslbuild命令。代码生成器将文件放在build folder,一个子文件夹ECG_DL_CWT_ERT_RTW在您当前的工作文件夹下。

状态= evalc("slbuild('ecg_dl_cwt')");

Generated CUDA® Code

这subfolder namedECG_DL_CWT_ERT_RTW包含与Simulink模型中不同块相对应的生成的C ++代码以及在这些块中执行的特定操作。金宝app例如,文件trainedNet0_ecg_dl_cwt0.hcontains the C++ class which contains certain attributes such asnumLayers和member functions such asgetBatchSize(),predict(). This class represents the pretrainedSqueezeNet已在Simulink模型中加载。金宝app

Cleanup

Close the Simulink model.

CLOSS_SYSTEM('ecg_dl_cwt/ECG Preprocessing');CLOSS_SYSTEM('ecg_dl_cwt');