使用多处理返回一个 numpy 数组

Posted

技术标签:

【中文标题】使用多处理返回一个 numpy 数组【英文标题】:return a numpy array using multiprocessing 【发布时间】:2021-06-22 01:08:09 【问题描述】:

我有以下功能,

def detect_face(image, return_val):
 frame = Image.open('input/' + image + '.jpg')
 face_boxes = face_recognition.face_locations(np.array(frame), model='cnn')

 if len(face_boxes) > 1:
    print("-----2 faces detected in  image-----".format(image))
    return_val.append(None)
    return

 elif len(face_boxes) == 0:
    print("-----No face detected in  image-----".format(image))
    return_val.append(None)
    return

 top, right, bottom, left = np.squeeze(face_boxes)
 frameCropped = frame.crop(box=(left, top, right, bottom + 15))

 frame_resized = np.array(frameCropped.resize((224, 224)))
 Image.fromarray(frame_resized).save('input/cropped_' + image + '.jpg')

 preprocess_image = np.expand_dims(np.array(frame_resized, dtype=np.float64), 0)
 preprocess_image = preprocess_input(np.array(preprocess_image), version=1)

 fig,ax = plt.subplots(nrows=1, ncols=3, figsize=(5,12))
 ax[0].imshow(np.array(frame))
 ax[0].axis('off')
 ax[1].imshow(np.array(frame_resized))
 ax[1].axis('off')
 ax[2].imshow(np.array(preprocess_image[0]))
 ax[2].axis('off')

 return_val.append(preprocess_image)
 return preprocess_image

manager = multiprocessing.Manager()
return_val = manager.list()

preprocess_image = Process(target=detect_face, args=('ammar', return_val))
preprocess_image.start()
preprocess_image.join()

print(return_val)

return_val 总是一个空列表 如何使用多处理模块返回我的 numpy 数组 我试图寻找答案,但似乎我做错了什么

【问题讨论】:

您的函数显式返回 nothing,并且您的代码也不会从中收到任何内容。为什么你会期望有一个返回值?您是否知道将列表传递给进程,就像return_val 参数/参数一样,只会发送其更改未反映在原始副本中的副本? 我已经编辑了我的代码以返回 preprocess_image,因为我希望从我的函数 @MisterMiyagi 中返回 numpy 数组 【参考方案1】:
import numpy as np
import multiprocessing as mp

n_elements = 1000 # how many elements your numpy should have

def myProc( shared_var ):
    '''
    here you convert your shared variable from mp.RawArray to numpy
    then treat it as it is numpy array e.g. fill it in with some 
    random numbers for demonstration purpose
    '''
    var = np.reshape( np.frombuffer( shared_var, dtype=np.uint32 ), -1 )
    for i in range( n_elements ):    
        var[i] = np.random.randint( 0, 2**16, 1 )
    print( 'myProc var.mean() = ', var.mean() )               
                                  
#buffer that contains the memory
mp_var = mp.RawArray( 'i', n_elements )
p = mp.Process( target=myProc, args=(mp_var,) )
p.start()                                      
p.join()
#after the process has ended, you convert the buffer that was passed to it
var = np.reshape( np.frombuffer( mp_var, dtype=np.uint32 ), -1)
#and again, you can treat it like a numpy array
print( '   out var.mean() = ',var.mean() )                                            

【讨论】:

我有 (1, 224, 224, 3) 数组形状,请您指教

以上是关于使用多处理返回一个 numpy 数组的主要内容,如果未能解决你的问题,请参考以下文章

在共享内存中使用 numpy 数组进行多处理

如何使用 Python 多处理 Pool.map 在 for 循环中填充 numpy 数组

多处理 - 具有多维 numpy 数组的共享内存

从多处理池函数返回值

使用 c++ Eigen 库处理 numpy 数组后,输出错误

python 多处理池示例,使用3D numpy数组作为输入,在计算密集型堆栈上使用所有CPU内核。