【发布时间】:2017-08-15 17:04:18
【问题描述】:
我已经将预训练的 .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 the unserialized graph_drf
with gfile.FastGFile("/path-to-FCN-model/FCN8.pb",'rb') as f:
#Set default graph as current graph
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
#sess.graph.as_default() #new line
# Import a graph_def into the current default Graph
tf.import_graph_def(graph_def, name='')
# Print the name of operations in the session
#for op in sess.graph.get_operations():
#print "Operation Name :",op.name # Operation name
#print "Tensor Stats :",str(op.values()) # Tensor name
# INFERENCE Here
l_input = graph.get_tensor_by_name('Placeholder:0')
l_output = graph.get_tensor_by_name('save/Assign_38:0')
print "l_input", l_input
print "l_output", l_output
print
print
# Acceptable feed values include Python scalars, strings, lists, numpy ndarrays, or TensorHandles.
result = sess.run(l_output, feed_dict={l_input : img})
print(results)
print("Inference done")
# Info
# First Tensor name : Placeholder:0
# Last tensor name : save/Assign_38:0"
错误是否来自图像格式(例如,我应该将 .png 转换为另一种格式吗?)。这是另一个根本性错误吗?
【问题讨论】:
标签: python tensorflow