-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
231 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
use anyhow::Result; | ||
use image::DynamicImage; | ||
use ndarray::{Array, Dim, IxDyn, IxDynImpl}; | ||
|
||
use crate::ops; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub struct X { | ||
pub tensor: Array<f32, IxDyn>, | ||
images: Option<Vec<DynamicImage>>, | ||
} | ||
|
||
impl From<Array<f32, IxDyn>> for X { | ||
fn from(tensor: Array<f32, IxDyn>) -> Self { | ||
Self { | ||
tensor, | ||
..Default::default() | ||
} | ||
} | ||
} | ||
|
||
impl From<Vec<DynamicImage>> for X { | ||
fn from(images: Vec<DynamicImage>) -> Self { | ||
Self { | ||
images: Some(images.to_vec()), | ||
..Default::default() | ||
} | ||
} | ||
} | ||
|
||
impl X { | ||
pub fn load(images: &[DynamicImage]) -> Self { | ||
Self { | ||
images: Some(images.to_vec()), | ||
..Default::default() | ||
} | ||
} | ||
|
||
pub fn zeros(shape: &[usize]) -> Self { | ||
Self { | ||
tensor: Array::zeros(Dim(IxDynImpl::from(shape.to_vec()))), | ||
..Default::default() | ||
} | ||
} | ||
|
||
pub fn data(&self) -> &Array<f32, IxDyn> { | ||
&self.tensor | ||
} | ||
|
||
pub fn images(&self) -> Option<&Vec<DynamicImage>> { | ||
self.images.as_ref() | ||
} | ||
|
||
pub fn shape(&self) -> &[usize] { | ||
self.tensor.shape() | ||
} | ||
|
||
pub fn dims(&self) -> &[usize] { | ||
self.tensor.shape() | ||
} | ||
|
||
pub fn normalize(mut self, min_: f32, max_: f32) -> Self { | ||
self.tensor = (self.tensor - min_) / (max_ - min_); | ||
self | ||
} | ||
|
||
pub fn standardize(mut self, mean: &[f32], std: &[f32]) -> Self { | ||
let mean = Array::from_shape_vec((1, mean.len(), 1, 1), mean.to_vec()).unwrap(); | ||
let std = Array::from_shape_vec((1, std.len(), 1, 1), std.to_vec()).unwrap(); | ||
self.tensor = (self.tensor - mean) / std; | ||
self | ||
} | ||
|
||
pub fn letterbox( | ||
mut self, | ||
height: u32, | ||
width: u32, | ||
filter: &str, | ||
bg: Option<u8>, | ||
) -> Result<Self> { | ||
match &self.images { | ||
None => anyhow::bail!("No images found"), | ||
Some(xs) => { | ||
self.tensor = ops::letterbox(xs, height, width, filter, bg)?; | ||
Ok(self) | ||
} | ||
} | ||
} | ||
|
||
pub fn resize(mut self, height: u32, width: u32, filter: &str) -> Result<Self> { | ||
match &self.images { | ||
None => anyhow::bail!("No images found"), | ||
Some(xs) => { | ||
self.tensor = ops::resize(xs, height, width, filter)?; | ||
Ok(self) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// #[derive(Debug, Clone)] | ||
// pub struct BaseTensor(pub Array<f32, IxDyn>); | ||
|
||
// impl From<Array<f32, IxDyn>> for BaseTensor { | ||
// fn from(x: Array<f32, IxDyn>) -> Self { | ||
// Self(x) | ||
// } | ||
// } | ||
|
||
// // TODO: from_dynamic_image | ||
|
||
// impl BaseTensor { | ||
// pub fn zeros(shape: &[usize]) -> Self { | ||
// Self(Array::zeros(Dim(IxDynImpl::from(shape.to_vec())))) | ||
// } | ||
|
||
// pub fn data(&self) -> &Array<f32, IxDyn> { | ||
// &self.0 | ||
// } | ||
|
||
// pub fn shape(&self) -> &[usize] { | ||
// self.0.shape() | ||
// } | ||
|
||
// pub fn dims(&self) -> &[usize] { | ||
// self.0.shape() | ||
// } | ||
|
||
// pub fn normalize(mut self, min_: f32, max_: f32) -> Self { | ||
// self.0 = (self.0 - min_) / (max_ - min_); | ||
// self | ||
// } | ||
|
||
// pub fn standardize(mut self, mean: &[f32], std: &[f32]) -> Self { | ||
// // let shape = self.0.shape(); | ||
// let mean = Array::from_shape_vec((1, mean.len(), 1, 1), mean.to_vec()).unwrap(); | ||
// let std = Array::from_shape_vec((1, std.len(), 1, 1), std.to_vec()).unwrap(); | ||
// self.0 = (self.0 - mean) / std; | ||
// self | ||
// } | ||
|
||
// pub fn into_image() { | ||
// todo!() | ||
// } | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.