torch2tflite
torch2tflite copied to clipboard
ERROR:root:Can not load PyTorch model. Please make surethat model saved like `torch.save(model, PATH)`
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!
I got exactly the same problem! Can't load pytorch model
@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.
I also faced the same problem
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)'
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 did you solve it ? I have the same issue
I don't remember, unfortunately. Most likely it is because I abandoned the prototype I was working on at that time.