Why do I get warnings and errors about an "uninitialized dlnetwork" when importing an Deep Learning network from a third-party framework (ONNX, TensorFlow, PyTorch, etc.) into MATLAB, or exporting it to Simulink?

8 次查看(过去 30 天)

When I import my ONNX model into MATLAB using "importNetworkFromONNX", I get the following error:

>> net = importNetworkFromONNX('model.onnx');
Warning: Returning an uninitialized dlnetwork because some input layers have unknown data formats or
undetermined image sizes. Initialize the network by passing example input data to the initialize object function.

>> net = importNetworkFromTensorFlow(modelFolder);
Warning: Network is imported as an uninitialized dlnetwork object. Before using the network, add input layer(s):

>> net = importNetworkFromPyTorch(modelfile)
Warning: Network was imported as an uninitialized dlnetwork. Before using the network, add input layer(s):
Then, when I try to import this dlnetwork into Simulink via the "exportNetworkToSimulink" function, I get an error:

>> exportNetworkToSimulink(net)
Error using exportNetworkToSimulink
Uninitialized dlnetwork object. Initialize and train network before calling exportNetworkToSimulink.

采纳的回答

MathWorks Support Team
MathWorks Support Team 2025-9-18,0:00
When importing a pretrained network from a third-party framework that results in an uninitialized network, you need to initialize the network. See the examples on each function's documentation page on how to do this:

Considerations for "importNetworkFromONNX":

For "importNetworkFromONNX", a Custom Input Layer is added if the input dimensions in the ONNX model are unknown. Try to apply "InputDataFormats" when importing the ONNX model:
By specifying InputDataFormats, you avoid the generation of the custom input layer, and MATLAB may be able to import more Deep Learning Toolbox layers instead of custom layers:
>> net = importNetworkFromONNX('model.onnx','InputDataFormats','BC');
>> net.Layers
ans =
10×1 Layer array with layers:
1 'input' Feature Input 4 features
2 'sequential_hidden1_4' Fully Connected 10 fully connected layer
3 'sequential_hidden1_2' Scaling Scaling
4 'sequential_hidden1_5' ReLU ReLU
5 'sequential_hidden2_4' Fully Connected 5 fully connected layer
6 'sequential_hidden2_2' Scaling Scaling
7 'sequential_hidden2_5' ReLU ReLU
8 'sequential_output__3' Fully Connected 1 fully connected layer
9 'sequential_output__1' Scaling Scaling
10 'outputOutput' Custom output ('CB') See the OutputInformation property to find the output dimension ordering that is produced by this layer.
When using "importNetworkFromONNX" without "InputDataFormats", the result may just be 3 layers:
>> net = importNetworkFromONNX('model.onnx')
Warning: Returning an uninitialized dlnetwork because some input layers have unknown data formats or undetermined image sizes. Initialize the network by passing example input data to the initialize object function.
>> net.Layers
ans =
3×1 Layer array with layers:
1 'input' Custom input ('UU') See the InputInformation property to find the required input dimension ordering for this layer.
2 'MatMul_To_AddLayer1000' model_keras.MatMul_To_AddLayer1000 model_keras.MatMul_To_AddLayer1000
3 'outputOutput' Custom output ('UU') See the OutputInformation property to find the output dimension ordering that is produced by this layer.
>> net.Layers(1).InputInformation
ans =
"Layer input must be a labeled dlarray with the dimension order shown in the ONNX model file.
You must pass the size:
(unk__6, 1)
The dlarray must have a format string consisting of one 'U' for each dimension: 'dlarray(data, 'UU')'"
When you inspect the Custom Input Layer's InputInformation using "net.Layers(1).InputInformation", placeholders like "unk", "unk__6", or "-1" may appear. If "InputDataFormats" can't be applied, you can try to manually initialize the network based on the InputInformation:
For (unk__6, 1), try:
net = net.initialize(dlarray(single(1),"U"));
For (unk__6, 4), try:
net = net.initialize(dlarray(ones([1,4],'single'),"UU"));

更多回答(0 个)

标签

尚未输入任何标签。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by