I made a simple pytorch MLP(GAN generator) and converted it to onnx using the tutorial (https://www.youtube.com/watch?v=Vs730jsRgO8), my code is a bit different but I cant catch the error.
class Generator(nn.Module):
def __init__(self, g_input_dim, g_output_dim):
super(Generator, self).__init__()
# g_input = 100
self.net = nn.Sequential(
nn.Linear(g_input_dim, 256),
nn.LeakyReLU(.2),
nn.Linear(256, 512),
nn.LeakyReLU(.2),
nn.Linear(512, 1024),
nn.LeakyReLU(.2),
nn.Linear(1024, 784),
nn.Tanh()
)
# forward method
def forward(self, x):
return self.net(x)
After training I export the model to onnx.
torch.save(G.state_dict(), "pytorch_model.pth")
import torch.onnx
model = Generator(z_dim,mnist_dim)
state_dict = torch.load("pytorch_model.pth")
model.load_state_dict(state_dict)
model.eval()
dummy_input = torch.zeros(100)
torch.onnx.export(model, dummy_input, "onnx_model.onnx", verbose=True)
Which gives the following onnx graph, which seems accurate.
graph(%input.1 : Float(100),
%net.0.bias : Float(256),
%net.2.bias : Float(512),
%net.4.bias : Float(1024),
%net.6.bias : Float(784),
%25 : Float(100, 256),
%26 : Float(256, 512),
%27 : Float(512, 1024),
%28 : Float(1024, 784)):
%10 : Float(256) = onnx::MatMul(%input.1, %25) # /usr/local/lib/python3.6/dist-packages/torch/nn/functional.py:1612:0
%11 : Float(256) = onnx::Add(%10, %net.0.bias)
%12 : Float(256) = onnx::LeakyRelu[alpha=0.20000000000000001](%11) # /usr/local/lib/python3.6/dist-packages/torch/nn/functional.py:1239:0
%14 : Float(512) = onnx::MatMul(%12, %26) # /usr/local/lib/python3.6/dist-packages/torch/nn/functional.py:1612:0
%15 : Float(512) = onnx::Add(%14, %net.2.bias)
%16 : Float(512) = onnx::LeakyRelu[alpha=0.20000000000000001](%15) # /usr/local/lib/python3.6/dist-packages/torch/nn/functional.py:1239:0
%18 : Float(1024) = onnx::MatMul(%16, %27) # /usr/local/lib/python3.6/dist-packages/torch/nn/functional.py:1612:0
%19 : Float(1024) = onnx::Add(%18, %net.4.bias)
%20 : Float(1024) = onnx::LeakyRelu[alpha=0.20000000000000001](%19) # /usr/local/lib/python3.6/dist-packages/torch/nn/functional.py:1239:0
%22 : Float(784) = onnx::MatMul(%20, %28) # /usr/local/lib/python3.6/dist-packages/torch/nn/functional.py:1612:0
%23 : Float(784) = onnx::Add(%22, %net.6.bias)
%24 : Float(784)
Then I imported the code into javascript.
<html>
<body>
<script src="./onnx.min.js"></script>
<script>
async function test() {
const sess = new onnx.InferenceSession()
await sess.loadModel('./onnx_model.onnx')
const input = new onnx.Tensor(new Float32Array(100), 'float32', [100])
const outputMap = await sess.run([input])
const outputTensor = outputMap.values().next().value
console.log(`Output tensor: ${outputTensor.data}`)
}
test()
</script>
</body>
</html>
I know the input dimension is correct but onnx gives me the following error.
onnx.min.js:8 Uncaught (in promise) Error: Can't use matmul on the given tensors
at e.createProgramInfo (onnx.min.js:8)
at t.run (onnx.min.js:8)
at e.run (onnx.min.js:8)
at t.<anonymous> (onnx.min.js:14)
at onnx.min.js:14
at Object.next (onnx.min.js:14)
at onnx.min.js:14
at new Promise (<anonymous>)
at r (onnx.min.js:14)
at onnx.min.js:14
I also know that matmul is a supported operator with onnx, but I can't figure out how or if my input tensor is correct.