-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Depth-Anything model
- Loading branch information
Showing
11 changed files
with
476 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
## Quick Start | ||
|
||
```shell | ||
cargo run -r --example depth-anything | ||
``` | ||
|
||
## ONNX Model | ||
|
||
- [depth-anything-s-dyn](https://github.com/jamjamjon/assets/releases/download/v0.0.1/depth-anything-s-dyn.onnx) | ||
- [depth-anything-b-dyn](https://github.com/jamjamjon/assets/releases/download/v0.0.1/depth-anything-b-dyn.onnx) | ||
- [depth-anything-l-dyn](https://github.com/jamjamjon/assets/releases/download/v0.0.1/depth-anything-l-dyn.onnx) | ||
|
||
|
||
## Results | ||
|
||
 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,23 @@ | ||
use usls::{models::DepthAnything, Annotator, DataLoader, Options}; | ||
|
||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
// visual | ||
let options = Options::default() | ||
.with_model("../models/depth-anything-s-dyn.onnx") | ||
.with_i00((1, 1, 8).into()) | ||
.with_i02((384, 512, 1024).into()) | ||
.with_i03((384, 512, 1024).into()); | ||
let model = DepthAnything::new(&options)?; | ||
|
||
// load | ||
let x = vec![DataLoader::try_read("./assets/2.jpg")?]; | ||
|
||
// run | ||
let y = model.run(&x)?; | ||
|
||
// annotate | ||
let annotator = Annotator::default().with_saveout("Depth-Anything"); | ||
annotator.annotate(&x, &y); | ||
|
||
Ok(()) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use crate::{ops, MinOptMax, Options, OrtEngine, Y}; | ||
use anyhow::Result; | ||
use image::{DynamicImage, ImageBuffer}; | ||
use ndarray::{Array, Axis, IxDyn}; | ||
|
||
#[derive(Debug)] | ||
pub struct DepthAnything { | ||
engine: OrtEngine, | ||
height: MinOptMax, | ||
width: MinOptMax, | ||
batch: MinOptMax, | ||
} | ||
|
||
impl DepthAnything { | ||
pub fn new(options: &Options) -> Result<Self> { | ||
let engine = OrtEngine::new(options)?; | ||
let (batch, height, width) = ( | ||
engine.batch().to_owned(), | ||
engine.height().to_owned(), | ||
engine.width().to_owned(), | ||
); | ||
engine.dry_run()?; | ||
|
||
Ok(Self { | ||
engine, | ||
height, | ||
width, | ||
batch, | ||
}) | ||
} | ||
|
||
pub fn run(&self, xs: &[DynamicImage]) -> Result<Vec<Y>> { | ||
let xs_ = ops::resize(xs, self.height.opt as u32, self.width.opt as u32)?; | ||
let xs_ = ops::normalize(xs_, 0.0, 255.0); | ||
let xs_ = ops::standardize(xs_, &[0.485, 0.456, 0.406], &[0.229, 0.224, 0.225]); | ||
let ys = self.engine.run(&[xs_])?; | ||
self.postprocess(ys, xs) | ||
} | ||
|
||
pub fn postprocess(&self, xs: Vec<Array<f32, IxDyn>>, xs0: &[DynamicImage]) -> Result<Vec<Y>> { | ||
let mut ys: Vec<Y> = Vec::new(); | ||
for (idx, luma) in xs[0].axis_iter(Axis(0)).enumerate() { | ||
let luma = luma | ||
.into_shape((self.height() as usize, self.width() as usize, 1))? | ||
.into_owned(); | ||
let v = luma.into_raw_vec(); | ||
let max_ = v.iter().max_by(|x, y| x.total_cmp(y)).unwrap(); | ||
let min_ = v.iter().min_by(|x, y| x.total_cmp(y)).unwrap(); | ||
let v = v | ||
.iter() | ||
.map(|x| (((*x - min_) / (max_ - min_)) * 255.).min(255.).max(0.) as u8) | ||
.collect::<Vec<_>>(); | ||
let luma: ImageBuffer<image::Luma<_>, Vec<u8>> = | ||
ImageBuffer::from_raw(self.width() as u32, self.height() as u32, v) | ||
.expect("Faild to create image from ndarray"); | ||
let luma = image::DynamicImage::from(luma); | ||
let luma = luma.resize_exact( | ||
xs0[idx].width(), | ||
xs0[idx].height(), | ||
image::imageops::FilterType::CatmullRom, | ||
); | ||
ys.push(Y::default().with_pixels(&luma.into_luma8().into_raw())); | ||
} | ||
Ok(ys) | ||
} | ||
|
||
pub fn batch(&self) -> isize { | ||
self.batch.opt | ||
} | ||
|
||
pub fn width(&self) -> isize { | ||
self.width.opt | ||
} | ||
|
||
pub fn height(&self) -> isize { | ||
self.height.opt | ||
} | ||
} |
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
Oops, something went wrong.