SciSharp / TensorFlow.NET

.NET Standard bindings for Google's TensorFlow for developing, training and deploying Machine Learning models in C# and F#.
https://scisharp.github.io/tensorflow-net-docs
Apache License 2.0
3.17k stars 507 forks source link

tf.keras.models.load_model() not implemented #1152

Open Yandychang1 opened 1 year ago

Yandychang1 commented 1 year ago

Description

I have been trying to work with tensorflow.net; I can train and save a neural net. When I try to load it using tf.keras.models.load_model() method throws a " method not implemented" exception. Does anyone know if this method is implemented? Can I load a model the same way I saved it, or is this an issue that needs implementation?

TensorFlow.NET 0.110.2 TensorFlow.Keras 0.11.2 .NET 6 Windows 11

Wanglongzhi2001 commented 1 year ago

Hello, keras.models.load_model works fine in most cases. May I ask if you used some layers seldom used or use some layer implemented in TensorFlow.NET recently. Can you provide a minimal reproduction code?

Yandychang1 commented 1 year ago

i think this is the basic example you guys provide:

       var layers = keras.layers;
        // input layer
        var inputs = keras.Input(shape: (32, 32, 3), name: "img");
        // convolutional layer
        var x = layers.Conv2D(32, 3, activation: "relu").Apply(inputs);
        x = layers.Conv2D(64, 3, activation: "relu").Apply(x);
        var block_1_output = layers.MaxPooling2D(3).Apply(x);
        x = layers.Conv2D(64, 3, activation: "relu", padding: "same").Apply(block_1_output);
        x = layers.Conv2D(64, 3, activation: "relu", padding: "same").Apply(x);
        var block_2_output = layers.Add().Apply(new Tensors(x, block_1_output));
        x = layers.Conv2D(64, 3, activation: "relu", padding: "same").Apply(block_2_output);
        x = layers.Conv2D(64, 3, activation: "relu", padding: "same").Apply(x);
        var block_3_output = layers.Add().Apply(new Tensors(x, block_2_output));
        x = layers.Conv2D(64, 3, activation: "relu").Apply(block_3_output);
        x = layers.GlobalAveragePooling2D().Apply(x);
        x = layers.Dense(256, activation: "relu").Apply(x);
        x = layers.Dropout(0.2f).Apply(x);
        // output layer
        var outputs = layers.Dense(10).Apply(x);

        // build keras model
        var model = keras.Model(inputs, outputs, name: "toy_resnet");
        model.summary();
        // compile keras model in tensorflow static graph
        model.compile(optimizer: keras.optimizers.Adam(),
            loss: keras.losses.SparseCategoricalCrossentropy(from_logits: true),
            metrics: new[] { "acc" });
        // prepare dataset
        var ((x_train, y_train), (x_test, y_test)) = keras.datasets.cifar10.load_data();
        // normalize the input
        x_train = x_train / 255.0f;
        // training
        ICallback result = model.fit(x_train, y_train,
                                     batch_size: 64,
                                     epochs: 15,
                                     validation_split: 0.2f);
        model.save("./toy_resnet_model");
        var model1 = tf.keras.models.load_model("./toy_resnet_model", options: new LoadOptions());
        model1.summary();

Screenshot 2023-07-21 22 17 00

Yandychang1 commented 11 months ago

Was checking up on this issue, wondering if there is time estimate for a solution. I have been wanting to switch to Tensorflow.NET. Love how easy it would be to go from python code to C# code. Unfortunately the problems with saving and loading the models have kept me from committing to the switch.

Wanglongzhi2001 commented 11 months ago

I'm very sorry, because the developer responsible for this api is busy recently, so it may not be so fast, I will let you know once it is completed.

Yandychang1 commented 11 months ago

No worries, looking forward to it.

Wanglongzhi2001 commented 9 months ago

Hello, the reason for this bug is that there exists some problems when use merge layer such as add, substract etc when use load_model. And this bug has been solved in #1192

Jucko13 commented 9 months ago

What i also noticed is that if you provide the options parameter the load_model throws a NotImplementedException. Ommiting this parameter or passing null should be fine if you don't need it.