主要内容

校准

模拟和收集深度神经网络的范围

描述

calibrationResults=校准(quantObj,calData)运用网络,收集网络的卷积层和全连接层中权重和偏差的动态范围,以及指定的网络各层中激活的动态范围数字量化器对象,quantObj,使用calData

calibrationResults=校准(quantObj,calData,名称,值)运用网络,收集网络的卷积层和全连接层中权重和偏差的动态范围,以及指定的网络各层中激活的动态范围数字量化器对象,quantObj,使用calData,具有由一个或多个名称-值对参数指定的附加参数。

要了解量化深度神经网络所需的产品,请参s manbetx 845阅量化工作流先决条件

例子

全部折叠

此示例演示如何量化神经网络卷积层中的可学习参数,并探索量化网络的行为squeezenet神经网络经过再训练后,根据网络的特征对新图像进行分类训练深度学习网络对新图像进行分类的例子。在这个例子中,网络所需的内存通过量化减少了约75%,而网络的准确性却没有受到影响。

加载预先训练的网络。

net = DAGNetwork with properties: Layers: [68x1 nnet.cnn.layer.Layer] Connections: [75x2 table] InputNames: {'data'} OutputNames: {'new_classoutput'}

定义用于量化的校准和验证数据。

标定数据用于收集网络卷积层和全连接层中权值和偏差的动态范围,以及网络各层中激活的动态范围。为了获得最好的量化结果,校准数据必须能代表网络的输入。

验证数据用于量化后的网络测试,以了解网络中量化卷积层的有限范围和精度的影响。

在本例中,使用水星数据集。定义一个augmentedImageDatastore对象以调整网络数据的大小。然后,将数据分解为校准和验证数据集。

解压(“MerchData.zip”);imd = imageDatastore (“水星”,...“IncludeSubfolders”符合事实的...“LabelSource”,“foldernames”); [calData,valData]=splitEachLabel(imds,0.7,“随机”);aug_calData=增强的图像数据存储([227 227],calData);aug_valData=增强的图像数据存储([227 227],valData);

创建一个数字量化器对象,并指定要量化的网络。

quantObj=数字量化器(净);

定义用于比较量化前后网络行为的度量函数。将此函数保存在本地文件中。

函数hComputeModelAccuracy = hComputeModelAccuracy(predictionScores, net, dataStore)%%计算模型级精度统计信息负载地面真实值tmp=readall(数据存储);groundTruth=tmp.response;%将预测标签与实际情况进行比较predictionError = {};idx=1:numel(groundTruth) [~, idy] = max(predictionScores(idx,:)); / /将groundTruth改为groundTruthyActual = net.Layers(结束). class(补贴);predictionError{end+1} = (yActual == groundTruth(idx));% #好吧结束%汇总所有预测错误。predictionError=[predictionError{:}];精度=总和(预测误差)/numel(预测误差);结束

指定度量函数中的度量值dlquantizationOptions对象。

quantOpts = dlquantizationOptions (“MetricFcn”,...{@ (x) hComputeModelAccuracy (x,净,aug_valData)});

使用校准函数通过样本输入来运行网络,并收集范围信息。的校准函数练习网络,并收集网络卷积层和完全连接层中的权重和偏差的动态范围,以及网络所有层中激活的动态范围。函数返回一个表。表的每一行包含优化网络的可学习参数的范围信息rk。

calResults = calibrate(quantObj, aug_calData)
calResults = 95 x5表优化层名称网络层名称可学的/激活MinValue MaxValue  __________________________________________________ _________________________ ________________________ __________ ___________ {' conv1_relu_conv1_Weights’}{‘relu_conv1}“权重”-0.91985 - 0.88489{‘conv1_relu_conv1_Bias} {' relu_conv1 '}“偏见”-0.07925 - 0.26343{‘fire2-squeeze1x1_fire2-relu_squeeze1x1_Weights}{‘fire2-relu_squeeze1x1}“权重”-1.38 - 1.2477{‘fire2-squeeze1x1_fire2-relu_squeeze1x1_Bias}{‘fire2-relu_squeeze1x1}“偏见”-0.11641 - 0.24273{‘fire2-expand1x1_fire2-relu_expand1x1_Weights}{‘fire2-relu_expand1x1}“权重”-0.7406 - 0.90982{' fire2-expand1x1_fire2-relu_expand1x1_Bias}{‘fire2-relu_expand1x1}“偏见”-0.060056 - 0.14602{‘fire2-expand3x3_fire2-relu_expand3x3_Weights}{‘fire2-relu_expand3x3}“权重”-0.74397 - 0.66905{‘fire2-expand3x3_fire2-relu_expand3x3_Bias}{‘fire2-relu_expand3x3}“偏见”-0.051778 - 0.074239 {' fire3-squeeze1x1_fire3-relu_squeeze1x1_Weights '}{'fire3-relu_squeeze1x1'} "Weights" -0.77263 0.68897 {'fire3-squeeze1x1_fire3-relu_squeeze1x1_Bias' } {'fire3-relu_squeeze1x1'} "Bias" -0.10141 0.32678 {'fire3-expand1x1_fire3-relu_expand1x1_Weights' } {'fire3-relu_expand1x1' } "Weights" -0.72131 0.97287 {'fire3-expand1x1_fire3-relu_expand1x1_Bias' } {'fire3-relu_expand1x1' } "Bias" -0.067043 0.30424 {'fire3-expand3x3_fire3-relu_expand3x3_Weights' } {'fire3-relu_expand3x3' } "Weights" -0.61196 0.77431 {'fire3-expand3x3_fire3-relu_expand3x3_Bias' } {'fire3-relu_expand3x3' } "Bias" -0.053612 0.10329 {'fire4-squeeze1x1_fire4-relu_squeeze1x1_Weights'} {'fire4-relu_squeeze1x1'} "Weights" -0.74145 1.0888 {'fire4-squeeze1x1_fire4-relu_squeeze1x1_Bias' } {'fire4-relu_squeeze1x1'} "Bias" -0.10886 0.13882 ...

使用验证函数对网络的卷积层中的可学习参数进行量化,并对网络进行锻炼。中定义的度量函数dlquantizationOptions目的比较网络量化前后的结果。

valResults=验证(quantObj、aug_valData、quantOpts)
validresults = struct with fields: NumSamples: 20 metrics results: [1x1 struct]

检查MetricResults。结果字段的验证输出以查看量化网络的性能。

valResults.MetricResults.Result
ans = 2 x3表NetworkImplementation MetricOutput LearnableParameterMemory(字节 ) _____________________ ____________ _______________________________ {' 浮点'}1 2.9003 e + 06{“量子化”}1 7.3393 e + 05

在这个例子中,网络所需的内存通过量化减少了大约75%。不影响网络的准确性。

中指定的网络卷积层的权重、偏差和激活数字量化器对象现在使用缩放的8位整数数据类型。

此示例演示如何量化神经网络卷积层中的可学习参数,并探索量化网络的行为标识网神经网络。量化通过将网络层的权重、偏差和激活量化为8位比例整数数据类型,有助于减少深度神经网络的内存需求。使用MATLAB®从目标设备检索预测结果。

要运行此示例,您需要下面列出的产品s manbetx 845FPGA量化工作流先决条件

有关其他要求,请参见量化工作流先决条件

在当前工作目录中创建一个名为getLogoNetwork.m.在文件中输入以下几行:

函数net = getLogoNetwork() data = getLogoNetwork(); / /网= data.convnet;结束函数data = getLogoData ()如果~isfile(“LogoNet.mat”) url ='//www.tianjin-qmedu.com/万博1manbetxsupportfiles/gpucoder/cnn_models/logo_detection/LogoNet.mat'; 韦伯萨夫(“LogoNet.mat”url);结束data =负载(“LogoNet.mat”);结束

加载预先训练的网络。

snet=getlogonnetwork();
snet = SeriesNetwork with properties: Layers: [22×1 net.cnn.layer. layer] InputNames: {'imageinput'} OutputNames: {'classoutput'}

定义用于量化的校准和验证数据。

标定数据用于收集网络卷积层和全连接层中权值和偏差的动态范围,以及网络各层中激活的动态范围。为了获得最好的量化结果,校准数据必须能代表网络的输入。

验证数据用于量化后的网络测试,以了解网络中量化卷积层的有限范围和精度的影响。

中的图像logos_dataset数据集。定义一个augmentedImageDatastore对象以调整网络数据的大小。然后,将数据分解为校准和验证数据集。

curDir = pwd;newDir = fullfile (matlabroot,“例子”,“深度学习共享”,“数据”,“logos_dataset.zip”);复制文件(newDir,curDir);解压缩(“logos_dataset.zip”); imageData=imageDatastore(完整文件(curDir,“logos_dataset”),...“IncludeSubfolders”符合事实的“FileExtensions”,“jpg”,“LabelSource”,“foldernames”);[calibrationData,validationData]=splitEachLabel(imageData,0.5,“随机”);

创建一个数字量化器对象,并指定要量化的网络。

dlQuantObj=dlquantizer(snet,“执行环境”,FPGA的);

使用校准函数通过样本输入来运行网络,并收集范围信息。的校准函数练习网络,并收集网络卷积层和完全连接层中的权重和偏差的动态范围,以及网络所有层中激活的动态范围。函数返回一个表。表的每一行包含优化网络的可学习参数的范围信息rk。

dlQuantObj.calibrate (calibrationData)
ans =优化层名称网络层名称可学的/激活MinValue MaxValue  ____________________________ __________________ ________________________ ___________ __________ {' conv_1_Weights’}{‘conv_1}“权重”-0.048978 - 0.039352{‘conv_1_Bias}{‘conv_1}“偏见”0.99996 - 1.0028{‘conv_2_Weights}{‘conv_2}“权重”-0.0555180.061901{‘conv_2_Bias}{‘conv_2}“偏见”-0.00061171 - 0.00227{‘conv_3_Weights}{‘conv_3}“权重”-0.045942 - 0.046927{‘conv_3_Bias}{‘conv_3}“偏见”-0.0013998 - 0.0015218{‘conv_4_Weights}{‘conv_4}“权重”-0.045967 - 0.051{‘conv_4_Bias}{‘conv_4}“偏见”-0.00164 - 0.0037892{‘fc_1_Weights}{‘fc_1}“权重”-0.0513940.054344 {'fc_1_Bias'} {'fc_1'} "Bias" -0.05016 0.051557 {'fc_2'} "Bias" -0.0017564 0.0018502 {'fc_3_Weights'} {'fc_3'} "Bias" -0.050706 0.04678 0.04678 {'fc_3_Bias'} {'fc_3'} "Bias" -0.02951 0.024855 {'imageinput'} {'imageinput'} "Activations" 0 255{'imageinput_normalization'} {'imageinput'} " activities " -139.34 198.72

为您的目标设备创建一个具有自定义名称的目标对象,并创建一个将目标设备连接到主机的接口。接口选项有JTAG和Ethernet。创建目标对象,输入:

hTarget=dlhdl.Target(“英特尔”,“界面”,“JTAG”);

定义用于比较量化前后网络行为的度量函数。将此函数保存在本地文件中。

函数hComputeAccuracy = hComputeAccuracy(predictionScores, net, dataStore)%% hComputeAccuracy测试助手函数计算模型级精度统计版权所有2020 The MathWorks, Inc.负载地面真实值groundTruth=数据存储。标签;%将预测标签与实际情况进行比较predictionError = {};idx=1:numel(groundTruth) [~, idy] = max(predictionScores(idx,:)); / /将groundTruth改为groundTruthyActual = net.Layers(结束). class(补贴);predictionError{end+1} = (yActual == groundTruth(idx));% #好吧结束%汇总所有预测错误。predictionError=[predictionError{:}];精度=总和(预测误差)/numel(预测误差);结束

指定度量函数中的度量值dlquantizationOptions对象。

选择= dlquantizationOptions (“MetricFcn”,...{@ (x) hComputeModelAccuracy (x, snet validationData)},“比特流”,“arria10soc_int8”,...“目标”, hTarget);

要编译和部署量化网络,请运行验证委员会的职能数字量化器对象。使用验证函数对网络的卷积层中的可学习参数进行量化,并对网络进行锻炼。该函数使用compile函数的输出,利用编程文件对FPGA板进行编程。它还下载了网络权重和偏差。deploy功能检查Intel Quartus工具和支持的工具版本。万博1manbetx然后它通过使用sof文件开始对FPGA设备编程,显示进度消息,以及部署网络所需的时间。中定义的度量函数dlquantizationOptions目的比较网络量化前后的结果。

预测= dlQuantObj.validate (validationData选项);
offset_name offset_address Allocated_space ____________________________________________________________________________________________________________________00000000“”0x03000000“”4.0 MB“”SYSTEMBUFFEROFFSET“”0x03400000“”0x03400000“”60.0 MB“”0x07000000“”8.0 MB“”CONCHWEIGHTDATAOFFSOFS“”。0x07800000“8.0 MB”“FCWeightDataOffset”“0x08000000”“12.0 MB”“0x08C00000”“0x08C00000”“总数:140.0 MB”###编程FPGA比特流使用JTAG ... ###编程FPGA比特流已成功完成.###加载权重到CONV处理器。###加载了Conr权重。当前时间是16-jul-2020 12:45:10 ###加载FC处理器的权重。###加载FC重量。当前时间为16-jul-2020 12:45:26 ###完成写入输入激活。###正在运行单个输入激活。深度学习处理器分析器绩效结果LastLayerLatency(周期)LastLayerLatency(秒)FramesNum Total Latency Frames / s -------------  -   -   -   -   -   -   -   -   -   -   -   -------------- ---------CONC_4 455434 0.00304 MAXPOOL_4 11251 0.00008 FC_MODULE 903173 0.00602 FC_1 536164 0.00357 FC_2 342643 0.00228 0.00228 FC_3 24364 0.00016 * DL处理器的时钟频率为:150MHz ###完成写入输入激活。 ### Running single input activations. Deep Learning Processor Profiler Performance Results LastLayerLatency(cycles) LastLayerLatency(seconds) FramesNum Total Latency Frames/s ------------- ------------- --------- --------- --------- Network 13570364 0.09047 30 380612682 11.8 conv_module 12667103 0.08445 conv_1 3939296 0.02626 maxpool_1 1544371 0.01030 conv_2 2910747 0.01940 maxpool_2 577654 0.00385 conv_3 2551829 0.01701 maxpool_3 676548 0.00451 conv_4 455396 0.00304 maxpool_4 11355 0.00008 fc_module 903261 0.00602 fc_1 536206 0.00357 fc_2 342688 0.00228 fc_3 24365 0.00016 * The clock frequency of the DL processor is: 150MHz ### Finished writing input activations. ### Running single input activations. Deep Learning Processor Profiler Performance Results LastLayerLatency(cycles) LastLayerLatency(seconds) FramesNum Total Latency Frames/s ------------- ------------- --------- --------- --------- Network 13571561 0.09048 30 380608338 11.8 conv_module 12668340 0.08446 conv_1 3939070 0.02626 maxpool_1 1545327 0.01030 conv_2 2911061 0.01941 maxpool_2 577557 0.00385 conv_3 2552082 0.01701 maxpool_3 676506 0.00451 conv_4 455582 0.00304 maxpool_4 11248 0.00007 fc_module 903221 0.00602 fc_1 536167 0.00357 fc_2 342643 0.00228 fc_3 24409 0.00016 * The clock frequency of the DL processor is: 150MHz ### Finished writing input activations. ### Running single input activations. Deep Learning Processor Profiler Performance Results LastLayerLatency(cycles) LastLayerLatency(seconds) FramesNum Total Latency Frames/s ------------- ------------- --------- --------- --------- Network 13569862 0.09047 30 380613327 11.8 conv_module 12666756 0.08445 conv_1 3939212 0.02626 maxpool_1 1543267 0.01029 conv_2 2911184 0.01941 maxpool_2 577275 0.00385 conv_3 2552868 0.01702 maxpool_3 676438 0.00451 conv_4 455353 0.00304 maxpool_4 11252 0.00008 fc_module 903106 0.00602 fc_1 536050 0.00357 fc_2 342645 0.00228 fc_3 24409 0.00016 * The clock frequency of the DL processor is: 150MHz ### Finished writing input activations. ### Running single input activations. Deep Learning Processor Profiler Performance Results LastLayerLatency(cycles) LastLayerLatency(seconds) FramesNum Total Latency Frames/s ------------- ------------- --------- --------- --------- Network 13570823 0.09047 30 380619836 11.8 conv_module 12667607 0.08445 conv_1 3939074 0.02626 maxpool_1 1544519 0.01030 conv_2 2910636 0.01940 maxpool_2 577769 0.00385 conv_3 2551800 0.01701 maxpool_3 676795 0.00451 conv_4 455859 0.00304 maxpool_4 11248 0.00007 fc_module 903216 0.00602 fc_1 536165 0.00357 fc_2 342643 0.00228 fc_3 24406 0.00016 * The clock frequency of the DL processor is: 150MHz offset_name offset_address allocated_space _______________________ ______________ _________________ "InputDataOffset" "0x00000000" "48.0 MB" "OutputResultOffset" "0x03000000" "4.0 MB" "SystemBufferOffset" "0x03400000" "60.0 MB" "InstructionDataOffset" "0x07000000" "8.0 MB" "ConvWeightDataOffset" "0x07800000" "8.0 MB" "FCWeightDataOffset" "0x08000000" "12.0 MB" "EndOffset" "0x08c00000" "Total: 140.0 MB" ### FPGA bitstream programming has been skipped as the same bitstream is already loaded on the target FPGA. ### Deep learning network programming has been skipped as the same network is already loaded on the target FPGA. ### Finished writing input activations. ### Running single input activations. Deep Learning Processor Profiler Performance Results LastLayerLatency(cycles) LastLayerLatency(seconds) FramesNum Total Latency Frames/s ------------- ------------- --------- --------- --------- Network 13572329 0.09048 10 127265075 11.8 conv_module 12669135 0.08446 conv_1 3939559 0.02626 maxpool_1 1545378 0.01030 conv_2 2911243 0.01941 maxpool_2 577422 0.00385 conv_3 2552064 0.01701 maxpool_3 676678 0.00451 conv_4 455657 0.00304 maxpool_4 11227 0.00007 fc_module 903194 0.00602 fc_1 536140 0.00357 fc_2 342688 0.00228 fc_3 24364 0.00016 * The clock frequency of the DL processor is: 150MHz ### Finished writing input activations. ### Running single input activations. Deep Learning Processor Profiler Performance Results LastLayerLatency(cycles) LastLayerLatency(seconds) FramesNum Total Latency Frames/s ------------- ------------- --------- --------- --------- Network 13572527 0.09048 10 127266427 11.8 conv_module 12669266 0.08446 conv_1 3939776 0.02627 maxpool_1 1545632 0.01030 conv_2 2911169 0.01941 maxpool_2 577592 0.00385 conv_3 2551613 0.01701 maxpool_3 676811 0.00451 conv_4 455418 0.00304 maxpool_4 11348 0.00008 fc_module 903261 0.00602 fc_1 536205 0.00357 fc_2 342689 0.00228 fc_3 24365 0.00016 * The clock frequency of the DL processor is: 150MHz

检查MetricResults。结果字段的验证输出以查看量化网络的性能。

validateOut=预测.MetricResults.Result
ans = NetworkImplementation MetricOutput  _____________________ ____________ {' 浮点}0.9875 0.9875{“量子化”}

检查量化网络FPS字段的验证输出,以查看量化网络的每秒帧性能。

预测。量化网络fps
ans = 11.8126

中指定的网络卷积层的权重、偏差和激活数字量化器对象现在使用缩放的8位整数数据类型。

输入参数

全部折叠

数字量化器对象包含要量化的网络。

用于量化网络校准的数据,指定为imageDatastore对象,一个augmentedImageDatastore对象,或像素标签图像数据存储(计算机视觉工具箱)对象。

名称-值对的观点

指定可选的逗号分隔的对名称,值参数。名称参数名和价值为对应值。名称必须出现在引号内。可以以任意顺序指定多个名称和值对参数Name1, Value1,…,的家

例子:calResults = calibrate(quantObj, calData, 'UseGPU', 'on')
FPGA执行环境选项

全部折叠

此属性仅影响FPGA目标。

使用GPU进行校准时的逻辑标志数字量化器对象ExecutionEnvironment设置为FPGA。

例子:“使用GPU”,“打开”

输出参数

全部折叠

网络层的动态范围,返回一个表。表格中的每一行显示了优化后的网络卷积层的一个可学习参数的最小值和最大值。该软件使用这些最小值和最大值来确定量化参数的数据类型的比例。

介绍了R2020a