2017-07-14 21 views
0

我已經使用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 

我無法理解爲什麼會發生這種情況。有人可以告訴如何加載第二張圖嗎?

+0

你能否提供更多的背景?預測在哪裏定義? – finbarr

+0

預測是另一個文件即predict.py含有getLabel()函數 –

回答

0

它看起來像正在發生的事情是,你正在呼籲雙方呼叫predict.getFinalLabel同一個會話。你應該做的是定義兩個單獨的會話,並分別初始化每個(例如具有predict1.getFinalLabelpredict2.getFinalLabel)。如果你發佈了更多的代碼,我可以提供更多的細節和代碼。

+0

這是所有邏輯。剩餘部分用於加載圖像。函數調用顯示結果後。 –

相關問題