Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to import model parameters without participating in training #1265

Open
zengnanhua opened this issue Mar 9, 2024 · 7 comments
Open

How to import model parameters without participating in training #1265

zengnanhua opened this issue Mar 9, 2024 · 7 comments

Comments

@zengnanhua
Copy link

How to import model parameters without participating in training

@zengnanhua
Copy link
Author

怎么导入模型参数不参与训练 如:
model = models.vgg16(pretrained = True)
for param in model.parameters():
param.requires_grad = False

@shaltielshmid
Copy link
Contributor

In TorchSharp it would look the same:

var model = ...;
foreach (var param in model.parameters())
    param.requires_grad = False;

@zengnanhua
Copy link
Author

model = models.vgg16(pretrained = True) How to import the trained parameters of python tourch model

@yueyinqiu
Copy link
Contributor

yueyinqiu commented Mar 14, 2024

model = models.vgg16(pretrained = True) How to import the trained parameters of python tourch model

I suppose it could be another issue. I've tried that, but it seems that currently there are no beautiful solutions available.

If you don't mind, maybe you could download the pre-trained model with pytorch, and then try TorchSharp.PyBridge or exportsd.py to load it into csharp.

Actually torch.hub.download_url_to_file could work in csharp. But I haven't found a simple enough way to resolve such a .pkl file. Perhaps some more features should be added into TorchSharp.PyBridge (to just load a dictionary of tensor, without a module or optimizer)?

@yueyinqiu
Copy link
Contributor

#713

@yueyinqiu
Copy link
Contributor

I have uploaded the converted model to hugging face. Could the following code work for you?

using TorchSharp;

var huggingFace = "https://huggingface.co/";
// var huggingFace = "https://hf-mirror.com/";

var file = new FileInfo("./vgg16.dat");
if (!file.Exists)
    torch.hub.download_url_to_file(
        $"{huggingFace}yueyinqiu/vision-TorchSharp/resolve/main/VGG16_Weights.IMAGENET1K_V1",
        file.FullName);

var model = torchvision.models.vgg16(weights_file: file.FullName);

@NiklasGustafsson
Copy link
Contributor

@zengnanhua - Is this still an issue? If resolved, please close. If not, please see if you can reply to @yueyinqiu's question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants