cancel
Showing results for 
Search instead for 
Did you mean: 

Error when exporting PyTorch model "NOT IMPLEMENTED: Order of dimensions of input cannot be interpreted"

JCame.3
Associate II

I am using the STM32Cube.AI Developer Cloud to convert my ONNX model that I built using PyTorch.

Here is my export code:

input_size = [1, 8, 1000]
 
        x = torch.randn(input_size)
 
        onnx_folder_path = 'onnx_models/'
        if not os.path.isdir(onnx_folder_path):
            os.mkdir(onnx_folder_path)
        onnx_filename = "{}{}.onnx".format(onnx_folder_path, filename)
 
        torch.onnx.export(model,  # model being run
                          x,  # model input (or a tuple for multiple inputs)
                          onnx_filename,  # 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=11,  # the ONNX version to export the model to
                          # do_constant_folding=True,  # whether to execute constant folding for optimization
                          input_names=['input_1'],  # the model's input names
                          output_names=['output_1'],  # the model's output names
                          )

And my model code:

class Custom1DCNN(nn.Module):
    def __init__(self, n_input=128, n_output=7, n_channel=8, pretrained=None):
        super().__init__()
 
        input_0 = n_channel
        input_1 = n_input
        input_2 = n_input // 4
        input_3 = n_input // 8
 
        self.conv1 = nn.Conv1d(input_0, input_1, kernel_size=3)
        self.bn1 = nn.BatchNorm1d(input_1)
 
        self.conv2 = nn.Conv1d(input_1, input_2, kernel_size=3)
        self.bn2 = nn.BatchNorm1d(input_2)
 
        self.conv3 = nn.Conv1d(input_2, input_3, kernel_size=3)
        self.bn3 = nn.BatchNorm1d(input_3)
 
        self.avgpool = nn.AdaptiveAvgPool1d(1)
 
        self.fc1 = nn.Linear(input_3, n_output)
 
        self.activation = nn.ReLU()
 
        if pretrained is not None:
            self.load_pretrained(pretrained)
            self.is_pretrained = True
        else:
            self.is_pretrained = False
 
    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.activation(x)
 
        x = self.conv2(x)
        x = self.bn2(x)
        x = self.activation(x)
 
        x = self.conv3(x)
        x = self.bn3(x)
        x = self.activation(x)
 
        x = self.avgpool(x)
        x = x.permute(0, 2, 1)
        x = self.fc1(x)
        x = x.flatten(1)
        x = F.softmax(x, dim=1)
 
        return x

I am getting the following error:

>>> stm32ai validate --model exported_1d_cnn_input_1000.onnx --workspace workspace --output output --allocate-inputs --allocate-outputs --relocatable --compression none --optimization balanced
Neural Network Tools for STM32AI v1.6.0 (STM.ai v7.3.0-RC5)
NOT IMPLEMENTED: Order of dimensions of input cannot be interpreted

I would appreciate guidance because this is blocking my research.

10 REPLIES 10
fauvarque.daniel
ST Employee

Can you share the onnx file (even trained with random data).

Thanks a lot


In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.
fauvarque.daniel
ST Employee

I've reproduced your issue on the current development code and I raised a bug to the development team. I'll let you know of possible workaround


In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.
JCame.3
Associate II

Thanks Daniel - a workaround would be excellent

CaoJuan
Associate

Hi, did you solve this problem? If so, could you please share the solution, thank you very much!

wg
Associate II

I have the same problem. How do you solve it? Thank you very much

I have the same problem. How do you solve it? Thank you very much

Hi,

I was able to recreate your issue. and was also able to pass the model through cubeai.

The structuring of your permute, fullyconnected then flatten layer in this order was leading to the creation of a "MatMul" node in your onnx thad has 3d inputs and 2d matrices (which is probably what is causing the issue in cubeai), I was able to fix this by shifting the fullyconnected layer after the flatten layer. I'm not sure if changing the architecture like this will affect your model performance in any way you would have to check that.

Thanks for your detailed submission.

lyx
Associate

Have you solved this problem? if so, can you share the workaround, thank you very much