exportONNXNetwork and then importNetworkFromONNX is not the same

2 visualizzazioni (ultimi 30 giorni)
Export to model.onnx via exportONNXNetwork(net, 'model.onnx'),
then net_2 = importNetworkFromONNX("model.onnx").
The results show that net and net_2 are inconsistent.
How to export the correct model.onnx?
Fig1 and 2 is for net, and Fig3 and 4 is for net_2.
Fig1
Fig2
Fig3
Fig4

Risposta accettata

Umar
Umar il 23 Set 2025

Hi @Darcy,

I saw your post about the inconsistencies between your original network and the one you got back after exporting to ONNX and reimporting. After looking into this issue, it appears this behavior is actually documented and expected.

Looking at your screenshots, your original network has 19 layers with a CustomOutputLayer at the end, but after the round-trip through ONNX, you're getting 25 layers with different layer types. The CustomOutputLayer got converted to a DropoutLayer and some other layers, and your ElementwiseAffineLayer also changed.

According to the MathWorks documentation, this is normal behavior rather than an error. The official documentation states that " because of architectural differences between MATLAB and ONNX, an exported network can have a different structure compared to the original network." It also mentions that " if you import an exported network, layers of the reimported network might differ from layers of the original network."

The documentation explains that when you export layers that don't have direct ONNX equivalents (like your CustomOutputLayer), MATLAB creates placeholder ONNX operators or breaks them down into simpler operations that achieve the same result. When you reimport, those operations become separate MATLAB layers, which explains why you're seeing more layers and different types.

Based on what I found in the documentation, here are some suggestions:

1. Check if you received any warnings during the export - the documentation indicates MATLAB warns about layers requiring special handling 2. Test both networks with identical input data to verify they produce the same results (the functionality should be preserved even if structure changes) 3. If maintaining the exact structure is critical, consider replacing custom layers with built-in ones that have better ONNX support

The documentation suggests that functional behavior typically remains intact even when the layer structure changes due to the translation process between MATLAB and ONNX formats.

Hope this helps clarify what's happening! If you test the outputs and find they're actually producing different results, that would indicate a different type of issue.

Più risposte (0)

Categorie

Scopri di più su Image Data Workflows in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by