2

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

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
4

1 回答 1

2

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

使用

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)  
于 2016-06-10T18:20:13.800 回答