Let's say I have an images
array that holds 100,000 images with 3 channels.
假设我有一个图像阵列,可容纳100,000个带3个通道的图像。
images = np.random.randint(0,255,(100000,32,32,3))
And I have a function foo
which accepts an image and performs some operation on it.
我有一个函数foo接受一个图像并对其执行一些操作。
def foo(img):
#some operation on the image, say histogram equalization
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?
我现在如何将foo函数并行应用于100000个图像?我认为numpy会有一些功能用于此目的,但我很失望,没有找到任何。我找到了numpy.apply_along_axis,但我读到它是相当迭代的。我该怎么办?
1 个解决方案
#1
1
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)
这是一个例子,使用joblib对图像执行直方图均衡,与n_jobs等于nprocs并行(这里有10个进程,但你可以根据需要改变)
# 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])))
#1
1
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)
这是一个例子,使用joblib对图像执行直方图均衡,与n_jobs等于nprocs并行(这里有10个进程,但你可以根据需要改变)
# 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])))