%% Running Convolution-Only Networks by Using FPGA Deployment
%
% Typical series classification networks include a sequence of  convolution layers followed by one or more fully connected layers.  Recent research results  indicate that better performance is achieved for feature extraction and recognition by using the convolution layer activations directly, instead of those from the subsequent fully connected layers.
% To understand and debug convolutional networks, running and visualizing data is a useful tool. This example shows how to deploy, run, and debug a convolution-only network by using FPGA deployment..
%% Prerequisites
% Xilinx™ Zynq™ ZCU102 Evaluation Kit
% Deep Learning HDL Toolbox™ Support Package for Xilinx FPGA and SoC
% Deep Learning Toolbox™
% Deep Learning HDL Toolbox™
% Deep Learning Toolbox™ Model for Resnet-50 Network
%% Resnet-50 Network
% ResNet-50 is a convolutional neural network that is 50 layers deep. This pretrained network can classify images into 1000 object categories (such as keyboard, mouse, pencil, and more).The network has learned rich feature representations for a wide range of images. The network has an image input size of 224-by-224. This example uses ResNet50 as a starting point.
%
%% Load Resnet-50 Network
% Load the ResNet-50 network.

rnet = resnet50;

% To visualize the structure of the Resnet-50 network, at the MATLAB® command prompt, enter:

analyzeNetwork(rnet)

%
%% Create A Convolution Only Network
% A convolution only network is created by selecting a subset of the ResNet-50 network. The subset includes only the first five layers of the ResNet50 network which are convolutional in nature.
% To create the convolution only network, enter:

layers = rnet.Layers(1:5);
outLayer = regressionLayer('Name','output');
layers(end+1) = outLayer;

snet = assembleNetwork(layers);

%
%% Create Target Object
% To deploy the network on an FPGA, create a target object with a custom name and an interface to connect your target device to the host computer. Interface options are JTAG and Ethernet. To use JTAG, install Xilinx™ Vivado™ Design Suite 2019.2. To set the Xilinx Vivado toolpath, enter:

%hdlsetuptoolpath('ToolName', 'Xilinx Vivado', 'ToolPath', 'D:/share/apps/HDLTools/Vivado/2019.2-mw-0/Win/Vivado/2019.2\bin\vivado.bat');

%

hTarget = dlhdl.Target('Xilinx','Interface','Ethernet');

%
%% 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 convolutional only 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 Xilinx ZCU102 SOC board. The bitstream uses a single data type. Use the dlhdl.Workflow object to deploy networks which include both convolution and fully connected layers or only convolution layers.

hW = dlhdl.Workflow('Network', snet, 'Bitstream', 'zcu102_single','Target',hTarget);

%
%% Compile Convolution Only Network
% To compile the convolution only network, run the compile function of the dlhdl.Workflow object.
% hW.compile

dn = hW.compile

%
%% Program Bitstream onto FPGA and Download Network Weights
% To deploy the network on the Xilinx ZCU102 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. The function also downloads the network weights and biases. The deploy function programs the FPGA device, displays progress messages, and the time it takes to deploy the network.

hW.deploy

%
%% Load Example Image
% Load and display an image to use as an input image to the series network.

I = imread('daisy.jpg');
imshow(I)

%
%% Run the Prediction
% Execute the predict function of the dlhdl.Workflow object.

[P, speed] = hW.predict(single(I),'Profile','on');

% The result data is returned as a 3-D array, with the third dimension indexing across the 64 feature images.

sz = size(P)

% To visualize all 64 features in a single image, the data is reshaped into four dimensions, which is appropriate input to the imtile function

R = reshape(P, [sz(1) sz(2) 1 sz(3)]);
sz = size(R)

% The third dimension in the input to imtile function represents the image color. Set the third dimension to size 1 because the activation signals in this example are scalars and do not include color. The fourth dimension indexes the channel.
% The input to imtile is normalized using mat2gray. All values are scaled so that the minimum activation is 0 and the maximum activation is 1.

J = imtile(mat2gray(R), 'GridSize', [8 8]);

%  A grid size of 8-by-8 is selected because there are 64 features to display.

imshow(J)

% The image shows activation data for each of the 64 features. Bright features indicate a strong activation.
% The output from the convolutional layers only network differs from that of a network with convolution and fully connected layers. Convolution layers are used to reduce the input image size while maintaining features which are needed to get a good prediction. Convolution only layer networks are used to study feature extraction. Earlier convolution layers are used to extract low level features such as edges, colors, gradients and so on. Later convolution layers are used to extract high level features such as patterns, curves, lines and so on. These high level features can then be used to identify objects.
%
% Copyright 2020 The MathWorks, Inc.