%% Get Started with Deep Learning FPGA Deployment on Intel Arria 10 SoC
% This example shows how to create, compile, and deploy a dlhdl.Workflow object that has a handwritten character detection series network object by using the Deep Learning HDL Toolbox™ Support Package for Intel FPGA and SoC. Use MATLAB® to retrieve the prediction results from the target device.
%% Prerequisites
% Intel Arria™ 10 SoC development kit
% Deep Learning HDL Toolbox™ Support Package for Intel FPGA and SoC
% Deep Learning HDL Toolbox™
% Deep Learning Toolbox™
%% Create a Folder and Copy Relevant Files
% Create a new folder in your current working folder where you have write permission and copy all the files into this folder.

unzip('dnnfpga_digits.zip');
[newDir, origDir] = cloneSetupDir('dnnfpga_digits');
cd(newDir);

%
%% Load the Pretrained SeriesNetwork
% To load the pretrained series network, that has been trained on the Modified National Institue Standards of Technolofy (MNIST) database, enter:

snet = getDigitsNetwork();

%
% To view the layers of the pretrained series network, enter:

analyzeNetwork(snet)

%
%% Create Target Object
% Create a target object that has a custom name for your target device and an interface to connect your target device to the host computer. Interface options are JTAG and Ethernet. To use JTAG, install Intel™ Quartus™ Prime Standard Edition 18.1. Set up the path to your installed Intel Quartus Prime executable if it is not already set up. For example, to set the toolpath, enter:

% hdlsetuptoolpath('ToolName', 'Altera Quartus II','ToolPath', 'C:\altera\18.1\quartus\bin64');

%

hTarget = dlhdl.Target('Intel')

%
%% Create Workflow Object
% Create an object of the dlhdl.Workflow class. When you create the object, specify the network and the bitstream name. Specify the saved pretrained MNIST neural network, snet, as the network. Make sure that the bitstream name matches the data type and the FPGA board that you are targeting. In this example, the target FPGA board is the Intel Arria 10 SOC board and the bitstream uses a single data type.

hW = dlhdl.Workflow('network', snet, 'Bitstream', 'arria10soc_single','Target',hTarget)

%
%% Compile the MNIST Series Network
% To compile the MNIST series network, run the compile function of the dlhdl.Workflow object.

dn = hW.compile;

%
%% Program Bitstream onto FPGA and Download Network Weights
% To deploy the network on the Intel Arria 10 SoC hardware, run the deploy function of the dlhdl.Workflow object. This function uses the output of the compile function to program the FPGA board by using the programming file. It also downloads the network weights and biases. The deploy function starts programming the FPGA device, displays progress messages, and the time it takes to deploy the network.

hW.deploy

%
%% Run Prediction for Example Image
% To load the example image, execute the predict function of the dlhdl.Workflow object, and then display the FPGA result, enter:

inputImg = imread('five_28x28.pgm');
imshow(inputImg);

%
% Run prediction with the profile 'on' to see the latency and throughput results.

[prediction, speed] = hW.predict(single(inputImg),'Profile','on');
[val, idx] = max(prediction);
fprintf('The prediction result is %d\n', idx-1);
cd(origDir);

%
% Copyright 2020 The MathWorks, Inc.