数据加载:LMDB 数据库#

概述#

此示例演示如何将以 Caffe 和 Caffe 2 格式存储在 LMDB 数据库中的数据与 DALI 一起使用。

Caffe LMDB 格式#

要使用以 Caffe 格式存储在 LMDB 中的数据,我们需要使用 readers.caffe 运算符。除了所有读取器通用的参数(例如 random_shuffle)之外,该运算符还接受 path 参数,该参数是 LMDB 存储目录的路径。

  1. 我们需要定义一个简单的流水线,该流水线获取以 Caffe 格式存储的图像,对其进行解码,并准备好将其摄取到 DL 框架中。

图像处理包括裁剪、归一化和完成 HWC -> CHW 转换过程。

DALI_EXTRA_PATH 环境变量应指向从 DALI extra 仓库 下载的数据所在的位置。

重要提示:确保您检出与已安装的 DALI 版本对应的正确发布标签。

[1]:
from nvidia.dali.pipeline import Pipeline
import nvidia.dali.fn as fn
import nvidia.dali.types as types
import numpy as np
from timeit import default_timer as timer
import numpy as np
import matplotlib.pyplot as plt
import os.path

test_data_root = os.environ["DALI_EXTRA_PATH"]
db_folder = os.path.join(test_data_root, "db", "lmdb")
batch_size = 9

pipe = Pipeline(batch_size=batch_size, num_threads=4, device_id=0)
with pipe:
    jpegs, labels = fn.readers.caffe(path=db_folder)
    images = fn.decoders.image(jpegs, device="mixed", output_type=types.RGB)
    output = fn.crop_mirror_normalize(
        images,
        dtype=types.FLOAT,
        crop=(224, 224),
        mean=[0.0, 0.0, 0.0],
        std=[1.0, 1.0, 1.0],
        crop_pos_x=fn.random.uniform(range=(0, 1)),
        crop_pos_y=fn.random.uniform(range=(0, 1)),
    )
    pipe.set_outputs(output, labels)
  1. 构建并运行流水线

[2]:
pipe.build()
pipe_out = pipe.run()
  1. 要可视化结果,请使用 matplotlib 库,该库期望图像采用 HWC 格式,但流水线的输出为 CHW 格式。

    注意CHW 是大多数深度学习框架的首选格式。

  2. 为了可视化目的,将图像转置回 HWC 布局。

[3]:
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt

%matplotlib inline


def show_images(image_batch):
    columns = 3
    rows = (batch_size + 1) // (columns)
    fig = plt.figure(figsize=(20, (20 // columns) * rows))
    gs = gridspec.GridSpec(rows, columns)
    for j in range(rows * columns):
        plt.subplot(gs[j])
        plt.axis("off")
        img_chw = image_batch.at(j)
        img_hwc = np.transpose(img_chw, (1, 2, 0)) / 255.0
        plt.imshow(img_hwc)
[4]:
images, labels = pipe_out
show_images(images.as_cpu())
../../../_images/examples_general_data_loading_dataloading_lmdb_6_0.svg

Caffe 2 LMDB 格式#

要使用以 Caffe 2 格式存储在 LMDB 中的数据,我们需要使用 readers.caffe2 运算符。与 readers.caffe 运算符类似,除了所有读取器通用的参数(例如 random_shuffle)之外,此运算符还接受 path 参数,该参数是 LMDB 存储目录的路径。

  1. 定义一个简单的流水线,该流水线加载以 Caffe 2 格式存储的图像,对其进行解码,并准备好将其摄取到 DL 框架中。

    图像处理包括裁剪、归一化和完成 HWC -> CHW 转换过程。

[5]:
db_folder = os.path.join(test_data_root, "db", "c2lmdb")

pipe = Pipeline(batch_size=batch_size, num_threads=4, device_id=0)
with pipe:
    jpegs, labels = fn.readers.caffe2(path=db_folder)
    images = fn.decoders.image(jpegs, device="mixed", output_type=types.RGB)
    output = fn.crop_mirror_normalize(
        images,
        dtype=types.FLOAT,
        crop=(224, 224),
        mean=[0.0, 0.0, 0.0],
        std=[1.0, 1.0, 1.0],
    )
    pipe.set_outputs(output, labels)
  1. 现在让我们构建并运行流水线

[6]:
pipe.build()
pipe_out = pipe.run()
  1. 要可视化结果,您可以使用 matplotlib 库,该库期望图像采用 HWC 格式,但流水线的输出为 CHW 格式。

    注意CHW 是大多数深度学习框架的首选格式。

  2. 为了可视化目的,将图像转置回 HWC 布局。

[7]:
images, labels = pipe_out
show_images(images.as_cpu())
../../../_images/examples_general_data_loading_dataloading_lmdb_12_0.svg