python - 使用tensorflow加载LSUN数据集

标签 python tensorflow deep-learning

最近我尝试找到读取lmdb形式的LSUN数据集的正确方法。但是,我没有找到任何有用的信息。我想知道如何从 lmdb 读取图像数据以及这种方式的优点是什么。谢谢!

最佳答案

最后,我使用以下代码从 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)

关于python - 使用tensorflow加载LSUN数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53154917/

相关文章:

deep-learning - Keras- LSTM- 输入大小错误

python - 无法定义名称 "userInput"

python - 添加辅助 y 轴

tensorflow - 在没有 CUDA 支持的情况下运行 Tensorboard

machine-learning - 图像预处理均值图像减法

machine-learning - 为什么我们在CNN末尾使用全连接层?

python - 从字符串中删除字符并进行替换不起作用

android - 使用 SL4A 将结果从 python 脚本返回到 Android 应用程序

python - 定义 TensorFlow 预制估计器的输入函数

python - 在具有多个输入和输出的 Keras model.fit 中将训练数据指定为元组 (x, y) 的正确方法