Errors in using importONNXLayers to import my pytorch model into Matlab

I am trying to run my pytorch models in matlab (2018b) via onnx (importONNXLayers), but I have encounted several errors. I do not know how to fix them.
The error messages are as follow:
layers = importONNXLayers('super_resolution.onnx')
警告: Imported layers have no output layer because ONNX files do not specify the network's output layer type. The
layers will not be trainable until an output layer is added. Either add an output layer to the imported layers, or
specify the output layer type using 'OutputLayerType' in the call to importONNXLayers.
> In nnet.internal.cnn.onnx.importONNXLayers>iValidateOutputLayerType (line 36)
In nnet.internal.cnn.onnx.importONNXLayers>iValidateInputs (line 28)
In nnet.internal.cnn.onnx.importONNXLayers (line 5)
In importONNXLayers (line 45)
错误使用 nnet.internal.cnn.onnx.transformConstantNodeToInitializer (line 27)
断言失败。
出错 nnet.internal.cnn.onnx.translateONNX>iReplaceWeightNodes (line 270)
[tf, dim, rawData] = nnet.internal.cnn.onnx.transformConstantNodeToInitializer(node);
出错 nnet.internal.cnn.onnx.translateONNX (line 46)
[thisGraph, initializerNames, initializerDimMap, initializerRawDataMap] = iReplaceWeightNodes(...
出错 nnet.internal.cnn.onnx.importONNXLayers (line 8)
LayersOrGraph = nnet.internal.cnn.onnx.translateONNX(modelProto, OutputLayerType, UserImageInputSize, ImportWeights);
出错 importONNXLayers (line 45)
Layers = nnet.internal.cnn.onnx.importONNXLayers(ModelFile, varargin{:});
My pytorch onnx model (which is from an example in https://pytorch.org/tutorials/advanced/super_resolution_with_onnxruntime.html):
class SuperResolutionNet(nn.Module):
def __init__(self, upscale_factor, inplace=False):
super(SuperResolutionNet, self).__init__()
self.relu = nn.ReLU(inplace=inplace)
self.conv1 = nn.Conv2d(1, 64, (5, 5), (1, 1), (2, 2))
self.conv2 = nn.Conv2d(64, 64, (3, 3), (1, 1), (1, 1))
self.conv3 = nn.Conv2d(64, 32, (3, 3), (1, 1), (1, 1))
self.conv4 = nn.Conv2d(32, upscale_factor ** 2, (3, 3), (1, 1), (1, 1))
self.pixel_shuffle = nn.PixelShuffle(upscale_factor)
self._initialize_weights()
def forward(self, x):
x = self.relu(self.conv1(x))
x = self.relu(self.conv2(x))
x = self.relu(self.conv3(x))
x = self.pixel_shuffle(self.conv4(x))
return x
def _initialize_weights(self):
init.orthogonal_(self.conv1.weight, init.calculate_gain('relu'))
init.orthogonal_(self.conv2.weight, init.calculate_gain('relu'))
init.orthogonal_(self.conv3.weight, init.calculate_gain('relu'))
init.orthogonal_(self.conv4.weight)
# Create the super-resolution model by using the above model definition.
torch_model = SuperResolutionNet(upscale_factor=3)
batch_size = 1 # just a random number
# set the model to inference mode
torch_model.eval()
# Input to the model
x = torch.randn(batch_size, 1, 224, 224, requires_grad=True)
torch_out = torch_model(x)
# Export the model
torch.onnx.export(torch_model, # model being run
x, # model input (or a tuple for multiple inputs)
"super_resolution.onnx", # where to save the model (can be a file or file-like object)
export_params=True, # store the trained parameter weights inside the model file
opset_version=10, # the ONNX version to export the model to
do_constant_folding=True, # whether to execute constant folding for optimization
input_names = ['input'], # the model's input names
output_names = ['output'], # the model's output names
dynamic_axes={'input' : {0 : 'batch_size'}, # variable length axes
'output' : {0 : 'batch_size'}})
Many thanks for the help!

Answers (1)

I am also puzzled by a similar problem when importing .onnx into matlab. The key is that, I am also using Matlab 2018b! I guess this is the problem.

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Asked:

on 7 May 2021

Commented:

on 12 Jul 2022

Community Treasure Hunt

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

Start Hunting!