【发布时间】:2018-01-26 14:47:10
【问题描述】:
我正在尝试使用两种不同的 mobilenet 模型。以下是我如何初始化模型的代码。
def initialSetup():
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
start_time = timeit.default_timer()
# This takes 2-5 seconds to run
# Unpersists graph from file
with tf.gfile.FastGFile('age/output_graph.pb', 'rb') as f:
age_graph_def = tf.GraphDef()
age_graph_def.ParseFromString(f.read())
tf.import_graph_def(age_graph_def, name='')
with tf.gfile.FastGFile('output_graph.pb', 'rb') as f:
gender_graph_def = tf.GraphDef()
gender_graph_def.ParseFromString(f.read())
tf.import_graph_def(gender_graph_def, name='')
print ('Took {} seconds to unpersist the graph'.format(timeit.default_timer() - start_time))
由于两者都是两个不同的模型,我该如何使用它进行预测?
更新
initialSetup()
age_session = tf.Session(graph=age_graph_def)
gender_session = tf.Session(graph=gender_graph_def)
with tf.Session() as sess:
start_time = timeit.default_timer()
# Feed the image_data as input to the graph and get first prediction
softmax_tensor = age_session.graph.get_tensor_by_name('final_result:0')
print ('Took {} seconds to feed data to graph'.format(timeit.default_timer() - start_time))
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
错误
Traceback(最近一次调用最后一次):文件 “C:/Users/Desktop/untitled/testimg/testimg/combo.py”,第 48 行,在 age_session = tf.Session(graph=age_graph_def) 文件 "C:\Program Files\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", 第 1292 行,在 init 中 super(Session, self).init(target, graph, config=config) File "C:\Program 文件\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", 第 529 行,在 init 中 raise TypeError('graph must be a tf.Graph, but got %s' % type(graph)) TypeError: graph must be a tf.Graph, but got Exception ignored in: > Traceback(最近一次调用最后一次):文件 "C:\程序 文件\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", 第 587 行,在 del 如果 self._session 不是 None: AttributeError: 'Session' object has no attribute '_session'
【问题讨论】:
-
您是否成功使用过这种方式加载的单个模型?通常的方法是将不同的非空
name参数传递给每个tf.import_graph_def()调用,然后使用这些名称作为您要提供和获取的每个模型中特定张量的前缀。 -
是的,它单独工作。如果我添加名称,它会说,不存在这样的张量
-
您能否添加您用于调用会话的代码以及打印的完整错误?如果将
name添加到导入的图表中,则需要在该图表中使用的任何张量名称前加上name的值,后跟/。 -
请检查更新的问题...
标签: python tensorflow