给定 tensorflow 冻结推理图的输入,我想提取pbtxt文件。为此,我使用以下脚本:
import tensorflow as tf
#from google.protobuf import text_format
from tensorflow.python.platform import gfile
def converter(filename):
with gfile.FastGFile(filename,'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def, name='')
tf.train.write_graph(graph_def, 'pbtxt/', 'protobuf.pbtxt', as_text=True)
print(graph_def)
return
#converter('ssd_mobilenet_v1_coco_2017_11_17/frozen_inference_graph.pb') # here you can write the name of the file to be converted
# and then a new file will be made in pbtxt directory.
converter('ssd_mobilenet_v1_coco_2017_11_17/frozen_inference_graph.pb')
例如,我正在使用ssd mobilenet体系结构。使用上面的代码,我得到的输出为pbtxt,但是我不能使用它。供参考,请参见下图当我使用右侧的Official pbtxt时,我会得到正确的结果。但是,当我使用上面的脚本生成的LEFT pbtxt时,没有得到任何预测
我在开放式简历DNN模块上使用这些预测
tensorflowNet = cv2.dnn.readNetFromTensorflow('ssd_mobilenet_v1_coco_2017_11_17/frozen_inference_graph.pb', 'pbtxt/protobuf.pbtxt')
如何将mobilenet冻结的推理图转换为正确的pbtxt格式,以便进行推理?参考文献:
https://gist.github.com/Arafatk/c063bddb9b8d17a037695d748db4f592
最佳答案
这对我有用
并运行以下脚本:
python3 tf_text_graph_ssd.py --input frozen_inference_graph.pb --output exported_pbtxt/output.pbtxt --config pipeline.config
这就是您所需要的,现在复制冻结的推理图和新生成的pbtxt文件。并且,使用以下脚本通过OpenCV运行模型:
import cv2
# Load a model imported from Tensorflow
tensorflowNet = cv2.dnn.readNetFromTensorflow('card_graph/frozen_inference_graph.pb', 'exported_pbtxt/output.pbtxt')
# Input image
img = cv2.imread('image.jpg')
rows, cols, channels = img.shape
# Use the given image as input, which needs to be blob(s).
tensorflowNet.setInput(cv2.dnn.blobFromImage(img, size=(300, 300), swapRB=True, crop=False))
# Runs a forward pass to compute the net output
networkOutput = tensorflowNet.forward()
# Loop on the outputs
for detection in networkOutput[0,0]:
score = float(detection[2])
if score > 0.9:
left = detection[3] * cols
top = detection[4] * rows
right = detection[5] * cols
bottom = detection[6] * rows
#draw a red rectangle around detected objects
cv2.rectangle(img, (int(left), int(top)), (int(right), int(bottom)), (0, 0, 255), thickness=2)
# Show the image with a rectagle surrounding the detected objects
cv2.imshow('Image', img)
cv2.waitKey()
cv2.destroyAllWindows()
关于python - 无法将tensorflow卡住图转换为pbtxt文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57007707/