Error when exporting model to onnx

username: Shbnm21
cooked: <p>The model is available on GitHub.It is for manipulation of multiple face attributes.<br>
Here is the link<br>
<aside class="onebox whitelistedgeneric">
  <header class="source">
      <img src="https://github.githubassets.com/favicon.ico" class="site-icon" width="16" height="16">
      <a href="https://github.com/Prinsphield/ELEGANT" target="_blank" rel="nofollow noopener">GitHub</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars2.githubusercontent.com/u/12531720?s=400&amp;amp;v=4" class="thumbnail onebox-avatar" width="60" height="60">

<h3><a href="https://github.com/Prinsphield/ELEGANT" target="_blank" rel="nofollow noopener">Prinsphield/ELEGANT</a></h3>

<p>ELEGANT: Exchanging Latent Encodings with GAN for Transferring Multiple Face Attributes - Prinsphield/ELEGANT</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
</p>
<p>It generates four files<br>
Enc-iter.pth<br>
Dec-iter.pth</p>
<pre><code>        D1-iter.pth

        D2-iter.pth
</code></pre>
<p>torch_out=torch.onnx.export(model,x,model.onnx, verbose=True)</p>
<p>Export function gives the graph but when I print torch_out it gives None but it should print tensor value</p>
<p>And this is causing AtrributError when comparing torch_out with caffe2 model</p>
<p>np.testing.assert_almost_equal(torch_out.data.cpu().numpy(), c2_out, decimal=3)</p>
<p>In this I am getting AtrributError<br>
Nonetype object has no attribute ‘data’</p>
<p>I have asked it on GitHub/onnx but they are saying ask this on pytorch forum .issue is in pytorch code.</p>

username: ptrblck
cooked: <p><code>torch.onnx.export</code> shouldn’t return anything, but store a binary protobuf file at the specified location.<br>
This file can then be loaded using <code>onnx</code>.</p>
<p>Have a look at <a href="https://pytorch.org/docs/stable/onnx.html" rel="nofollow noopener">the doc</a> for some example code.</p>

username: Shbnm21
cooked: <p>thank you for the response</p>
<h1>Verify the numerical correctness upto 3 decimal places</h1>
<p>np.testing.assert_almost_equal(torch_out.data.cpu().numpy(), c2_out, decimal=3)</p>
<p>in this we are comparing two multidimensional arrays</p>
<p>for this i ran example of super resolution and here is the output</p>
<p>import io<br>
import numpy as np</p>
<p>from torch import nn<br>
import torch.utils.model_zoo as model_zoo<br>
import torch.onnx<br>
torch_model = SuperResolutionNet(upscale_factor=3)</p>
<h1>Input to the model</h1>
<p>x = torch.randn(batch_size, 1, 224, 224, requires_grad=True)</p>
<h1>Export the model</h1>
<p>torch_out = torch.onnx._export(torch_model,             # model being run<br>
x,                       # model input (or a tuple for multiple inputs)<br>
“super_resolution.onnx”, # where to save the model (can be a file or file-like object)<br>
export_params=True,verbose=True)      # store the trained parameter weights inside the model file<br>
print(“torch_out”,torch_out)</p>
<p>this is the output i.e. graph and to see what is the output of torch_out i.e. tensor value</p>
<p>torch_out tensor([[[[ 6.8571e-01,  9.3982e-02,  1.8855e-01,  …,  1.1247e-01,<br>
3.9000e-01, -3.4897e-01],<br>
[ 8.2722e-02, -6.9161e-01,  1.0651e-01,  …,  1.6725e-01,<br>
8.2437e-02, -1.2075e-02],<br>
[ 5.9089e-02, -1.4693e-01, -2.1743e-01,  …, -1.1221e-01,<br>
9.0878e-02,  2.8896e-01],<br>
…,<br>
[-9.9215e-02,  6.8443e-02,  3.1096e-01,  …,  6.0085e-05,<br>
6.3814e-02,  5.8048e-02],<br>
[ 8.1212e-02, -8.7285e-02,  3.3845e-02,  …, -4.4617e-03,<br>
-7.5614e-02,  2.7088e-01],<br>
[-1.1385e-01,  3.1267e-01, -3.3085e-01,  …, -2.5969e-01,<br>
5.3383e-01, -2.2986e-01]]]], grad_fn=)</p>
<p>and</p>
<p>import onnx<br>
import onnx_caffe2.backend</p>
<h1>Load the ONNX ModelProto object. model is a standard Python protobuf object</h1>
<p>model = onnx.load(“super_resolution.onnx”)</p>
<h1>prepare the caffe2 backend for executing the model this converts the ONNX model into a</h1>
<h1>Caffe2 NetDef that can execute it. Other ONNX backends, like one for CNTK will be</h1>
<h1>availiable soon.</h1>
<p>prepared_backend = onnx_caffe2.backend.prepare(model)</p>
<h1>run the model in Caffe2</h1>
<h1>Construct a map from input names to Tensor data.</h1>
<h1>The graph of the model itself contains inputs for all weight parameters, after the input image.</h1>
<h1>Since the weights are already embedded, we just need to pass the input image.</h1>
<h1>Set the first input.</h1>
<p>W = {model.graph.input[0].name: x.data.numpy()}</p>
<h1>Run the Caffe2 net:</h1>
<p>c2_out = prepared_backend.run(W)[0]<br>
print(“c2_out”,c2_out)</p>
<h1>Verify the numerical correctness upto 3 decimal places</h1>
<p>np.testing.assert_almost_equal(torch_out.data.cpu().numpy(), c2_out, decimal=3)</p>
<p>print(“Exported model has been executed on Caffe2 backend, and the result looks good!”)</p>
<p>here output of c2_out is tensor which is</p>
<p>c2_out [[[[ 0.19614297 -0.14641185  0.63868225 … -0.12567244 -0.09246139<br>
0.1514894 ]<br>
[ 0.2977414   0.30597484  0.07216635 …  0.3880918   0.31021857<br>
-0.00942096]<br>
[ 0.29691827  0.26158085  0.67230344 …  0.5879603  -0.22218955<br>
-0.04444573]<br>
…<br>
[-0.40999678 -0.31105435 -0.9089918  …  0.965673   -0.32325625<br>
0.47099867]<br>
[-0.34081718 -0.12496098 -0.7071778  … -0.48262012 -0.28351355<br>
0.305478  ]<br>
[-0.10641275 -0.42513472 -0.41691086 …  0.01121937  0.36214358<br>
0.11300252]]]]</p>

username: Shbnm21
cooked: <p>the model which i am trying to export does not output tensor value<br>
when i am trying to print torch_out it gives ouput as None</p>
<p>and that’s why it is giving attribute error</p>

username: ptrblck
cooked: <p><code>torch.onnx.export</code> does not return anything, thus will yield an error, if you try to access the return value.<br>
In your code snippet, you are using <code>torch.onnx._export</code> (note the underscore), which seems to be an internal method, and will yield an output.</p>
<p>I’m not sure, which method you are currently using.</p>

username: Shbnm21
cooked: <p>thank u for the response<br>
I was using torch.onnx.export .And i suppose to use torch.onnx._export.<br>
Silly mistake.<br>
Thanks a ton</p>