%% Deep Learning Prediction with ARM Compute Using |cnncodegen|
% 
% This example shows how to use |cnncodegen| to generate code for a Logo 
% classification application that uses deep learning on ARM(R) processors.
% The logo classification application uses the |LogoNet| series network to
% perform logo recognition from images. The generated code takes advantage 
% of the ARM Compute library for computer vision and machine learning.
%
% Copyright 2018 The MathWorks, Inc.
%% Prerequisites
% * ARM processor that supports the NEON extension
% * Open Source Computer Vision Library (OpenCV) v3.1
% * Environment variables for ARM Compute and OpenCV libraries 
% * MATLAB(R) Coder(TM) for C++ code generation
% * The support package MATLAB Coder Interface for Deep Learning
% * Deep Learning Toolbox(TM) for using the |SeriesNetwork| object
%
% For more information, 
% see <docid:coder_ug#mw_99a4acec-aced-453c-aadd-a18e83133574>.
%
% This example is supported on Linux(R) and Windows(R) platforms and not
% supported for MATLAB Online. 
%% Get the Pretrained SeriesNetwork
% Download the pretrained |LogoNet| network and save it as |logonet.mat|,
% if it does not exist. The network was developed in MATLAB(R) and
% its architecture is similar to that of AlexNet. This network can 
% recognize 32 logos under various lighting conditions and camera
% angles.
net = getLogonet();

%%
% The network contains 22 layers including convolution, fully connected,
% and the classification output layers.
net.Layers
%% Set Environment Variables 
% On the ARM target hardware, make sure that ARM_COMPUTELIB is set and that
% LD_LIBRARY_PATH contains the path to the ARM Compute Library folder.
%
% See <docid:coder_ug#mw_99a4acec-aced-453c-aadd-a18e83133574>. 
% 
%% Generate Code 
% For deep learning on ARM targets, you generate code on
% the host development computer. Then, you move the generated code to the ARM platform  
% where you build and run the executable program. The
% target platform must support the Neon instruction set architecture (ISA).
% Raspberry Pi3, Firefly, HiKey are some of the target platforms on which
% the generated code can be executed. 
%
% Call |cnncodegen|, specifying that the target library is the ARM Compute Library.
% Specify the version of the library and the architecture of the target ARM processor. 
% The |ARMArchitecture| parameter is required.
%
%   cnncodegen(net,'targetlib','arm-compute','targetparams', struct('ArmComputeVersion','19.05','ARMArchitecture','armv8'));
%
%% Copy the Generated Files to the Target
% Move the codegen folder and other required files from the host
% development computer to the target platform by using your preferred 
% |scp/ssh| client.
%
% For example on the Linux platform, to transfer the files to the 
% Raspberry Pi, use the scp command with the format:
%
% |system('sshpass -p [password] scp (sourcefile) [username]@[targetname]:~/');|
%
%   system('sshpass -p password scp main_arm_logo.cpp username@targetname:~/');
%   system('sshpass -p password scp coderdemo_google.png username@targetname:~/');
%   system('sshpass -p password scp makefile_arm_logo.mk username@targetname:~/');
%   system('sshpass -p password scp synsetWordsLogoDet.txt username@targetname:~/');
%   system('sshpass -p password scp -r codegen username@targetname:~/');
%
% On the Windows platform, you can use the |pscp| tool that comes with
% a PuTTY installation. For example,
%
%   system('pscp -pw password -r codegen username@targetname:/home/username');
%
% Note: PSCP utilities must be either on your PATH or in your current
% directory.

%% Build and Execute
% To build the library on the target platform, use the generated makefile 
% |cnnbuild_rtw.mk|.
%
% For example, to build the library on the Raspberry Pi from the Linux
% platform:
%
%   system('sshpass -p password ssh username@targetname "make -C /home/username/codegen -f cnnbuild_rtw.mk"');
% 
% On the Windows platform, you can use the |putty| command with |-ssh|
% argument to log in and run the make command. For example:
%
%   system('putty -ssh username@targetname -pw password');                  
%
%%
% To build and run the executable on the target platform, use the command with the
% format:
% |make -C /home/$(username)| and |./execfile -f makefile_arm_logo.mk|
%
% For example, on the Raspberry Pi:
%
%   make -C /home/pi arm_neon -f makefile_arm_logo.mk
%
%%
% Run the executable with an input image file.
%
%
%   ./logo_recognition_exe coderdemo_google.png
%%
% The top five predictions for the input image file are:
%
% <<../coderdemo_arm_neon_output.png>>
%
% <<../coderdemo_google.png>>