Caffe使用:如何将一维数据或其他非图像数据转换成lmdb

转载地址:http://www.cnblogs.com/dcsds1/p/5205669.html


在caffe的google group里我找到了这个网址:http://deepdish.io/2015/04/28/creating-lmdb-in-Python/


[python]  view plain  copy
  1. import numpy as np  
  2. import lmdb  
  3. import caffe  
  4.   
  5. N = 1000  
  6.   
  7. # Let's pretend this is interesting data  
  8. X = np.zeros((N, 33232), dtype=np.uint8)  
  9. y = np.zeros(N, dtype=np.int64)  
  10.   
  11. # We need to prepare the database for the size. We'll set it 10 times  
  12. # greater than what we theoretically need. There is little drawback to  
  13. # setting this too big. If you still run into problem after raising  
  14. # this, you might want to try saving fewer entries in a single  
  15. # transaction.  
  16. map_size = X.nbytes * 10  
  17.   
  18. env = lmdb.open('mylmdb', map_size=map_size)  
  19.   
  20. with env.begin(write=True) as txn:  
  21.     # txn is a Transaction object  
  22.     for i in range(N):  
  23.         datum = caffe.proto.caffe_pb2.Datum()  
  24.         datum.channels = X.shape[1]  
  25.         datum.height = X.shape[2]  
  26.         datum.width = X.shape[3]  
  27.         datum.data = X[i].tobytes()  # or .tostring() if numpy < 1.9  
  28.         datum.label = int(y[i])  
  29.         str_id = '{:08}'.format(i)  
  30.   
  31.         # The encode is only essential in Python 3  
  32.         txn.put(str_id.encode('ascii'), datum.SerializeToString())  

这是用python将数据转为lmdb的代码,但是我用这个处理完数据再使用caffe会出现std::bad_alloc错误,后来经过艰苦奋斗,查阅了大量的资料,我发现了问题所在:

1、caffe的数据格式默认为四维(n_samples, n_channels, height, width) .所以必须把我的数据处理成这种格式

2、最后一行txn.put(str_id.encode('ascii'), datum.SerializeToString())一定要加上,我一开始一维python2不用写这个,结果老是出错,后来才发现这行必须写!

3、如果出现mdb_put: MDB_MAP_FULL: Environment mapsize limit reached的错误,是因为lmdb默认的map_size比较小,我把lmdb/cffi.py里面的map_size默认值改了一下,改成了1099511627776(也就是1Tb),我也不知道是不是这么改,然后我又把上面python程序里map_size = X.nbytes 这句改成了map_size = X.nbytes * 10,然后就成功了!


找资料的过程中,我还发现了用python写leveldb的程序,网址在这里:https://github.com/BVLC/caffe/issues/745和http://stackoverflow.com/questions/32707393/whats-caffes-input-format

用python写HDF5的程序在这里:http://stackoverflow.com/questions/31774953/test-labels-for-regression-caffe-float-not-allowed/31808324#31808324


参考:


  1.http://stackoverflow.com/questions/30983213/how-to-use-1-dim-vector-as-input-for-caffe/30991590#30991590

  2.关于lmdb的map_size大小的问题:https://github.com/BVLC/caffe/issues/1298和http://stackoverflow.com/questions/31820976/lmdb-increase-map-size

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值