如何找到已冻结模型的输入和输出节点

15
3个回答

1

使用tensorflow对象检测API保存的所有模型都将 image_tensor 作为输入节点名称。 目标检测模型有4个输出:

  1. num_detections:预测给定图像的检测数
  2. detection_classes:模型训练的类别数
  3. detection_boxes:预测(ymin,xmin,ymax,xmax)坐标
  4. detection_scores:预测每个类的置信度,应选择具有最高预测值的类别

用于saved_model推理的代码

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']

1

我认为你可以使用以下代码。我从这里下载了ssd_mobilenet_v1_coco冻结模型,并能够获取如下所示的输入和输出名称。

!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 name = 
detection_classes

Input name = 
image_tensor

请查看此处的代码片段


0
你可以直接执行 model.summary() 来查看所有层的名称(以及它们的类型)。这是第一列。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接