1 import numpy as np 2 import sys 3 from PIL import Image 4 import lmdb 5 import random 6 import os 7 8 sys.path.append('../caffe/python/') 9 10 import caffe 11 12 if __name__ == '__main__' : 13 train_list_file = './trainlist.txt' 14 train_images_root = './train/images/' 15 16 f = open(train_list_file, 'r') 17 trainlist = f.readlines() 18 f.close() 19 20 random.shuffle(trainlist) 21 22 # creating images lmdb 23 in_db = lmdb.open('my-train-data-lmdb', map_size=int(1e12)) 24 with in_db.begin(write=True) as in_txn : 25 for in_idx, in_ in enumerate(trainlist) : 26 fid = in_.strip() 27 fn = os.path.join(train_images_root, fid) 28 im = np.array(Image.open(fn)) 29 im = im[:,:,::-1] 30 im = im.transpose((2, 0, 1)) 31 im_dat = caffe.io.array_to_datum(im) 32 in_txn.put('{:0>10d}'.format(in_idx), im_dat.SerializeToString()) 33 in_db.close()
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:caffe使用python创建lmdb - Python技术站