開始使用免費開始

前處理音訊資料集

你正在強化精準農業應用,讓農民可以用語音指令控制機械。系統需要能辨識指令中的關鍵字,例如「Turn on the sprinkler irrigation system.」。

你將利用一個關鍵字偵測資料集,裡面包含像「on」這類關鍵字的音訊片段。請將音訊檔前處理為可供預先訓練的 Transformer 模型使用!

部分資料已預先載入:

  • dataset 包含音訊檔的訓練資料樣本。它已經包含 train 切分,因此在使用 dataset 時不需要再指定 train
  • 已從 transformers 匯入 AutoFeatureExtractor
  • model 等於 facebook/wav2vec2-base
  • max_duration 被設定為 1 秒。

本練習屬於課程

使用 PyTorch 高效訓練 AI 模型

檢視課程

練習說明

  • 使用 AutoFeatureExtractor 類別載入預先訓練的 feature_extractor
  • 使用 feature_extractor 中的取樣率設定 sampling_rate
  • 使用 max_duration 設定 audio_arraysmax_length

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Load a pre-trained feature extractor
feature_extractor = ____.____(model)

def preprocess_function(examples):
    audio_arrays = [x["array"] for x in examples["audio"]]
    inputs = feature_extractor(
        audio_arrays,
        # Set the sampling rate
        sampling_rate=____.____, 
        # Set the max length
        max_length=int(feature_extractor.sampling_rate * max_duration), 
        truncation=True)
    return inputs

encoded_dataset = dataset.map(preprocess_function, remove_columns=["audio", "file"], batched=True)
編輯並執行程式碼