主要内容

深度学习网络的代码生成

这个示例演示了如何为使用深度学习的图像分类应用程序执行代码生成。它使用codegen命令生成MEX函数,通过使用MobileNet-V2,Reset和Googlenet等图像分类网络运行预测。

第三方先决条件

要求

此示例生成CUDA MEX并具有以下第三方要求。

  • CUDA®支持NVIDIA®GPU和兼容的驱动程序。

可选的

对于非mex构建,如静态、动态库或可执行文件,本例有以下附加要求。

验证GPU环境

使用coder.checkGpuInstall验证运行此示例所需的编译器和库是否已正确设置。

envcfg = coder.gpuenvconfig(“主机”);envcfg.deeplibtarget ='cudnn';envCfg。DeepCodegen = 1;envCfg。安静= 1;coder.checkGpuInstall (envCfg);

mobilenetv2_predict入学点函数

MobileNet-v2是一个卷积神经网络,在ImageNet数据库的100多万张图像上进行训练。该网络有155层深度,可以将图像分为1000个对象类别,如键盘、鼠标、铅笔和许多动物。该网络的图像输入尺寸为224 × 224。使用分析(深度学习工具箱)函数显示深度学习网络架构的交互式可视化。

网= mobilenetv2 ();analyzeNetwork(净);

MobileNetv2_predict.m.入口点函数取图像输入,并使用预先训练的MobileNet-v2卷积神经网络对图像进行预测。该函数使用持久对象mynet加载系列网络对象并重用持久对象以进行后续调用的预测。

类型('mobilenetv2_predict.m')
%Copyright 2017-2019 MathWorks,Inc。函数out = mobileNetv2_predict(in)%#codegen persistent mynet;如果是isempty(mynet)mynet = coder.loaddeeplearningnetwork('mobilenetv2','mobilenetv2');输入Out = mynet.predict(in);

运行MEX代码生成

生成的CUDA代码mobilenetv2_predict入口点函数,为MEX目标创建GPU代码配置对象,并将目标语言设置为C ++。使用编码器。DeepLearningConfig功能创建一个CUDNN.的深度学习配置对象,并将其分配给DeepLearningConfig图形处理器代码配置对象的属性。运行codegen命令并指定[224,224,3]的输入大小。该值对应于MobileNet-V2网络的输入层大小。

cfg = coder.gpuConfig ('mex');cfg。TargetLang =“c++”;cfg.deeplearningconfig = coder.deeplearningconfig('cudnn');codegen配置cfgmobilenetv2_predictarg游戏{1 (224224 3)}-报告
代码生成成功:要查看报告,请打开('codegen / mex / mobileNetv2_predict / html / eport.mldatx')。

生成的代码描述

系列网络是作为一个c++类生成的,包含155个层类和函数,用于设置、调用预测和清理网络。

班级b_mobilenetv2_0....公众:b_mobilenetv2_0 ();无效的设置();无效的预测();无效的清理();~ b_mobilenetv2_0 ();};

设置()该类的方法设置句柄并为网络对象的每层分配内存。的预测()方法对网络中的155层中的每一个执行预测。

的入口点函数mobilenetv2_predict ()在生成的代码文件中mobilenetv2_predict.cu构建一个静态对象b_mobilenetv2类类型和调用该网络对象的设置和预测。

静止的b_mobilenetv2_0.mynet;静止的Boolean_t.mynet_not_empty
/ *函数定义* /无效mobilenetv2_predict(const real_T in[150528], real32_T out[1000])如果!mynet_not_empty){Deeplearningnetwork_setup(&mynet);mynet_not_empty = true;}
/*通过输入*/ DeepLearningNetwork_predict(&mynet, in, out);}

二进制文件导出的层与参数,如完全连接和卷积层在网络。例如,文件cnn_mobilenetv2_conv*_w和cnn_mobilenetv2_conv*_b对应于网络中卷积层的权值和偏置参数。要查看生成的文件列表,请使用:

dir (fullfile (pwd,“codegen”'mex''mobilenetv2_predict')))

运行生成的mex.

加载输入图像。

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

调用mobileNetv2_predict_mex.在输入图像上。

Im = imresize(Im, [224,224]);predict_scores = mobilenetv2_predict_mex(双(im));

获得前五名的预测得分和他们的标签。

[得分,Indx] =排序(预测_coves,“下降”);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,'可能性') yticklabels (ax2 classNamesTop (5: 1:1)) ax2。YAxisLocation =“对”;sgtitle (“使用MobileNet-v2的五大预测”)

分类的视频

包括的辅助功能mobilenet_live.m从网络摄像头抓取帧,执行预测,并在每个捕获的视频帧上显示分类结果。本示例使用摄像头(USB网络摄像金宝app头MATLAB支持包)MATLAB®支持包支持USB W金宝appebCams™的功能。您可以通过支持包安装程序下载并安装支持包。金宝app

类型('mobilenet_live.m')
% Copyright 2017-2019 The MathWorks, Inc. function mobilenet_live % Connect to a camera camera = webcam;%预测得分前5名的标签%映射到相应的标签net = mobilenetv2();一会= net.Layers .ClassNames(结束);Imfull = 0 (uint8); / / uint8fps = 0;ax =轴;%拍照ipicture = camera.snapshot;%调整大小和cast图片到单一图片= imresize(ipicture,[224,224]);%为MobileNet-v2预测调用MEX函数;撅嘴= mobilenetv2_predict(单(图)); newt = toc; % fps fps = .9*fps + .1*(1/newt); % top 5 scores [top5labels, scores] = getTopFive(pout,classnames); % display if isvalid(ax) dispResults(ax, imfull, picture, top5labels, scores, fps); else break; end end end function dispResults(ax, imfull, picture, top5labels, scores, fps) for k = 1:3 imfull(:,177:end,k) = picture(:,:,k); end h = imshow(imfull, 'InitialMagnification',200, 'Parent', ax); scol = 1; srow = 20; text(get(h, 'Parent'), scol, srow, sprintf('MobileNet-v2 Demo'), 'color', 'w', 'FontSize', 20); srow = srow + 20; text(get(h, 'Parent'), scol, srow, sprintf('Fps = %2.2f', fps), 'color', 'w', 'FontSize', 15); srow = srow + 20; for k = 1:5 t = text(get(h, 'Parent'), scol, srow, top5labels{k}, 'color', 'w','FontSize', 15); pos = get(t, 'Extent'); text(get(h, 'Parent'), pos(1)+pos(3)+5, srow, sprintf('%2.2f%%', scores(k)), 'color', 'w', 'FontSize', 15); srow = srow + 20; end drawnow; end function [labels, scores] = getTopFive(predictOut,classnames) [val,indx] = sort(predictOut, 'descend'); scores = val(1:5)*100; labels = classnames(indx(1:5)); end

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

清晰的墨西哥人

基于ResNet-50网络的图像分类

您还可以使用DAG网络Reset-50进行图像分类。Reset-50支持包的Deep Learning Toolbox的Reset-50支持包中提供了Pretry Reset-50模型。金宝app要下载和安装支持包,请使用Add-On Explorer金宝app。要了解有关查找和安装附加组件的更多信息,请参见获取和管理附加组件

net = resnet50;disp(净)
带有属性的DAGNetwork: Layers: [177×1 nnet.cnn.layer.Layer] Connections: [192×2 table] InputNames: {'input_1'} OutputNames: {'ClassificationLayer_fc1000'}

运行MEX代码生成

生成的CUDA代码resnet_predict.m.为MEX目标创建一个GPU代码配置对象,并将目标语言设置为c++。这个入口点函数调用resnet50函数加载网络并对输入图像执行预测。

cfg = coder.gpuConfig ('mex');cfg。TargetLang =“c++”;cfg.deeplearningconfig = coder.deeplearningconfig('cudnn');codegen配置cfgresnet_predictarg游戏{1 (224224 3)}-报告
代码生成成功:要查看报告,打开('codegen/mex/resnet_predict/html/report.mldatx')。

调用resnet_predict_mex在输入图像上。

predict_cores = resnet_predict_mex(double(im));

获得前五名的预测得分和他们的标签。

[得分,Indx] =排序(预测_coves,“下降”);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,'可能性') yticklabels (ax2 classNamesTop (5: 1:1)) ax2。YAxisLocation =“对”;sgtitle (“使用ResNet-50的五大预测”)

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

清晰的墨西哥人

基于GoogLeNet (Inception)网络的图像分类

深度学习工具箱中的GoogLeNet支持包中有一个用于MATLAB的预先训练的GoogLeNet模型。金宝app要下载和安装支持包,请使用Add-On Explorer金宝app。要了解有关查找和安装附加组件的更多信息,请参见获取和管理附加组件

网= googlenet;disp(净)
具有属性的Dagnetwork:图层:[144×1 nnet.cnn.layer.layer]连接:[170×2表]输入名称:{'data'} OutputNames:{'输出'}

运行MEX代码生成

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

cfg = coder.gpuConfig ('mex');cfg。TargetLang =“c++”;cfg.deeplearningconfig = coder.deeplearningconfig('cudnn');codegen配置cfggooglenet_predict.arg游戏{1 (224224 3)}-报告
代码生成成功:要查看报告,打开('codegen/mex/googlenet_predict/html/report.mldatx')。

调用googlenet_predict_mex.在输入图像上。

Im = imresize(Im, [224,224]);predict_scores = googlenet_predict_mex(双(im));

获得前五名的预测得分和他们的标签。

[得分,Indx] =排序(预测_coves,“下降”);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,'可能性') yticklabels (ax2 classNamesTop (5: 1:1)) ax2。YAxisLocation =“对”;sgtitle (“使用googlenet的五大预测”)

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

清晰的墨西哥人

也可以看看

功能

对象

相关的话题