gpt4 book ai didi

python - 如何使用Python并发多进程功能但仅调用此 session 一次

转载 作者:行者123 更新时间:2023-12-01 06:39:13 24 4
gpt4 key购买 nike

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

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

但我需要 processImage 中的 sess 变量。有什么办法,如何修改此代码,以便我仅调用 with detector_graph.as_default(): 和 with tf.Session(graph=detection_graph) as sess: 部分一次?

最佳答案

假设检测图和 session 对象是可选取的(即,可以序列化以发送到不同的进程)并且可以安全地分发(即,对不同副本的操作是有意义且安全的),可以做这样的事情。

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)

关于python - 如何使用Python并发多进程功能但仅调用此 session 一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59534843/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com