2017-09-06 84 views
0

我正在研究一個涉及人臉檢測,人臉識別(基於facenet),年齡/性別檢測和人臉表情分析的項目。對於每個提到的功能,我有一個正常工作的tensorflow圖。現在,我需要將它們全部結合在一個代碼中。我的做法如下:在張量流圖之間切換

with tf.Graph().as_default(): 

     sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) 
     with sess.as_default(): 

      #configure my camera 
      video_capture = cv2.VideoCapture(0) 


      while True: 

       #read one frame from camera 
       ret, frame = video_capture.read() 



      #here I load face recognition graph using saver restore 
      facenet.load_model(modeldir) 
      images_placeholder tf.get_default_graph().get_tensor_by_name("input:0") 
      embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0") 


      #Here I reset the graph and load another tensorflow graph for age/gender detection 
      tf.reset_default_graph() 
      .... 


      #Here I reset the graph and load another tensorflow graph for face expression analysis 
      tf.reset_default_graph() 
      ..... 

現在我的問題是代碼效率不高,速度很慢。原因是對於視頻的每一幀,我需要從我的磁盤加載(恢復)幾張圖(在while中)。但是,我想一次加載所有圖形(在while之前),只需在while循環中切換圖形以減少運行時間。如果你想利用GPU,小心你的GPU內存分配

graph_face_detection = tf.Graph() 
sess_face_detection = tf.Session(graph=graph_face_detection) 

graph_face_recognition = tf.Graph() 
sess_face_recognition = tf.Session(graph=graph_face_recognition) 

... 

sess_face_detection.run(operations) 

... 

sess_face_recognition.run(operations) 

:我很感激您的評論

回答

0

嘗試以下表格。

更新

首先,一個會話一個圖。

二,指定哪個圖形操作應該使用:

graph_face_detection = tf.Graph() 
sess_face_detection = tf.Session(graph=graph_face_detection) 

graph_face_recognition = tf.Graph() 
sess_face_recognition = tf.Session(graph=graph_face_recognition) 

with graph_face_detection.as_default() as g: 
    output_face_detection = tf.matmul(x, y) 

with graph_face_recognition.as_default() as g: 
    output_face_recognition = tf.matmul(x, y) 

... 

sess_face_detection.run([output_face_detection]) 
sess_face_recognition.run([output_face_recognition]) 

此外,當您運行output_face_detection = tf.matmul(x, y),您只需創建一個節點,並將其添加成圖形,沒有任何實際的計算。因此,您可以先創建所有圖形,然後在循環中使用sess_xxx.run(operation)

+0

謝謝。假設我在代碼「softmax_output = tf.nn.softmax(logits)」中添加了這樣的一行,我想知道它屬於哪個會話和圖表?我可以強制系統將它添加到上述會話/圖表之一中嗎? – user2867237

+0

還有一個問題:我可以將所有圖形添加到1個會話嗎?還是對於每個圖表,我需要創建一個會話?謝謝 – user2867237

+0

@ user2867237我編輯我的答案。 – Sraw