深度学习之提升数据加载速度--lmdb--1

直接读取文件->保存到lmdb->从lmdb读入数据,python3.6

import lmdb
import numpy as np
def write_lmdb(filename,lmdb_path="output/test"):
    env = lmdb.open(lmdb_path)
    with env.begin(write=True) as txn:
        with open(filename) as f:
            for line in f:
                line_list = line.strip().split("\t")
                if len(line_list)<2:
                    continue
                img, index = line_list
                txn.put(key=index.encode(), value=img.encode())


def read_lmdb(lmdb_path="output/test"):
    env = lmdb.open(lmdb_path)
    with env.begin(write=False) as txn:
        lmdb_cursor = txn.cursor()
        for key, value in lmdb_cursor:
            print(key,value)
            print(key.decode(),value.decode())

if __name__ == '__main__':
   dataset_path="train_sample/img2id"
   write_lmdb(dataset_path,lmdb_path="output/test")