Skip to main content
 首页 » 编程设计

python之Caffe NN 中的前向传递并行

2024年10月24日8hnrainll

我有一个 Caffe 神经网络,我想在不阻塞主线程的情况下在网络中进行前向传播(使用 GPU)。我正在使用 python,我尝试使用线程和多处理。他们正在使用 CPU,即使我要求它使用 GPU。这是我的代码:

class ThreadingWorker(threading.Thread): 
    def __init__(self): 
        super(ThreadingWorker,self).__init__() 
        param = config() 
        self.model = param.model 
        self.net = caffe.Net(self.model.deployFile, self.model.caffemodel, caffe.TEST) 
 
    def run(self): 
        input_data = np.random.rand(1,4,self.model.width,self.model.height) 
        start = time() 
        self.net.forward(data=input_data) 
        print 'Success, took %f seconds' % (time()-start)       
 
class MultProcessingWorker(mp.Process): 
    def run(self): 
        param = config() 
        self.model = param.model 
        self.net = caffe.Net(self.model.deployFile, self.model.caffemodel, caffe.TEST) 
        input_data = np.random.rand(1,4,self.model.width,self.model.height) 
        start = time() 
        self.net.forward(data=input_data) 
        print 'Success, took %f seconds' % (time()-start)       
 
class NormalWorker(object): 
    '''Using the main thread, no parallelization is being used here''' 
    def __init__(self): 
        param = config() 
        self.model = param.model 
        self.net = caffe.Net(self.model.deployFile, self.model.caffemodel, caffe.TEST) 
 
    def run(self): 
        input_data = np.random.rand(1,4,self.model.width,self.model.height) 
        start = time() 
        self.net.forward(data=input_data) 
        print 'Success, took %f seconds' % (time()-start)    
 
p = NormalWorker() 
p.run() 
 
>> Success, took 0.34 seconds 
 
thread = ThreadingWorker() 
thread.start()    
 
>> Success, took 3.54 seconds 
 
p = MultProcessingWorker() 
p.start() 
 
>> Success, took 3.45 seconds 

请您参考如下方法:

我有一个非常相似的问题。

使用

caffe.set_mode_gpu() 

在子线程中自己解决了。

所以尝试:

class MultProcessingWorker(mp.Process): 
    def run(self): 
 
        caffe.set_mode_gpu() # <---  should fix the problem 
 
        param = config() 
        self.model = param.model 
        self.net = caffe.Net(self.model.deployFile, self.model.caffemodel, caffe.TEST) 
        input_data = np.random.rand(1,4,self.model.width,self.model.height) 
        start = time() 
        self.net.forward(data=input_data) 
        print 'Success, took %f seconds' % (time()-start)