Loading Python Exported Model into TorchSharp
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 32/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
调研方向
先从 docfx/articles/saveload.md 和 src/Python/exportsd.py 开始,然后检查 TorchSharpExamples 中引用的 ResNet.cs 模型。比较 Python 的 state_dict 名称与 TorchSharp 生成的模块名称,并使用 ResNet18 示例重现 load() 失败。完成标准是:Python 导出的 ResNet 模型能够在该示例中成功加载,且不再出现报告中的名称或序列化异常。
由索引模型根据 Issue 内容生成。
描述
Originally posted in dotnet/TorchSharp by @jimquittenton:
https://github.com/dotnet/TorchSharp/issues/586
The naming scheme for layers are different in the ResNet example model found in this repo and the ResNet models found in TorchVision, which prevents a model saved from Python from being loaded in TorchSharp using this example code.
Original post:
Hi,
I'm new to TorchSharp and am having trouble loading a python trained ResNet18 model. I've been following this article: https://github.com/dotnet/TorchSharp/blob/main/docfx/articles/saveload.md and have exported my python model using the 'save_state_dict' function in this script: https://github.com/dotnet/TorchSharp/blob/main/src/Python/exportsd.py .
In TorchSharp I have copied the ResNet model from https://github.com/dotnet/TorchSharpExamples/blob/main/src/CSharp/Models/ResNet.cs and then call the following:
int numClasses = 3;
ResNet myModel = ResNet.ResNet18(numClasses);
myModel.to(DeviceType.CPU);
myModel.load(mPath);
The load() line throws an exception with message Mismatched module state names: the target modules does not have a submodule or buffer named 'conv1.weight'.
If I examine the state_dict from 'myModel' prior to load(), it contains entries like:
{[layers.conv2d-first.weight, {TorchSharp.Modules.Parameter}]}
{[layers.bnrm2d-first.weight, {TorchSharp.Modules.Parameter}]}
{[layers.bnrm2d-first.bias, {TorchSharp.Modules.Parameter}]}
{[layers.bnrm2d-first.running_mean, {TorchSharp.torch.Tensor}]}
{[layers.bnrm2d-first.running_var, {TorchSharp.torch.Tensor}]}
{[layers.bnrm2d-first.num_batches_tracked, {TorchSharp.torch.Tensor}]}
{[layers.blck-64-0.layers.blck-64-0-conv2d-1.weight, {TorchSharp.Modules.Parameter}]}
{[layers.blck-64-0.layers.blck-64-0-bnrm2d-1.weight, {TorchSharp.Modules.Parameter}]}
{[layers.blck-64-0.layers.blck-64-0-bnrm2d-1.bias, {TorchSharp.Modules.Parameter}]}
whereas the corresponding entries prior to saving from python are:
conv1.weight torch.Size([64, 3, 7, 7])
bn1.weight torch.Size([64])
bn1.bias torch.Size([64])
bn1.running_mean torch.Size([64])
bn1.running_var torch.Size([64])
bn1.num_batches_tracked torch.Size([])
layer1.0.conv1.weight torch.Size([64, 64, 3, 3])
layer1.0.bn1.weight torch.Size([64])
layer1.0.bn1.bias torch.Size([64])
I tried amending the ResNet.cs code to reflect the python names, but could not get them to exactly match.
I also tried calling load() with strict=false myModel.load(mPath, false);. This seemed to get past the Mismatched names exception, but throws another exception with message Too many bytes in what should have been a 7 bit encoded Int32.
I've been struggling with this for a couple of days now so would really appreciate any help you guys could offer.
Thanks
Jim
- 主要语言
- Jupyter Notebook
- 星标
- 175
- 派生
- 61
- PR 合并指标
- 30 天内没有已合并 PR
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
dotnet/TorchSharpExamples 的其他 Issue
-
难度 1/5 1 小时以内 新手友好度 45/100
dotnet/TorchSharpExamples#56 ·
-
难度 5/5 一周以上 新手友好度 20/100
dotnet/TorchSharpExamples#52 ·
-
难度 1/5 1 小时以内 新手友好度 45/100
dotnet/TorchSharpExamples#50 ·
-
难度 5/5 一周以上 新手友好度 20/100
dotnet/TorchSharpExamples#30 ·
-
难度 5/5 一周以上 新手友好度 25/100
dotnet/TorchSharpExamples#29 ·
查看 dotnet/TorchSharpExamples 的全部 Issue
相似的 Issue
-
bug module:classification
难度 2/5 1-3 小时 新手友好度 75/100
-
low AI-generated score register-submission
难度 2/5 1-3 小时 新手友好度 70/100
UKGovernmentBEIS/inspect_evals#2521 · 2 条评论 ·
-
bug
难度 2/5 1-3 小时 新手友好度 65/100
stanfordnlp/stanza#1683 · 2 条评论 ·
-
wer_score / mer_score / cer_score return 0 (a perfect lower-is-better score) for an empty prediction 未关闭
难度 2/5 1-3 小时 新手友好度 75/100
stanford-crfm/helm#4359 ·
-
难度 1/5 1 小时以内 新手友好度 90/100
ml-explore/mlx-lm#1915 · 1 条评论 ·