我能够将二进制格式(cifar10 data_batch1.bin)读入python中的numpy matirx,但我很难将其写入lmdb文件。你能帮我指点一下吗?
答案 0 :(得分:0)
如果我没记错,下面的代码对我有用(使用uint,8位数据):
import lmdb
import caffe
# Let images be a N x 3 x H x W matrix, i.e. N samples,
# 3 color channels (in BGR) height H and width W;
# you will need to get your images into the above
# blob shape (i.e. samples x channels x height x width).
# Let labels be a N x 1 matrix containing the labels.
env = lmdb.open('lmdb_path', map_size = X.nbytes * 10)
with env.begin(write = True) as txn:
for i in range(N):
datum = caffe.proto.caffe_pb2.Datum()
datum.channels = images.shape[1]
datum.height = images.shape[2]
datum.width = images.shape[3]
datum.data = images[i].tostring()
label = int(labels[i])
datum.label = label
# Alternatively, use:
# datum = caffe.io.array_to_datum(images[i], label)
str_id = '{:08}'.format(i)
# You might need to check whether the encode is necessary in Python 2.7, I used Python 3:
txn.put(str_id.encode('ascii'), datum.SerializeToString())
确保为图像使用BGR色彩空间:https://github.com/BVLC/caffe/wiki/Image-Format:-BGR-not-RGB。