1. 程式人生 > >caffe中兩個lmdb的合並 [python]

caffe中兩個lmdb的合並 [python]

base 運行 事務 ubunt env == mit utf odin

1、安裝lmdb

2、Ubuntu 系統命令:pip install lmdb

3、運行代碼:combine_lmdb.py

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sun Aug 12 17:50:48 2018
@author: Sarah
"""

import lmdb
env1 = lmdb.open("/home/www/www_python/dataset/train_001_lmdb")
env2 = lmdb.open("/home/www/www_python/dataset/train_002_lmdb")

txn1 = env1.begin()
txn2 = env2.begin()

database1 = txn1.cursor()
database2 = txn2.cursor()


env3 = lmdb.open("/home/www/www_python/dataset/train_result_lmdb", map_size=int(1e12))
txn3 = env3.begin(write=True)
print(env3.stat())

count =0
for (key, value) in database1:
# 將數據放到結果數據庫事務中
txn3.put(key, value)
count += 1
if(count % 1000 == 0):
# 將數據寫入數據庫,必須的,否則數據不會寫入到數據庫中
txn3.commit()
count = 0
txn3 = env3.begin(write=True)
if(count % 1000 != 0):
txn3.commit()
count = 0
txn3 = env3.begin(write=True)

for (key, value) in database2:
txn3.put(key, value)
if(count % 1000 == 0):
txn3.commit()
count = 0
txn3= env3.begin(write=True)
if(count % 1000 != 0):
txn3.commit()
count = 0
txn_3 = env3.begin(write=True)

#查詢合並前後的lmdb的數據,確認合並成功
print txn1.stat()[‘entries‘]
print txn2.stat()[‘entries‘]
print txn3.stat()[‘entries‘]
print("success")

caffe中兩個lmdb的合並 [python]