国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

python生成lmdb格式的文件實(shí)例

2020-01-04 14:06:54
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

在crnn訓(xùn)練的時(shí)候需要用到lmdb格式的數(shù)據(jù)集,下面是python生成lmdb個(gè)是數(shù)據(jù)集的代碼,注意一定要在linux系統(tǒng)下,否則會(huì)讀入圖像的時(shí)候出問(wèn)題,可能遇到的問(wèn)題都在代碼里面注釋了,看代碼即可。

#-*- coding:utf-8 -*- import osimport lmdb#先pip install這個(gè)模塊哦import cv2import globimport numpy as np  def checkImageIsValid(imageBin): if imageBin is None:  return False imageBuf = np.fromstring(imageBin, dtype=np.uint8) img = cv2.imdecode(imageBuf, cv2.IMREAD_GRAYSCALE) if img is None:  return False imgH, imgW = img.shape[0], img.shape[1] if imgH * imgW == 0:  return False return True def writeCache(env, cache): with env.begin(write=True) as txn:  for k, v in cache.iteritems():   txn.put(k, v) def createDataset(outputPath, imagePathList, labelList, lexiconList=None, checkValid=True): """ Create LMDB dataset for CRNN training.# ARGS:  outputPath : LMDB output path  imagePathList : list of image path  labelList  : list of corresponding groundtruth texts  lexiconList : (optional) list of lexicon lists  checkValid : if true, check the validity of every image """ # print (len(imagePathList) , len(labelList)) assert(len(imagePathList) == len(labelList)) nSamples = len(imagePathList) print '...................' env = lmdb.open(outputPath, map_size=8589934592)#1099511627776)所需要的磁盤空間的最小值,之前是1T,我改成了8g,否則會(huì)報(bào)磁盤空間不足,這個(gè)數(shù)字是字節(jié)  cache = {} cnt = 1 for i in xrange(nSamples):  imagePath = imagePathList[i]  label = labelList[i]  if not os.path.exists(imagePath):   print('%s does not exist' % imagePath)   continue  with open(imagePath, 'r') as f:   imageBin = f.read()  if checkValid:   if not checkImageIsValid(imageBin):    print('%s is not a valid image' % imagePath)#注意一定要在linux下,否則f.read就不可用了,就會(huì)輸出這個(gè)信息    continue   imageKey = 'image-%09d' % cnt  labelKey = 'label-%09d' % cnt  cache[imageKey] = imageBin  cache[labelKey] = label  if lexiconList:   lexiconKey = 'lexicon-%09d' % cnt   cache[lexiconKey] = ' '.join(lexiconList[i])  if cnt % 1000 == 0:   writeCache(env, cache)   cache = {}   print('Written %d / %d' % (cnt, nSamples))  cnt += 1 nSamples = cnt - 1 cache['num-samples'] = str(nSamples) writeCache(env, cache) print('Created dataset with %d samples' % nSamples)  def read_text(path):  with open(path) as f:  text = f.read() text = text.strip()  return text  if __name__ == '__main__': # lmdb 輸出目錄 outputPath = 'D:/ruanjianxiazai/tuxiangyangben/fengehou/train'#訓(xùn)練集和驗(yàn)證集要跑兩遍這個(gè)程序,分兩次生成  path = "D:/ruanjianxiazai/tuxiangyangben/fengehou/chenguang/*.jpg"#將txt與jpg的都放在同一個(gè)文件里面 imagePathList = glob.glob(path) print '------------',len(imagePathList),'------------' imgLabelLists = [] for p in imagePathList:  try:   imgLabelLists.append((p, read_text(p.replace('.jpg', '.txt'))))  except:   continue    # imgLabelList = [ (p, read_text(p.replace('.jpg', '.txt'))) for p in imagePathList] # sort by labelList imgLabelList = sorted(imgLabelLists, key = lambda x:len(x[1])) imgPaths = [ p[0] for p in imgLabelList] txtLists = [ p[1] for p in imgLabelList]  createDataset(outputPath, imgPaths, txtLists, lexiconList=None, checkValid=True) 

以上這篇python生成lmdb格式的文件實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到python教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 沽源县| 通辽市| 沁源县| 宁远县| 永州市| 桂林市| 唐山市| 大荔县| 阜南县| 合阳县| 晋城| 十堰市| 彭阳县| 香河县| 白城市| 南岸区| 嵊州市| 贵溪市| 孙吴县| 抚顺县| 昌黎县| 慈利县| 长沙县| 河北省| 南宁市| 奈曼旗| 上林县| 洱源县| 北辰区| 虹口区| 莱州市| 桃江县| 宁津县| 绥宁县| 福泉市| 色达县| 驻马店市| 凤阳县| 秦皇岛市| 阿勒泰市| 昭苏县|