pub use crate::optimized_impl::{
CacheStatistics, DatasetConfig, DatasetMetadata, OptimizedCIFARDataset, OptimizedDataset,
OptimizedImageDataset,
};
pub type DatasetError = crate::VisionError;
pub type DatasetStats = CacheStatistics;
use crate::utils::{image_to_tensor, load_images_from_dir};
use crate::{Result, VisionError};
use image::DynamicImage;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use torsh_tensor::Tensor;
#[derive(Debug)]
pub struct ImageFolder {
pub class_to_idx: HashMap<String, usize>,
pub samples: Vec<(PathBuf, usize)>,
}
impl ImageFolder {
pub fn new<P: AsRef<Path>>(root: P) -> Result<Self> {
let mut class_to_idx = HashMap::new();
let mut samples = Vec::new();
let root_path = root.as_ref();
if let Ok(entries) = std::fs::read_dir(root_path) {
for (class_idx, entry) in entries.enumerate() {
if let Ok(entry) = entry {
if entry.file_type().map_or(false, |ft| ft.is_dir()) {
let class_name = entry.file_name().to_string_lossy().to_string();
class_to_idx.insert(class_name, class_idx);
if let Ok(class_entries) = std::fs::read_dir(entry.path()) {
for class_entry in class_entries.flatten() {
if let Some(ext) = class_entry.path().extension() {
if ["jpg", "jpeg", "png", "bmp"]
.contains(&ext.to_string_lossy().to_lowercase().as_str())
{
samples.push((class_entry.path(), class_idx));
}
}
}
}
}
}
}
}
Ok(Self {
class_to_idx,
samples,
})
}
pub fn len(&self) -> usize {
self.samples.len()
}
pub fn is_empty(&self) -> bool {
self.samples.is_empty()
}
}
pub mod optimized {
pub use super::{
DatasetConfig, DatasetMetadata, OptimizedCIFARDataset, OptimizedDataset,
OptimizedImageDataset,
};
}
pub mod legacy {
pub use super::ImageFolder;
}