本文主要是介绍【caffe】读取lmdb文件中的内容,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
参考: https://www.jb51.net/article/142988.htm
https://blog.csdn.net/jyl1999xxxx/article/details/53942824
# read.py
from PIL import Image
from caffe.proto import caffe_pb2
import lmdb
import numpy as nppath='/home/...../caffe/examples/cifar10/cifar10_test_lmdb'
def read_from_lmdb(lmdb_path, img_save_to):lmdb_env=lmdb.open(lmdb_path)lmdb_txn=lmdb_env.begin()lmdb_cursor=lmdb_txn.cursor()datum=caffe_pb2.Datum()datum_index=0for key,value in lmdb_cursor:datum.ParseFromString(value)label=datum.labeldata=datum.datachannel=datum.channelsprint ('Datum channels: %d' % datum.channels)print ('Datum width: %d' % datum.width)print ('Datum height: %d' % datum.height)print ('Datum data length: %d' % len(datum.data))print ('Datum label: %d' % datum.label)size=datum.width*datum.heightpixles1=datum.data[0:size]pixles2=datum.data[size:2*size]pixles3=datum.data[2*size:3*size]#Extract images of different channelimage1=Image.frombytes('L', (datum.width, datum.height), pixles1)image2=Image.frombytes('L', (datum.width, datum.height), pixles2)image3=Image.frombytes('L', (datum.width, datum.height), pixles3)image4=Image.merge("RGB",(image3,image2,image1))image4.save(img_save_to+str(key)+".jpg")datum_index+=1print "extracted"lmdb_env.close()read_from_lmdb(path, 'pic')
执行文件
python read.py
则能看到打印输出和获得的图片文件
.......
09998,1
(3, 32, 32)
09999,7
(3, 32, 32)
Datum channels: 3
Datum width: 32
Datum height: 32
Datum data length: 3072
Datum label: 3
extracted
......
这篇关于【caffe】读取lmdb文件中的内容的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!