Given a tensor flow model graph, how to find the input node and output node names
Try this:
run python
>>> import tensorflow as tf
>>> gf = tf.GraphDef()
>>> gf.ParseFromString(open('/your/path/to/graphname.pb','rb').read())
and then
>>> [n.name + '=>' + n.op for n in gf.node if n.op in ( 'Softmax','Placeholder')]
Then, you can get result similar to this:
['Mul=>Placeholder', 'final_result=>Softmax']
But I'm not sure it's the problem of node names regarding the error messages.
I guess you provided wrong arguements when loading the graph file or your generated graph file is something wrong?
Check this part:
E/AndroidRuntime(16821): java.lang.IllegalArgumentException: Incompatible
shapes: [1,224,224,3] vs. [32,1,1,2048]
UPDATE:
Sorry,
if you're using (re)trained graph , then try this:
[n.name + '=>' + n.op for n in gf.node if n.op in ( 'Softmax','Mul')]
It seems that (re)trained graph saves input/output op name as "Mul" and "Softmax", while optimized and/or quantized graph saves them as "Placeholder" and "Softmax".
BTW, using retrained graph in mobile environment is not recommended according to Peter Warden's post: https://petewarden.com/2016/09/27/tensorflow-for-mobile-poets/ . It's better to use quantized or memmapped graph due to performance and file size issue, I couldn't find out how to load memmapped graph in android though...:(
(no problem loading optimized / quantized graph in android)
Get input and output node name from .ckpt and .meta files tensorflow
The .meta
file contains information about the different node in the tensorflow graph. This has been better explained here.
The values of the different variables in the graph at that moment are stored separately in the checkpoint folder in checkpoint.data-xxxx-of-xxxx
file.
There is no concept of an input or output node in the normal checkpoint process, as opposed to the case of a frozen model. Freezing a model outputs a subset of the whole tensorflow graph. This subset of the main graph has only those nodes present on which the output node is dependent on. Because freezing a model is done for serving purposes, it converts the tensorflow variables to constants, eliminating the need for storing additional information like gradients of the different variables at each step.
If you still want to identify the nodes you would be interested in, you can restore your graph from the .meta
file and visualize it in tensorboard.
import tensorflow as tf
from tensorflow.summary import FileWriter
sess = tf.Session()
tf.train.import_meta_graph("your-meta-graph-file.meta")
FileWriter("__tb", sess.graph)
This will create a __tb
folder in your current directory and you can then view the graph by issuing the following command.
tensorboard --logdir __tb
Here is a link to the screenshot of some model with a node selected. You can get the name of the node from the top right corner.
How to find the exact name of the output node in .pb file?
Question 26 in the ‘Model Optimizer Frequently Asked Questions’ page addresses the error that you are facing. See https://docs.openvinotoolkit.org/2020.3/_docs_MO_DG_prepare_model_Model_Optimizer_FAQ.html
I would suggest you try the following potential methods that have been shared in Github, available at the following links:
https://github.com/tensorflow/tensorflow/issues/3986#issuecomment-304619868
https://github.com/tensorflow/tensorflow/issues/3986#issuecomment-368282326
https://github.com/tensorflow/tensorflow/issues/3986#issuecomment-470083664
https://github.com/tensorflow/tensorflow/issues/3986#issuecomment-508897579Do let us know if any of these works for you.
What are input and output node names in inception v3 with slim library?
(A). If you will make it to bottom of this link. You would find this somewhere(specific to inceptionV3) :
input_layer=input
output_layer=InceptionV3/Predictions/Reshape_1
(B). Another way is to print all tensors of the model and get input/output tensor
from tensorflow.python.tools.inspect_checkpoint import print_tensors_in_checkpoint_file
ckpt_path="model.ckpt"
print_tensors_in_checkpoint_file(file_name=ckpt_path, tensor_name='', all_tensors=True, all_tensor_names=True)
(C). If you need to print tensor names of .pb file. You can use this simple code.
Check what would work for you.
Related Topics
Android: How to Create Fade-In/Fade-Out Sound Effects for Any Music File That My App Plays
Retrofit2: Modifying Request Body in Okhttp Interceptor
Saving Arraylists in SQLite Databases
Android: How to Update a Imageview/Imagebutton with a Number to Show the Number of New Messages
Practical Way to Find Out If Sms Has Been Sent
Dagger 2 Injecting Android Application Context
How to Know If a Fragment Is Visible
How to Use Default Android Drawables
Start Google Hangout from Intent in New Hangouts Android App
How to Detect Bottom Soft Navigation Bar Available in Android Programmatically
How to Use Tabhost.Ontabchangelistener in Android
Android:Dither="True" Does Not Dither, What's Wrong
How to Close the Status Bar/Notification Panel After Notification Button Click
Why Is Calling Process.Killprocess(Process.Mypid()) a Bad Idea
Java.Io.Ioexception: Unexpected End of Stream on Connection in Android
What Is the Android UI Thread Stack Size Limit and How to Overcome It
Android Error - Caused By: Java.Lang.Noclassdeffounderror: Android.Support.V4.Util.Sparsearraycompat