如何将一个函数并行应用于numpy数组中的多个图像? [英] How to apply a function in parallel to multiple images in a numpy array?
问题描述
假设我有一个 images
数组,其中包含3个频道的100,000张图片。
Let's say I have an images
array that holds 100,000 images with 3 channels.
images = np.random.randint(0,255,(100000,32,32,3))
我有一个函数 foo
,它接受一个图像并对其执行一些操作。
And I have a function foo
which accepts an image and performs some operation on it.
def foo(img):
#some operation on the image, say histogram equalization
我现在如何将 foo
函数并行应用于100000张图像?我认为numpy会有一些功能用于此目的,但我很失望,没有找到任何。我发现 numpy.apply_along_axis
但是我读到它是相当迭代的。我该怎么办?
How do I now apply the foo
function to 100000 images in parallel? I thought numpy would have some function for this purpose, but I was disappointed to not find any. I found numpy.apply_along_axis
but I read it is rather iterative. What should I do?
推荐答案
这是一个例子,使用 joblib
执行直方图均衡化, parallel , n_jobs
等于 nprocs
(这里有10个流程,但您可以根据需要进行更改)
Here is an example, using joblib
which performs histogram equalization on the images, in parallel with n_jobs
equal to nprocs
(here 10 processes but you can change as per your need)
# imports
import numpy as np
from skimage import exposure
from joblib import Parallel, delayed
# number of processes
nprocs = 10
# batched image array
img_arr = np.random.randint(0, 255, (1000, 32, 32, 3))
# function to be applied on all images
def process_image(img):
img_eq = exposure.equalize_hist(img)
return img_eq
result = []
# run `process_image()` in parallel
result.extend(Parallel(n_jobs=nprocs)(delayed(process_image)(img_arr[idx]) for idx in range(img_arr.shape[0])))
这篇关于如何将一个函数并行应用于numpy数组中的多个图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!