我正在尝试创建一个包含我所有数据库图像的 lmdb 文件(以训练 CNN)。
这是我从这里获取的“测试代码” :
import numpy as np
import lmdb
import caffe
import cv2
import glob
N = 18
# Let's pretend this is interesting data
X = np.zeros((N, 1, 32, 32), dtype=np.uint8)
y = np.zeros(N, dtype=np.int64)
# 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
train_data = [img for img in glob.glob("/home/roishik/Desktop/Thesis/Code/cafe_cnn/third/code/train_images/*png")]
for i , img_path in enumerate(train_data):
img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
X[i]=img
y[i]=i%2
env = lmdb.open('train', map_size=map_size)
print X
print y
with env.begin(write=True) as txn:
# txn is a Transaction object
for i in range(N):
datum = caffe.proto.caffe_pb2.Datum()
datum.channels = X.shape[1]
datum.height = X.shape[2]
datum.width = X.shape[3]
datum.data = X[i].tobytes() # or .tostring() if numpy < 1.9
print 'a ' + str(X[i])
datum.label = int(y[i])
print 'b ' + str(datum.label)
str_id = '{:08}'.format(i)
txn.put(str_id.encode('ascii'), datum.SerializeToString())
如您所见,我指定了随机二进制标签(0 或 1,分别表示偶数或奇数)。在创建更大的 lmdb 文件之前,我想确保我以正确的方式进行操作。
创建此文件后,我想“查看文件”并检查它是否正常,但我做不到。该文件未使用 python、Access 2016 和 .mdb 阅读器(linux ubunto 软件)正确打开。我的问题是:
我不明白这段代码在做什么。什么是
str_id
?什么是X[i].tobytes
?最后一行是做什么的?运行代码后,我得到了 2 个文件:“data.mdb”和“key.mdb”。那两个是什么?也许这两个文件是我无法打开数据库的原因?
非常感谢,非常感谢您的帮助!