While loading ONNX i faced this [Fatal error: adaptive_avg_pool2d is not a registered function/op]

username: Priyank_Thakkar
cooked: <p>Platform : Google Colab<br>
onnx version : 1.11.0<br>
onnxruntime verstion : 1.11.1<br>
python = 3.9<br>
pytorch alexnet = v0.10.0</p>
<p>I am running pytorch alexnet v0.10.0 for my data set. I did convert my .pth pytorch model into ONNX model like this.</p>
<pre><code class="lang-auto">import torch.onnx 
# batch_size = 1
#Function to Convert to ONNX 
def Convert_ONNX(): 

    # set the model to inference mode 
    model.eval() 

    # Let's create a dummy input tensor  
    dummy_input = torch.randn(BATCH_SIZE, 3, 112, 112, requires_grad=True, device="cuda")  

    # Export the model   
    torch.onnx.export(
        model,         # model being run 
        dummy_input,       # model input (or a tuple for multiple inputs) 
        "ImageClassifier.onnx",       # where to save the model  
        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 = ['modelInput'],   # the model's input names 
        output_names = ['modelOutput'], # the model's output names 
        dynamic_axes={'modelInput' : {0 : 'BATCH_SIZE'},    # variable length axes 
                        'modelOutput' : {0 : 'BATCH_SIZE'}},
        operator_export_type=torch.onnx.OperatorExportTypes.ONNX_ATEN_FALLBACK) 
    print(" ") 
    print('Model has been converted to ONNX') 
</code></pre>
<p>Meanwhile converting ONNX model it gives warnings like this but at end I get my .onnx file after conversion:</p>
<pre><code class="lang-auto">/usr/local/lib/python3.7/dist-packages/torch/onnx/utils.py:286: UserWarning: `add_node_names' can be set to True only when 'operator_export_type' is `ONNX`. Since 'operator_export_type' is not set to 'ONNX', `add_node_names` argument will be ignored.
  "`{}` argument will be ignored.".format(arg_name, arg_name))
/usr/local/lib/python3.7/dist-packages/torch/onnx/utils.py:286: UserWarning: `do_constant_folding' can be set to True only when 'operator_export_type' is `ONNX`. Since 'operator_export_type' is not set to 'ONNX', `do_constant_folding` argument will be ignored.
  "`{}` argument will be ignored.".format(arg_name, arg_name))
/usr/local/lib/python3.7/dist-packages/torch/onnx/symbolic_helper.py:258: UserWarning: ONNX export failed on adaptive_avg_pool2d because output size that are not factor of input size not supported
  warnings.warn("ONNX export failed on " + op + " because " + msg + " not supported")
</code></pre>
<p>Now, when I try to load the .onnx file it gives me error like this.<br>
CODE :</p>
<pre><code class="lang-auto">import onnxruntime as ort

ort_session = ort.InferenceSession("/content/drive/MyDrive/255_Final_Project/Model/ImageClassifier.onnx")

outputs = ort_session.run(
    None,
    {"actual_input_1": np.random.randn(10, 3, 224, 224).astype(np.float32)},
)
print(outputs[0])
</code></pre>
<p>ERROR:</p>
<pre><code class="lang-auto">Fail: [ONNXRuntimeError] : 1 : FAIL : Load model from /content/drive/MyDrive/255_Final_Project/Model/ImageClassifier.onnx failed:Fatal error: adaptive_avg_pool2d is not a registered function/op
</code></pre>
<p>I have no clue I am stuck from last 2 days and I do not find any way out. But, when I check all the Github issues regarding this on ONNXRUNTIME i found this links which says that it’s Pytorch’s issue.</p>
<ol>
<li>[Runtime Error with KeyError: ‘org.pytorch.aten’ <span class="hashtag">#604</span>] (Can’t put more than 2 links because i am new user but google this words and you will get it.)</li>
<li><a href="https://github.com/microsoft/onnxruntime/issues/3878" rel="noopener nofollow ugc">[ONNXRuntimeError] Fatal error: adaptive_max_pool2d is not a registered function/op #3878</a></li>
<li><a href="https://github.com/microsoft/onnxruntime/issues/4998" rel="noopener nofollow ugc">Loading onnx model failed with Fatal error: max_pool2d is not a registered function/op #4998</a></li>
</ol>
<p>But, when i check on Pytorch I do not find anything that works for me, or anyone who reported this. I badly wants to solve this issue. I want to convert this into the TensorRT model.</p>

username: abetancordelrosario
cooked: <p>Did you find a solution for this problem?</p>