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::idx::IdxSplit;
use crate::core::pack::{self, FetchProgress};
const RECORD_LEN: usize = 3_074;
#[path = "cifar100.gen.rs"]
mod generated;
pub(crate) use generated::CIFAR100_PACK;
pub use generated::{Cifar100Config, Cifar100Split};
pub struct Cifar100 {
dataset: MatrixDataset,
split: Cifar100Split,
}
impl Cifar100 {
pub fn decode(config: &Cifar100Config) -> Result<Decoded> {
Self::decode_with_progress(config, &mut |_| true)
}
pub fn decode_with_progress(
config: &Cifar100Config,
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(&CIFAR100_PACK, &root, progress)?;
let tar = extract::gunzip(&dir.join("cifar-100-binary.tar.gz"))?;
let member = match config.split {
IdxSplit::Train => "train.bin",
IdxSplit::Test => "test.bin",
IdxSplit::Validation => {
return Err(Error::invalid_argument(
"cifar100 carries no validation split",
));
}
};
let records = extract::tar_member(&tar, member)?;
if records.len() % RECORD_LEN != 0 {
return Err(Error::data_loss(format!(
"cifar100 {member} is {} bytes, not a multiple of {RECORD_LEN}",
records.len()
)));
}
let count = records.len() / RECORD_LEN;
let mut labels = Vec::with_capacity(count);
let mut pixels = Vec::with_capacity(count * 3_072);
for record in records.as_chunks::<RECORD_LEN>().0 {
labels.push(record[1]);
pixels.extend_from_slice(&record[2..]);
}
let scale = if config.normalize { 1.0 / 255.0 } else { 1.0 };
Ok(Decoded {
matrices: vec![
MatrixData {
name: "images",
shape: vec![count, 3, 32, 32],
values: pixels
.iter()
.map(|&v| f32::from(v) * scale)
.collect::<Vec<_>>(),
},
MatrixData {
name: "labels",
shape: vec![count],
values: labels.iter().map(|&v| f32::from(v)).collect::<Vec<_>>(),
},
],
text: None,
seq_len: 0,
})
}
pub fn load_with_progress_on(
engine: &Engine,
config: &Cifar100Config,
progress: &mut impl FnMut(FetchProgress) -> bool,
) -> Result<Self> {
let decoded = Self::decode_with_progress(config, progress)?;
Ok(Self {
dataset: MatrixDataset::new(
decoded.upload("images", engine)?,
decoded.upload("labels", engine)?,
)?,
split: config.split,
})
}
pub const fn images(&self) -> &Matrix {
self.dataset.x()
}
pub const fn labels(&self) -> &Matrix {
self.dataset.y()
}
pub const fn split(&self) -> Cifar100Split {
self.split
}
}
impl Dataset for Cifar100 {
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)
}
}