-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathtrain_huggingface.py
More file actions
179 lines (149 loc) · 5.58 KB
/
Copy pathtrain_huggingface.py
File metadata and controls
179 lines (149 loc) · 5.58 KB
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
import os
from datasets import load_dataset, Audio
from transformers import Trainer, TrainingArguments
import torch
from train import DeepInfantModel, DeepInfantDataset
from sklearn.metrics import accuracy_score, precision_recall_fscore_support
import numpy as np
from pathlib import Path
import librosa
from torch.utils.data import Dataset
from transformers import PreTrainedModel, AutoConfig
class DeepInfantHFModel(PreTrainedModel):
def __init__(self, config):
super().__init__(config)
self.model = DeepInfantModel(num_classes=config.num_labels)
def forward(self, input_values, labels=None):
outputs = self.model(input_values)
loss = None
if labels is not None:
loss_fct = torch.nn.CrossEntropyLoss()
loss = loss_fct(outputs, labels)
return {"loss": loss, "logits": outputs} if loss is not None else outputs
class DeepInfantHFDataset(Dataset):
def __init__(self, dataset, transform=None):
self.dataset = dataset
self.transform = transform
def _process_audio(self, waveform, sample_rate):
# Resample if necessary
if sample_rate != 16000:
waveform = librosa.resample(waveform, orig_sr=sample_rate, target_sr=16000)
sample_rate = 16000
# Ensure consistent length (7 seconds)
target_length = 7 * 16000
if len(waveform) > target_length:
waveform = waveform[:target_length]
else:
waveform = np.pad(waveform, (0, target_length - len(waveform)))
# Generate mel spectrogram
mel_spec = librosa.feature.melspectrogram(
y=waveform,
sr=sample_rate,
n_fft=1024,
hop_length=256,
n_mels=80,
fmin=20,
fmax=8000
)
# Convert to log scale
mel_spec = librosa.power_to_db(mel_spec, ref=np.max)
return torch.FloatTensor(mel_spec)
def __len__(self):
return len(self.dataset)
def __getitem__(self, idx):
item = self.dataset[idx]
audio = item['audio']
waveform = audio['array']
sample_rate = audio['sampling_rate']
# Process audio to mel spectrogram
mel_spec = self._process_audio(waveform, sample_rate)
mel_spec = mel_spec.unsqueeze(0) # Add channel dimension
return {
"input_values": mel_spec,
"labels": item['label']
}
def compute_metrics(pred):
labels = pred.label_ids
preds = pred.predictions.argmax(-1)
precision, recall, f1, _ = precision_recall_fscore_support(
labels, preds, average='weighted'
)
acc = accuracy_score(labels, preds)
return {
'accuracy': acc,
'f1': f1,
'precision': precision,
'recall': recall
}
def main():
# Create checkpoint directory if it doesn't exist
checkpoint_dir = Path("./deepinfant/checkpoints")
checkpoint_dir.mkdir(parents=True, exist_ok=True)
# Load dataset from HuggingFace
dataset = load_dataset("your_username/your_dataset")
dataset = dataset.cast_column("audio", Audio(sampling_rate=16000))
# Split dataset
train_dataset = DeepInfantHFDataset(dataset['train'])
eval_dataset = DeepInfantHFDataset(dataset['validation'])
# Configure model with custom config instead of BERT
config = AutoConfig.from_dict({
"num_labels": 5, # Number of cry classifications
"hidden_size": 512, # Match LSTM hidden size
"num_attention_heads": 8,
"num_hidden_layers": 2, # Match LSTM layers
"model_type": "deepinfant",
"architectures": ["DeepInfantHFModel"]
})
model = DeepInfantHFModel(config)
# Define training arguments
training_args = TrainingArguments(
output_dir="./deepinfant",
num_train_epochs=50,
per_device_train_batch_size=32,
per_device_eval_batch_size=32,
evaluation_strategy="steps",
eval_steps=100,
save_strategy="steps",
save_steps=100,
save_total_limit=5,
load_best_model_at_end=True,
metric_for_best_model="accuracy",
greater_is_better=True,
push_to_hub=True,
hub_model_id="deepinfant",
logging_dir='./logs',
logging_steps=50,
resume_from_checkpoint=True,
)
# Initialize trainer
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
compute_metrics=compute_metrics,
)
# Check for existing checkpoints
last_checkpoint = None
if checkpoint_dir.exists():
checkpoints = [str(x) for x in checkpoint_dir.glob("checkpoint-*")]
if checkpoints:
last_checkpoint = max(checkpoints, key=lambda x: int(x.split("-")[-1]))
print(f"Resuming from checkpoint: {last_checkpoint}")
# Train model
trainer.train(resume_from_checkpoint=last_checkpoint)
# Save final model
trainer.save_model("./deepinfant/final")
# Push model to hub
trainer.push_to_hub()
# Save additional checkpoint information
checkpoint_info = {
"last_checkpoint": str(last_checkpoint) if last_checkpoint else None,
"total_steps": trainer.state.global_step,
"best_metric": trainer.state.best_metric,
}
with open(checkpoint_dir / "checkpoint_info.txt", "w") as f:
for key, value in checkpoint_info.items():
f.write(f"{key}: {value}\n")
if __name__ == "__main__":
main()