Skip to content

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

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

Open
Yandychang1 opened this issue Jul 22, 2023 · 7 comments
Open

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

Yandychang1 opened this issue Jul 22, 2023 · 7 comments
Labels
bug Something isn't working

Comments

@Yandychang1
Copy link

Yandychang1 commented Jul 22, 2023

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
Copy link
Collaborator

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
Copy link
Author

Yandychang1 commented Jul 22, 2023

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

@Wanglongzhi2001 Wanglongzhi2001 added the bug Something isn't working label Jul 22, 2023
@Yandychang1
Copy link
Author

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
Copy link
Collaborator

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
Copy link
Author

No worries, looking forward to it.

@Wanglongzhi2001
Copy link
Collaborator

Wanglongzhi2001 commented Oct 11, 2023

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
Copy link
Contributor

Jucko13 commented Oct 11, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants