TorchSharp icon indicating copy to clipboard operation
TorchSharp copied to clipboard

How to import model parameters without participating in training

Open zengnanhua opened this issue 11 months ago • 6 comments

How to import model parameters without participating in training

zengnanhua avatar Mar 09 '24 02:03 zengnanhua

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

zengnanhua avatar Mar 09 '24 02:03 zengnanhua

In TorchSharp it would look the same:

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

shaltielshmid avatar Mar 09 '24 21:03 shaltielshmid

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

zengnanhua avatar Mar 11 '24 09:03 zengnanhua

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 avatar Mar 14 '24 06:03 yueyinqiu

#713

yueyinqiu avatar Apr 16 '24 13:04 yueyinqiu

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);

yueyinqiu avatar Apr 18 '24 14:04 yueyinqiu