主要内容

dlquantizationOptions.

用于量化训练有素的深神经网络的选项

描述

dlquantizationOptions.对象提供了用于量化培训的深神经网络以缩放8位整数数据类型的选项。使用dlquantizationOptions.对象以定义要使用的度量功能,该功能比较量化之前和之后网络的准确性。

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

创建

描述

Quantopts.= dlquantizationOptions.创造一个dlquantizationOptions.对象具有默认属性值。

Quantopts.= dlQuantizationOptions(名称,值)创造一个dlquantizationOptions.具有附加属性的对象指定为名称,价值对论点。

属性

展开全部

功能阵列函数处理指定用于计算量化网络的验证度量的功能。

例:选项= dlquantizationOptions('metricfcn',{@(x)hcomputemodelagcuracy(x,net,tounttruth)});

数据类型:细胞

FPGA执行环境选项

此属性仅影响FPGA目标。

指定为字符向量的FPGA比特流的名称。

例:'比特流''zcu102_int8'

此属性仅影响FPGA目标。

的名字dlhdl.target.具有板名称和电路板接口信息的对象。

例:'目标'H T

例子

全部收缩

此示例显示如何在神经网络的卷积层中量化可学习参数,并探索量化网络的行为。在此示例中,您量化了挤压培训网络后神经网络根据培训深度学习网络以分类新图像例。在该示例中,通过量化不受量化,网络所需的存储器减少了大约75%的量化。

加载佩带的网络。

net =具有属性的dagnetwork:图层:[68x1 nnet.cnn.layer.layer]连接:[75x2表] InputNames:{'data'} OutputNames:{'new_classoutput'}

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

校准数据用于在卷积和网络的完全连接层中收集权重和偏置的动态范围以及网络中所有层中的激活的动态范围。为了最佳量化结果,校准数据必须代表网络的输入。

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

在此示例中,使用图像中的图像merchdata.数据集。定义A.AugmentedimageGedataStore.对象调整网络的数据大小。然后,将数据拆分为校准和验证数据集。

解压缩('merchdata.zip');imds = imageageatastore('merchdata'......'insertumbfolders',真正,......'labelsource''foldernames');[Caldata,Valdata] = SpliteachLabel(IMDS,0.7,'随机');aug_caldata = augmentedimagedataStore([227 227],Caldata);aug_valdata = augmentedimagedataStore([227 227],valdata);

创建一个dlquantizer对象并指定网络以量化。

Quantobj = DlQuantizer(网);

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

功能准确性= hcomputemodelaguracy(预测电机,网络,数据存储)%%计算模型级精度统计信息%负荷地面真相tmp = readall(数据存储);地面= tmp.response;%与预测标签与实际的原始真相相比predictionerror = {};对于idx = 1:numel(tounttruth)[〜,idy] = max(预测icores(idx,:));YACTUAL = NET.LAYERS(END).CLASS(IDY);predictionerror {end + 1} =(YACTUAN == Tounttruth(IDX));%#好结束%总和所有预测错误。predictionerror = [predictionerror {:}];精度= sum(predictionerror)/ numel(predictionError);结束

指定A中的度量标准函数dlquantizationOptions.目的。

quantopts = dlquantizationoptions('metricfcn'......{@(x)hcomputemodelagcuracy(x,net,aug_valdata)});

使用校准使用示例输入锻炼网络并收集范围信息的功能。该校准功能练习网络,并在网络的卷积和完全连接的层中收集权重和偏置的动态范围,以及网络的所有层中激活的动态范围。该函数返回一个表。表中的每一行包含用于优化网络的学习参数的范围信息。

Calresults = Calibrate(Quantobj,Aug_Caldata)
calResults = 95x5表优化的层名称网络层名称Learnables /的激活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'}“bias”-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-Advance3x3_Fire2-Relu_expand3x3_bias'} {'Fire2-Relu_expand3x3'}“偏见”-0.051778 0.074239 {'Fire3-Squeeze1x1_Fire3-Relu_squeeze1x1_Weights '} {' FIRE3-relu_squeeze1x1 '} “权重” -0.77263 0.68897 {' FIRE3-squeeze1x1_fire3-relu_squeeze1x1_Bias'} { 'FIRE3-relu_squeeze1x1'} “斜交” -0.10141 0.32678 { 'FIRE3-expand1x1_fire3-relu_expand1x1_Weights'} {'FIRE3-relu_expand1x1'}“权重”-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'}“权重“-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'}”权重“-0.74145 1.0888 {'fire4-squeeze1x1_fire4-relu_squeze1x1_bias'} {'fire4-relu_squeze1x1'}“偏见”-0.10886 0.13882 ...

使用验证函数来量化网络卷积层中的学习参数并锻炼网络。该函数使用定义的度量函数dlquantizationOptions.对象可以在量化之前和之后比较网络的结果。

Valresults =验证(Quantobj,Aug_Valdata,Quantopts)
Valresults =带有字段的结构:NumSamples:20个Metricresults:[1x1 struct]

检查metricresults.result.验证输出的字段,以查看量化网络的性能。

Valresults.Metricresults.result.
ans = 2x3表网络自动阶段测量recarablememory(字节)______________________________________________________________________________________________________________________________________________________________________________________________________________________________________________函数'} 1 7.3393e + 05

在该示例中,通过量化,网络所需的存储器减少了大约75%。网络的准确性不受影响。

在中指定的网络的卷积层的权重,偏置和激活dlquantizer对象现在使用缩放的8位整数数据类型。

此示例显示如何在神经网络的卷积层中量化可学习参数,并探索量化网络的行为。在此示例中,您量化了记录神经网络。量化有助于通过量化权重,偏置和网络层的激活来降低深神经网络的存储器要求,以8位缩放整数数据类型。使用MATLAB®从目标设备检索预测结果。

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

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

在您当前的工作目录中创建一个文件getlogonetwork.m.。将这些行输入到文件中:

功能net = getlogonetwork()data = getLogodata();net = data.convnet;结束功能data = getlogodata()如果〜ISFILE('logonet.mat')URL =.'//www.tianjin-qmedu.com/万博1manbetxsupportfiles/gpucoder/cnn_models/logo_detection/logonet.mat';WebSave('logonet.mat',URL);结束data = load('logonet.mat');结束

加载佩带的网络。

snet = getLogonetwork();
SNET =具有属性的系列网络:图层:[22×1 nnet.cnn.layer.layer] InputNames:{'ImageInput'} OutputNames:{'ClassOutput'}

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

校准数据用于在卷积和网络的完全连接层中收集权重和偏置的动态范围以及网络中所有层中的激活的动态范围。为了最佳量化结果,校准数据必须代表网络的输入。

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

此示例使用图像中的图像logos_dataset.数据集。定义A.AugmentedimageGedataStore.对象调整网络的数据大小。然后,将数据拆分为校准和验证数据集。

Curdir = PWD;newdir = fullfile(matlabroot,'例子''deeplearning_shared''数据''logos_dataset.zip');Copyfile(Newdir,Curdir);解压缩('logos_dataset.zip');imagedata = imagedataStore(fullfile(curdir,'logos_dataset'),......'insertumbfolders',真正,'fileextensions''.jpg''labelsource''foldernames');[CalibrationData,ValidationData] = SpliteachLabel(Imagedata,0.5,'随机');

创建一个dlquantizer对象并指定网络以量化。

dlquantobj = dlquantizer(snet,'executionenvironment''FPGA');

使用校准使用示例输入锻炼网络并收集范围信息的功能。该校准功能练习网络,并在网络的卷积和完全连接的层中收集权重和偏置的动态范围,以及网络的所有层中激活的动态范围。该函数返回一个表。表中的每一行包含用于优化网络的学习参数的范围信息。

dlquantobj.calibrate(CalibrationData)
ANS =优化层名网络层名称Learnables /的激活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.055518 0.061901 {'conv_2_bias'} {'conv_2'} {'conv_2'}“偏见”-0.00061171 0.00227 {'conv_3_weights'} {'conv_3'}} {'conv_3'}} {'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'} {'conv_4'}} {'conv_4'}“bias”-0.00164 0.0037892 {'fc_1_weights'}{'fc_1'}“权重”-0.051394 0.054344 {'fc_1_bias'} {'fc_1'} {'fc_1'} {'fc_1'}“偏见”-0.00052319 0.00084454 {'fc_2_weights'} {'fc_2_weights'} {'fc_2_weights'} {fc_2'}“权重”-0.05016 0.051557 {'fc_2_bias'} {'fc_2'}“偏见”-0.0017564 0.0018502 {'fc_3_weights'} {fc_3'}} {'fc_3'}} {'fc_3'}“权重”-0.050706 0.04678 {'fc_3_bias'} {fc_3_bias'} {fc_3'} {'fc_3'} {'fc_3'} {'fc_3'}“偏见”-0.02951 0.024855 {'imageInput'} {'ImageInput'}“激活”0 255 {'imageInput_normalization'} {'imageInput_normalization'} {'imageInput'}“激活”-139.34 198.72

使用自定义名称创建一个目标对象,为目标设备和连接目标设备到主计算机的接口。接口选项是JTAG和以太网。要创建目标对象,请输入:

htarget = dlhdl.target('英特尔''接口''JTAG');

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

功能精度= hcomplueAccuracy(预测码,网,数据存储)%% hcompluteacuracy测试辅助功能计算模型级别精度统计%Copyright 2020 Mathworks,Inc。%负荷地面真相地面= datastore.labels;%与预测标签与实际的原始真相相比predictionerror = {};对于idx = 1:numel(tounttruth)[〜,idy] = max(预测icores(idx,:));YACTUAL = NET.LAYERS(END).CLASS(IDY);predictionerror {end + 1} =(YACTUAN == Tounttruth(IDX));%#好结束%总和所有预测错误。predictionerror = [predictionerror {:}];精度= sum(predictionerror)/ numel(predictionError);结束

指定A中的度量标准函数dlquantizationOptions.目的。

选项= dlQuantizationOptions('metricfcn'......{@(x)hcomputemodelagcuracy(x,snet,validationdata)},'比特流''Arria10soc_int8'......'目标',htarget);

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

预测= dlquantobj.validate(validationData,选项);
offset_name offset_address allocated_space __________________________________________________________________________________________________________________________00000000“”0x03000000“”4.0 MB“”systembufferoffset“”0x03400000“”60.0 mb“”nightsdataOffset“”0x07000000“”8.0 MB“”concreweightdataOffsoffs“”0x07800000“8.0 mb”“fcweightdataOffset”“0x08000000”“12.0 MB”“0x08C00000”“0x08C00000”“总数:140.0 MB”###编程FPGA比特流使用JTAG ... ###编程FPGA比特流已成功完成。###加载权重到CONC处理器。###加载Conr权重。当前时间是16-jul-2020 12:45:10 ###加载FC处理器的权重。### FC重量加载。当前时间是16-jul-2020 12:45:26 ###完成写入输入激活。###正在运行单个输入激活。深度学习处理器分析器性能结果LastLayerLatency(周期)LastLayerLatency(秒)FramesNum总延迟帧/ s ----------------------------------------- ----------网络13570959 0.09047 30 380609145 11.8 0.09045 MAXPOOL_1 0.09045 30 380609145 0.09045 MAXPOOL_11544560 0.01030 0.01030 0.01030 0.01030303030303030303030303030303030303030303030303030303030707Conv_4 455434 0.00304 MAXPOOL_4 11251 0.00008 FC_MODULE 903173 0.00602 FC_1 536164 0.00357 FC_2 342643 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.result.验证输出的字段,以查看量化网络的性能。

validateout = predition.metricresults.result.
ANS = Networkimplementation Metricutput _____________________ ____________ {'浮点'} 0.9875 {'量化'} 0.9875

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

预测.QuantizedNetWorkfps.
ans = 11.8126.

在中指定的网络的卷积层的权重,偏置和激活dlquantizer对象现在使用缩放的8位整数数据类型。

在R2020A中介​​绍