我有一个 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