如何使用Python并发多进程功能,但仅在上调用此会话

2024-04-24 10:53:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个类似这样的代码:

def processImage(filename):
    with detection_graph.as_default():
        with tf.Session(graph=detection_graph) as sess:
            # Definite input and output Tensors for detection_graph
            image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
            # Each box represents a part of the image where a particular object was detected.
            detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
            # Each score represent how level of confidence for each of the objects.
            # Score is shown on the result image, together with the class label.
            detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
            detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
            num_detections = detection_graph.get_tensor_by_name('num_detections:0')

            #Do Other Stuff Here with "sess" variable like:
            sess.run([abc, xyz, stuff])


def main():
with concurrent.futures.ProcessPoolExecutor(max_workers=2) as executor:
                #{executor.map(processImage, filesToProcess):  filesToProcess for filesToProcess in filesToProcess}
                {executor.submit(processImage, filesToProcess): filesToProcess for filesToProcess in filesToProcess}
if __name__ == '__main__':
    main()

但是我只想调用这个代码一次

with detection_graph.as_default():
    with tf.Session(graph=detection_graph) as sess:
        # Definite input and output Tensors for detection_graph
        image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
        # Each box represents a part of the image where a particular object was detected.
        detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
        # Each score represent how level of confidence for each of the objects.
        # Score is shown on the result image, together with the class label.
        detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
        detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
        num_detections = detection_graph.get_tensor_by_name('num_detections:0')

但是我需要sess内的processImage变量。有没有办法,我如何修改这段代码,所以我只调用with detection_graph.as_default(): and with tf.Session(graph=detection_graph) as sess:部分一次


Tags: ofthenameimageforgetbyas
1条回答
网友
1楼 · 发布于 2024-04-24 10:53:26

假设检测图和会话对象是可拾取的(即,可以序列化以发送到不同的进程)和可安全分发的(即,不同副本上的操作是有意义和安全的),您可以这样做

def processImage(f, detection_graph, sess):
   ...

def main():
    with detection_graph.as_default() as dg:
        with tf.Session(graph=detection_graph) as sess:
            with concurrent.futures.ProcessPoolExecutor(max_workers=2) as executor:
                for f in filesToProcess:
                    executor.submit(processImage, f, dg, sess)

相关问题 更多 >