Unexpected problem in creating a tensor on saved model session.
System information
- OS Platform and Distribution: Linux Ubuntu 20.04 x64
- TensorFlow installed from: pip for Python 3.9; Maven for Java
- TensorFlow version : 2.7.0 on Python; 0.3.3 on Java
- Java version : openjdk 11.0.11 2021-04-20
- Python version : 3.9
- CUDA/cuDNN version: cuda_11.5.r11.5/compiler.30411180_0
- GPU model and memory: Nvidia Geforce 960m
Describe the current behavior The model (a Keras Sequential) is saved using python's "tf.saved_model.save". This model is then loaded in Java using SavedModelBundle from which the related graph is extracted. Then and Ops is created using this graph. When trying to create a tensor using .constant method, an exception is thrown on line 413, /core/op/Constant.java saying
Duplicate node name in graph: 'Const'
I checked my graph nodes using getNodeList of MetaGraph and there is only one node named "Const".
Describe the expected behavior Successful creation of Tensor as there is no duplicate in the graph nodes. Code to reproduce the issue
SavedModelBundle model = SavedModelBundle.load("/path/to/model");
Graph graph = model.graph();
MetaGraphDef mataGF = model.metaGraphDef();
var nodes = mataGF.getGraphDef().getNodeList().toArray();
Ops tf = Ops.create(graph);
double[] labels = {0.52,0.65,0.23,0.54,0.65,0.16,0.97};
var input = tf.constant(labels);
Other info / logs
Here is a list of all the 37 nodes. Only node 31 is called Const;

It's saying it's a duplicate because there's a const node in your python graph and the Java code is trying to use the same name. I think the node naming logic is different in the latest snapshot, so try that.
A workaround is to make a subscope with a name before creating any Java ops.
Thank's for your response; I confirm that creating a subscope solves the issue. I found out that on the same instance of Ops with subscope, you can define multiple consts as the makeUnique method of op/NameScope takes care of the similar name prefixes. As for input1, and input2 in the code below the NameScope prefixes are child/Const and child/Const_1 respectively. Nonetheless trying to create input3, an exception is raised:
Duplicate node name in graph: 'child/Const'
For the code:
Scope root = new Scope(graph);
Scope child = root.withSubScope("child");
Ops tfWithSubScope = Ops.create(graph).withSubScope("child");
var input1 = tfWithSubScope.constant(labels);
var input2 = tfWithSubScope.constant(labels);
var input3 = Constant.tensorOf(child,labels);
Isn't it better to use a naming logic for NameScopes to avoid such problems? Or is it expected that all operations be executed using the same Ops?
The naming and scoping logic has been rewritten in the current master branch, and that will come with the 0.4.0 release. Can you check the 0.4.0-SNAPSHOT releases to see if they fix that issue? Otherwise we can look at the naming logic again.