File size: 5,052 Bytes
344b95e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | {
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "shellscript"
}
},
"outputs": [],
"source": [
"pip install transformers"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from transformers import WhisperForAudioClassification\n",
"# Load pre-trained Whisper model\n",
"model = WhisperForAudioClassification.from_pretrained(\"openai/whisper-medium\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"# Load the CSV file\n",
"df = pd.read_csv('dataset.csv')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from transformers import WhisperProcessor\n",
"\n",
"# Initialize the Whisper processor\n",
"processor = WhisperProcessor.from_pretrained(\"openai/whisper-medium\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import librosa\n",
"import torch\n",
"\n",
"# Create a custom dataset class\n",
"class LispDataset(torch.utils.data.Dataset):\n",
" def __init__(self, df):\n",
" self.df = df\n",
"\n",
" def __len__(self):\n",
" return len(self.df)\n",
" \n",
" def __getitem__(self, idx):\n",
" row = self.df.iloc[idx]\n",
" audio_path = row['file_path']\n",
" label = row['label']\n",
"\n",
" audio, original_sr = librosa.load(audio_path, sr=44100)\n",
"\n",
" # Resample to target sample rate (if needed)\n",
" target_sr = 16000\n",
" if original_sr != target_sr:\n",
" audio = librosa.resample(audio, orig_sr=original_sr, target_sr=target_sr)\n",
"\n",
" # Extract mel features\n",
" mel_spectrogram = librosa.feature.melspectrogram(y=audio, sr=target_sr, n_mels=80, hop_length=512)\n",
" mel_spectrogram_db = librosa.power_to_db(mel_spectrogram) # Convert to decibels\n",
"\n",
" # Pad mel spectrogram to fixed length (assuming max_len is pre-defined)\n",
" max_len = 3000 # Replace with your desired maximum length\n",
" pad_width = (0, max_len - mel_spectrogram_db.shape[1]) # Calculate padding width\n",
" mel_spectrogram_db_padded = torch.nn.functional.pad(torch.from_numpy(mel_spectrogram_db).float(), \n",
" pad_width, mode='constant', value=0)\n",
"\n",
" # Convert to tensor\n",
" input_features = mel_spectrogram_db_padded\n",
"\n",
" # # Convert to tensor\n",
" # input_features = torch.from_numpy(mel_spectrogram_db_padded).float()\n",
"\n",
" # Create dictionary with expected key\n",
" return {'input_features': input_features, 'labels': label}\n",
" \n",
"# Create a DataLoader\n",
"train_dataset = LispDataset(df)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from transformers import TrainingArguments\n",
"\n",
"# Training arguments (adjust learning rate as needed)\n",
"training_args = TrainingArguments(\n",
" output_dir=\"./results\",\n",
" num_train_epochs=10,\n",
" per_device_train_batch_size=2,\n",
" learning_rate=5e-5,\n",
" fp16=True,\n",
" use_cpu=True,\n",
" warmup_ratio=0.1,\n",
" metric_for_best_model=\"accuracy\",\n",
" gradient_accumulation_steps=1 # No gradient accumulation (equivalent to no_auto_optimize=True)\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from torch.optim import AdamW # Import AdamW from PyTorch\n",
"\n",
"# Create the optimizer (adjust other hyperparameters as needed)\n",
"optimizer = AdamW(model.parameters(), lr=training_args.learning_rate)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from torch.optim.lr_scheduler import LambdaLR\n",
"\n",
"lambda1 = lambda epoch: epoch // 30\n",
"scheduler = LambdaLR(optimizer, lr_lambda=[lambda1,])\n",
"\n",
"optimizertuple = (optimizer,scheduler)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from transformers import Trainer\n",
"\n",
"# Trainer instance\n",
"trainer = Trainer(\n",
" model=model,\n",
" args=training_args,\n",
" train_dataset=train_dataset,\n",
" optimizers=optimizertuple, # Wrap optimizer in a tuple\n",
")\n",
"\n",
"# Start training\n",
"trainer.train()"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|