code generation for a deep learning simulink model to classify ecg signals -凯发k8网页登录
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 the classify time series using wavelet analysis and deep learning example of the wavelet toolbox™ to classify ecg signals based on images from the cwt of the time series data. for information on training, see (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 .
this example illustrates the following concepts:
model the classification application in simulink. use
matlab function
blocks to perform preprocessing and wavelet transforms of the ecg data. use theimage classifier
block from the deep learning toolbox™ for loading the pretrained network and performing the classification of the ecg data.configure the model for code generation.
generate a cuda executable for the simulink model.
third-party prerequisites
cuda enabled nvidia gpu.
nvidia cuda toolkit and driver.
nvidia cudnn library.
environment variables for the compilers and libraries. for more information, see third-party hardware and .
verify gpu environment
to verify that the compilers and libraries necessary for running this example are set up correctly, use the function.
envcfg = coder.gpuenvconfig('host'); envcfg.deeplibtarget = 'cudnn'; envcfg.deepcodegen = 1; envcfg.quiet = 1; coder.checkgpuinstall(envcfg);
ecg data description
this example uses ecg data from database. it contains data from three groups of people:
persons with cardiac arrhythmia (arr)
persons with congestive heart failure (chf)
persons with normal sinus rhythms (nsr)
it includes 96 recordings from persons with arr, 30 recordings from persons with chf, and 36 recordings from persons with nsr. the ecg_signals
mat-file contains the test ecg data in time series format. the image classifier in this example distinguishes between arr, chf, and nsr.
algorithmic workflow
the block diagram for the algorithmic workflow of the simulink model is shown.
ecg deep learning simulink model
the simulink model for classifying the ecg signals is shown. when the model runs, the video viewer
block displays the classified ecg signal.
open_system('ecg_dl_cwt');
ecg preprocessing subsystem
the ecg preprocessing
subsystem contains a matlab function
block that performs cwt to obtain scalogram of the ecg signal and then processes the scalogram to obtain an image and an image classifier
block that loads the pretrained network from trainednet.mat
and performs prediction for image classification based on squeezenet deep learning cnn.
open_system('ecg_dl_cwt/ecg preprocessing');
the scalogramfromecg
function block defines a function called ecg_to_scalogram
that:
uses 65536 samples of double-precision ecg data as input.
create time frequency representation from the ecg data by applying wavelet transform.
obtain scalogram from the wavelet coefficients.
convert the scalogram to image of size (227x227x3).
the function signature of ecg_to_scalogram
is shown.
type ecg_to_scalogram
function ecg_image = ecg_to_scalogram(ecg_signal) % 凯发官网入口首页 copyright 2020 the mathworks, inc. persistent jetdata; if(isempty(jetdata)) jetdata = ecgcolormap(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
the ecg postprocessing
matlab function block defines the label_prob_image
function that finds the label for the scalogram image based on the highest score from the scores outputed by the image classifier. it outputs the scalogram image with the label and confidence printed on it.
type label_prob_image
function final_image = label_prob_image(ecg_image, scores, labels) % 凯发官网入口首页 copyright 2020-2021 the mathworks, inc. scores = double(scores); % obtain maximum confidence [prob,index] = max(scores); confidence = prob*100; % obtain label corresponding to maximum confidence label = erase(char(labels(index)),'_label'); text = cell(2,1); text{1} = ['classification: ' label]; text{2} = ['confidence: ' sprintf('%0.2f',confidence) '%']; position = [135 20 0 0; 130 40 0 0]; final_image = insertobjectannotation(ecg_image,'rectangle',position,... text,'textboxopacity',0.9,'fontsize',9); end
run the simulation
open configuration parameters dialog box.
in simulation target pane, select gpu acceleration. in the deep learning group, select the target library as 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
in code generation pane, select the language as c and enable generate gpu code.
open code generation > gpu code pane. in the subcategory libraries, enable cublas, cusolver and cufft.
generate and build the simulink model on the host gpu by using the slbuild
command. the code generator places the files in a build folder, a subfolder named ecg_dl_cwt_ert_rtw
under your current working folder.
status = evalc("slbuild('ecg_dl_cwt')");
generated cuda® code
the subfolder named ecg_dl_cwt_ert_rtw
contains the generated c codes corresponding to the different blocks in the simulink model and the specific operations being performed in those blocks. for example, the file trainednet0_ecg_dl_cwt0.h
contains the c class which contains certain attributes such as numlayers
and member functions such as getbatchsize()
, predict()
. this class represents the pretrained squeezenet
which has been loaded in the simulink model.
cleanup
close the simulink model.
close_system('ecg_dl_cwt/ecg preprocessing'); close_system('ecg_dl_cwt');
see also
functions
- (simulink) | (simulink) | (simulink) | (simulink) | (simulink) | (simulink) | (simulink) | (simulink) |
slbuild
(simulink)