%% Custom Deep Learning Processor Generation to Meet Performance Requirements
% This example shows how to create a custom processor configuration and estimate the performance of a pretrained series network. You can then modify parameters of the custom processor configuration and re-estimate the performance. Once you have achieved your performance requirements you can generate a custom bitstream by using the custom processor configuration.
%
%% Prerequisites
%  Deep Learning HDL Toolbox™Support Package for Xilinx FPGA and SoC
%  Deep Learning Toolbox™
%  Deep Learning HDL Toolbox™
% Deep Learning Toolbox Model Quantization Library
% MATLAB Coder Interface for Deep Learning Libraries
%
%% Load Pretrained Series Network
% To load the pretrained series network LogoNet, enter:

snet = getLogoNetwork;

%% Create Custom Processor Configuration
% To create a custom processor configuration, use the dlhdl.ProcessorConfig object. For more information, see dlhdl.ProcessorConfig. To learn about modifiable parameters of the processor configuration, see getModuleProperty and setModuleProperty.

hPC = dlhdl.ProcessorConfig;
hPC.TargetFrequency = 220;
hPC

%
%% Estimate LogoNet Performance
% To estimate the performance of the LogoNet series network, use the estimatePerformance function of the dlhdl.ProcessorConfig object. The function returns the estimated layer latency, network latency, and network performance in frames per second (Frames/s).

hPC.estimatePerformance(snet)

% The estimated frames per second is 5.5 Frames/s. To improve the network performance, modify the custom processor convolution module kernel data type, convolution processor thread number, fully connected module kernel data type, and fully connected module thread number. For more information about these processor parameters, see getModuleProperty and setModuleProperty.
%
%% Create Modified Custom Processor Configuration
% To create a custom processor configuration, use the dlhdl.ProcessorConfig object. For more information, see dlhdl.ProcessorConfig. To learn about modifiable parameters of the processor configuration, see getModuleProperty and setModuleProperty.

hPCNew = dlhdl.ProcessorConfig;
hPCNew.TargetFrequency = 300;
hPCNew.ProcessorDataType = 'int8';
hPCNew.setModuleProperty('conv', 'ConvThreadNumber', 64);
hPCNew.setModuleProperty('fc', 'FCThreadNumber',   16);
hPCNew

%
%
%% Quantize LogoNet Series Network
%  To quantize the LogoNet network, enter:

dlquantObj = dlquantizer(snet,'ExecutionEnvironment','FPGA');
Image = imageDatastore('heineken.png','Labels','Heineken');
dlquantObj.calibrate(Image);

%
%% Estimate LogoNet Performance
% To estimate the performance of the LogoNet series network, use the estimatePerformance function of the dlhdl.ProcessorConfig object. The function returns the estimated layer latency, network latency, and network performance in frames per second (Frames/s).

hPCNew.estimatePerformance(dlquantObj)

%
% The estimated frames per second is 21.2 Frames/s.
%
%% Generate Custom Processor and Bitstream
% Use the new custom processor configuration to build and generate a custom processor and bitstream. Use the custom bitstream to deploy the LogoNet network to your target FPGA board.

% hdlsetuptoolpath('ToolName', 'Xilinx Vivado', 'ToolPath', 'C:\Xilinx\Vivado\2019.2\bin\vivado.bat');
% dlhdl.buildProcessor(hPCNew);

% To learn how to use the generated bitstream file, see Generate Custom Bitstream.
% The generated bitstream in this example is similar to the zcu102_int8 bitstream. To deploy the quantized LogoNet network using the zcu102_int8 bitstream, see Obtain Prediction results for Quantized LogoNet Network.
% Copyright 2020 The MathWorks, Inc.
%