发布于2019-12-03 17:17 阅读(641) 评论(0) 点赞(30) 收藏(0)
如何在执行其他处理时编写异步数据层以预加载批处理?有一些示例代码吗?谢谢
有几种方法可以实现您想要的。我将尝试在此处草绘一个选项。
系统的总体视图是:您已经n
Loader
异步加载数据并提供了队列。然后,该层batch_size
从队列中读取项目,并在forward()
函数中提供网络。
import caffe, multiprocessing
class Loader(multiprocessing.Process):
def __init__(self, outq, *args, **kwargs):
super(Loader, self).__init__()
self.daemon = True
self.outq = outq
self.start() # start working
def run(self):
while True: # read and never stop at all!
try:
# do your magic here
# assuming you load x,y pairs
self.outq.put((x[None, ...], y[None, ...])) # add singleton "batch" dimension
except Exception as e:
# handle errors?
pass
class MultiProcessInputLayer(caffe.Layer):
def setup(self, bottom, top):
# verify no bottoms, right number of tops etc.
self.dataQ = multiprocessing.Queue()
for _ in xrange(n):
Loader(self.dataQ) # start n Loaders
# some other stuff here...
def reshape(self, bottom, top):
# reshape the inputs to the right sizes
def forward(self, bottom, top):
for i in xrange(batch_size):
item = self.dataQ.get()
top[0].data[i, ...] = item[0]
top[1].data[i, ...] = item[1]
def backward(self, top, propagate_down, bottom):
pass # no backward for data layer
我通过困难的方法学到了一些提示和技巧:
1. 由于GIL的原因,请使用multiprocessing
而不是threading
打包。
2.有时(例如,如果很大)从队列中逐项读取以形成每个批次将花费很长时间。在这种情况下,您可以添加另一个将异步从中读取项目并将整个批次写入中的操作。然后将只等待一个单一的项目,从每个电话。
3.注意不要过多地复制数据。使用大图像/标签会使所有这些复制成为瓶颈。batch_size
forward()
multiprocessing.Process
batch_size
self.dataQ
self.batchQ
forward()
self.batchQ
作者:黑洞官方问答小能手
链接:https://www.pythonheidong.com/blog/article/166400/21dade8e4aae5f9710c6/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!