0

I'm new to Tensorflow and I'm trying to import a frozen graph (.pb file) that was trained in Python into a Java project using Deeplearning4j.

It seems that the model was saved successfully and it is working in Python, but when I try to import it with DL4J I'm getting the following issue and I don't know why:

Exception in thread "main" java.lang.IllegalStateException: Could not find class for TF Ops: TensorListFromTensor
at org.nd4j.common.base.Preconditions.throwStateEx(Preconditions.java:639)
at org.nd4j.common.base.Preconditions.checkState(Preconditions.java:301)
at org.nd4j.imports.graphmapper.tf.TFGraphMapper.importGraph(TFGraphMapper.java:283)
at org.nd4j.imports.graphmapper.tf.TFGraphMapper.importGraph(TFGraphMapper.java:141)
at org.nd4j.imports.graphmapper.tf.TFGraphMapper.importGraph(TFGraphMapper.java:87)
at org.nd4j.imports.graphmapper.tf.TFGraphMapper.importGraph(TFGraphMapper.java:73)
at MLModel.loadModel(MLModel.java:30)

This is my model in Python:

def RNN():
inputs = tf.keras.layers.Input(name='inputs',shape=[max_len])
layer = tf.keras.layers.Embedding(max_words,50,input_length=max_len)(inputs)
layer = tf.keras.layers.LSTM(64)(layer)
layer = tf.keras.layers.Dense(256,name='FC1')(layer)
layer = tf.keras.layers.Activation('relu')(layer)
layer = tf.keras.layers.Dropout(0.5)(layer)
layer = tf.keras.layers.Dense(12,name='out_layer')(layer)
layer = tf.keras.layers.Activation('softmax')(layer)
model = tf.keras.models.Model(inputs=inputs,outputs=layer)
return model

Actually I based on this blog how to export the model: Save, Load and Inference From TensorFlow 2.x Frozen Graph

And this is how I'm trying to import the model in Java with DeepLearning4J:

public static void loadModel(String filepath) throws Exception{
    File file = new File(filepath);
    if (!file.exists()){
        file = new File(filepath);
    }

    sd = TFGraphMapper.importGraph(file);

    if (sd == null) {
        throw new Exception("Error loading model : " + file);
    }
}

I'm getting the exception in sd = TFGraphMapper.importGraph(file);

Does anyone know if I'm missing something?

1 Answers1

0

That is the old model import. Please use the new one. The old one is not and will not be supported. You can find that here:

https://deeplearning4j.konduit.ai/samediff/explanation/model-import-framework Both tensorflow and onnx work similarly. For tensorflow use:

  //create the framework importer
   TensorflowFrameworkImporter tensorflowFrameworkImporter = new TensorflowFrameworkImporter();
   File pathToPbFile = ...;
   SameDiff graph = tensorflowFrameworkImporter.runImport(pathToPbFile.getAbsolutePath(),Collections.emptyMap());

File an issue on the github repo: https://github.com/deeplearning4j/deeplearning4j/issues/new if something doesn't work for you.

Also note that if you use the tf keras api you can also import it using the keras hdf5 format (the old one).

For many graphs, you may also need to save the model and freeze it. You can use that here:

def convert_saved_model(saved_model_dir) -> GraphDef:
    """
    Convert the saved model (expanded as a directory)
    to a frozen graph def
    :param saved_model_dir: the input model directory
    :return:  the loaded graph def with all parameters in the model
    """
    saved_model = tf.saved_model.load(saved_model_dir)
    graph_def = saved_model.signatures['serving_default']
    frozen = convert_variables_to_constants_v2(graph_def)
    return frozen.graph.as_graph_def()

We publish more code and utilities for that kind of thing here: https://github.com/deeplearning4j/deeplearning4j/tree/master/contrib/omnihub/src/omnihub/frameworks

Adam Gibson
  • 3,055
  • 1
  • 10
  • 12