Cannot load Keras functional model

Hello,

I’m trying to run a simple example of inference with a Keras functional model. I’m getting a strange error: "

Exception in thread “main” org.deeplearning4j.nn.modelimport.keras.exceptions.InvalidKerasConfigurationException: Expected model class name Model (found Functional)

So while trying the load a functional model, it complains that the model class name is Functional instead of Model? The code is given below. BTW, I tried with sequential as well, and I get the expected error:

Exception in thread “main” org.deeplearning4j.nn.modelimport.keras.exceptions.InvalidKerasConfigurationException: Model class name must be Sequential (found Functional)

private static final String FILE_NAME = "model_300_900_xce1_2.37-0.12.h5";
private final ComputationGraph mModel;

public OpwClassifier() throws IOException, InvalidKerasConfigurationException,
        UnsupportedKerasConfigurationException {
    String fullModel = new File(FILE_NAME).getAbsolutePath();
    mModel = KerasModelImport.importKerasModelAndWeights(fullModel, false);
}

Any help would be greatly appreciated!
Thanks,
R.

When importing functional models, it expects them to be defined like in this example:
https://deeplearning4j.konduit.ai/keras-import/model-functional#getting-started-with-importing-keras-functional-models

Are you using DL4J version 1.0.0-beta7?

Also can you share the definition of the model?

Hello,

Sorry for the delay. So, I asked the person who generated the model, I can share the code used to write it:

import numpy as np
import tensorflow as tf
import pandas as pd
from tensorflow import keras

model = keras.models.load_model("model_300_900_xce1_2.37-0.12.h5")
model.summary()
model_json = model.to_json()  # save just the config. replace with "to_yaml" for YAML serialization
with open("model_config.json", "w") as f:
    f.write(model_json)
model.save("new_model.h5")
print("model saved")

I am using 1.0.0-beta7, yes. The error is still

Exception in thread "main" org.deeplearning4j.nn.modelimport.keras.exceptions.InvalidKerasConfigurationException: Expected model class name Model (found Functional)

I don’t know if it matters, but I think he’s using the Keras version that is shipped with the nightly build of Tensorflow (2.3).

The JSON export for the model is here.

It is very similar to this Transfer learning & fine-tuning that uses a sequential component for augmentation. Maybe that is the issue? In any regards, the error message is not very helpful :slight_smile:

Thanks!