use std::path::PathBuf;
use oa::matrix::Matrix;
use oa::{Engine, Error, Result};
use crate::idx::IdxSplit;
use crate::pack::{FetchProgress, Pack};
pub struct MatrixData {
pub name: &'static str,
pub shape: Vec<usize>,
pub values: Vec<f32>,
}
pub struct Decoded {
pub matrices: Vec<MatrixData>,
pub text: Option<String>,
pub seq_len: usize,
}
impl Decoded {
pub fn matrix(&self, name: &str) -> Result<&MatrixData> {
self
.matrices
.iter()
.find(|matrix| matrix.name == name)
.ok_or_else(|| Error::data_loss(format!("decoded pack has no '{name}' matrix")))
}
pub fn upload(&self, name: &str, engine: &Engine) -> Result<Matrix> {
let matrix = self.matrix(name)?;
Matrix::new_from_slice_on(engine, matrix.shape.clone(), &matrix.values)
}
}
#[derive(Clone, Debug)]
pub struct DecodeConfig {
pub split: IdxSplit,
pub normalize: bool,
pub count: Option<usize>,
pub cache_dir: Option<PathBuf>,
}
impl Default for DecodeConfig {
fn default() -> Self {
Self {
split: IdxSplit::Train,
normalize: true,
count: None,
cache_dir: None,
}
}
}
pub fn manifest(name: &str) -> Result<&'static Pack> {
match name {
"mnist" => Ok(&crate::image::mnist::MNIST_PACK),
"fashion_mnist" => Ok(&crate::image::fashion_mnist::FASHION_MNIST_PACK),
"kmnist" => Ok(&crate::image::kmnist::KMNIST_PACK),
"cifar10" => Ok(&crate::image::cifar10::CIFAR10_PACK),
"tiny_shakespeare" => Ok(&crate::text::tiny_shakespeare::SHAKESPEARE_PACK),
"moving_mnist" => Ok(&crate::video::moving_mnist::MOVING_MNIST_PACK),
_ => Err(Error::invalid_argument(format!(
"unknown dataset pack '{name}'"
))),
}
}
pub fn load(name: &str, config: &DecodeConfig) -> Result<Decoded> {
load_with_progress(name, config, &mut |_| true)
}
pub fn load_with_progress(
name: &str,
config: &DecodeConfig,
progress: &mut impl FnMut(FetchProgress) -> bool,
) -> Result<Decoded> {
match name {
"mnist" => crate::image::mnist::Mnist::decode_with_progress(
&crate::image::mnist::MnistConfig {
split: config.split,
cache_dir: config.cache_dir.clone(),
normalize: config.normalize,
},
progress,
),
"fashion_mnist" => crate::image::fashion_mnist::FashionMnist::decode(
&crate::image::fashion_mnist::FashionMnistConfig {
split: config.split,
cache_dir: config.cache_dir.clone(),
normalize: config.normalize,
},
),
"kmnist" => crate::image::kmnist::KMnist::decode_with_progress(
&crate::image::kmnist::KMnistConfig {
split: config.split,
cache_dir: config.cache_dir.clone(),
normalize: config.normalize,
},
progress,
),
"cifar10" => crate::image::cifar10::Cifar10::decode_with_progress(
&crate::image::cifar10::Cifar10Config {
split: config.split,
cache_dir: config.cache_dir.clone(),
normalize: config.normalize,
},
progress,
),
"tiny_shakespeare" => crate::text::tiny_shakespeare::TinyShakespeare::decode(
&crate::text::tiny_shakespeare::TinyShakespeareConfig {
seq_len: 1,
cache_dir: config.cache_dir.clone(),
},
),
"moving_mnist" => crate::video::moving_mnist::MovingMnist::decode(
&crate::video::moving_mnist::MovingMnistConfig {
count: config.count,
cache_dir: config.cache_dir.clone(),
normalize: config.normalize,
},
),
_ => Err(Error::invalid_argument(format!(
"unknown dataset pack '{name}'"
))),
}
}