'Building models in ONNX

Is it possible to build a model in ONNX without using a different deep learning framework (e.g. PyTorch, TensorFlow, etc.)?

In PyTorch, I would write a model like this:

import torch.nn as nn
import torch.nn.functional as F

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(-1, 16 * 5 * 5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

net = Net()

What would be the equivalent of defining an identical model in ONNX? I understand how to export models, but I would like to use ONNX without using external libraries.



Solution 1:[1]

As of now, you can do inference of the model and also train your model much faster using ONNXRuntime but cannot build a model using ONNX. For reference, https://cloudblogs.microsoft.com/opensource/2020/05/19/announcing-support-for-accelerated-training-with-onnx-runtime/

Solution 2:[2]

The most you can do is use the onnx.helper which allows you to define nodes, initializers and connections between the nodes but it is very low level compared to pytorch/ keras or other frameworks. https://github.com/onnx/onnx/blob/main/docs/PythonAPIOverview.md I wouldn't try to build a model in ONNX. But I would definitely use the onnx.helper as a method of ONNX graph manipulation such as disconnecting or changing parameters inside an existing model. ( model surgery)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Asmita Khaneja
Solution 2 petre Trusca