-
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
12 changed files
with
326 additions
and
90 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 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
[toolchain] | ||
channel = "1.75" | ||
channel = "1.79" |
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use ndarray::{Array, Dim, IxDyn, IxDynImpl}; | ||
|
||
#[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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use crate::{BaseTensor, Options, Y}; | ||
|
||
pub trait Vision: Sized { | ||
type Input; // DynamicImage | ||
// type TensorType; // TODO: make it fixed? | ||
|
||
/// Creates a new instance of the model with the given options. | ||
fn new(options: Options) -> anyhow::Result<Self>; | ||
|
||
/// Preprocesses the input data. | ||
fn preprocess(&self, xs: &[Self::Input]) -> anyhow::Result<Vec<BaseTensor>>; | ||
|
||
/// Executes the model on the preprocessed data. | ||
fn inference(&mut self, xs: Vec<BaseTensor>) -> anyhow::Result<Vec<BaseTensor>>; | ||
|
||
/// Postprocesses the model's output. | ||
fn postprocess(&self, xs: Vec<BaseTensor>, xs0: &[Self::Input]) -> anyhow::Result<Vec<Y>>; | ||
|
||
/// Executes the full pipeline. | ||
fn run(&mut self, xs: &[Self::Input]) -> anyhow::Result<Vec<Y>> { | ||
let ys = self.preprocess(xs)?; | ||
let ys = self.inference(ys)?; | ||
let ys = self.postprocess(ys, xs)?; | ||
Ok(ys) | ||
} | ||
|
||
/// Executes the full pipeline. | ||
fn forward(&mut self, xs: &[Self::Input], profile: bool) -> anyhow::Result<Vec<Y>> { | ||
let t_pre = std::time::Instant::now(); | ||
let ys = self.preprocess(xs)?; | ||
let t_pre = t_pre.elapsed(); | ||
|
||
let t_exe = std::time::Instant::now(); | ||
let ys = self.inference(ys)?; | ||
let t_exe = t_exe.elapsed(); | ||
|
||
let t_post = std::time::Instant::now(); | ||
let ys = self.postprocess(ys, xs)?; | ||
let t_post = t_post.elapsed(); | ||
|
||
if profile { | ||
println!("> Pre: {t_pre:?} | Execution: {t_exe:?} | Post: {t_post:?}"); | ||
} | ||
|
||
Ok(ys) | ||
} | ||
} |
Oops, something went wrong.