#For example, so not exported
from fastai.vision.core import *
from fastai.vision.data import *数据块
DataLoaders 的高级 APITransformBlock
TransformBlock (type_tfms:list=None, item_tfms:list=None, batch_tfms:list=None, dl_type:fastai.data.core.TfmdDL=None, dls_kwargs:dict=None)
一个基础包装器,用于关联数据块 API 的默认变换
| 类型 | 默认值 | 详情 | |
|---|---|---|---|
| type_tfms | 列表 | 无 | 一个或多个 Transform |
| item_tfms | 列表 | 无 | 应用于单个项目的 ItemTransform |
| batch_tfms | 列表 | 无 | 应用于批次的 Transform 或 RandTransform |
| dl_type | TfmdDL | 无 | 任务特定的 TfmdDL,默认为 TfmdDL |
| dls_kwargs | 字典 | 无 | 传递给 DataLoaders 的附加参数 |
CategoryBlock
CategoryBlock (vocab:collections.abc.MutableSequence|pandas.core.series. Series=None, sort:bool=True, add_na:bool=False)
用于单标签分类目标的 TransformBlock
| 类型 | 默认值 | 详情 | |
|---|---|---|---|
| 词汇表 | collections.abc.MutableSequence | pandas.core.series.Series | 无 | 唯一类别名称列表 |
| 排序 | 布尔值 | 真 | 按字母顺序排序类别 |
| 添加 NA | 布尔值 | 假 | 将 #na# 添加到 vocab |
MultiCategoryBlock
MultiCategoryBlock (encoded:bool=False, vocab:collections.abc.MutableSequence|pandas.core.ser ies.Series=None, add_na:bool=False)
用于多标签分类目标的 TransformBlock
| 类型 | 默认值 | 详情 | |
|---|---|---|---|
| 已编码 | 布尔值 | 假 | 数据是否为独热编码格式 |
| 词汇表 | collections.abc.MutableSequence | pandas.core.series.Series | 无 | 唯一类别名称列表 |
| 添加 NA | 布尔值 | 假 | 将 #na# 添加到 vocab |
RegressionBlock
RegressionBlock (n_out:int=None)
用于浮点数目标的 TransformBlock
| 类型 | 默认值 | 详情 | |
|---|---|---|---|
| n_out | 整数 | 无 | 输出值数量 |
通用 API
DataBlock
DataBlock (blocks:list=None, dl_type:TfmdDL=None, getters:list=None, n_inp:int=None, item_tfms:list=None, batch_tfms:list=None, get_items=None, splitter=None, get_y=None, get_x=None)
用于快速构建 Datasets 和 DataLoaders 的通用容器。
| 类型 | 默认值 | 详情 | |
|---|---|---|---|
| 数据块 | 列表 | 无 | 一个或多个 TransformBlock |
| dl_type | TfmdDL | 无 | 任务特定的 TfmdDL,默认为 block 的 dl_type 或 TfmdDL |
| getter 函数 | 列表 | 无 | 应用于 get_items 结果的 getter 函数 |
| n_inp | 整数 | 无 | 输入数量 |
| item_tfms | 列表 | 无 | 应用于单个项目的 ItemTransform |
| batch_tfms | 列表 | 无 | 应用于批次的 Transform 或 RandTransform |
| get_items | NoneType | 无 | |
| 分割器 | NoneType | 无 | |
| get_y | NoneType | 无 | |
| get_x | NoneType | 无 |
要构建一个 DataBlock,你需要向库提供四项信息:输入/标签的类型,以及至少两个函数:get_items 和 splitter。你可能还需要包含 get_x 和 get_y,或者一个应用于 get_items 结果的更通用的 getters 列表。
splitter 是一个可调用对象,当使用 items 调用时,它会返回一个可迭代对象组成的元组,代表训练集和验证集的数据索引。
提供这些信息后,你将自动获得一个 Datasets 或一个 DataLoaders
DataBlock.datasets
DataBlock.datasets (source, verbose:bool=False)
从 source 创建一个 Datasets 对象
| 类型 | 默认值 | 详情 | |
|---|---|---|---|
| 源 | 数据源 | ||
| 详细模式 | 布尔值 | 假 | 显示详细信息 |
| 返回值 | Datasets |
DataBlock.dataloaders
DataBlock.dataloaders (source, path:str='.', verbose:bool=False, bs:int=64, shuffle:bool=False, num_workers:int=None, do_setup:bool=True, pin_memory=False, timeout=0, batch_size=None, drop_last=False, indexed=None, n=None, device=None, persistent_workers=False, pin_memory_device='', wif=None, before_iter=None, after_item=None, before_batch=None, after_batch=None, after_iter=None, create_batches=None, create_item=None, create_batch=None, retain=None, get_idxs=None, sample=None, shuffle_fn=None, do_batch=None)
从 source 创建一个 DataLoaders 对象
| 类型 | 默认值 | 详情 | |
|---|---|---|---|
| 源 | 数据源 | ||
| 路径 | 字符串 | . | 数据源和默认 Learner 路径 |
| 详细模式 | 布尔值 | 假 | 显示详细信息 |
| 批次大小 | 整数 | 64 | 批次大小 |
| 打乱 | 布尔值 | 假 | 是否打乱数据 |
| worker 数量 | 整数 | 无 | 并行使用的 CPU 核心数量(默认值:最多 16 个可用核心) |
| 执行 setup | 布尔值 | 真 | 是否对批次变换执行 setup() |
| pin_memory | 布尔值 | 假 | |
| 超时 | 整数 | 0 | |
| batch_size | NoneType | 无 | |
| drop_last | 布尔值 | 假 | |
| 已索引 | NoneType | 无 | |
| n | NoneType | 无 | |
| 设备 | NoneType | 无 | |
| persistent_workers | 布尔值 | 假 | |
| pin_memory_device | 字符串 | ||
| wif | NoneType | 无 | |
| before_iter | NoneType | 无 | |
| after_item | NoneType | 无 | |
| before_batch | NoneType | 无 | |
| after_batch | NoneType | 无 | |
| after_iter | NoneType | 无 | |
| create_batches | NoneType | 无 | |
| create_item | NoneType | 无 | |
| create_batch | NoneType | 无 | |
| retain | NoneType | 无 | |
| get_idxs | NoneType | 无 | |
| 采样 | NoneType | 无 | |
| shuffle_fn | NoneType | 无 | |
| do_batch | NoneType | 无 | |
| 返回值 | DataLoaders |
你可以通过传递函数来创建一个 DataBlock
mnist = DataBlock(blocks = (ImageBlock(cls=PILImageBW),CategoryBlock),
get_items = get_image_files,
splitter = GrandparentSplitter(),
get_y = parent_label)每种类型都带有将应用的默认变换
- 在基础层级,从基础元素(如文件名)创建元组形式(通常是输入、目标)的项目
- 在数据集的项目层级
- 在批次层级
它们分别称为类型变换、项目变换、批次变换。以 MNIST 为例,类型变换是创建 PILImageBW(用于输入)的方法和 Categorize 变换(用于目标),项目变换是 ToTensor,批次变换是 Cuda 和 IntToFloatTensor。你可以通过将任何其他变换传递给 DataBlock.datasets 或 DataBlock.dataloaders 来添加它们。
test_eq(mnist.type_tfms[0], [PILImageBW.create])
test_eq(mnist.type_tfms[1].map(type), [Categorize])
test_eq(mnist.default_item_tfms.map(type), [ToTensor])
test_eq(mnist.default_batch_tfms.map(type), [IntToFloatTensor])dsets = mnist.datasets(untar_data(URLs.MNIST_TINY))
test_eq(dsets.vocab, ['3', '7'])
x,y = dsets.train[0]
test_eq(x.size,(28,28))
show_at(dsets.train, 0, cmap='Greys', figsize=(2,2));
test_fail(lambda: DataBlock(wrong_kwarg=42, wrong_kwarg2='foo'))我们可以向 DataBlock 传递任意数量的块,然后通过更改 n_inp 来定义哪些是输入块,哪些是目标块。例如,将 n_inp 定义为 2 将把传递的前两个块视为输入,其余视为目标。
mnist = DataBlock((ImageBlock, ImageBlock, CategoryBlock), get_items=get_image_files, splitter=GrandparentSplitter(),
get_y=parent_label)
dsets = mnist.datasets(untar_data(URLs.MNIST_TINY))
test_eq(mnist.n_inp, 2)
test_eq(len(dsets.train[0]), 3)test_fail(lambda: DataBlock((ImageBlock, ImageBlock, CategoryBlock), get_items=get_image_files, splitter=GrandparentSplitter(),
get_y=[parent_label, noop],
n_inp=2), msg='get_y contains 2 functions, but must contain 1 (one for each output)')mnist = DataBlock((ImageBlock, ImageBlock, CategoryBlock), get_items=get_image_files, splitter=GrandparentSplitter(),
n_inp=1,
get_y=[noop, Pipeline([noop, parent_label])])
dsets = mnist.datasets(untar_data(URLs.MNIST_TINY))
test_eq(len(dsets.train[0]), 3)调试
DataBlock.summary
DataBlock.summary (source, bs:int=4, show_batch:bool=False, **kwargs)
逐步执行一个批次的变换管道,并可选地在临时 Dataloaders 上调用 show_batch(**kwargs)。
| 类型 | 默认值 | 详情 | |
|---|---|---|---|
| 源 | 数据源 | ||
| 批次大小 | 整数 | 4 | 批次大小 |
| show_batch | 布尔值 | 假 | 在摘要后调用 show_batch |
| kwargs | VAR_KEYWORD |
DataBlock.summary
DataBlock.summary (source, bs:int=4, show_batch:bool=False, **kwargs)
逐步执行一个批次的变换管道,并可选地在临时 Dataloaders 上调用 show_batch(**kwargs)。
| 类型 | 默认值 | 详情 | |
|---|---|---|---|
| 源 | 数据源 | ||
| 批次大小 | 整数 | 4 | 批次大小 |
| show_batch | 布尔值 | 假 | 在摘要后调用 show_batch |
| kwargs | VAR_KEYWORD |
除了逐步执行变换之外,summary() 还提供了一个快捷方式 dls.show_batch(...) 来查看数据。例如:
pets.summary(path/"images", bs=8, show_batch=True, unique=True,...)
是以下内容的快捷方式:
pets.summary(path/"images", bs=8)
dls = pets.dataloaders(path/"images", bs=8)
dls.show_batch(unique=True,...) # See different tfms effect on the same image.