torch2tflite icon indicating copy to clipboard operation
torch2tflite copied to clipboard

ERROR:root:Can not load PyTorch model. Please make surethat model saved like `torch.save(model, PATH)`

Open IvanStoykov opened this issue 4 years ago • 7 comments
trafficstars

I'm trying to convert a YOLOv5 best.pt weights file to a .tflite file so we can deploy the model on a flutter app.

This is the code:

`import torch weights_path = '/content/drive/MyDrive/Weights/best.pt' yolo_path = '/content/yolov5'

model = torch.hub.load(yolo_path, 'custom', weights_path, source='local') # local repo torch.save(model, '/content/pipeline.pt' )`

You can also go straight to the colab for all the colde. https://colab.research.google.com/drive/19JeOkrxlGP6KtkfGbrkO87COc4pvSZSE?usp=sharing

I assume I'm not saving the model how the converter wants it, but I can't figure it out. Can you please explain exactly how the file needs to be saved so the code will work ?

Thanks a lot in advance!

IvanStoykov avatar Oct 25 '21 20:10 IvanStoykov

I got exactly the same problem! Can't load pytorch model

lawsonX avatar Nov 03 '21 01:11 lawsonX

@IvanStoykov To understand what causes the problem you can log the exception on 72 line in convertert.py, simply write logging.error(str(e)) and run the script again. The problem may be in the wrong path to your model and not in pytorch itself.

Anyway check your saving script, be sure that you save the entire model, not only the model parameters.

trueangle avatar Nov 15 '21 08:11 trueangle

I also faced the same problem

larrywal-express avatar Jan 04 '22 22:01 larrywal-express

while I am running this command: python3.8 -m torch2tflite.converter --torch-path model/model.pth --tflite-path model.tflite --target-shape 225 225 3 --seed 1 the error: ERROR:root:Can not load PyTorch model. Please make sure that model saved like 'torch.save(model, PATH)'

celikmustafa89 avatar Feb 28 '22 13:02 celikmustafa89

I have the same error, the problem occurs here model = model.eval() https://github.com/omerferhatt/torch2tflite/blob/master/torch2tflite/converter.py#L65

For me, torch.load returns an OrderedDict, which doesn't have an eval method, hence the error: 'collections.OrderedDict' object has no attribute 'eval'. Clearly, this is not happening because the path to the PyTorch model is incorrect.

ralienpp avatar Apr 17 '22 10:04 ralienpp

@ralienpp did you solve it ? I have the same issue

jeungmin717 avatar Apr 25 '23 05:04 jeungmin717

I don't remember, unfortunately. Most likely it is because I abandoned the prototype I was working on at that time.

ralienpp avatar Apr 26 '23 15:04 ralienpp