Search code examples
pythonpytorchattributeerrorseekonnx

AttributeError: 'ConvertModel' object has no attribute 'seek'


I tried converting a MATLAB model to PyTorch using ONNX, like proposed here by Andrew Naguib:

How to import deep learning models from MATLAB to PyTorch?

I tried running the model using the following code:

import onnx
from onnx2pytorch import ConvertModel
import torch

onnx_model = onnx.load ('resnet50.onnx')

pytorch_model = ConvertModel(onnx_model)

model = torch.load(pytorch_model)

But I got this error:

AttributeError: 'ConvertModel' object has no attribute 'seek'. You can only torch.load from a file that is seekable. Please pre-load the data into a buffer like io.BytesIO and try to load from it instead.

How can I fix it, please? Any ideas on how can I "pre-load the data into a buffer like io.BytesIO"?


Solution

  • Assuming my_data.dat is a file containing binary data, the following code loads it into an ioBytesIO buffer that is seekable:

    import io
    
    with open('my_data.dat', 'rb') as f:
        buf = io.BytesIO(f.read())
    

    You can now write things like

        buf.seek(4)
    

    and

        x = buf.read(1)
    

    Of course, in your case, you're going through a onnx.load methdod, and I don't know what that does. But if does return a file object, on a binary file, then the above might help you.