简体   繁体   English

如何使用 Python 并发多进程功能但仅调用此会话一次

[英]How to use Python concurrent multiprocess feature but calling this session only once

I have a code something like this:我有一个类似这样的代码:

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

But I want to call this code only once.但我只想调用此代码一次。

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

But I need the sess variable inside processImage .但我需要processImagesess变量。 Is there any way, how can I modify this code, so I call the with detection_graph.as_default(): and with tf.Session(graph=detection_graph) as sess: part only once?有什么办法,我该如何修改这段代码,所以我只调用with detection_graph.as_default(): and with tf.Session(graph=detection_graph) as sess: part 一次?

Assuming detection graph and session objects are pickable (ie, can be serialized to be sent to different processes) and safe to be distributed (ie, operations on different copies are meaningful and safe), you can do something like this.假设检测图和会话对象是可选择的(即,可以被序列化以发送到不同的进程)并且可以安全地分发(即,在不同副本上的操作是有意义且安全的),您可以执行这样的操作。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM