我已經使用retrain.py重新訓練了兩個不同的分類模型模型。張量使用舊圖而不是新圖
爲我創造getLabel方法從Label_image.py兩個圖像預測標籤如下:
def getLabel(localFile, graphKey, labelKey):
image_data_str = tf.gfile.FastGFile(localFile, 'rb').read()
# Loads label file, strips off carriage return
label_lines = [line.rstrip() for line
in tf.gfile.GFile(labelKey)]
# Unpersists graph from file
with tf.gfile.FastGFile(graphKey, 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(graph_def, name='')
sess = tf.Session()
with sess:
# Feed the image_data as input to the graph and get first prediction
softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
predictions = sess.run(softmax_tensor, {'DecodeJpeg/contents:0': image_data_str})
# Sort to show labels of first prediction in order of confidence
top_k = predictions[0].argsort()[-len(predictions[0]):][::-1]
series = []
count = 1
for node_id in top_k:
human_string = label_lines[node_id]
if count==1:
label = human_string
count+=1
score = predictions[0][node_id]
print('%s (score = %.5f)' % (human_string, score))
series.append({"name": human_string, "data": [score * 100]})
sess.close()
return label, series
而且我稱他們爲
label,series = predict.getLabel(localFile, 'graph1.pb', 'labels1.txt')
label,series = predict.getLabel(localFile, 'graph2.pb', 'labels2.txt')
但對於第二個功能是使用呼叫舊圖形即graph1.pb &它給下面錯誤,因爲模型1具有多個類別比模型2.
human_string = label_lines[node_id]
IndexError: list index out of range
我無法理解爲什麼會發生這種情況。有人可以告訴如何加載第二張圖嗎?
你能否提供更多的背景?預測在哪裏定義? – finbarr
預測是另一個文件即predict.py含有getLabel()函數 –