'ImageClassifierOutput' object has no attribute 'requires_grad' to conversion of model from pytorch to ONNX

Hi there, I have the above error while running the following piece of code, from the tutorial :


import onnxruntime

ort_session = onnxruntime.InferenceSession(model_onnx)

def to_numpy(tensor):
    return tensor.detach().cpu().numpy() if tensor.requires_grad else tensor.cpu().numpy()

# compute ONNX Runtime output prediction
ort_inputs = {ort_session.get_inputs()[0].name: to_numpy(x)}
ort_outs = ort_session.run(None, ort_inputs)

# compare ONNX Runtime and PyTorch results
np.testing.assert_allclose(to_numpy(torch_out), ort_outs[0], rtol=1e-03, atol=1e-05)

print("Exported model has been tested with ONNXRuntime, and the result looks good!")

Can you please help me with that? Many THANKS!!!

The whole error that I get is this one:


---------------------------------------------------------------------------

AttributeError Traceback (most recent call last)

[<ipython-input-29-129a29912fd2>](https://9b63epfevop-496ff2e9c6d22116-0-colab.googleusercontent.com/outputframe.html?vrz=colab-20220926-060050-RC01_476951119#) in <module> 11 12 # compare ONNX Runtime and PyTorch results ---> 13 np.testing.assert_allclose(to_numpy(torch_out), ort_outs[0], rtol=1e-03, atol=1e-05) 14 15 print("Exported model has been tested with ONNXRuntime, and the result looks good!")

[<ipython-input-29-129a29912fd2>](https://9b63epfevop-496ff2e9c6d22116-0-colab.googleusercontent.com/outputframe.html?vrz=colab-20220926-060050-RC01_476951119#) in to_numpy(tensor) 4 5 def to_numpy(tensor): ----> 6 return tensor.detach().cpu().numpy() if tensor.requires_grad else tensor.cpu().numpy() 7 8 # compute ONNX Runtime output prediction

AttributeError: 'ImageClassifierOutput' object has no attribute 'requires_grad'