Loading Python Exported Model into TorchSharp
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 32/100
- Tipo di issue
- Bug
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Ferma
- Ambito
- machine-learning
Direzione di ricerca
Inizia da docfx/articles/saveload.md e src/Python/exportsd.py, quindi esamina il modello ResNet.cs referenziato da TorchSharpExamples. Confronta i nomi del state_dict Python con i nomi dei moduli generati da TorchSharp e riproduci il fallimento di load() usando l’esempio ResNet18. Il lavoro è completato quando un modello ResNet esportato da Python viene caricato correttamente nell’esempio senza le eccezioni di nome o di serializzazione segnalate.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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
- Lingua principale
- Jupyter Notebook
- Stelle
- 175
- Fork
- 61
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di dotnet/TorchSharpExamples
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 45/100
dotnet/TorchSharpExamples#56 ·
-
Trying Chinese translation Aperta
Difficoltà 5/5 Più di una settimana Idoneità per principianti 20/100
dotnet/TorchSharpExamples#52 ·
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 45/100
dotnet/TorchSharpExamples#50 ·
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 20/100
dotnet/TorchSharpExamples#30 ·
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
dotnet/TorchSharpExamples#29 ·
Tutte le issue di dotnet/TorchSharpExamples
Issue simili
-
bug configuration documentation examples policies processor tests training
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
huggingface/lerobot#4727 ·
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 86/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
huggingface/sentence-transformers#4068 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
DataTalksClub/machine-learning-zoomcamp#723 · 1 reazione ·