我有一个lmdb数据库,我试图阅读其内容!
具有讽刺意味的是,屏幕上没有任何内容!
这是我为从lmdb读取而编写的代码片段:
import caffe
import lmdb
import numpy as np
from caffe.proto import caffe_pb2
import cv2
import sys
db_train = lmdb.open('mnist_train_lmdb')
db_train_txn = db_train.begin()
cursor = db_train_txn.cursor()
print db_train
print db_train_txn
print db_train_txn.cursor()
datum = caffe_pb2.Datum()
index = sys.argv[0]
size_train = 50000
size_test = 10000
data_train = np.zeros((size_train, 1, 28, 28))
label_train = np.zeros(size_train, dtype=int)
print 'Reading training data...'
i = -1
for key, value in cursor:
i = i + 1
if i % 1000 == 0:
print i
if i == size_train:
break
datum.ParseFromString(value)
label = datum.label
data = caffe.io.datum_to_array(datum)
data_train[i] = data
label_train[i] = label
打印:
<Environment object at 0x0000000009CE3990>
<Transaction object at 0x0000000009CE1810>
<Cursor object at 0x0000000009863738>
Reading training data...
Reading test data...
似乎for循环根本没有运行!
我在这里想念的是什么?
我检查过,这似乎是从lmdb读取的正常方式,我见过的所有源代码示例都有类似的方法。
答案 0 :(得分:1)
纠正自己:
使用lmdb.Cursor()
for key, value in cursor:
和
while cursor.next():
是对的,原来的答案我错了。
您没有正确使用cursor
,并且应在您的代码中稍作修改,例如:
... # original stuff
print 'Reading training data...'
i = -1
while cursor.next(): # Move to the next element, and
i = i + 1 # note cursor starts in an unpositioned state
if i % 1000 == 0:
print i
if i == size_train:
break
datum.ParseFromString(cursor.value())
label = datum.label
data = caffe.io.datum_to_array(datum)
data_train[i] = data
label_train[i] = label
有关lmdb python绑定的更多用法,可以参考here。
答案 1 :(得分:1)