我有大约100万张图像,一次要在此数据集中添加10000张。
我确定map_size与本文的参考文献有误
使用此行创建集合
env = lmdb.open(Path+'mylmdb', map_size=int(1e12)
每10000个样本使用此行将数据写入文件,其中X和Y是要放入LMDB中的数据的占位符。
env = create(env, X[:counter,:,:,:],Y,counter)
def create(env, X,Y,N):
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].tostring() # or .tostring() if numpy < 1.9
datum.label = int(Y[i])
str_id = '{:08}'.format(i)
# The encode is only essential in Python 3
txn.put(str_id.encode('ascii'), datum.SerializeToString())
#pdb.set_trace()
return env
我如何编辑此代码,以便将新数据添加到此LMDB而不替换,因为此当前方法将其替换在同一位置。我已经用env.stat()检查了生成后的长度。
我在上面对我的评论进行扩展。
LMDB中的所有条目都是根据唯一键存储的,并且您的数据库已经包含的键i = 0, 1, 2, ...
。您需要一种找到每个键的唯一键的方法i
。最简单的方法是在现有数据库中找到最大的密钥并不断添加。
假设现有键是连续的,
max_key = env.stat()["entries"]
否则,一种更彻底的方法是遍历所有键。(检查一下。)
max_key = 0
for key, value in env.cursor():
max_key = max(max_key, key)
最后,只需替换for
循环的第7行,
str_id = '{:08}'.format(i)
经过
str_id = '{:08}'.format(max_key + 1 + i)
追加到现有数据库。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句