How to find the Input and Output Nodes of a Frozen Model
Asked Answered
S

3

15

I want to use tensorflow's optimize_for_inference.py script on a frozen Model from the model zoo: the ssd_mobilenet_v1_coco.

How do i find/determine the names of the input and output name of the model?

Here is a link to the graph generated by tensorboard

Hires version of the graph generated by tensorboard

This question might help: Given a tensor flow model graph, how to find the input node and output node names (for me it did not)

Shepard answered 11/1, 2018 at 14:57 Comment(0)
L
1

I think you can do using the following code. I downloaded ssd_mobilenet_v1_coco frozen model from here and was able to get the input and output names as shown below

!pip install tensorflow==1.15.5

import tensorflow as tf
tf.__version__ # TF1.15.5

gf = tf.GraphDef()  
m_file = open('/content/frozen_inference_graph.pb','rb')
gf.ParseFromString(m_file.read())
 
with open('somefile.txt', 'a') as the_file:
   for n in gf.node:
       the_file.write(n.name+'\n')
 
file = open('somefile.txt','r')
data = file.readlines()
print("output name = ")
print(data[len(data)-1])
 
print("Input name = ")
file.seek ( 0 )
print(file.readline())

Output is

output name = 
detection_classes

Input name = 
image_tensor

Please check the gist here.

Lutyens answered 17/11, 2021 at 23:51 Comment(0)
M
1

all the models saved using tensorflow object detection api have image_tensor as the input node name. Object detection model has 4 outputs:

  1. num_detections : Predicts the number of detection for a given image
  2. detection_classes: Number of classes that the model is trained on
  3. detection_boxes : predicts (ymin, xmin, ymax, xmax) coordinates
  4. detection_scores : predicts the confidence for each class, the class which has the highest prediction should be selected

code for saved_model inference

def load_image_into_numpy_array(path):
    'Converts Image into numpy array'
    img_data = tf.io.gfile.GFile(path, 'rb').read()
    image = Image.open(BytesIO(img_data))
    im_width, im_height = image.size
    return np.array(image.getdata()).reshape((im_height, im_width, 3)).astype(np.uint8)


# Load saved_model 
model = tf.saved_model.load_model('custom_mode/saved_model',tags=none)

# Convert image into numpy array
numpy_image = load_image_into_numpy_array('Image_path')

# Expand dimensions
input_tensor = np.expand_dims(numpy_image, 0)

# Send image to the model
model_output = model(input_tensor)
# Use output_nodes to predict the outputs 
num_detections = int(model_output.pop('num_detections'))
detections = {key: value[0, :num_detections].numpy()
              for key, value in detections.items()}

detections['num_detections'] = num_detections
detections['detection_classes'] = detections['detection_classes'].astype(np.int64)
boxes = detections['detection_boxes']
scores = detections['detection_scores']
pred_class = detections['detection_classes']
Marginalia answered 11/3, 2022 at 19:30 Comment(0)
G
-1

you can just do model.summary() to see all the Layer names (and also their type). It is the first column.

Garth answered 28/10, 2022 at 8:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.