fix(test): force test training to run on CPU

This commit is contained in:
Hervé BREDIN committed 2023-05-24 17:17:08 +02:00
1 parent c12077c2ad
commit d19a728a0c
4 files changed
+87 -74

No files matched your search

+4 -5
View File
@@ -1,13 +1,13 @@
import numpy as np
import pytest
import pytorch_lightning as pl
from pyannote.core import SlidingWindowFeature
from pyannote.database import FileFinder, get_protocol
from pyannote.audio import Inference, Model
from pyannote.audio.core.task import Resolution
from pyannote.audio.models.segmentation.debug import SimpleSegmentationModel
from pyannote.audio.tasks import VoiceActivityDetection
from pyannote.core import SlidingWindowFeature
from pyannote.database import FileFinder, get_protocol
HF_SAMPLE_MODEL_ID = "pyannote/TestModelForContinuousIntegration"
@@ -29,8 +29,8 @@ def trained():
)
vad = VoiceActivityDetection(protocol, duration=2.0, batch_size=16, num_workers=4)
model = SimpleSegmentationModel(task=vad)
trainer = pl.Trainer(fast_dev_run=True)
trainer.fit(model, vad)
trainer = pl.Trainer(fast_dev_run=True, accelerator="cpu")
trainer.fit(model)
return protocol, model
@@ -91,7 +91,6 @@ def test_on_file_path(trained):
def test_skip_aggregation(pretrained_model, dev_file):
inference = Inference(pretrained_model, skip_aggregation=True)
scores = inference(dev_file)
assert len(scores.data.shape) == 3
+19 -25
View File
@@ -20,125 +20,119 @@ def protocol():
def test_train_segmentation(protocol):
segmentation = SpeakerDiarization(protocol)
model = SimpleSegmentationModel(task=segmentation)
trainer = Trainer(fast_dev_run=True)
trainer = Trainer(fast_dev_run=True, accelerator="cpu")
trainer.fit(model)
def test_train_voice_activity_detection(protocol):
voice_activity_detection = VoiceActivityDetection(protocol)
model = SimpleSegmentationModel(task=voice_activity_detection)
trainer = Trainer(fast_dev_run=True)
trainer = Trainer(fast_dev_run=True, accelerator="cpu")
trainer.fit(model)
def test_train_overlapped_speech_detection(protocol):
overlapped_speech_detection = OverlappedSpeechDetection(protocol)
model = SimpleSegmentationModel(task=overlapped_speech_detection)
trainer = Trainer(fast_dev_run=True)
trainer = Trainer(fast_dev_run=True, accelerator="cpu")
trainer.fit(model)
def test_finetune_with_task_that_does_not_need_setup_for_specs(protocol):
voice_activity_detection = VoiceActivityDetection(protocol)
model = SimpleSegmentationModel(task=voice_activity_detection)
trainer = Trainer(fast_dev_run=True)
trainer = Trainer(fast_dev_run=True, accelerator="cpu")
trainer.fit(model)
voice_activity_detection = VoiceActivityDetection(protocol)
model.task = voice_activity_detection
trainer = Trainer(fast_dev_run=True)
trainer = Trainer(fast_dev_run=True, accelerator="cpu")
trainer.fit(model)
def test_finetune_with_task_that_needs_setup_for_specs(protocol):
segmentation = SpeakerDiarization(protocol)
model = SimpleSegmentationModel(task=segmentation)
trainer = Trainer(fast_dev_run=True)
trainer = Trainer(fast_dev_run=True, accelerator="cpu")
trainer.fit(model)
segmentation = SpeakerDiarization(protocol)
model.task = segmentation
trainer = Trainer(fast_dev_run=True)
trainer = Trainer(fast_dev_run=True, accelerator="cpu")
trainer.fit(model)
def test_transfer_with_task_that_does_not_need_setup_for_specs(protocol):
segmentation = SpeakerDiarization(protocol)
model = SimpleSegmentationModel(task=segmentation)
trainer = Trainer(fast_dev_run=True)
trainer = Trainer(fast_dev_run=True, accelerator="cpu")
trainer.fit(model)
voice_activity_detection = VoiceActivityDetection(protocol)
model.task = voice_activity_detection
trainer = Trainer(fast_dev_run=True)
trainer = Trainer(fast_dev_run=True, accelerator="cpu")
trainer.fit(model)
def test_transfer_with_task_that_needs_setup_for_specs(protocol):
voice_activity_detection = VoiceActivityDetection(protocol)
model = SimpleSegmentationModel(task=voice_activity_detection)
trainer = Trainer(fast_dev_run=True)
trainer = Trainer(fast_dev_run=True, accelerator="cpu")
trainer.fit(model)
segmentation = SpeakerDiarization(protocol)
model.task = segmentation
trainer = Trainer(fast_dev_run=True)
trainer = Trainer(fast_dev_run=True, accelerator="cpu")
trainer.fit(model)
def test_finetune_freeze_with_task_that_needs_setup_for_specs(protocol):
segmentation = SpeakerDiarization(protocol)
model = SimpleSegmentationModel(task=segmentation)
trainer = Trainer(fast_dev_run=True)
trainer = Trainer(fast_dev_run=True, accelerator="cpu")
trainer.fit(model)
segmentation = SpeakerDiarization(protocol)
model.task = segmentation
model.freeze_up_to("mfcc")
trainer = Trainer(fast_dev_run=True)
trainer = Trainer(fast_dev_run=True, accelerator="cpu")
trainer.fit(model)
def test_finetune_freeze_with_task_that_does_not_need_setup_for_specs(protocol):
vad = VoiceActivityDetection(protocol)
model = SimpleSegmentationModel(task=vad)
trainer = Trainer(fast_dev_run=True)
trainer = Trainer(fast_dev_run=True, accelerator="cpu")
trainer.fit(model)
vad = VoiceActivityDetection(protocol)
model.task = vad
model.freeze_up_to("mfcc")
trainer = Trainer(fast_dev_run=True)
trainer = Trainer(fast_dev_run=True, accelerator="cpu")
trainer.fit(model)
def test_transfer_freeze_with_task_that_does_not_need_setup_for_specs(protocol):
segmentation = SpeakerDiarization(protocol)
model = SimpleSegmentationModel(task=segmentation)
trainer = Trainer(fast_dev_run=True)
trainer = Trainer(fast_dev_run=True, accelerator="cpu")
trainer.fit(model)
voice_activity_detection = VoiceActivityDetection(protocol)
model.task = voice_activity_detection
model.freeze_up_to("mfcc")
trainer = Trainer(fast_dev_run=True)
trainer = Trainer(fast_dev_run=True, accelerator="cpu")
trainer.fit(model)
def test_transfer_freeze_with_task_that_needs_setup_for_specs(protocol):
voice_activity_detection = VoiceActivityDetection(protocol)
model = SimpleSegmentationModel(task=voice_activity_detection)
trainer = Trainer(fast_dev_run=True)
trainer = Trainer(fast_dev_run=True, accelerator="cpu")
trainer.fit(model)
segmentation = SpeakerDiarization(protocol)
model.task = segmentation
model.freeze_up_to("mfcc")
trainer = Trainer(fast_dev_run=True)
trainer = Trainer(fast_dev_run=True, accelerator="cpu")
trainer.fit(model)
+47 -43
View File
@@ -1,6 +1,7 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
@@ -32,6 +33,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
@@ -48,6 +50,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
@@ -57,6 +60,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
@@ -82,6 +86,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
@@ -125,6 +130,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
@@ -176,54 +182,52 @@
" augmentation=augmentation,\n",
" )\n",
"\n",
" def setup(self, stage=None):\n",
" def setup(self):\n",
"\n",
" if stage == \"fit\":\n",
" # load metadata for training subset\n",
" self.train_metadata_ = list()\n",
" for training_file in self.protocol.train():\n",
" self.training_metadata_.append({\n",
" # path to audio file (str)\n",
" \"audio\": training_file[\"audio\"],\n",
" # duration of audio file (float)\n",
" \"duration\": training_file[\"duration\"],\n",
" # reference annotation (pyannote.core.Annotation)\n",
" \"annotation\": training_file[\"annotation\"],\n",
" })\n",
"\n",
" # load metadata for training subset\n",
" self.train_metadata_ = list()\n",
" for training_file in self.protocol.train():\n",
" self.training_metadata_.append({\n",
" # path to audio file (str)\n",
" \"audio\": training_file[\"audio\"],\n",
" # duration of audio file (float)\n",
" \"duration\": training_file[\"duration\"],\n",
" # reference annotation (pyannote.core.Annotation)\n",
" \"annotation\": training_file[\"annotation\"],\n",
" })\n",
" # gather the list of classes\n",
" classes = set()\n",
" for training_file in self.train_metadata_:\n",
" classes.update(training_file[\"reference\"].labels())\n",
" classes = sorted(classes)\n",
"\n",
" # gather the list of classes\n",
" classes = set()\n",
" for training_file in self.train_metadata_:\n",
" classes.update(training_file[\"reference\"].labels())\n",
" classes = sorted(classes)\n",
" # specify the addressed problem\n",
" self.specifications = Specifications(\n",
" # it is a multi-label classification problem\n",
" problem=Problem.MULTI_LABEL_CLASSIFICATION,\n",
" # we expect the model to output one prediction \n",
" # for the whole chunk\n",
" resolution=Resolution.CHUNK,\n",
" # the model will ingest chunks with that duration (in seconds)\n",
" duration=self.duration,\n",
" # human-readable names of classes\n",
" classes=classes)\n",
"\n",
" # specify the addressed problem\n",
" self.specifications = Specifications(\n",
" # it is a multi-label classification problem\n",
" problem=Problem.MULTI_LABEL_CLASSIFICATION,\n",
" # we expect the model to output one prediction \n",
" # for the whole chunk\n",
" resolution=Resolution.CHUNK,\n",
" # the model will ingest chunks with that duration (in seconds)\n",
" duration=self.duration,\n",
" # human-readable names of classes\n",
" classes=classes)\n",
" # `has_validation` is True iff protocol defines a development set\n",
" if not self.has_validation:\n",
" return\n",
"\n",
" # `has_validation` is True iff protocol defines a development set\n",
" if not self.has_validation:\n",
" return\n",
"\n",
" # load metadata for validation subset\n",
" self.validation_metadata_ = list()\n",
" for validation_file in self.protocol.development():\n",
" self.validation_metadata_.append({\n",
" \"audio\": validation_file[\"audio\"],\n",
" \"num_samples\": math.floor(validation_file[\"duration\"] / self.duration),\n",
" \"annotation\": validation_file[\"annotation\"],\n",
" })\n",
" \n",
" \n",
" # load metadata for validation subset\n",
" self.validation_metadata_ = list()\n",
" for validation_file in self.protocol.development():\n",
" self.validation_metadata_.append({\n",
" \"audio\": validation_file[\"audio\"],\n",
" \"num_samples\": math.floor(validation_file[\"duration\"] / self.duration),\n",
" \"annotation\": validation_file[\"annotation\"],\n",
" })\n",
" \n",
" \n",
"\n",
" def train__iter__(self):\n",
" # this method generates training samples, one at a time, \"ad infinitum\". each worker \n",
+17 -1
View File
@@ -20,6 +20,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
@@ -39,6 +40,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
@@ -49,6 +51,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
@@ -84,6 +87,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
@@ -103,6 +107,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
@@ -110,6 +115,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
@@ -130,6 +136,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
@@ -147,6 +154,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
@@ -161,10 +169,11 @@
"source": [
"import pytorch_lightning as pl\n",
"trainer = pl.Trainer(max_epochs=10)\n",
"trainer.fit(model, osd)"
"trainer.fit(model)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
@@ -185,6 +194,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
@@ -212,6 +222,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
@@ -219,6 +230,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
@@ -242,6 +254,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
@@ -258,6 +271,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
@@ -265,6 +279,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
@@ -297,6 +312,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [