gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:13:06 26 4
gpt4 key购买 nike

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

关于python - Caffe NN 中的前向传递并行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37320922/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com