我想在模型动物园中使用tensorflow的optimize_for_inference.py
脚本,用于冻结的模型: ssd_mobilenet_v1_coco
。
如何找到/确定模型的输入和输出名称?
这个问题可能会有所帮助:给定一个TensorFlow模型图,如何找到输入节点和输出节点名称(对我没有帮助)。
我想在模型动物园中使用tensorflow的optimize_for_inference.py
脚本,用于冻结的模型: ssd_mobilenet_v1_coco
。
如何找到/确定模型的输入和输出名称?
这个问题可能会有所帮助:给定一个TensorFlow模型图,如何找到输入节点和输出节点名称(对我没有帮助)。
使用tensorflow对象检测API保存的所有模型都将 image_tensor 作为输入节点名称。 目标检测模型有4个输出:
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']
我认为你可以使用以下代码。我从这里下载了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
请查看此处的代码片段。
model.summary()
来查看所有层的名称(以及它们的类型)。这是第一列。