最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

pytorch制作自己的LMDB數(shù)據(jù)操作示例

 更新時(shí)間:2019年12月18日 12:04:58   作者:團(tuán)長(zhǎng)sama  
這篇文章主要介紹了pytorch制作自己的LMDB數(shù)據(jù)操作,結(jié)合實(shí)例形式分析了pytorch使用lmdb的相關(guān)操作技巧與使用注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了pytorch制作自己的LMDB數(shù)據(jù)操作。分享給大家供大家參考,具體如下:

前言

記錄下pytorch里如何使用lmdb的code,自用

制作部分的Code

code就是ASTER里數(shù)據(jù)制作部分的代碼改了點(diǎn),aster_train.txt里面就算圖片的完整路徑每行一個(gè),圖片同目錄下有同名的txt,里面記著jpg的標(biāo)簽

import os
import lmdb # install lmdb by "pip install lmdb"
import cv2
import numpy as np
from tqdm import tqdm
import six
from PIL import Image
import scipy.io as sio
from tqdm import tqdm
import re
def checkImageIsValid(imageBin):
 if imageBin is None:
  return False
 imageBuf = np.fromstring(imageBin, dtype=np.uint8)
 img = cv2.imdecode(imageBuf, cv2.IMREAD_GRAYSCALE)
 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.items():
   txn.put(k.encode(), v)
def _is_difficult(word):
 assert isinstance(word, str)
 return not re.match('^[\w]+$', word)
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
 """
 assert(len(imagePathList) == len(labelList))
 nSamples = len(imagePathList)
 env = lmdb.open(outputPath, map_size=1099511627776)#最大空間1048576GB
 cache = {}
 cnt = 1
 for i in range(nSamples):
  imagePath = imagePathList[i]
  label = labelList[i]
  if len(label) == 0:
   continue
  if not os.path.exists(imagePath):
   print('%s does not exist' % imagePath)
   continue
  with open(imagePath, 'rb') as f:
   imageBin = f.read()
  if checkValid:
   if not checkImageIsValid(imageBin):
    print('%s is not a valid image' % imagePath)
    continue
  #數(shù)據(jù)庫(kù)中都是二進(jìn)制數(shù)據(jù)
  imageKey = 'image-%09d' % cnt#9位數(shù)不足填零
  labelKey = 'label-%09d' % cnt
  cache[imageKey] = imageBin
  cache[labelKey] = label.encode()
  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).encode()
 writeCache(env, cache)
 print('Created dataset with %d samples' % nSamples)
def get_sample_list(txt_path:str):
  with open(txt_path,'r') as fr:
    jpg_list=[x.strip() for x in fr.readlines() if os.path.exists(x.replace('.jpg','.txt').strip())]
  txt_content_list=[]
  for jpg in jpg_list:
    label_path=jpg.replace('.jpg','.txt')
    with open(label_path,'r') as fr:
      try:
        str_tmp=fr.readline()
      except UnicodeDecodeError as e:
        print(label_path)
        raise(e)
      txt_content_list.append(str_tmp.strip())
  return jpg_list,txt_content_list
if __name__ == "__main__":
 txt_path='/home/gpu-server/disk/disk1/NumberData/8NumberSample/aster_train.txt'
 lmdb_output_path = '/home/gpu-server/project/aster/dataset/train'
 imagePathList,labelList=get_sample_list(txt_path)
 createDataset(lmdb_output_path, imagePathList, labelList)

讀取部分

這里用的pytorch的dataloader,簡(jiǎn)單記錄一下,人比較懶,代碼就直接抄過(guò)來(lái),不整理拆分了,重點(diǎn)看__getitem__

from __future__ import absolute_import
# import sys
# sys.path.append('./')
import os
# import moxing as mox
import pickle
from tqdm import tqdm
from PIL import Image, ImageFile
import numpy as np
import random
import cv2
import lmdb
import sys
import six
import torch
from torch.utils import data
from torch.utils.data import sampler
from torchvision import transforms
from lib.utils.labelmaps import get_vocabulary, labels2strs
from lib.utils import to_numpy
ImageFile.LOAD_TRUNCATED_IMAGES = True
from config import get_args
global_args = get_args(sys.argv[1:])
if global_args.run_on_remote:
 import moxing as mox
 #moxing是一個(gè)分布式的框架 跳過(guò)
class LmdbDataset(data.Dataset):
 def __init__(self, root, voc_type, max_len, num_samples, transform=None):
  super(LmdbDataset, self).__init__()
  if global_args.run_on_remote:
   dataset_name = os.path.basename(root)
   data_cache_url = "/cache/%s" % dataset_name
   if not os.path.exists(data_cache_url):
    os.makedirs(data_cache_url)
   if mox.file.exists(root):
    mox.file.copy_parallel(root, data_cache_url)
   else:
    raise ValueError("%s not exists!" % root)
   self.env = lmdb.open(data_cache_url, max_readers=32, readonly=True)
  else:
   self.env = lmdb.open(root, max_readers=32, readonly=True)
  assert self.env is not None, "cannot create lmdb from %s" % root
  self.txn = self.env.begin()
  self.voc_type = voc_type
  self.transform = transform
  self.max_len = max_len
  self.nSamples = int(self.txn.get(b"num-samples"))
  self.nSamples = min(self.nSamples, num_samples)
  assert voc_type in ['LOWERCASE', 'ALLCASES', 'ALLCASES_SYMBOLS','DIGITS']
  self.EOS = 'EOS'
  self.PADDING = 'PADDING'
  self.UNKNOWN = 'UNKNOWN'
  self.voc = get_vocabulary(voc_type, EOS=self.EOS, PADDING=self.PADDING, UNKNOWN=self.UNKNOWN)
  self.char2id = dict(zip(self.voc, range(len(self.voc))))
  self.id2char = dict(zip(range(len(self.voc)), self.voc))
  self.rec_num_classes = len(self.voc)
  self.lowercase = (voc_type == 'LOWERCASE')
 def __len__(self):
  return self.nSamples
 def __getitem__(self, index):
  assert index <= len(self), 'index range error'
  index += 1
  img_key = b'image-%09d' % index
  imgbuf = self.txn.get(img_key)
  #由于Image.open需要一個(gè)類(lèi)文件對(duì)象 所以這里需要把二進(jìn)制轉(zhuǎn)為一個(gè)類(lèi)文件對(duì)象
  buf = six.BytesIO()
  buf.write(imgbuf)
  buf.seek(0)
  try:
   img = Image.open(buf).convert('RGB')
   # img = Image.open(buf).convert('L')
   # img = img.convert('RGB')
  except IOError:
   print('Corrupted image for %d' % index)
   return self[index + 1]
  # reconition labels
  label_key = b'label-%09d' % index
  word = self.txn.get(label_key).decode()
  if self.lowercase:
   word = word.lower()
  ## fill with the padding token
  label = np.full((self.max_len,), self.char2id[self.PADDING], dtype=np.int)
  label_list = []
  for char in word:
   if char in self.char2id:
    label_list.append(self.char2id[char])
   else:
    ## add the unknown token
    print('{0} is out of vocabulary.'.format(char))
    label_list.append(self.char2id[self.UNKNOWN])
  ## add a stop token
  label_list = label_list + [self.char2id[self.EOS]]
  assert len(label_list) <= self.max_len
  label[:len(label_list)] = np.array(label_list)
  if len(label) <= 0:
   return self[index + 1]
  # label length
  label_len = len(label_list)
  if self.transform is not None:
   img = self.transform(img)
  return img, label, label_len

更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Python圖片操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門(mén)與進(jìn)階經(jīng)典教程

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • django 前端頁(yè)面如何實(shí)現(xiàn)顯示前N條數(shù)據(jù)

    django 前端頁(yè)面如何實(shí)現(xiàn)顯示前N條數(shù)據(jù)

    這篇文章主要介紹了django 前端頁(yè)面如何實(shí)現(xiàn)顯示前N條數(shù)據(jù)。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • python文件路徑操作方法總結(jié)

    python文件路徑操作方法總結(jié)

    在本篇文章里小編給大家整理的是一篇關(guān)于python文件路徑操作方法總結(jié)內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。
    2020-12-12
  • Python函數(shù)屬性和PyC詳解

    Python函數(shù)屬性和PyC詳解

    這篇文章主要介紹了Python函數(shù)屬性和PyC,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2021-10-10
  • Python操作Jira庫(kù)常用方法解析

    Python操作Jira庫(kù)常用方法解析

    這篇文章主要介紹了Python操作Jira庫(kù)常用方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • python 讀取文件并替換字段的實(shí)例

    python 讀取文件并替換字段的實(shí)例

    今天小編就為大家分享一篇python 讀取文件并替換字段的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • 基于python實(shí)現(xiàn)語(yǔ)音錄入識(shí)別代碼實(shí)例

    基于python實(shí)現(xiàn)語(yǔ)音錄入識(shí)別代碼實(shí)例

    這篇文章主要介紹了如何通過(guò)python實(shí)現(xiàn)語(yǔ)音錄入識(shí)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • 使用Python構(gòu)建帶GUI的郵件自動(dòng)發(fā)送工具

    使用Python構(gòu)建帶GUI的郵件自動(dòng)發(fā)送工具

    在本篇博客中,我們將深入解析一個(gè)使用 wxPython 構(gòu)建的郵件發(fā)送器 GUI 程序,這個(gè)工具能夠自動(dòng)查找指定目錄中的文件作為附件,并提供郵件發(fā)送功能,本文將從功能、代碼結(jié)構(gòu)、關(guān)鍵技術(shù)等方面進(jìn)行詳細(xì)分析,需要的朋友可以參考下
    2025-03-03
  • Pandas條件篩選與組合篩選的使用

    Pandas條件篩選與組合篩選的使用

    本文主要介紹了Pandas條件篩選與組合篩選的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • python性能測(cè)試對(duì)手機(jī)號(hào)綁定進(jìn)行壓測(cè)

    python性能測(cè)試對(duì)手機(jī)號(hào)綁定進(jìn)行壓測(cè)

    這篇文章主要為大家介紹了python性能測(cè)試對(duì)手機(jī)號(hào)綁定進(jìn)行壓測(cè)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • 講解Python中for循環(huán)下的索引變量的作用域

    講解Python中for循環(huán)下的索引變量的作用域

    這篇文章主要介紹了講解Python中for循環(huán)下的索引變量的作用域,是Python學(xué)習(xí)當(dāng)中的基礎(chǔ)知識(shí),本文給出了Python3的示例幫助讀者理解,需要的朋友可以參考下
    2015-04-04

最新評(píng)論

潜山县| 新平| 那曲县| 凉山| 新安县| 木里| 林芝县| 石柱| 西城区| 邢台县| 措美县| 绥江县| 恭城| 商洛市| 大名县| 凌云县| 泸水县| 绥宁县| 宝应县| 高平市| 元江| 浦北县| 龙海市| 甘肃省| 东平县| 宁阳县| 靖州| 罗江县| 东丰县| 龙川县| 保定市| 博爱县| 徐水县| 肥城市| 九寨沟县| 睢宁县| 南雄市| 玉屏| 通榆县| 修水县| 永靖县|