exportONNXNetwork and then importNetworkFromONNX is not the same

조회 수: 2 (최근 30일)
Darcy
Darcy 2025년 9월 23일
답변: Umar 2025년 9월 23일
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

채택된 답변

Umar
Umar 2025년 9월 23일

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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by