我已设法使用此脚本将预先训练的.ckpt模型转换为.pb(protobuf)格式:
import os
import tensorflow as tf
# Get the current directory
dir_path = os.path.dirname(os.path.realpath(__file__))
print "Current directory : ", dir_path
save_dir = dir_path + '/Protobufs'
graph = tf.get_default_graph()
# Create a session for running Ops on the Graph.
sess = tf.Session()
print("Restoring the model to the default graph ...")
saver = tf.train.import_meta_graph(dir_path + '/model.ckpt.meta')
saver.restore(sess,tf.train.latest_checkpoint(dir_path))
print("Restoring Done .. ")
print "Saving the model to Protobuf format: ", save_dir
#Save the model to protobuf (pb and pbtxt) file.
tf.train.write_graph(sess.graph_def, save_dir, …Run Code Online (Sandbox Code Playgroud) 我已经将预训练的 .ckpt 文件转换为 .pb 文件,冻结模型并保存重量。我现在要做的是使用该 .pb 文件进行简单的推断,然后提取并保存输出图像。该模型是从这里下载的(用于语义分割的完全卷积网络):https : //github.com/MarvinTeichmann/KittiSeg。到目前为止,我已经设法加载图像,设置默认的 tf 图并导入模型定义的图,读取输入和输出张量并运行会话(此处为错误)。
import tensorflow as tf
import os
import numpy as np
from tensorflow.python.platform import gfile
from PIL import Image
# Read the image & get statstics
img=Image.open('/path-to-image/demoImage.png')
img.show()
width, height = img.size
print(width)
print(height)
#Plot the image
#image.show()
with tf.Graph().as_default() as graph:
with tf.Session() as sess:
# Load the graph in graph_def
print("load graph")
# We load the protobuf file from the disk and parse it to retrive …Run Code Online (Sandbox Code Playgroud)