Tensorflow запускает 2 замороженных графика одновременно (параллельно) - PullRequest
0 голосов
/ 16 января 2020

Можно ли запускать несколько моделей обнаружения тензорных объектов одновременно? (Я обучил две модели и хочу запустить обе параллели) Я написал этот код и попытался запустить, но он не работает.

# First Frozen
detection_graph1 = tf.Graph()
with detection_graph1.as_default():
  od_graph_def = tf.GraphDef()
  with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH1, 'rb') as fid:
    serialized_graph = fid.read()
    od_graph_def.ParseFromString(serialized_graph)
    tf.import_graph_def(od_graph_def, name='')

# Second Frozen
detection_graph2 = tf.Graph()
with detection_graph2.as_default():
  od_graph_def = tf.GraphDef()
  with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH2, 'rb') as fid:
    serialized_graph = fid.read()
    od_graph_def.ParseFromString(serialized_graph)
    tf.import_graph_def(od_graph_def, name='')

def run_inference_for_multiple_images(path,graph1,graph2):
  with graph1.as_default():
    with tf.Session() as sess1:
      with graph2.as_default():
        with tf.Session() as sess2:
          #detection code..

1 Ответ

0 голосов
/ 16 января 2020

Да, это абсолютно возможно, но вы делаете это неправильно. Не определяйте две модели в двух отдельных графиках, просто загрузите их в один и тот же (и добавьте надлежащие области имен, чтобы избежать конфликтов имен):

graph = tf.Graph() # just one graph, with both models loaded
with graph.as_default():
  od_graph_def = tf.GraphDef()
  with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH1, 'rb') as fid:
    serialized_graph = fid.read()
    od_graph_def.ParseFromString(serialized_graph)
    tf.import_graph_def(od_graph_def, name='first_graph')
  with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH2, 'rb') as fid:
    serialized_graph = fid.read()
    od_graph_def.ParseFromString(serialized_graph)
    tf.import_graph_def(od_graph_def, name='second_graph')

# [...] get the correct input and output tensors for the two graphs via their names

with tf.Session(graph=graph) as sess: # just one session
  # Running only one of the two at a time
  res_1 = sess.run(outputs_from_graph_1, feed_dict=graph_1_feeds)
  res_2 = sess.run(outputs_from_graph_2, feed_dict=graph_2_feeds)

  # Actually running them in parallel (might not fit in memory!)
  res_1_and_2 = sess.run( outputs_from_graph_1 + outputs_from_graph_2, {**graph_1_feeds, **graph_2_feeds} )

Примечание : I ' м при условии, что каналы dict с tensor_name:values или placeholder_tensor:values пара ключ / значение

...