简体   繁体   English

与theano一起使用多处理

[英]using multiprocessing with theano

I'm trying to use theano with cpu-multiprocessing with a neural network library, Keras. 我正在尝试使用带有cpu多处理的theano和神经网络库Keras。

I use device=gpu flag and load the keras model. 我使用device=gpu flag并加载keras模型。 Then for extracting features for over a million images, im using multiprocessing pool. 然后,为了提取超过一百万个图像的功能,我使用多处理池。

The function looks something like this: 该函数看起来像这样:

from keras import backend as K

f = K.function([model.layers[0].input, K.learning_phase()], [model.layers[-3].output,])

def feature_gen(flz):
    im = imread(flz)
    cPickle.dump(f([im, 0])[0][0], open(flz, 'wb'), -1)

pool = mp.Pool(processes=10)
results = [pool.apply_async(feature_gen, args=(f, )) for f in filelist]]

This however starts creating pools in the GPU memory and my code fails with memory error. 然而,这开始在GPU内存中创建池,并且我的代码因内存错误而失败。 Is it possible to force multiprocessing to create threads in CPU memory and then use specific parts for feature extraction such as f([im, 0])[0][0] with GPU? 是否有可能强制多处理在CPU内存中创建线程,然后使用特定部分进行特征提取,例如f([im, 0])[0][0]与GPU?

If not, is there an alternative to do the same thing in parallel in python? 如果没有,是否有替代方法在python中并行执行相同的操作?

It is possible to use multiple processes if the other processes do not use keras, to my knowledge you need to restrict usage of keras to a single process. 如果其他进程不使用keras,则可以使用多个进程,据我所知,您需要将keras的使用限制为单个进程。 This seems to include all keras classes and methods, even those who do not seem to use the gpu, eg ImageDataGenerator. 这似乎包括所有keras类和方法,甚至那些似乎没有使用gpu的人,例如ImageDataGenerator。

If the workload is GPU limited it is also possible to use the threading library, which creates threads instead of processes, eg to load data while the GPU processes the previous batch, then the restriction does not apply. 如果工作负载是GPU限制的,那么也可以使用线程库来创建线程而不是进程,例如在GPU处理前一批时加载数据,然后限制不适用。 Due to the global interpreter lock this is not a solution in CPU limited environments. 由于全局解释器锁定,这不是CPU受限环境中的解决方案。

Your situation looks like a parallel [read, do work on GPU, write]. 您的情况看起来像并行[读取,在GPU上工作,写入]。 This can be reformed to a pipeline, eg some processes reading, the main process performing GPU work and some processes writing. 这可以改为管道,例如一些进程读取,执行GPU工作的主进程和一些进程编写。

  1. Create Queue objects for input/output (threading.Queue or multiprocessing.Queue) 为输入/输出创建队列对象(threading.Queue或multiprocessing.Queue)
  2. Create background worker threads/processes which read data from disk and feed it to the input queue 创建后台工作线程/进程,从磁盘读取数据并将其提供给输入队列
  3. Create background worker threads/processes which write data from the output queue to disk 创建后台工作线程/进程,将数据从输出队列写入磁盘
  4. main loop which takes data from the input queue, creates batches, processes the data on the gpu and fills the output queue 主循环从输入队列获取数据,创建批处理,处理gpu上的数据并填充输出队列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM