环境:win10 pyhton3.7 lmdb0.9
问题描述:做CRNN实验时,自己制作数据集时,用别人封装好的函数,将图片数据和标签数据存入mdb数据文件中。能正常生成对应mdb数据文件,但是我在删除该文件后,发现磁盘空间未释放。比如,我申请了10GB空间,在将data.mdb和lock.mdb文件删除后,磁盘未恢复10GB的空间,这应该怎么解决?是需要将环境先关闭吗?倘若之前未关闭,文件也已删除,有无解决方法?
代码如下:
43行是申请了15GB的空间
import os
import lmdb # install lmdb by "pip install lmdb"
import cv2
import numpy as np
import chardet
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():
if type(v) == type('1'):
v = v.encode()
txn.put(k.encode(), 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
"""
assert(len(imagePathList) == len(labelList))
nSamples = len(imagePathList)
# 在这里申请了15GB的空间,但是删除相应文件后,空间未释放
env = lmdb.open(outputPath, map_size=16106127360)
cache = {}
cnt = 1
for i in range(nSamples):
imagePath = imagePathList[i]
label = labelList[i]
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
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 check_charset(file_path):
with open(file_path, "rb") as f:
data = f.read(4)
charset = chardet.detect(data)['encoding']
return charset
def getParam():
outputPath = 'D:/MLWorkspace/crnn-master/crnn-master/data/train_data'
datasetPath = 'D:/MLWorkspace/dataset/crnn/DataSet/'
dataTrainTxtPath = datasetPath + 'data_train.txt'
imagePath = datasetPath + 'Synthetic_Chinese_String_Dataset/images/'
imagePathList = []
labelPathList = []
with open(dataTrainTxtPath) as f:
lines = f.readlines()
for i in range(len(lines)):
arr = lines[i].split()
filename = arr[0]
labelArr = arr[1:11]
label = ''
for item in labelArr:
label += item + ','
imagePathList.append(imagePath + filename)
labelPathList.append(label)
return outputPath, imagePathList, labelPathList
if __name__ == '__main__':
params = getParam()
createDataset(params[0], params[1], params[2])
# pass