Implement Unsupported Deep Learning Layer Blocks
This example shows how to implement layers using Simulink blocks or MATLAB code in a MATLAB Function block.
The exportNetworkToSimulink
function converts deep learning layer objects in a dlnetwork
object to deep learning layer blocks in a Simulink model. For layer objects that do not have corresponding layer blocks, the function generates placeholder subsystems that contain an Assertion block. This example shows how to manually edit the model to remove the Assertion blocks and recreate the functionality of the input network.
You can implement layers by using Simulink blocks or MATLAB code in a MATLAB Function block. Some layers are unsupported because they are not relevant to Simulink and can be removed.
Import and Analyze Network in MATLAB
Import the simplenet
ONNX network to MATLAB as a dlnetwork
object.
net = importNetworkFromONNX("simplenet.onnx");
The network contains nine layer objects. Analyze the network to view the layers and connections in the network.
analyzeNetwork(net)
Warning: The size of the object is less than the new minimum size. The size will be adjusted to the new minimum size.
The network contains three layer objects that do not have corresponding layer blocks: imageinput_Sub
(an Elementwise Affine layer), softmax_Flatten
(an ONNX Flatten layer), and softmax1002Output
(a Custom output layer).
Export Network to Simulink
Export the network to a Simulink model.
exportNetworkToSimulink(net);
Warning: nnet.cnn.layer.ScalingLayer is not supported. A placeholder subsystem will be added for 'imageinput_Sub'.
Warning: nnet.onnx.layer.FlattenInto2dLayer is not supported. A placeholder subsystem will be added for 'softmax_Flatten'.
Warning: nnet.onnx.layer.CustomOutputLayer is not supported. A placeholder subsystem will be added for 'softmax1002Output'.
Warning: Failed to update the model. Check the model to resolve any issues.
The model contains a subsystem that contains the layer blocks. Open the subsystem.
The subsystem contains a block for each layer in the network. For the layers that do not have corresponding layer blocks, the subsystem contains red placeholder subsystems. Each placeholder subsystem contains an Assertion block. To use the model, you must manually replace or remove each Assertion block.
Remove Softmax Output Layer
The softmax1002Output
subsystem represents a CustomOutputLayer
object. The CustomOutputLayer
object applies formatting to the dlarray
that a dlnetwork
object outputs. Because this does not apply to Simulink models, the subsystem is unnecessary. Remove the subsystem and connect the Softmax Layer block softmax1002
to the outport.
Implement ONNX Flatten Layer by Using MATLAB Code
The softmax_Flatten
subsystem represents an ONNX Flatten layer. Open the subsystem.
You can implement the ONNX Flatten layer by using a MATLAB Function block. Replace the Assertion block with a MATLAB Function block and connect it to the outport.
Place this code inside the MATLAB Function block.
function y = fcn(u)
y = reshape(permute(u, [2,3,1]),[],1);
Exit the softmax_Flatten
subsystem.
Implement Elementwise Affine Layer by Using Simulink Blocks
The imageinput_Sub
subsystem represents an Elementwise Affine layer. Open the subsystem.
The Elementwise Affine layer adds a constant bias to each element of the input matrix. The Offset
property of the layer object contains the bias value to add. You can implement this functionality by using an Add Constant block. Replace the Assertion block with an Add Constant block and connect it to the outport. Set the Bias parameter of the Add Constant block to net.Layers(2).Offset
.
Exit the imageinput_Sub
subsystem.
Run Model
The model now contains no Assertion blocks. Run the model.