2017-09-06 9 views
0

私は、顔検出、顔認識(顔面に基づく)、年齢/性別の検出、顔の表情解析を含むプロジェクトに取り組んでいます。上記の各機能について、うまく動作するテンソルフローグラフが1つあります。今、私はすべてを1つのコードで結合する必要があります。私のアプローチは以下の通りです:グラフをテンソルフローで切り替える

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ループ内のグラフを切り替えてランタイムを減らしたいと思います。あなたは、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

は、次の形式を試してみてくださいあなたのコメントをいただければ幸いです。

更新

最初に、1つのセッショングラフ。

第二には、使用すべきグラフの動作を指定するには:あなたがoutput_face_detection = tf.matmul(x, y)を実行したときに

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]) 

また、あなただけのノードを作成し、任意の実際の計算せずにグラフにそれを追加します。したがって、最初にすべてのグラフを作成してから、ループ内でsess_xxx.run(operation)を使用することができます。

+0

ありがとうございました。このような行をコード "softmax_output = tf.nn.softmax(logits)"に追加すると仮定します。これは、セッションやグラフに属しているのでしょうか?上記セッション/グラフのいずれかにシステムを追加することはできますか? – user2867237

+0

もう1つの質問:1つのセッションにすべてのグラフを追加することはできますか?または各グラフについて1つのセッションを作成する必要がありますか?ありがとう – user2867237

+0

@ user2867237私は自分の答えを編集します。 – Sraw