分享好友 人工智能首页 频道列表

caffe数据集——LMDB caffe scale

Caffe教程  2023-03-08 14:104600

LMDB介紹


Caffe使用LMDB來存放訓練/測試用的數據集,以及使用網絡提取出的feature(為了方便,以下還是統稱數據集)。數據集的結構很簡單,就是大量的矩陣/向量數據平鋪開來。數據之間沒有什麼關聯,數據內沒有復雜的對象結構,就是向量和矩陣。既然數據並不復雜,Caffe就選擇了LMDB這個簡單的數據庫來存放數據。

 

LMDB的全稱是Lightning Memory-Mapped Database,閃電般的內存映射數據庫。它文件結構簡單,一個文件夾,裡面一個數據文件,一個鎖文件。數據隨意複製,隨意傳輸。它的訪問簡單,不需要運行單獨的數據庫管理進程,只要在訪問數據的代碼裡引用LMDB庫,訪問時給文件路徑即可。

 

圖像數據集歸根究底從圖像文件而來。既然有ImageDataLayer可以直接讀取圖像文件,為什麼還要用數據庫來放數據集,增加讀寫的麻煩呢?我認為,Caffe引入數據庫存放數據集,是為了減少IO開銷。讀取大量小文件的開銷是非常大的,尤其是在機械硬盤上。 LMDB的整個數據庫放在一個文件裡,避免了文件系統尋址的開銷。 LMDB使用內存映射的方式訪問文件,使得文件內尋址的開銷非常​​小,使用指針運算就能實現。數據庫單文件還能減少數據集複製/傳輸過程的開銷。一個幾萬,幾十萬文件的數據集,不管是直接複製,還是打包再解包,過程都無比漫長而痛苦。 LMDB數據庫只有一個文件,你的介質有多塊,就能複制多快,不會因為文件多而慢如蝸牛。

 

 

Datum數據結構

首先需要注意的是,Caffe並不是把向量和矩陣直接放進數據庫的,而是將數據通過caffe.proto裡定義的一個datum類來封裝。數據庫裡放的是一個個的datum序列化成的字符串。 Datum的定義摘錄如下:

caffe数据集——LMDB

 

所以要使用的話   首先要用pip 下載 lmdb

由於小編已經安裝過了

所以顯示already satisfied

caffe数据集——LMDB

 

程式碼:

1.從  array  做出  lmdb

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

 

產生的資料如下:

caffe数据集——LMDB

 

2.從lmdb讀取資料:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import caffe
import lmdb
 
lmdb_env = lmdb.open('mylmdb')
lmdb_txn = lmdb_env.begin()
lmdb_cursor = lmdb_txn.cursor()
datum = caffe.proto.caffe_pb2.Datum()
i=0
for key, value in lmdb_cursor:
    i=i+1
    datum.ParseFromString(value)
    label = datum.label
    data = caffe.io.datum_to_array(datum)
     
    print "This is counter:",i
    print "This is data: ",data.shape
    print "This is label:",label,"\n"

 

運行結果如下:

caffe数据集——LMDB

 

 

參考資料:

http://darren1231.pixnet.net/blog/post/328463403-%E5%AD%B8%E6%9C%83%E5%81%9A%E5%87%BA%E8%87%AA%E5%B7%B1%E7%9A%84%E6%95%B8%E6%93%9A%E9%9B%86%28imdb%29--caffe

http://deepdish.io/2015/04/28/creating-lmdb-in-python/

http://rayz0620.github.io/2015/05/25/lmdb_in_caffe/

https://lmdb.readthedocs.io/en/release/

http://stackoverflow.com/questions/33117607/caffe-reading-lmdb-from-python

查看更多关于【Caffe教程】的文章

展开全文
相关推荐
反对 0
举报 0
图文资讯
热门推荐
优选好物
更多热点专题
更多推荐文章
caffe调试 ubuntu1404+eclipse
转自:http://blog.csdn.net/yaoxingfu72/article/details/47999795首先确保你caffe编译成功,而且makefile.config中将DEBUG:=1那一行取消注释,我的caffe根目录为 caffe-master。你也可以在Eclipse中编译caffe,我是先编译好caffe,然后进入Eclipse中调试1

0评论2023-03-08522

Caffe hdf5 layer data 大于2G 的导入
问题:      Datatype class: H5T_FLOAT,      Check failed: error == cudaSuccess (2 vs. 0)  out of memory.      hdf5 layer 最大的导入的大小是2G, 超过会报错[1]。解决方法:     有人 h5repart -m1g 将数据集分割成多个文件每个是

0评论2023-02-10987

caffe神经网络中不同的lr_policy间的区别
lr_policy可以设置为下面这些值,相应的学习率的计算为:- fixed:   保持base_lr不变.- step:    如果设置为step,则还需要设置一个stepsize,  返回 base_lr * gamma ^ (floor(iter / stepsize)),其中iter表示当前的迭代次数- exp:     返回base_lr

0评论2023-02-10949

Ubuntu配置GPU+CUDA+CAFFE ubuntu配置dns
参考网站:http://blog.csdn.net/xizero00/article/details/43227019/ (主要参考)http://www.cnblogs.com/platero/p/3993877.html (caffe+cudaGPU)http://www.cnblogs.com/platero/p/4118139.html (cuDNN)http://developer.download.nvidia.com/compute/cuda/

0评论2023-02-10616

关于深度学习(deep learning)的常见疑问 --- 谷歌大脑科学家 Caffe缔造者 贾扬清
问答环节问:在finetuning的时候,新问题的图像大小不同于pretraining的图像大小,只能缩放到同样的大小吗?” 答:对的:)问:目前dl在时序序列分析中的进展如何?研究思路如何,能简单描述一下么答:这个有点长,可以看看google最近的一系列machine trans

0评论2023-02-10646

深度学习框架Caffe —— Deep learning in Practice
因工作交接需要, 要将caffe使用方法及整体结构描述清楚。 鉴于也有同学问过我相关内容, 决定在本文中写个简单的tutorial, 方便大家参考。 本文简单的讲几个事情:Caffe能做什么?为什么选择caffe?环境整体结构Protocol buffer训练基本流程Python中训练Debu

0评论2023-02-09598

使用caffe的HDF5数据完毕回归任务
    一直在研究怎样用caffe做行人检測问题。然而參考那些经典结构比方faster-rcnn等,都是自己定义的caffe层来完毕的检測任务。这些都要求对caffe框架有一定程度的了解。近期看到了怎样用caffe完毕回归的任务,就想把检測问题当成回归问题来解决。   

0评论2023-02-09943

Caffe 编译: undefined reference to imencode()
本系列文章由 @yhl_leo 出品,转载请注明出处。 文章链接:http://blog.csdn.net/yhl_leo/article/details/52150781 整理之前编译工程中遇到的一个Bug,贴上提示log信息:...CXX/LD -o .build_release/examples/siamese/convert_mnist_siamese_data.bin.build

0评论2023-02-09884

[caffe]caffe资料收集 Caffeine.
1.caffe主页,有各种tutorial。2.Evan Shelhamer的tutorial,包括视频。 

0评论2023-02-09776

caffe_ssd学习-用自己的数据做训练 ssd caffe
几乎没用过linux操作系统,不懂shell编程,linux下shell+windows下UltraEdit勉勉强强生成了train.txt和val.txt期间各种错误辛酸不表,照着examples/imagenet/readme勉勉强强用自己的数据,按imagenet的训练方法,把reference_caffenet训起来了,小笔记本的风

0评论2023-02-09664

caffe Python API 之中值转换
# 编写一个函数,将二进制的均值转换为python的均值def convert_mean(binMean,npyMean):blob = caffe.proto.caffe_pb2.BlobProto()bin_mean = open(binMean, 'rb' ).read()blob.ParseFromString(bin_mean)arr = np.array( caffe.io.blobproto_to_array(blob)

0评论2023-02-09843

Windows10安装ubuntu & caffe GPU版
1.Ubuntu https://www.cnblogs.com/EasonJim/p/7112413.htmlhttps://blog.csdn.net/jesse_mx/article/details/61425361 安装后启动不了,直接进入windows.解决方案:https://www.cnblogs.com/lymboy/p/7783756.htmlhttps://jingyan.baidu.com/article/5553fa

0评论2023-02-091018

caffe(1) 网络结构层参数详解
prototxt文件是caffe的配置文件,用于保存CNN的网络结构和配置信息。prototxt文件有三种,分别是deploy.prototxt,train_val.prototxt和solver.prototxt。solver.prototxt是caffe的配置文件。里面定义了网络训练时候的各种参数,比如学习率、权重衰减、迭代次

0评论2023-02-09738

更多推荐