Preprocess

Preprocess数据预处理

文本

使用Tokenizer将文本转换为标记序列,创建标记的数值表示,并将它们组装成张量。

预处理文本数据的主要工具是标记器。标记器根据一组规则将文本拆分为标记。标记被转换为数字,然后转换为张量,这些张量成为模型输入。模型所需的任何其他输入都由标记器添加。

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 , 0000000000000000 ]' attention_mask '[ 111111111111111111111111111 ] }
'''
input_ids是句子中每个标记对应的索引。
tention_mask表示是否应该关注一个标记。
当有多个序列时,token_type_ids标识一个 token 属于哪个序列。
'''
# 通过解码返回输入的内容
tokenizer.decode(encoded_input["input_ids"])
'​​[CLS] 不要干涉巫师的事务,因为他们很狡猾,而且很容易发怒。[SEP]'

padding填充

句子的长度并不总是相同的,但是张量(模型输入)需要具有统一的形状。因此填充是一种通过向较短的句子添加特殊填充标记来确保张量为矩形的策略。

将参数设置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 ] , 
               [ 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 ]
  ] , 
  ' 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 ]
  ] , 
  ' attention_mask ' : [ [ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 ] , 
                    [ 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 ]]}

Truncation 截断

另一方面,有时序列可能太长,模型无法处理。在这种情况下,您需要将序列截断为较短的长度。

将参数设置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,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 ] ,​​​​​​​​​​​​​
 ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
                    ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
                    [ 000000000000000 ] ]' attention_mask '[ [ 111111111000000 ][ 1111111111111 ][ 11111110000000 ] ] }

构建张量

基于Pytorch构建

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]])}

基于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="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)>}

不同的管道在其 __call__() 中对标记器参数的支持不同。text-2-text-generation 管道仅支持(即传递)truncationtext-generation 管道支持 max_lengthtruncationpaddingadd_special_tokens。在 fill-mask 管道中,标记器参数可以在 tokenizer_kwargs 参数(字典)中传递。

音频

对于音频任务,您需要一个特征提取器来为模型准备数据集。特征提取器旨在从原始音频数据中提取特征,并将其转换为张量。

from datasets import load_dataset, Audio
# 从公共数据中下载数据
dataset = load_dataset("PolyAI/minds14", name="en-US", split="train")
# 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指每秒测量语音信号中的数据点数。
"""

改变采样频率的方法有两种:

# 在加载数据时可以规定采样频率
dataset = dataset.cast_column("audio", Audio(sampling_rate=16_000))

# 在传递给特征提取时可以规定采样频率
audio_input = [dataset[0]["audio"]["array"]]
feature_extractor(audio_input, sampling_rate=16000)

如果存在数据的尺寸不合适,可以采用填充或者截断来处理可变序列;

# 创建一个函数来预处理数据集,使音频样本具有相同的长度。指定最大样本长度,特征提取器将填充或截断序列以匹配它;
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

计算机视觉

对于计算机视觉任务,您需要一个图像处理器来为模型准备数据集。图像预处理包括几个步骤,将图像转换为模型所需的输入。这些步骤包括但不限于调整大小、规范化、颜色通道校正以及将图像转换为张量。

图像预处理通常遵循某种形式的图像增强。图像预处理和图像增强都会转换图像数据,但它们的用途不同:

图像增强可以改变图像,有助于防止过度拟合并提高模型的稳健性。您可以发挥创意来增强数据 - 调整亮度和颜色、裁剪、旋转、调整大小、缩放等。但是,请注意不要通过增强改变图像的含义。
图像预处理可确保图像与模型的预期输入格式相匹配。在微调计算机视觉模型时,必须像最初训练模型时一样对图像进行预处理。
可以使用任何库来进行图像增强。对于图像预处理,请使用ImageProcessor与模型相关的库。

from transformers import AutoImageProcessor

image_processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224")


# 一些图像增强的功能
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)])

多模态数据

对于涉及多模态输入的任务,需要一个处理器来为模型准备数据集。处理器将两个处理对象(例如标记器和特征提取器)结合在一起。

使用AutoProcessor.from_pretrained()加载处理器:


from transformers import AutoProcessor

processor = AutoProcessor.from_pretrained("facebook/wav2vec2-base-960h")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值