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

Platform : Google Colab
onnx version : 1.11.0
onnxruntime verstion : 1.11.1
python = 3.9
pytorch alexnet = v0.10.0

I am running pytorch alexnet v0.10.0 for my data set. I did convert my .pth pytorch model into ONNX model like this.

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') 

Meanwhile converting ONNX model it gives warnings like this but at end I get my .onnx file after conversion:

/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")

Now, when I try to load the .onnx file it gives me error like this.
CODE :

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])

ERROR:

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

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.

  1. [Runtime Error with KeyError: ‘org.pytorch.aten’ #604] (Can’t put more than 2 links because i am new user but google this words and you will get it.)
  2. [ONNXRuntimeError] Fatal error: adaptive_max_pool2d is not a registered function/op #3878
  3. Loading onnx model failed with Fatal error: max_pool2d is not a registered function/op #4998

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.

2 Likes