预处理
在您可以在数据集上训练模型之前,需要将其预处理为预期的模型输入格式。无论您的数据是文本、图像还是音频,都需要将其转换并组装成张量批次。🤗 Transformers 提供了一组预处理类,以帮助您为模型准备数据。在本教程中,您将学习以下内容:
- 文本,使用Tokenizer将文本转换为一系列标记,创建标记的数字表示,并将它们组装成张量。
- 语音和音频,使用特征提取器从音频波形中提取序列特征并将其转换为张量。
- 图像输入使用ImageProcessor将图像转换为张量。
- 多模态输入,使用Processor来结合一个分词器和一个特征提取器或图像处理器。
AutoProcessor
总是有效,并自动为您使用的模型选择正确的类,无论您使用的是分词器、图像处理器、特征提取器还是处理器。
在开始之前,请安装 🤗 Datasets,以便您可以加载一些数据集进行实验:
pip install datasets
自然语言处理
预处理文本数据的主要工具是tokenizer。Tokenizer根据一组规则将文本分割成tokens。这些tokens被转换成数字,然后转换成张量,成为模型的输入。模型所需的任何额外输入都由tokenizer添加。
如果你计划使用预训练模型,使用与之关联的预训练分词器非常重要。这确保了文本的分割方式与预训练语料库相同,并且在预训练期间使用相同的对应标记到索引(通常称为词汇表)。
开始使用AutoTokenizer.from_pretrained()方法加载预训练的分词器。这将下载模型预训练时使用的词汇表:
>>> from transformers import AutoTokenizer
>>> tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-cased")
然后将您的文本传递给分词器:
>>> encoded_input = tokenizer("Do not meddle in the affairs of wizards, for they are subtle and quick to anger.")
>>> print(encoded_input)
{'input_ids': [101, 2079, 2025, 19960, 10362, 1999, 1996, 3821, 1997, 16657, 1010, 2005, 2027, 2024, 11259, 1998, 4248, 2000, 4963, 1012, 102],
'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]}
分词器返回一个包含三个重要项的字典:
- input_ids 是对应句子中每个标记的索引。
- attention_mask 表示是否应该关注某个标记。
- token_type_ids 用于在有多个序列时识别一个令牌属于哪个序列。
通过解码input_ids
返回您的输入:
>>> tokenizer.decode(encoded_input["input_ids"])
'[CLS] Do not meddle in the affairs of wizards, for they are subtle and quick to anger. [SEP]'
如你所见,分词器在句子中添加了两个特殊标记 - CLS
和 SEP
(分类器和分隔符)。并非所有模型都需要特殊标记,但如果需要,分词器会自动为你添加它们。
如果你有几个句子想要预处理,将它们作为列表传递给分词器:
>>> batch_sentences = [
... "But what about second breakfast?",
... "Don't think he knows about second breakfast, Pip.",
... "What about elevensies?",
... ]
>>> encoded_inputs = tokenizer(batch_sentences)
>>> print(encoded_inputs)
{'input_ids': [[101, 1252, 1184, 1164, 1248, 6462, 136, 102],
[101, 1790, 112, 189, 1341, 1119, 3520, 1164, 1248, 6462, 117, 21902, 1643, 119, 102],
[101, 1327, 1164, 5450, 23434, 136, 102]],
'token_type_ids': [[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]],
'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1]]}
填充
句子的长度并不总是相同,这可能是一个问题,因为张量(模型的输入)需要具有统一的形状。填充是一种策略,通过向较短的句子添加特殊的填充标记来确保张量是矩形的。
将 padding
参数设置为 True
,以便将批次中较短的序列填充以匹配最长的序列:
>>> batch_sentences = [
... "But what about second breakfast?",
... "Don't think he knows about second breakfast, Pip.",
... "What about elevensies?",
... ]
>>> encoded_input = tokenizer(batch_sentences, padding=True)
>>> print(encoded_input)
{'input_ids': [[101, 1252, 1184, 1164, 1248, 6462, 136, 102, 0, 0, 0, 0, 0, 0, 0],
[101, 1790, 112, 189, 1341, 1119, 3520, 1164, 1248, 6462, 117, 21902, 1643, 119, 102],
[101, 1327, 1164, 5450, 23434, 136, 102, 0, 0, 0, 0, 0, 0, 0, 0]],
'token_type_ids': [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]]}
第一句和第三句现在用0
填充,因为它们较短。
截断
在另一个极端,有时序列可能太长,模型无法处理。在这种情况下,您需要将序列截断为较短的长度。
将 truncation
参数设置为 True
以将序列截断为模型接受的最大长度:
>>> batch_sentences = [
... "But what about second breakfast?",
... "Don't think he knows about second breakfast, Pip.",
... "What about elevensies?",
... ]
>>> encoded_input = tokenizer(batch_sentences, padding=True, truncation=True)
>>> print(encoded_input)
{'input_ids': [[101, 1252, 1184, 1164, 1248, 6462, 136, 102, 0, 0, 0, 0, 0, 0, 0],
[101, 1790, 112, 189, 1341, 1119, 3520, 1164, 1248, 6462, 117, 21902, 1643, 119, 102],
[101, 1327, 1164, 5450, 23434, 136, 102, 0, 0, 0, 0, 0, 0, 0, 0]],
'token_type_ids': [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]]}
查看填充和截断概念指南,了解更多不同的填充和截断参数。
构建张量
最后,你希望分词器返回实际输入模型的张量。
将 return_tensors
参数设置为 pt
以使用 PyTorch,或设置为 tf
以使用 TensorFlow:
>>> batch_sentences = [
... "But what about second breakfast?",
... "Don't think he knows about second breakfast, Pip.",
... "What about elevensies?",
... ]
>>> encoded_input = tokenizer(batch_sentences, padding=True, truncation=True, return_tensors="pt")
>>> print(encoded_input)
{'input_ids': tensor([[101, 1252, 1184, 1164, 1248, 6462, 136, 102, 0, 0, 0, 0, 0, 0, 0],
[101, 1790, 112, 189, 1341, 1119, 3520, 1164, 1248, 6462, 117, 21902, 1643, 119, 102],
[101, 1327, 1164, 5450, 23434, 136, 102, 0, 0, 0, 0, 0, 0, 0, 0]]),
'token_type_ids': tensor([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]),
'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]])}
>>> batch_sentences = [
... "But what about second breakfast?",
... "Don't think he knows about second breakfast, Pip.",
... "What about elevensies?",
... ]
>>> encoded_input = tokenizer(batch_sentences, padding=True, truncation=True, return_tensors="tf")
>>> print(encoded_input)
{'input_ids': <tf.Tensor: shape=(2, 9), dtype=int32, numpy=
array([[101, 1252, 1184, 1164, 1248, 6462, 136, 102, 0, 0, 0, 0, 0, 0, 0],
[101, 1790, 112, 189, 1341, 1119, 3520, 1164, 1248, 6462, 117, 21902, 1643, 119, 102],
[101, 1327, 1164, 5450, 23434, 136, 102, 0, 0, 0, 0, 0, 0, 0, 0]],
dtype=int32)>,
'token_type_ids': <tf.Tensor: shape=(2, 9), dtype=int32, numpy=
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int32)>,
'attention_mask': <tf.Tensor: shape=(2, 9), dtype=int32, numpy=
array([[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int32)>}
音频
对于音频任务,您需要一个特征提取器来为模型准备数据集。特征提取器旨在从原始音频数据中提取特征,并将其转换为张量。
加载 MInDS-14 数据集(有关如何加载数据集的更多详细信息,请参阅 🤗 Datasets 教程),以了解如何将特征提取器与音频数据集一起使用:
>>> from datasets import load_dataset, Audio
>>> dataset = load_dataset("PolyAI/minds14", name="en-US", split="train")
访问audio
列的第一个元素以查看输入。调用audio
列会自动加载并重新采样音频文件:
>>> dataset[0]["audio"]
{'array': array([ 0. , 0.00024414, -0.00024414, ..., -0.00024414,
0. , 0. ], dtype=float32),
'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~JOINT_ACCOUNT/602ba55abb1e6d0fbce92065.wav',
'sampling_rate': 8000}
这将返回三个项目:
array
是加载的语音信号 - 并且可能已经重新采样 - 作为一维数组。path
指向音频文件的位置。sampling_rate
指的是每秒测量语音信号中的数据点的数量。
在本教程中,您将使用Wav2Vec2模型。查看模型卡片,您将了解到Wav2Vec2是在16kHz采样的语音音频上预训练的。确保您的音频数据的采样率与用于预训练模型的数据集的采样率相匹配非常重要。如果您的数据的采样率不同,那么您需要重新采样您的数据。
- 使用 🤗 Datasets 的 cast_column 方法将采样率上采样至 16kHz:
>>> dataset = dataset.cast_column("audio", Audio(sampling_rate=16_000))
- 再次调用
audio
列以重新采样音频文件:
>>> dataset[0]["audio"]
{'array': array([ 2.3443763e-05, 2.1729663e-04, 2.2145823e-04, ...,
3.8356509e-05, -7.3497440e-06, -2.1754686e-05], dtype=float32),
'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~JOINT_ACCOUNT/602ba55abb1e6d0fbce92065.wav',
'sampling_rate': 16000}
接下来,加载一个特征提取器来归一化和填充输入。当填充文本数据时,较短的序列会添加一个0
。同样的想法适用于音频数据。特征提取器会向array
中添加一个0
- 解释为静音。
使用AutoFeatureExtractor.from_pretrained()加载特征提取器:
>>> from transformers import AutoFeatureExtractor
>>> feature_extractor = AutoFeatureExtractor.from_pretrained("facebook/wav2vec2-base")
将音频array
传递给特征提取器。我们还建议在特征提取器中添加sampling_rate
参数,以便更好地调试可能发生的任何静默错误。
>>> audio_input = [dataset[0]["audio"]["array"]]
>>> feature_extractor(audio_input, sampling_rate=16000)
{'input_values': [array([ 3.8106556e-04, 2.7506407e-03, 2.8015103e-03, ...,
5.6335266e-04, 4.6588284e-06, -1.7142107e-04], dtype=float32)]}
就像分词器一样,你可以应用填充或截断来处理批次中的可变序列。看看这两个音频样本的序列长度:
>>> dataset[0]["audio"]["array"].shape
(173398,)
>>> dataset[1]["audio"]["array"].shape
(106496,)
创建一个函数来预处理数据集,使音频样本的长度相同。指定一个最大样本长度,特征提取器将填充或截断序列以匹配它:
>>> def preprocess_function(examples):
... audio_arrays = [x["array"] for x in examples["audio"]]
... inputs = feature_extractor(
... audio_arrays,
... sampling_rate=16000,
... padding=True,
... max_length=100000,
... truncation=True,
... )
... return inputs
将preprocess_function
应用于数据集中的前几个示例:
>>> processed_dataset = preprocess_function(dataset[:5])
样本长度现在相同并且匹配指定的最大长度。您现在可以将处理后的数据集传递给模型!
>>> processed_dataset["input_values"][0].shape
(100000,)
>>> processed_dataset["input_values"][1].shape
(100000,)
计算机视觉
对于计算机视觉任务,您需要一个图像处理器来为模型准备数据集。 图像预处理包括将图像转换为模型期望的输入的几个步骤。这些步骤 包括但不限于调整大小、归一化、颜色通道校正和将图像转换为张量。
图像预处理通常在某种形式的图像增强之后进行。图像预处理和图像增强都会转换图像数据,但它们的目的不同:
- 图像增强以有助于防止过拟合并增加模型鲁棒性的方式改变图像。你可以在如何增强数据方面发挥创意——调整亮度和颜色、裁剪、旋转、调整大小、缩放等。然而,要注意不要通过增强改变图像的含义。
- 图像预处理确保图像与模型的预期输入格式匹配。在微调计算机视觉模型时,必须按照模型最初训练时的方式进行图像预处理。
你可以使用任何你喜欢的库进行图像增强。对于图像预处理,请使用与模型关联的ImageProcessor
。
加载 food101 数据集(有关如何加载数据集的更多详细信息,请参阅 🤗 Datasets 教程),以了解如何将图像处理器与计算机视觉数据集一起使用:
使用 🤗 Datasets 的 split
参数,由于数据集非常大,只加载训练分割中的一小部分样本!
>>> from datasets import load_dataset
>>> dataset = load_dataset("food101", split="train[:100]")
接下来,使用🤗 Datasets的Image
功能查看图像:
>>> dataset[0]["image"]
![](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/vision-preprocess-tutorial.png)
使用AutoImageProcessor.from_pretrained()加载图像处理器:
>>> from transformers import AutoImageProcessor
>>> image_processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224")
首先,让我们添加一些图像增强。你可以使用任何你喜欢的库,但在本教程中,我们将使用torchvision的transforms
模块。如果你有兴趣使用其他数据增强库,可以在Albumentations或Kornia notebooks中学习如何使用。
- 这里我们使用
Compose
将几个转换链接在一起 -RandomResizedCrop
和ColorJitter
。请注意,对于调整大小,我们可以从image_processor
中获取图像大小要求。对于某些模型,期望有确切的高度和宽度,而对于其他模型,只定义了shortest_edge
。
>>> from torchvision.transforms import RandomResizedCrop, ColorJitter, Compose
>>> size = (
... image_processor.size["shortest_edge"]
... if "shortest_edge" in image_processor.size
... else (image_processor.size["height"], image_processor.size["width"])
... )
>>> _transforms = Compose([RandomResizedCrop(size), ColorJitter(brightness=0.5, hue=0.5)])
- 模型接受
pixel_values
作为其输入。ImageProcessor
可以负责图像的归一化,并生成适当的张量。创建一个函数,结合图像增强和图像预处理,为一批图像生成pixel_values
:
>>> def transforms(examples):
... images = [_transforms(img.convert("RGB")) for img in examples["image"]]
... examples["pixel_values"] = image_processor(images, do_resize=False, return_tensors="pt")["pixel_values"]
... return examples
在上面的示例中,我们设置了do_resize=False
,因为我们已经通过图像增强变换调整了图像的大小,并利用了适当的image_processor
中的size
属性。如果你在图像增强过程中没有调整图像大小,请省略此参数。默认情况下,ImageProcessor
会处理调整大小。
如果您希望将图像归一化作为增强转换的一部分,请使用 image_processor.image_mean
和 image_processor.image_std
值。
- 然后使用 🤗 Datasetsset_transform 来动态应用转换:
>>> dataset.set_transform(transforms)
- 现在,当你访问图像时,你会注意到图像处理器已经添加了
pixel_values
。你现在可以将处理后的数据集传递给模型了!
>>> dataset[0].keys()
这是应用变换后的图像效果。图像已被随机裁剪,并且其颜色属性已发生变化。
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> img = dataset[0]["pixel_values"]
>>> plt.imshow(img.permute(1, 2, 0))
![](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/preprocessed_image.png)
对于目标检测、语义分割、实例分割和全景分割等任务,ImageProcessor
提供了后处理方法。这些方法将模型的原始输出转换为有意义的预测,例如边界框或分割图。
填充
在某些情况下,例如在微调DETR时,模型在训练时应用了尺度增强。这可能导致批次中的图像大小不同。您可以使用DetrImageProcessor中的DetrImageProcessor.pad()
并定义一个自定义的collate_fn
来将图像批量处理在一起。
>>> def collate_fn(batch):
... pixel_values = [item["pixel_values"] for item in batch]
... encoding = image_processor.pad(pixel_values, return_tensors="pt")
... labels = [item["labels"] for item in batch]
... batch = {}
... batch["pixel_values"] = encoding["pixel_values"]
... batch["pixel_mask"] = encoding["pixel_mask"]
... batch["labels"] = labels
... return batch
多模态
对于涉及多模态输入的任务,您需要一个处理器来为模型准备数据集。处理器将两个处理对象(如分词器和特征提取器)结合在一起。
加载 LJ Speech 数据集(有关如何加载数据集的更多详细信息,请参阅 🤗 Datasets 教程),以了解如何使用处理器进行自动语音识别(ASR):
>>> from datasets import load_dataset
>>> lj_speech = load_dataset("lj_speech", split="train")
对于ASR,您主要关注的是audio
和text
,因此您可以删除其他列:
>>> lj_speech = lj_speech.map(remove_columns=["file", "id", "normalized_text"])
现在看一下 audio
和 text
列:
>>> lj_speech[0]["audio"]
{'array': array([-7.3242188e-04, -7.6293945e-04, -6.4086914e-04, ...,
7.3242188e-04, 2.1362305e-04, 6.1035156e-05], dtype=float32),
'path': '/root/.cache/huggingface/datasets/downloads/extracted/917ece08c95cf0c4115e45294e3cd0dee724a1165b7fc11798369308a465bd26/LJSpeech-1.1/wavs/LJ001-0001.wav',
'sampling_rate': 22050}
>>> lj_speech[0]["text"]
'Printing, in the only sense with which we are at present concerned, differs from most if not from all the arts and crafts represented in the Exhibition'
请记住,您应该始终将音频数据集的采样率重新采样,以匹配用于预训练模型的数据集的采样率!
>>> lj_speech = lj_speech.cast_column("audio", Audio(sampling_rate=16_000))
使用AutoProcessor.from_pretrained()加载处理器:
>>> from transformers import AutoProcessor
>>> processor = AutoProcessor.from_pretrained("facebook/wav2vec2-base-960h")
- 创建一个函数来处理包含在
array
中的音频数据到input_values
,并将text
标记化为labels
。这些是模型的输入:
>>> def prepare_dataset(example):
... audio = example["audio"]
... example.update(processor(audio=audio["array"], text=example["text"], sampling_rate=16000))
... return example
- 将
prepare_dataset
函数应用于样本:
>>> prepare_dataset(lj_speech[0])
处理器现在已经添加了input_values
和labels
,采样率也已经正确降采样到16kHz。你现在可以将处理后的数据集传递给模型了!