%% Add Parameters to Imported ONNX Model Function
% Import a network saved in the ONNX format as a function and modify the network parameters.
% Create an ONNX model from the pretrained AlexNet network. Then import alexnet.onnx as a function. Import the pretrained ONNX network using importONNXFunction, which returns an ONNXParamaters object that contains the network parameters. The function also creates a new model function in the current folder that contains the network architecture. Specify the name of the model function as alexnetFcn.

net = alexnet;
exportONNXNetwork(net,'alexnet.onnx');
params = importONNXFunction('alexnet.onnx','alexnetFcn');

% Display the parameters that are updated during training (params.Learnables) and the parameters that remain unchanged during training (params.Nonlearnables).

params.Learnables
params.Nonlearnables

%
% The network has parameters that represent three fully connected layers. You can add a fully connected layer in the original parameters params between layers fc7 and fc8. The new layer might increase the classification accuracy.
% Name the new layer fc9, because each added parameter name must be unique. The addParameter function always adds a new parameter sequentially to the params.Learnables or params.Nonlearnables structure. The order of the layers in the model function alexnetFcn determines the order in which the network layers are executed. The order or the names of the parameters do not influence the execution order.
% Add a new fully connected layer fc9 with the same parameters as fc7.

params = addParameter(params,'fc9_W',params.Learnables.fc7_W,'Learnable');
params = addParameter(params,'fc9_B',params.Learnables.fc7_B,'Learnable');
params = addParameter(params,'fc9_Stride',params.Nonlearnables.fc7_Stride,'Nonlearnable');
params = addParameter(params,'fc9_DilationFactor',params.Nonlearnables.fc7_DilationFactor,'Nonlearnable');
params = addParameter(params,'fc9_Padding',params.Nonlearnables.fc7_Padding,'Nonlearnable');

% Display the updated learnable and nonlearnable parameters.

params.Learnables
params.Nonlearnables

% Modify the architecture of the model function to reflect the changes in params so you can use the network for prediction with the new parameters or retrain the network. Open the model function by using open alexnetFcn and add the fully connected layer fc9 between layers fc7 and fc8.
% Copyright 2020 The MathWorks, Inc.