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

Failing to Load Saved Model #105

Open
Kezyma opened this issue Aug 1, 2022 · 0 comments
Open

Failing to Load Saved Model #105

Kezyma opened this issue Aug 1, 2022 · 0 comments

Comments

@Kezyma
Copy link

Kezyma commented Aug 1, 2022

I'm finding the library very useful, however I've noticed that the TryLoad for loading a saved model fails and always returns null.

I'm training and saving the model with the below code;

var result = NetworkManager.TrainNetwork(
                network,                                                  
                trainingDataSet,                                           
                TrainingAlgorithms.AdaDelta(),                                 
                _epochsToRun,                                               
                _dropoutProb,                                      
                null,                                                        
                (prog) =>
                {
                    _currentTrainAccuracy = prog.Result.Accuracy;
                    _currentTrainCost = prog.Result.Cost;
                },                                                          
                null,                                                            
                testingDataSet,                                                
                cancellationToken.ShutdownToken);                                 

var fileInfo = new FileInfo(_settings.NeuralNetworkFilePath);
network.Save(fileInfo);

And then loading with the following;

var fileInfo = new FileInfo(_settings.NeuralNetworkFilePath);
var model = NetworkLoader.TryLoad(fileInfo, ExecutionModePreference.Cpu);

However, the model is always null, regardless of the file I try to load.

I've taken a look at the serialization and deserialization and noticed the issue occurs in the FullyConnectedLayer's Deserialize function.

public static INetworkLayer Deserialize([NotNull] Stream stream)
        {
            if (!stream.TryRead(out TensorInfo input)) return null;
            if (!stream.TryRead(out TensorInfo output)) return null;
            if (!stream.TryRead(out ActivationType activation)) return null;
            if (!stream.TryRead(out int wLength)) return null;
            float[] weights = stream.ReadUnshuffled(wLength);
            if (!stream.TryRead(out int bLength)) return null;
            float[] biases = stream.ReadUnshuffled(bLength);
            return new FullyConnectedLayer(input, output.Size, weights, biases, activation);
        }

I monitored the bytes being stored in the serialization for the lengths of the weights and bias arrays, and noticed that the wLength value is correctly read from the stream, but when it gets to reading the bLength value, the bytes read are entirely different to those that were recorded and an exception is thrown because bLength becomes an absurdly large or small number and an array of that size isn't possible to create.

My assumption is that there's some issue with the reading of weights that ends up moving the stream position to the wrong place, but streams aren't exactly something I'm overly used to working with!

I'm aware this project is no longer maintained, but if anyone could suggest a workaround or fix, it'd certainly be helpful and very much appreciated!

I was able to quickly reproduce the problem by creating a new CLI project and using the following Program.cs;

using NeuralNetworkNET.APIs;
using NeuralNetworkNET.APIs.Structs;

var testNet = NetworkManager.NewSequential(TensorInfo.Linear(505),
    NetworkLayers.FullyConnected(14, NeuralNetworkNET.APIs.Enums.ActivationType.ReLU),
    NetworkLayers.Softmax(2));

testNet.Save(new FileInfo("D:\\TestNet.nnet"));

var file = "D:\\TestNet.nnet";
var fInfo = new FileInfo(file);
var model = NetworkLoader.TryLoad(fInfo, NeuralNetworkNET.APIs.Enums.ExecutionModePreference.Cpu);
var res = model != null;
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

1 participant