tensorflow/java

org.tensorflow.TensorFlowException: Can't parse /<modelPath>/<somePathToFolder>/saved_model.pb as binary proto - JDK 17

17patelumang opened this issue · 15 comments

Code:

import com.google.common.io.Resources;
import org.junit.Test;
import org.tensorflow.SavedModelBundle;
import org.tensorflow.Session;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Paths;

public class TestClass {

    @Test
    public void TFPredictor() throws IOException, URISyntaxException {
        SavedModelBundle b = SavedModelBundle.load("/<modelPath>/<somePathToFolder>", "serve");
        Session sess = b.session();
    }

}

/<modelPath>/<somePathToFolder>/
Has following files:

  1. assets/vocab.txt
  2. saved_model.pb
  3. variables/variables.data-00000-of-00001
  4. variables/variables.index

I am using java binding code to run the inference. Libraries I am using are

<groupId>org.tensorflow</groupId>
<artifactId>tensorflow</artifactId>
<artifactId>1.15.0</artifactId>


<groupId>org.tensorflow</groupId>
<artifactId>libtensorflow</artifactId>
<artifactId>1.15.0</artifactId>


<groupId>org.tensorflow</groupId>
<artifactId>proto</artifactId>
<artifactId>1.15.0</artifactId>

<groupId>org.tensorflow</groupId>
<artifactId>libtensorflow_jni</artifactId>
<artifactId>1.15.0</artifactId>

TensorFlow version - 1.15.0
JDK - Oracle Open Jdk Version 17.0.5
OS - RHEL 8 version, 8.7.5
Describe the current behavior

org.tensorflow.TensorFlowException: Can't parse </modelPath>/<somePathToFolder>/saved_model.pb as binary proto
	at app//org.tensorflow.SavedModelBundle.load(Native Method)
	at app//org.tensorflow.SavedModelBundle.access$000(SavedModelBundle.java:27)
	at app//org.tensorflow.SavedModelBundle$Loader.load(SavedModelBundle.java:32)
	at app//org.tensorflow.SavedModelBundle.load(SavedModelBundle.java:95)
	at app//com.main.java.main.tensorflow.TestClass.TFPredictor(TestClass.java:19)

Describe the expected behavior
In JDK 11, the code runs while in JDK 17 its throws error. The model file is not corrupted & same model path file is able to run successfully in JDK 11

The TF v1 Java API isn't supported anymore. Do you still have this issue with the new API from this repo?

Which new API i should use ?, Also savedModel is in TF 1.15.x version so should we use new version of all 4 libraries i mentioned in ticket ?

You should add the appropriate dependencies for your platform as mentioned in the README, and remove all dependencies on TF v1.15 jars. The current version is v0.5.0 which uses TF 2.10.1.

@Craigacp model training in 1.15.x should work n TF 2.10.0 since 1.15.x is middle ground between 1.x & 2.x ?

A model from v1.15 should load without error unless it's using something that was removed in v2.

@Craigacp I tried , I got below error
Now i am only using "org.tensorflow:tensorflow-core-platform:0.5.0"

Running test: Test TFPredictor
I external/org_tensorflow/tensorflow/cc/saved_model/reader.cc:45] Reading SavedModel from: <some_path>
I external/org_tensorflow/tensorflow/cc/saved_model/loader.cc:305] SavedModel load for tags { serve }; Status: fail: DATA_LOSS: Can't parse <some_path>/saved_model.pb as binary proto. Took 286 microseconds.

TFPredictor FAILED
org.tensorflow.exceptions.TensorFlowException: Can't parse <some_path>/saved_model.pb as binary proto
    at app//org.tensorflow.internal.c_api.AbstractTF_Status.throwExceptionIfNotOK(AbstractTF_Status.java:101)
    at app//org.tensorflow.SavedModelBundle.load(SavedModelBundle.java:591)
    at app//org.tensorflow.SavedModelBundle$Loader.load(SavedModelBundle.java:92)
    at app//org.tensorflow.SavedModelBundle.load(SavedModelBundle.java:336)

And you still get the same issue where the model loads fine on Java 11 but doesn't load with 17? If so, what's the exact version of each JDK, and are you modifying any other dependencies (or moving from the classpath to the module path)?

Is the path a path on disk or to a classpath/modulepath resource?

Jdk 17 - jdk 17.0.5
jdk 11 - 11.0.4
For jdk 17 - for the time being i am storing the model on git, for jdk 11 - for the time being model is also stored on git. Not modifying any dependency . Is the path a path on disk or to a classpath/modulepath resource? - No

So the model is on the local disk then? TF-Java can't load models out of a remote git repository.

It's very surprising that the native code would fail due to differing Java versions. Do you have anything else that would manipulate the JVM on the classpath?

yes model is on local disk. Nothing else is manipulating path.

Is the path using non-ASCII characters? I'm wondering if there was a change in the string encoding between 11 and 17, and the path is being mangled by something before it hits the TF native code.

i checked that there is no non-ASCII character path

Ok. Can you make the model files available somewhere?

We cannot make model files available, however let me try to convert model to TF 2.x and get back. It will take few days from my side. Will reply on this thread if needed. Thank you for the help @Craigacp

This is resolved, issue was .gitattribute file. It was replacing CRLF to LF thus corrupting the variables.data-00000-of-00001. Thanks for the help.