TorchSharp
TorchSharp copied to clipboard
How to import model parameters without participating in training
How to import model parameters without participating in training
怎么导入模型参数不参与训练 如: model = models.vgg16(pretrained = True) for param in model.parameters(): param.requires_grad = False
In TorchSharp it would look the same:
var model = ...;
foreach (var param in model.parameters())
param.requires_grad = False;
model = models.vgg16(pretrained = True) How to import the trained parameters of python tourch model
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)?
#713
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);