gpt4 book ai didi

python - 使用tensorflow加载LSUN数据集

转载 作者:行者123 更新时间:2023-12-01 08:50:15 25 4
gpt4 key购买 nike

最近我尝试找到读取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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com