use std::collections::BTreeMap;
use oa::data::{Dataset, MatrixDataset, Sample};
use oa::matrix::Matrix;
use oa::{Engine, Error, Result};
use crate::core::decode::{Decoded, MatrixData};
use crate::core::extract;
use crate::core::pack::{self, FetchProgress};
use crate::core::wav;
#[path = "speech_commands.gen.rs"]
mod generated;
pub(crate) use generated::SPEECH_COMMANDS_PACK;
pub use generated::{
CLIP_SAMPLE_RATE, CLIP_SAMPLES, COMMANDS, SpeechCommandsConfig, SpeechCommandsSplit,
};
pub struct SpeechCommands {
dataset: MatrixDataset,
split: SpeechCommandsSplit,
}
fn parse_list(text: &str) -> Vec<&str> {
text
.lines()
.map(str::trim)
.filter(|line| !line.is_empty())
.collect()
}
impl SpeechCommands {
pub fn decode(config: &SpeechCommandsConfig) -> Result<Decoded> {
Self::decode_with_progress(config, &mut |_| true)
}
pub fn decode_with_progress(
config: &SpeechCommandsConfig,
progress: &mut impl FnMut(FetchProgress) -> bool,
) -> Result<Decoded> {
let root = match &config.cache_dir {
Some(dir) => dir.clone(),
None => pack::data_root()?,
};
let dir = pack::ensure_pack_with_progress(&SPEECH_COMMANDS_PACK, &root, progress)?;
let tar = extract::gunzip(&dir.join("speech_commands_v0.02.tar.gz"))?;
let test_list = std::str::from_utf8(extract::tar_member(&tar, "testing_list.txt")?)
.map_err(|_| Error::data_loss("testing_list.txt is not valid UTF-8"))?;
let validation_list = std::str::from_utf8(extract::tar_member(&tar, "validation_list.txt")?)
.map_err(|_| Error::data_loss("validation_list.txt is not valid UTF-8"))?;
let (test_set, validation_set): (Vec<&str>, Vec<&str>) =
(parse_list(test_list), parse_list(validation_list));
let wanted = |name: &str| -> Option<()> {
match config.split {
SpeechCommandsSplit::Test if test_set.contains(&name) => Some(()),
SpeechCommandsSplit::Validation if validation_set.contains(&name) => Some(()),
SpeechCommandsSplit::Train
if !test_set.contains(&name) && !validation_set.contains(&name) =>
{
Some(())
}
_ => None,
}
};
let scale = if config.normalize {
1.0_f32 / 32_768.0
} else {
1.0
};
let mut members = BTreeMap::new();
extract::walk_tar(&tar, |name, bytes| {
let name = name.strip_prefix("./").unwrap_or(name);
if wanted(name).is_some() {
members.insert(name.to_owned(), bytes.to_vec());
}
Ok(())
})?;
let mut waveforms = Vec::new();
let mut label_values = Vec::new();
for (name, data) in members {
let Some((label, file)) = name.split_once('/') else {
continue;
};
if !file.ends_with(".wav") {
continue;
}
let Some(index) = COMMANDS.iter().position(|command| *command == label) else {
continue;
};
let clip = wav::parse(&data)?;
if clip.channels != 1 || clip.sample_rate != CLIP_SAMPLE_RATE || clip.bits_per_sample != 16 {
return Err(Error::data_loss(format!(
"{name}: expected mono {CLIP_SAMPLE_RATE} Hz s16 wav"
)));
}
let base = waveforms.len();
for &[lo, hi] in clip.pcm.as_chunks::<2>().0.iter().take(CLIP_SAMPLES) {
waveforms.push(i16::from_le_bytes([lo, hi]) as f32 * scale);
}
waveforms.resize(base + CLIP_SAMPLES, 0.0);
label_values.push(index as f32);
}
let count = label_values.len();
Ok(Decoded {
matrices: vec![
MatrixData {
name: "samples",
shape: vec![count, 1, CLIP_SAMPLES],
values: waveforms,
},
MatrixData {
name: "labels",
shape: vec![count],
values: label_values,
},
],
text: None,
seq_len: 0,
})
}
pub fn load_with_progress_on(
engine: &Engine,
config: &SpeechCommandsConfig,
progress: &mut impl FnMut(FetchProgress) -> bool,
) -> Result<Self> {
let decoded = Self::decode_with_progress(config, progress)?;
Ok(Self {
dataset: MatrixDataset::new(
decoded.upload("samples", engine)?,
decoded.upload("labels", engine)?,
)?,
split: config.split,
})
}
pub const fn samples(&self) -> &Matrix {
self.dataset.x()
}
pub const fn labels(&self) -> &Matrix {
self.dataset.y()
}
pub const fn split(&self) -> SpeechCommandsSplit {
self.split
}
}
impl Dataset for SpeechCommands {
fn len(&self) -> i64 {
self.dataset.len()
}
fn get_item(&self, index: i64) -> Result<Matrix> {
self.dataset.get_item(index)
}
fn get_sample(&self, index: i64) -> Result<Sample> {
self.dataset.get_sample(index)
}
}