根据我的经验,我已经有50-100毫秒写入LMDB从Python写入在Ubuntu上的ext4硬盘上的Caffe数据.这就是我使用tmpfs(Linux内置的RAM磁盘功能)并在0.07毫秒左右完成这些写入的原因.您可以在ramdisk上创建较小的数据库并将它们复制到硬盘上,然后再对所有数据库进行训练.我正在制作大约20-40GB的内存,因为我有64 GB的内存.
一些代码可以帮助您动态创建,填充和移动LMDB到存储.随意编辑它以适合您的情况.它可以节省你一些时间来了解LMDB和文件操作如何在Python中工作.
import shutil
import lmdb
import random
def move_db():
global image_db
image_db.close();
rnd = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(5))
shutil.move( fold + 'ram/train_images', '/storage/lmdb/'+rnd)
open_db()
def open_db():
global image_db
image_db = lmdb.open(os.path.join(fold, 'ram/train_images'),
map_async=True,
max_dbs=0)
def write_to_lmdb(db, key, value):
"""
Write (key,value) to db
"""
success = False
while not success:
txn = db.begin(write=True)
try:
txn.put(key, value)
txn.commit()
success = True
except lmdb.MapFullError:
txn.abort()
# double the map_size
curr_limit = db.info()['map_size']
new_limit = curr_limit*2
print '>>> Doubling LMDB map size to %sMB ...' % (new_limit>>20,)
db.set_mapsize(new_limit) # double it
...
image_datum = caffe.io.array_to_datum( transformed_image, label )
write_to_lmdb(image_db, str(itr), image_datum.SerializeToString())