我正在尝试将我的一个程序转换为使用多进程,最好是多进程池,因为多进程池看起来更简单。从高层次上讲,这个过程是从图像中创建一个补丁阵列,然后将它们传递给GPU进行对象检测。CPU和GPU部分都需要大约4s,但是CPU有8个内核,它不必等待GPU,因为数据通过GPU后不会进行进一步的操作。
下面是我设想的工作原理图:

为了帮助这个过程,我想要一个我的实现的高级版本的演示。假设我们正在遍历一个包含10个图像的文件夹中的图像列表。我们一次调整4张图片的大小。然后我们一次将它们转换为两个黑白图像,我们可以将转换过程作为GPU过程的一部分。下面是代码的样子:
def im_resize(im, num1, num2):
return im.resize((num1, num2), Image.ANTIALIAS)
def convert_bw(im):
return im.convert('L')
def read_images(path):
imlist = []
for pathAndFileName in glob.iglob(os.path.join(path, "*")):
if pathAndFileName.endswith(tuple([".jpg", ".JPG"])):
imlist.append(Image.open(pathAndFileName))
return imlist
img_list = read_images("path/to/images/")
final_img_list = []
for image in img_list:
# Resize needs to run concurrently on 4 processes so that the next img_tmp is always ready to go for convert
img_tmp = im_resize(image, 100, 100)
# Convert is limited, need to run on 2 processes
img_tmp = convert_bw(img_tmp)
final_img_list.append(img_tmp)进程的特定数量的原因是由于系统性能指标,这将减少运行时间。我只想确保GPU不必等待CPU完成图像处理,并且我希望有一个恒定的队列,其中充满了预处理的图像,以便GPU运行。我希望在队列中保持大约4-10个预处理图像的最大大小。如果你们能帮助我用这个简单的例子说明我将如何实现这一点,我相信我可以弄清楚如何将它转换成我需要的东西。
谢谢!
发布于 2015-10-29 18:42:45
下面是实现你想要的东西的试探性尝试:
...
# Mapping functions can only take one arg, we provide tuple
def img_resize_splat(a):
img_resize(*a)
if __name__=="__main__":
# Make a CPU pool and a GPU pool
cpu = Pool(4)
gpu = Pool(2)
# Hopefully this returns an iterable, and not a list with all images read into memory
img_list = read_images("path/to/images/")
# I'm assuming you want images to be processed as soon as ready, order doesn't matter
resized = cpu.imap_unordered(img_resize_splat, ((img, 100, 100) for img in img_list))
converted = gpu.imap_unordered(convert_bw, resized)
# This is an iterable with your results, slurp them up one at a time
for bw_img in converted:
# do somethinghttps://stackoverflow.com/questions/33411272
复制相似问题