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
【JavaAPI】IllegalStateException happended while running a model loading from SavedModel and the graph instance cant close itself #51648
Comments
Hi @SennriSyunnga !we see that you are using old version of Tensorflow which is officially considered as end of life, We recommend that you upgrade to 2.6 version and let us know if the issue still persists in newer versions .Please open a new issue in case you face any errors, we will get you the right help .Thanks! |
Thank you for your help! |
I fixed this problem by learning from this issue[https://github.com/tensorflow/java/issues/365] after using a higher version api: <dependency>
<groupId>org.tensorflow</groupId>
<artifactId>tensorflow-core-api</artifactId>
<version>0.3.1</version>
</dependency>
<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>tensorflow-core-api</artifactId>
<version>0.3.1</version>
<classifier>linux-x86_64</classifier>
</dependency> In this version of artifact, I finnaly can use session.init in Java, like other ones who solved the problem did in python public void test_10_justTestAPI() {
float[] a = new float[]{1.53672f, 2.047399f, 1.42194f, 1.494959f, -0.69123f, -0.39482f, 0.236573f, 0.733827f, -0.531855f, -0.973978f, 1.704854f, 2.085134f, 1.615931f, 1.723842f, 0.102458f, -0.017833f, 0.693043f, 1.263669f, -0.217664f, -1.058611f, 1.300499f, 2.260938f, 1.156857f, 1.291565f, -0.42401f, -0.069758f, 0.252202f, 0.808431f, -0.189161f, -0.490556f};
try (SavedModelBundle savedModelBundle = SavedModelBundle.load(".", "serve")) {
FloatNdArray m = StdArrays.ndCopyOf(new float[][]{a});
try (TFloat32 data = TFloat32.tensorOf(m)) {
try (Session session = savedModelBundle.session()) {
SignatureDef modelInfo = savedModelBundle.metaGraphDef().getSignatureDefMap().get("serving_default");
Map<String, TensorInfo> inputs = modelInfo.getInputsMap();
String inputName = null;
for (Map.Entry<String, TensorInfo> input : inputs.entrySet()) {
TensorInfo ti = input.getValue();
inputName = ti.getName();
break;
}
String outputName = null;
Map<String, TensorInfo> outputs = modelInfo.getOutputsMap();
for (Map.Entry<String, TensorInfo> output : outputs.entrySet()) {
outputName = output.getValue().getName();
break;
}
session.run("init");
Session.Runner runner = session.runner();
runner.feed(inputName, data)
.fetch(outputName);
try (TFloat32 out = (TFloat32) runner.run().get(0)) {
FloatNdArray matrix = StdArrays.ndCopyOf(new float[1][1]);
out.copyTo(matrix);
FloatDataBuffer floatDataBuffer = DataBuffers.ofFloats(1);
matrix.read(floatDataBuffer);
float[] res = new float[1];
floatDataBuffer.read(res);
BigDecimal pro = BigDecimal.valueOf(res[0]);
}
}
}
}catch (Exception e) {
logger.error(e);
throw e;
}
} Soon after that, I found out the reason for why graph instance failed to close: SavedModelBundle savedModelBundle = SavedModelBundle.load(".", "serve");
Graph graph = savedModelBundle.graph();
Tensor<Float> data = Tensor.create(shape, FloatBuffer.wrap(a));
Session session = new Session(graph);// ← this line In the correct way, I should use savedModelBundlesession() directly. Update |
System information
Describe the current behavior
I try to load a Saved Model from keras. It all works well till I try to run the session——
Strange thing occurs: the code just can't continue and throw no exception.
When i use 'try catch finally' style instead of 'try with resource' style, i finally got such error message below:
And what's more, though I got the error message, the graph instance can't close itself,
when I paused the test code in idea, i found the code stop at the Object.wait() method,
which means that the graph.refcount kept 1 value all the time.
The code couldn't escape from graph.close() method.
To prove the correctness of saved model, I try to load model in python code like below:
It works well and print predict result, in that case, I think the problem may not lie in the model. (maybe?)
I tried hard to find solution or workaround on stackoverflow and issues here,
I saw several similar problems to mine, but they all occurs in python, such as :
#28287
and
#22362
the second issues seems most alike, but the model export method is different.
Standalone code to reproduce the issue
Here is my model:
model.zip
Here is the test code, because it fails all over the time, i ommit the code to close the resources.
Other info / logs
The model is produced by webank federal learining program,
In their code, the model is build by code using keras api:
The json content is definded like:
the model is saved by code below:
You can check the code in this link :
https://github.com/FederatedAI/FATE/blob/master/python/federatedml/nn/backend/tf_keras/nn_model.py
In case, here is the log of my test code:
I really stuck on this problem.
I would appreciate it if someone can help me out, many thanks!
The text was updated successfully, but these errors were encountered: