Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds regression tests for #2007 #2018

Merged
merged 7 commits into from
May 13, 2022
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 54 additions & 10 deletions tests/ludwig/features/test_audio_feature.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import os
from random import choice
from string import ascii_lowercase, ascii_uppercase, digits
from typing import Dict

import pandas as pd
import pytest
import torch

from ludwig.features.audio_feature import AudioInputFeature
from ludwig.backend import LOCAL_BACKEND
from ludwig.constants import FILL_WITH_MEAN, PROC_COLUMN
from ludwig.features.audio_feature import AudioFeatureMixin, AudioInputFeature
from tests.integration_tests.utils import audio_feature, category_feature, generate_data

BATCH_SIZE = 2
SEQ_SIZE = 20
Expand All @@ -16,9 +20,9 @@
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"


@pytest.fixture(scope="module")
def audio_config():
return {
@pytest.mark.parametrize("encoder", ["rnn", "stacked_cnn", "parallel_cnn", "stacked_parallel_cnn", "rnn", "cnnrnn"])
def test_audio_input_feature(encoder: str) -> None:
audio_config = {
"name": "audio_feature",
"type": "audio",
"preprocessing": {
Expand All @@ -34,13 +38,53 @@ def audio_config():
"vocab": VOCAB,
"max_sequence_length": SEQ_SIZE,
"embedding_size": AUDIO_W_SIZE,
"encoder": encoder,
}


@pytest.mark.parametrize("encoder", ["rnn", "stacked_cnn", "parallel_cnn", "stacked_parallel_cnn", "rnn", "cnnrnn"])
def test_audio_input_feature(audio_config: Dict, encoder: str) -> None:
audio_config.update({"encoder": encoder})
audio_input_feature = AudioInputFeature(audio_config).to(DEVICE)
audio_tensor = torch.randn([BATCH_SIZE, SEQ_SIZE, AUDIO_W_SIZE], dtype=torch.float32).to(DEVICE)
encoder_output = audio_input_feature(audio_tensor)
assert encoder_output["encoder_output"].shape[1:] == audio_input_feature.output_shape


@pytest.mark.parametrize("feature_type", ["raw", "stft", "stft_phase", "group_delay", "fbank"])
def test_add_feature_data(feature_type, tmpdir):
preprocessing_params = {
"audio_file_length_limit_in_s": 3.0,
"missing_value_strategy": FILL_WITH_MEAN,
"in_memory": True,
"padding_value": 0,
"norm": "per_file",
"audio_feature": {
"type": feature_type,
"window_length_in_s": 0.04,
"window_shift_in_s": 0.02,
"num_filter_bands": 80,
},
}
audio_dest_folder = os.path.join(tmpdir, "generated_audio")
audio_feature_config = audio_feature(audio_dest_folder, preprocessing=preprocessing_params)
data_df_path = generate_data(
[audio_feature_config],
[category_feature(vocab_size=5, reduce_input="sum")],
os.path.join(tmpdir, "data.csv"),
num_examples=10,
)
data_df = pd.read_csv(data_df_path)
metadata = {
audio_feature_config["name"]: AudioFeatureMixin.get_feature_meta(
data_df[audio_feature_config["name"]], preprocessing_params, LOCAL_BACKEND
)
}

proc_df = {}
AudioFeatureMixin.add_feature_data(
feature_config=audio_feature_config,
input_df=data_df,
proc_df=proc_df,
metadata=metadata,
preprocessing_parameters=preprocessing_params,
backend=LOCAL_BACKEND,
skip_save_processed_input=False,
)

assert len(proc_df[audio_feature_config[PROC_COLUMN]]) == 10