用十位数加载LSUN数据集

2024-05-17 02:36:21 发布

您现在位置:Python中文网/ 问答频道 /正文

最近,我试图找到正确的方法来读取LSUN数据集,它是lmdb的形式。但是,我没有找到任何有用的信息。我想知道如何从lmdb读取图像数据,以及这样做的好处是什么。谢谢您!在


Tags: 数据方法图像信息lmdb形式lsun
1条回答
网友
1楼 · 发布于 2024-05-17 02:36:21

最后,我使用下面的代码从lmbd文件中提取LUSN图像。在

import os
import lmdb
from PIL import Image
import tempfile

def _export_mdb_images(db_path, out_dir=None, flat=True, limit=-1, size=256):
    out_dir = out_dir
    env = lmdb.open(
        db_path, map_size=1099511627776,
        max_readers=1000, readonly=True
    )
    count = 0
    with env.begin(write=False) as txn:
        cursor = txn.cursor()
        for key, val in cursor:
            key = str(key, 'utf-8')
            # decide image out directory
            if not flat:
                image_out_dir = os.path.join(out_dir, '/'.join(key[:6]))
            else:
                image_out_dir = out_dir

            # create the directory if an image out directory doesn't exist
            if not os.path.exists(image_out_dir):
                os.makedirs(image_out_dir)

            with tempfile.NamedTemporaryFile('wb') as temp:
                temp.write(val)
                temp.flush()
                temp.seek(0)
                image_out_path = os.path.join(image_out_dir, key + '.jpg')
                Image.open(temp.name).resize((size, size)).save(image_out_path)
            count += 1
            if count == limit:
                break
            if count % 1000 == 0:
                print('Finished', count, 'images')

print("start")
db_path = "path to lmbd"
out_dir = os.path.join(db_path, "data")
_export_mdb_images(db_path, out_dir)

相关问题 更多 >