【问题标题】:How to create lmdb file from cifar10 data_batch.bin如何从 cifar10 data_batch.bin 创建 lmdb 文件
【发布时间】:2016-09-06 03:31:50
【问题描述】:

我能够将二进制格式 (cifar10 data_batch1.bin) 读入 python 中的 numpy matirx,但我很难将其写入 lmdb 文件。能给我指路吗?

【问题讨论】:

  • 查看this thread 的答案 - 您将看到如何在 python 中为 caffe 编写 LMDB。

标签: python-2.7 caffe conv-neural-network


【解决方案1】:

几个月前我遇到了同样的问题。以下资源对我帮助很大:

如果我没记错的话,以下代码对我有用(使用 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 1970-01-01
    • 2017-10-12
    • 1970-01-01
    • 2015-08-26
    • 2017-03-07
    相关资源
    最近更新 更多