不基于caffe库查看lmdb内容

import lmdb
from PIL import Image
import sys
from torch.utils.data import Dataset
import six

class LmdbDataset(Dataset):

    def __init__(self, root):

        self.root = root
        self.env = lmdb.open(root, max_readers=1, readonly=True, lock=False, readahead=False, meminit=False)
        if not self.env:
            print('cannot create lmdb from %s' % (root))
            sys.exit(0)

        with self.env.begin(write=False) as txn:
            nSamples = int(txn.get('num-samples'.encode()))
            self.nSamples = nSamples

            # Filtering
            self.filtered_index_list = []
            for index in range(self.nSamples):
                
                label_key = 'label-%09d'.encode() % index
                if txn.get(label_key) is None:
                    continue
                
                self.filtered_index_list.append(index)
                index += 1  # lmdb starts with 1

            self.nSamples = len(self.filtered_index_list)

    def __len__(self):
        return self.nSamples

    def __getitem__(self, index):
        assert index <= len(self), 'index range error'
        index = self.filtered_index_list[index]

        with self.env.begin(write=False) as txn:
            label_key = 'label-%09d'.encode() % index
            label = txn.get(label_key).decode('utf-8')
            img_key = 'image-%09d'.encode() % index
            imgbuf = txn.get(img_key)

            buf = six.BytesIO()
            buf.write(imgbuf)
            buf.seek(0)
            try:
                img = Image.open(buf).convert('L')

            except IOError:
                print(f'Corrupted image for {index}')
                # make dummy image and dummy label for corrupted image.
                img = Image.new('L', (256, 32))
                label = '[dummy_label]'
            

        return (img, label)


datebase = LmdbDataset('val/2k_chs_test')
index = 5
(img, label) = datebase.__getitem__(index)
img.show()

 

你可能感兴趣的:(小石的机器学习专栏)