Main Content

Code Generation for Deep Learning Networks

This example shows how to perform code generation for an image classification application that uses deep learning. It uses the代码根命令通过使用图像分类网络(例如Mobilenet-V2,Resnet和GoogLenet)生成一个MEX函数,该功能通过使用图像分类网络来运行预测。

Third-Party Prerequisites

Required

This example generates CUDA MEX and has the following third-party requirements.

  • CUDA® enabled NVIDIA® GPU and compatible driver.

选修的

For non-MEX builds such as static, dynamic libraries or executables, this example has the following additional requirements.

验证GPU环境

使用coder.checkGpuInstall((GPU Coder)function to verify that the compilers and libraries necessary for running this example are set up correctly.

envCfg = coder.gpuEnvConfig('host');envcfg.deeplibtarget ='cudnn';envCfg.DeepCodegen = 1; envCfg.Quiet = 1; coder.checkGpuInstall(envCfg);

mobilenetv2_predict入口点功能

MobileNet-v2 is a convolutional neural network that is trained on more than a million images from the ImageNet database. The network is 155 layers deep and can classify images into 1000 object categories, such as keyboard, mouse, pencil, and many animals. The network has an image input size of 224-by-224. Use theanalyzeNetworkfunction to display an interactive visualization of the deep learning network architecture.

net = mobilenetv2();分析(NET);

MOBILENETV2_PREDICT.Mentry-point function takes an image input and runs prediction on the image using the pretrained MobileNet-v2 convolutional neural network. The function uses a persistent objectmynet加载串联网络对象并重用持久对象,以预测后续呼叫。

类型('mobilenetv2_predict.m'
% Copyright 2017-2019 The MathWorks, Inc. function out = mobilenetv2_predict(in) %#codegen persistent mynet; if isempty(mynet) mynet = coder.loadDeepLearningNetwork('mobilenetv2','mobilenetv2'); end % pass in input out = mynet.predict(in);

Run MEX Code Generation

为生成CUDA代码mobilenetv2_predictentry-point function, create a GPU code configuration object for a MEX target and set the target language to C++. Use thecoder.DeepLearningConfig((GPU Coder)创建一个功能库丁深度学习配置对象并将其分配给DeepLearningConfigGPU代码配置对象的属性。跑过代码根命令并指定输入大小为[224,224,3]。该值对应于Mobilenet-V2网络的输入层大小。

cfg = coder.gpuconfig('Mex');cfg.targetlang ='C ++';CFG。DeepLearningConfig = coder.DeepLearningConfig('cudnn');代码根-configCFGmobilenetv2_predict-args{一个(224,224,3)}-report
代码生成成功:查看报告

生成的代码说明

系列网络作为C ++类生成,其中包含155个层类和功能的数组,以设置,调用预测和清理网络。

班级b_mobilenetv2_0{...public: b_mobilenetv2_0(); void setup(); void predict(); void cleanup(); ~b_mobilenetv2_0(); };

设置()method of the class sets up handles and allocates memory for each layer of the network object. The预测()方法对网络中的155层中的每一层执行预测。

入口点功能mobilenetv2_predict()在生成的代码文件中mobilenetv2_predict.cu构建一个静态对象b_mobilenetv2班级type and invokes setup and predict on this network object.

静止的b_mobilenetv2_0mynet;静止的boolean_Tmynet_not_empty;
/* Function Definitions*/voidmobilenetv2_predict(const real_T in[150528], real32_T out[1000]){if((!mynet_not_empty){DeepLearningNetwork_setup(&mynet); mynet_not_empty = true; }
/ *传递输入 */ deeplearningnetwork_predict(&mynet,in,of);}

二进制文件是针对具有参数(例如完全连接和网络中的卷积层)的层导出的。例如,文件CNN_MOBILENETV2_CONV*_W和CNN_MOBILENETV2_CONV*_B对应于网络中卷积层的权重和偏置参数。要查看生成的文件列表,请使用:

dir(fullfile(PWD,'codegen',,,,'Mex',,,,'mobileenetv2_predict'))

Run Generated MEX

加载输入图像。

im = imread('peppers.png');imshow(im);

称呼mobilenetv2_predict_mex在输入图像上。

IM = Imresize(IM,[224,224]);prectiv_scores = mobilenetv2_predict_mex(double(im));

获得前五名的预测分数及其标签。

[得分,indx] = stort(prectiv_scores,“下降”);classNames = net.layers(end).classNames;ClassNamestop = classNames(Indx(1:5));h =图;h.position(3)= 2*h.position(3);AX1 =子图(1,2,1);AX2 =子图(1,2,2);图像(AX1,IM);barh(AX2,得分(5:-1:1))Xlabel(AX2,'Probability')yticklabels(ax2,classNamesTop(5:-1:1)) ax2.YAxisLocation ='right';sgtitle(“使用Mobilenet-V2的前五名预测”

清除加载在内存中的静态网络对象。

clearmex;

Classification of Images by Using ResNet-50 network

You can also use the DAG network ResNet-50 for image classification. A pretrained ResNet-50 model for MATLAB is available in the ResNet-50 support package of Deep Learning Toolbox. To download and install the support package, use the Add-On Explorer. To learn more about finding and installing add-ons, see获取并管理附加组件

net = resnet50; disp(net)
DAGNetwork with properties: Layers: [177×1 nnet.cnn.layer.Layer] Connections: [192×2 table] InputNames: {'input_1'} OutputNames: {'ClassificationLayer_fc1000'}

Run MEX Code Generation

为生成CUDA代码resnet_predict.mentry-point function,create a GPU code configuration object for a MEX target and set the target language to C++. This entry-point function calls theresnet50功能以加载网络并在输入图像上执行预测。

cfg = coder.gpuconfig('Mex');cfg.targetlang ='C ++';CFG。DeepLearningConfig = coder.DeepLearningConfig('cudnn');代码根-configCFGresnet_predict-args{一个(224,224,3)}-report
代码生成成功:查看报告

称呼resnet_predict_mex在输入图像上。

predict_scores = resnet_predict_mex(double(im));

获得前五名的预测分数及其标签。

[得分,indx] = stort(prectiv_scores,“下降”);classNames = net.layers(end).classNames;ClassNamestop = classNames(Indx(1:5));h =图;h.position(3)= 2*h.position(3);AX1 =子图(1,2,1);AX2 =子图(1,2,2);图像(AX1,IM);barh(AX2,得分(5:-1:1))Xlabel(AX2,'Probability')yticklabels(ax2,classNamesTop(5:-1:1)) ax2.YAxisLocation ='right';sgtitle(“使用Resnet-50的前五名预测”

清除加载在内存中的静态网络对象。

clearmex;

使用GoogleNet(Inception)网络对图像进行分类

在GoogleNet的Deep Learning Toolbox的GoogleNet支持软件包中,可用概括的MATLAB GOOGLENET金宝app模型。要下载并安装支持软件包,请使用附加探索器。金宝app要了解有关查找和安装附加组件的更多信息,请参阅获取并管理附加组件

net = googlenet;分配(净)
DAGNetwork with properties: Layers: [144×1 nnet.cnn.layer.Layer] Connections: [170×2 table] InputNames: {'data'} OutputNames: {'output'}

Run MEX Code Generation

生成CUDA代码googlenet_predict.m入口点功能。此入口点函数调用googlenet功能以加载网络并在输入图像上执行预测。要生成此入口点功能的代码,请为MEX目标创建GPU配置对象。

cfg = coder.gpuconfig('Mex');cfg.targetlang ='C ++';CFG。DeepLearningConfig = coder.DeepLearningConfig('cudnn');代码根-configCFGgooglenet_predict-args{一个(224,224,3)}-report
代码生成成功:查看报告

称呼googlenet_predict_mex在输入图像上。

IM = Imresize(IM,[224,224]);prectiv_scores = googlenet_predict_mex(double(im));

获得前五名的预测分数及其标签。

[得分,indx] = stort(prectiv_scores,“下降”);classNames = net.layers(end).classNames;ClassNamestop = classNames(Indx(1:5));h =图;h.position(3)= 2*h.position(3);AX1 =子图(1,2,1);AX2 =子图(1,2,2);图像(AX1,IM);barh(AX2,得分(5:-1:1))Xlabel(AX2,'Probability')yticklabels(ax2,classNamesTop(5:-1:1)) ax2.YAxisLocation ='right';sgtitle('Top Five Predictions That Use GoogLeNet'

清除加载在内存中的静态网络对象。

clearmex;

Related Topics