TensorFlow.NET icon indicating copy to clipboard operation
TensorFlow.NET copied to clipboard

tf.keras.models.load_model() not implemented

Open Yandychang1 opened this issue 1 year ago • 7 comments

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

Yandychang1 avatar Jul 22 '23 01:07 Yandychang1

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?

Wanglongzhi2001 avatar Jul 22 '23 01:07 Wanglongzhi2001

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 avatar Jul 22 '23 02:07 Yandychang1

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.

Yandychang1 avatar Aug 08 '23 03:08 Yandychang1

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.

Wanglongzhi2001 avatar Aug 08 '23 03:08 Wanglongzhi2001

No worries, looking forward to it.

Yandychang1 avatar Aug 08 '23 14:08 Yandychang1

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

Wanglongzhi2001 avatar Oct 11 '23 15:10 Wanglongzhi2001

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.

Jucko13 avatar Oct 11 '23 16:10 Jucko13