%% Import ONNX Network with Multiple Outputs
% Import an ONNX network that has multiple outputs by using importONNXLayers. The function inserts placeholder layers for the outputs. After importing, you can find and replace the placeholder layers by using findPlaceholderLayers and replaceLayer, respectively.
% Specify the network file from which to import layers and weights.

modelfile = 'digitsMIMO.onnx';

% The network in digitsMIMO.onnx has two output layers: one classification layer to classify digits and one regression layer to compute the mean squared error for the predicted angles of the digits. Import the layers and weights from modelfile.

layers = importONNXLayers('digitsMIMO.onnx','ImportWeights',true)

% importONNXLayers displays a warning and inserts placeholder layers for the output layers.
% Plot the layer graph using plot.

plot(layers)

% The layer graph has two output layers: Output_fc_1_Flatten and Output_sm_1. These two layers are the placeholders for the outputs. You can check the placeholder layers by viewing the Layers property or by using the findPlaceholderLayers function.

layers.Layers
placeholderLayers = findPlaceholderLayers(layers)

% Create output layers to replace the placeholder layers. First, create a classification layer with the name Output_sm_1. Specify the classes of the output layer as 0, 1, ..., 9. If you do not specify the classes, then the software automatically sets them to 1, 2, ..., N, where N is the number of classes.

output1 = classificationLayer('Name','Output_sm_1','Classes',string(0:9)); 

% Create a regression layer with the name Output_fc_1_Flatten.

output2 = regressionLayer('Name','Output_fc_1_Flatten'); 

% Replace the placeholder layers with output1 and output2 using replaceLayer.

layers = replaceLayer(layers,'Output_sm_1',output1);
layers = replaceLayer(layers,'Output_fc_1_Flatten',output2);

% Display the Layers property of the layer graph to confirm the replacement.

layers.Layers

% Assemble the layer graph using assembleNetwork. The function returns a DAGNetwork object that is ready to use for prediction.

assembledNet = assembleNetwork(layers)

% Copyright 2019 The MathWorks, Inc.