question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Load model from pyTorch issue

See original GitHub issue

I try load trained nn in pyTorch(in pth file format) to TorchSharp but give some errors.

  1. from static method Module module = Module.Load("C:\\ir50-model.pth"); module.forward(tensor_image); throw System.NotImplementedException: ‘forward(t)’

  2. useStrict: false

var myModel = new MyModel("test");
myModel.load("C:\\ir50-model.pth", false); ;
myModel.forward(tensor_image);

public class MyModel : Module
{
        public MyModel() : base("test") {}      

        public MyModel(string name) : base(name) {}     

        public override Tensor forward(Tensor input)
        {
            return this.forward(input);
        }
}

throw System.FormatException: ‘Too many bytes in what should have been a 7-bit encoded integer.’

Does it exist worked way to load pth model from pyTorch to TorchSharp ?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
NiklasGustafssoncommented, Jan 4, 2022

Sorry, I’ve been out on vacation for a month. First day back…

What does the Python model that you are exporting look like? It has to have the same components as the model you’re loading the weights into.

From the code you included, it looks like:

  1. There are no submodules in your model. Is this intentional, or did you simplify it for the purpose of brevity in the issue report?
  2. Your forward() function recurses infinitely.

UPDATE: I tried to reproduce the error on my own, but it didn’t fail. It would be helpful to have the source code for the Python model that you exported. I don’t need the training code, just the definition of the model class.

0reactions
GeorgeS2019commented, Feb 19, 2022

@anytoany

Please see a work around using the recommended save_dict codes

Read more comments on GitHub >

github_iconTop Results From Across the Web

Saving and Loading Models
To load the models, first initialize the models and optimizers, then load the dictionary locally using torch.load() . From here, you can easily...
Read more >
Pytorch issue: torch.load() does not correctly load a saved ...
To solve this, try defining a new model and loading the parameters to it via load like so: my_model = MyModelClass(parameters).to(device) ...
Read more >
pytorch 1.4 can not load model saved by 1.7 · Issue #48915
Bug model trained by pytorch 1.7.0 cuda 11.0.221, but cannot load by pytorch1.4.0, cuda 10.0.130 To Reproduce Steps to reproduce the ...
Read more >
Save and Load Your PyTorch Models
In this post, you will discover how to save your PyTorch models to files and load them up again to make predictions.
Read more >
How to Save and Load Models in PyTorch
This article is a tutorial that covers how to correctly save and load your trained machine learning models in PyTorch using Weights &...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found