量化训练的深度神经网络的选项
的dlquantizationOptions
对象提供了用于量化培训的深神经网络以缩放8位整数数据类型的选项。使用dlquantizationOptions
对象定义用于比较量化前后网络精度的度量函数。
要了解量化深度神经网络所需的产品,请参下载188bet金宝搏阅量化工作流先决条件.
创建一个Quantopts.
= dlquantizationOptions.dlquantizationOptions
具有默认属性值的对象。
创建一个Quantopts.
= dlquantizationOptions(名称,值)dlquantizationOptions
对象,其附加属性指定为名称,值
对论点。
MetricFcn
- - - - - -用于计算验证度量的函数函数句柄的单元格数组,指定用于计算量化网络的验证度量的函数。
例子:选项= dlquantizationOptions('metricfcn',{@(x)hcomputemodelagcuracy(x,net,tounttruth)});
数据类型:细胞
比特流
- - - - - -比特流的名字此属性仅影响FPGA目标。
指定为字符向量的FPGA比特流的名称。
例子:“比特流”
,'zcu102_int8'
目标
- - - - - -的名称dlhdl。目标
对象此属性仅影响FPGA目标。
的名称dlhdl。目标
具有板名称和电路板接口信息的对象。
例子:“目标”
,hT
这个例子展示了如何量化神经网络的卷积层中的可学习参数,并探讨了量化网络的行为。在这个例子中,你量化squeezenet
神经网络经过再训练后,对新的图像进行分类培训深度学习网络以分类新图像例子。在该示例中,通过量化不受量化,网络所需的存储器减少了大约75%的量化。
加载预先训练的网络。网
.网
输出网络是培训深度学习网络以分类新图像例子。
网
net =具有属性的dagnetwork:图层:[68x1 nnet.cnn.layer.layer]连接:[75x2表] InputNames:{'data'} OutputNames:{'new_classoutput'}
定义用于量化的校准和验证数据。
标定数据用于收集网络卷积层和全连接层中权值和偏差的动态范围,以及网络各层中激活的动态范围。为了获得最好的量化结果,校准数据必须能代表网络的输入。
利用验证数据对量化后的网络进行测试,了解量化后的卷积层的有限范围和精度对网络的影响。
在本例中,使用MerchData
数据集。定义一个augmentedImageDatastore
对象调整网络的数据大小。然后,将数据拆分为校准和验证数据集。
解压缩('merchdata.zip');imds = imageageatastore(“MerchData”,...'insertumbfolders',真的,...'labelsource',“foldernames”);[calData, valData] = splitachlabel (imds, 0.7,'随机');aug_calData = augmentedImageDatastore([227 227], calData);aug_valData = augmentedImageDatastore([227 227], valData);
创建一个dlquantizer
对象,并指定要量化的网络。
quantObj = dlquantizer(净);
定义一个度量函数,用来比较网络量化前后的行为。将此函数保存在本地文件中。
功能hComputeModelAccuracy = hComputeModelAccuracy(predictionScores, net, dataStore)%%计算模型级精度统计负载地面真实值tmp = readall(数据存储);groundTruth = tmp.response;%与预测标签与实际的原始真相相比predictionerror = {};为idx = 1:numel(tounttruth)[〜,idy] = max(预测icores(idx,:));yActual = net.Layers(结束). class(补贴);predictionError{end+1} = (yActual == groundTruth(idx));% #好吧结束%总和所有预测错误。predictionError = [predictionError {}):;精度= (predictionError) /元素个数之和(predictionError);结束
指定度量函数dlquantizationOptions
对象。
quantOpts = dlquantizationOptions ('metricfcn',...{@ (x) hComputeModelAccuracy (x,净,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
对象可以在量化之前和之后比较网络的结果。
validresults = validate(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%。不影响网络的准确性。
中规定的网络卷积层的权值、偏差和激活dlquantizer
对象现在使用缩放的8位整数数据类型。
这个例子展示了如何量化神经网络的卷积层中的可学习参数,并探讨了量化网络的行为。在这个例子中,你量化LogoNet
神经网络。量化通过将网络层的权重、偏差和激活量化为8位比例整数数据类型,有助于减少深度神经网络的内存需求。使用MATLAB®从目标设备检索预测结果。
要运行此示例,您需要列出的产品下载188bet金宝搏FPGA
在量化工作流先决条件.
有关其他要求,请参阅量化工作流先决条件.
在当前工作目录中创建一个名为getlogonetwork.m.
.在文件中输入以下几行:
功能net = getLogonetwork()data = getLogodata();net = data.convnet;结束功能data = getLogoData ()如果~ isfile (“LogoNet.mat”) url =“//www.tatmou.com/金宝appsupportfiles/gpucoder/cnn_models/logo_detection/LogoNet.mat”;websave (“LogoNet.mat”,URL);结束data =负载(“LogoNet.mat”);结束
加载预先训练的网络。
snet = getLogoNetwork ();
snet = SeriesNetwork with properties: Layers: [22×1 net.cnn.layer. layer] InputNames: {'imageinput'} OutputNames: {'classoutput'}
定义用于量化的校准和验证数据。
标定数据用于收集网络卷积层和全连接层中权值和偏差的动态范围,以及网络各层中激活的动态范围。为了获得最好的量化结果,校准数据必须能代表网络的输入。
利用验证数据对量化后的网络进行测试,了解量化后的卷积层的有限范围和精度对网络的影响。
此示例使用图像中的图像logos_dataset
数据集。定义一个augmentedImageDatastore
对象调整网络的数据大小。然后,将数据拆分为校准和验证数据集。
curDir = pwd;newDir = fullfile (matlabroot,'例子',“deeplearning_shared”,'数据','logos_dataset.zip');拷贝文件(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 =优化层名称网络层名称可学的/激活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。目标(“英特尔”,'界面','JTAG');
定义一个度量函数,用来比较网络量化前后的行为。将此函数保存在本地文件中。
功能hComputeModelAccuracy = hComputeModelAccuracy(predictionScores, net, dataStore)%% hcomputemodelagcuracy测试辅助功能计算模型级别精度统计版权所有2020 The MathWorks, Inc.负载地面真实值groundTruth = dataStore.Labels;%与预测标签与实际的原始真相相比predictionerror = {};为idx=1:numel(groundTruth) [~, idy] = max(predictionScores(idx,:)); / /将groundTruth改为groundTruthyActual = net.Layers(结束). class(补贴);predictionError{end+1} = (yActual == groundTruth(idx));% #好吧结束%总和所有预测错误。predictionError = [predictionError {}):;精度= (predictionError) /元素个数之和(predictionError);结束
指定度量函数dlquantizationOptions
对象。
选项= dlQuantizationOptions('metricfcn',...{@(x)hcomputemodelagcuracy(x,snet,validationdata)},“比特流”,“arria10soc_int8”,...“目标”,htarget);
要编译和部署量化网络,请运行验证
的函数dlquantizer
对象。使用验证
函数对网络的卷积层中的可学习参数进行量化,并对网络进行锻炼。此功能使用编译功能的Compile函数的输出来使用编程文件编程FPGA板。它还下载网络权重和偏见。Intel Quartus工具和支持的工具版本的部署功能检查。金宝app然后,它通过使用SOF文件开始编程FPGA设备,显示进度消息,以及部署网络所需的时间。中定义的度量函数dlquantizationOptions
对象可以在量化之前和之后比较网络的结果。
预测= dlQuantObj.validate (validationData选项);
offset_name offset_address allocated_space _______________________ ______________ _________________ “InputDataOffset”, “00000000”, “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” “总计:140.0 MB” ###使用JTAG编程FPGA比特流... ###编程FPGA比特流已成功完成.###加载权重于CONV处理器。###转化率重量加载。当前时间是16-JUL-2020十二时45分10秒###加载权重FC处理器。### FC重量加载。当前时间是16-JUL-2020 12点45分26秒###写完输入激活。###运行单个输入激活。深度学习处理器探查性能结果LastLayerLatency(循环)LastLayerLatency(秒)FramesNum的总延迟时间帧/秒------------- ------------- --------- --------- ---------网络13570959 0.09047 30 380609145 11.8 conv_module 12667786 0.08445 conv_1 3938907 0.02626 maxpool_1 1544560 0.01030 conv_2 2910954 0.01941 maxpool_2 577524 0.00385 conv_3 2552707 0.01702 maxpool_3 676542 0.00451conv_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。结果
验证输出的字段,以查看量化网络的性能。
validateOut = prediction.MetricResults.Result
ANS = Networkimplementation Metricutput _____________________ ____________ {'浮点'} 0.9875 {'量化'} 0.9875
检查QuantizedNetworkFPS
验证输出的字段,以查看尺寸化网络的每秒性能的帧。
预测。QuantizedNetworkFPS
ans = 11.8126.
中规定的网络卷积层的权值、偏差和激活dlquantizer
对象现在使用缩放的8位整数数据类型。
次のMATLABコマンドに対応するリンクがクリックされました。
コマンドをMATLABコマンドウィンドウに入力して実行してください。WebブラウザーはMATLABコマンドをサポートしていません。
您还可以从以下列表中选择一个网站:
选择中国网站(中文或英文)以获得最佳网站性能。其他MathWorks国家站点没有针对您所在位置的访问进行优化。