caffe python reads lmdb file

Generally, when training a model with caffe, the data will first be written to the database, lmdb/leveldb, but if you want to check whether it is written correctly after writing, how to read the lmdb file?
Here, python code is used to read the lmdb file and check the content to see if it is as expected.


The code is as follows:


-Python code
01
import caffe
02
import lmdb
03
 
04
lmdb_env = lmdb.open('train_lmdb') #caffe saves the directory of lmdb files
05
lmdb_txn = lmdb_env.begin()
06
lmdb_cursor = lmdb_txn.cursor()
07
datum = caffe.proto.caffe_pb2.Datum()
08
 
09
for key, value in lmdb_cursor:
10
    datum.ParseFromString(value)
11
    label = datum.label
12
    data = caffe.io.datum_to_array(datum)
13
    print label, data


Two libraries, lmdb and caffe, are imported here. Explain how to import


1. lmdb. You need to install lmdb with pip, enter the root account, and enter pip install lmdb (Under other users, the sudo command cannot be installed successfully )


2. Import caffe, after installing caffe, add environment variables in bashrc, export PYTHONPYTH="xxx/caffe-master/python", and then source ~/.bashrc


import caffe, an error will be reported, because Without compiling pycaffe, under caffe-master, just make pycaffe.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324625069&siteId=291194637