Skip to content

Commit

Permalink
Add doc for models
Browse files Browse the repository at this point in the history
  • Loading branch information
jamjamjon committed Dec 30, 2024
1 parent a30adb7 commit 615f3c0
Show file tree
Hide file tree
Showing 71 changed files with 673 additions and 100 deletions.
6 changes: 6 additions & 0 deletions examples/beit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## Quick Start

```shell
cargo run -r -F cuda --example beit -- --device cuda --dtype fp16
```

47 changes: 47 additions & 0 deletions examples/beit/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use usls::{models::ImageClassifier, Annotator, DataLoader, Options};

#[derive(argh::FromArgs)]
/// Example
struct Args {
/// dtype
#[argh(option, default = "String::from(\"auto\")")]
dtype: String,

/// device
#[argh(option, default = "String::from(\"cpu:0\")")]
device: String,

/// source image
#[argh(
option,
default = "vec![
String::from(\"images/dog.jpg\"),
String::from(\"images/siamese.png\"),
String::from(\"images/ailurus-fulgens.jpg\"),
]"
)]
source: Vec<String>,
}

fn main() -> anyhow::Result<()> {
let args: Args = argh::from_env();

// build model
let options = Options::beit_base()
.with_model_dtype(args.dtype.as_str().try_into()?)
.with_model_device(args.device.as_str().try_into()?)
.commit()?;
let mut model = ImageClassifier::try_from(options)?;

// load images
let xs = DataLoader::try_read_batch(&args.source)?;

// run
let ys = model.forward(&xs)?;

// annotate
let annotator = Annotator::default().with_saveout(model.spec());
annotator.annotate(&xs, &ys);

Ok(())
}
6 changes: 6 additions & 0 deletions examples/convnext/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## Quick Start

```shell
cargo run -r -F cuda --example convnext -- --device cuda --dtype fp16
```

47 changes: 47 additions & 0 deletions examples/convnext/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use usls::{models::ImageClassifier, Annotator, DataLoader, Options};

#[derive(argh::FromArgs)]
/// Example
struct Args {
/// dtype
#[argh(option, default = "String::from(\"auto\")")]
dtype: String,

/// device
#[argh(option, default = "String::from(\"cpu:0\")")]
device: String,

/// source image
#[argh(
option,
default = "vec![
String::from(\"images/dog.jpg\"),
String::from(\"images/siamese.png\"),
String::from(\"images/ailurus-fulgens.jpg\"),
]"
)]
source: Vec<String>,
}

fn main() -> anyhow::Result<()> {
let args: Args = argh::from_env();

// build model
let options = Options::convnext_v2_atto()
.with_model_dtype(args.dtype.as_str().try_into()?)
.with_model_device(args.device.as_str().try_into()?)
.commit()?;
let mut model = ImageClassifier::try_from(options)?;

// load images
let xs = DataLoader::try_read_batch(&args.source)?;

// run
let ys = model.forward(&xs)?;

// annotate
let annotator = Annotator::default().with_saveout(model.spec());
annotator.annotate(&xs, &ys);

Ok(())
}
5 changes: 5 additions & 0 deletions examples/d-fine/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Quick Start

```shell
cargo run -r --example d-fine
```
6 changes: 1 addition & 5 deletions examples/dfine/main.rs → examples/d-fine/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ use anyhow::Result;
use usls::{models::RTDETR, Annotator, DataLoader, Options};

fn main() -> Result<()> {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
.init();

// options
let options = Options::dfine_n_coco().commit()?;
let options = Options::d_fine_n_coco().commit()?;
let mut model = RTDETR::new(options)?;

// load
Expand Down
3 changes: 2 additions & 1 deletion examples/dfine/README.md → examples/deim/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Quick Start

```shell
cargo run -r --example dfine
cargo run -r --example deim
```


23 changes: 23 additions & 0 deletions examples/deim/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use anyhow::Result;
use usls::{models::RTDETR, Annotator, DataLoader, Options};

fn main() -> Result<()> {
// options
let options = Options::deim_dfine_s_coco().commit()?;
let mut model = RTDETR::new(options)?;

// load
let x = [DataLoader::try_read("./assets/bus.jpg")?];

// run
let y = model.forward(&x)?;
println!("{:?}", y);

// annotate
let annotator = Annotator::default()
.with_bboxes_thickness(3)
.with_saveout(model.spec());
annotator.annotate(&x, &y);

Ok(())
}
7 changes: 7 additions & 0 deletions examples/deit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Quick Start

```shell
cargo run -r -F cuda --example deit -- --device cuda --dtype fp16
```


47 changes: 47 additions & 0 deletions examples/deit/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use usls::{models::ImageClassifier, Annotator, DataLoader, Options};

#[derive(argh::FromArgs)]
/// Example
struct Args {
/// dtype
#[argh(option, default = "String::from(\"auto\")")]
dtype: String,

/// device
#[argh(option, default = "String::from(\"cpu:0\")")]
device: String,

/// source image
#[argh(
option,
default = "vec![
String::from(\"images/dog.jpg\"),
String::from(\"images/siamese.png\"),
String::from(\"images/ailurus-fulgens.jpg\"),
]"
)]
source: Vec<String>,
}

fn main() -> anyhow::Result<()> {
let args: Args = argh::from_env();

// build model
let options = Options::deit_tiny_distill()
.with_model_dtype(args.dtype.as_str().try_into()?)
.with_model_device(args.device.as_str().try_into()?)
.commit()?;
let mut model = ImageClassifier::try_from(options)?;

// load images
let xs = DataLoader::try_read_batch(&args.source)?;

// run
let ys = model.forward(&xs)?;

// annotate
let annotator = Annotator::default().with_saveout(model.spec());
annotator.annotate(&xs, &ys);

Ok(())
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Quick Start

```shell
cargo run -r -F cuda --example image-classification -- --device cuda --dtype fp16
cargo run -r -F cuda --example mobileone -- --device cuda --dtype fp16
```


Expand Down
52 changes: 52 additions & 0 deletions examples/fastvit/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use usls::{models::ImageClassifier, Annotator, DataLoader, Options};

#[derive(argh::FromArgs)]
/// Example
struct Args {
/// dtype
#[argh(option, default = "String::from(\"auto\")")]
dtype: String,

/// device
#[argh(option, default = "String::from(\"cpu:0\")")]
device: String,

/// source image
#[argh(
option,
default = "vec![
String::from(\"images/dog.jpg\"),
String::from(\"images/siamese.png\"),
String::from(\"images/ailurus-fulgens.jpg\"),
]"
)]
source: Vec<String>,
}

fn main() -> anyhow::Result<()> {
let args: Args = argh::from_env();

// build model
let options = Options::fastvit_t8_distill()
.with_model_dtype(args.dtype.as_str().try_into()?)
.with_model_device(args.device.as_str().try_into()?)
.commit()?;
let mut model = ImageClassifier::try_from(options)?;

// load images
let xs = DataLoader::try_read_batch(&args.source)?;

// run
let ys = model.forward(&xs)?;

// results
for (i, y) in ys.iter().enumerate() {
println!("{}: {:?}", i, y);
}

// annotate
let annotator = Annotator::default().with_saveout(model.spec());
annotator.annotate(&xs, &ys);

Ok(())
}
13 changes: 13 additions & 0 deletions examples/mobileone/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Quick Start

```shell
cargo run -r -F cuda --example mobileone -- --device cuda --dtype fp16
```


```shell
0: Y { Probs: { Top5: [(263, 0.6109131, Some("Pembroke, Pembroke Welsh corgi")), (264, 0.2062352, Some("Cardigan, Cardigan Welsh corgi")), (231, 0.028572788, Some("collie")), (273, 0.015174894, Some("dingo, warrigal, warragal, Canis dingo")), (248, 0.014367299, Some("Eskimo dog, husky"))] } }
1: Y { Probs: { Top5: [(284, 0.9907692, Some("siamese cat, Siamese")), (285, 0.0015794479, Some("Egyptian cat")), (174, 0.0015189401, Some("Norwegian elkhound, elkhound")), (225, 0.00031838714, Some("malinois")), (17, 0.00027021166, Some("jay"))] } }
2: Y { Probs: { Top5: [(387, 0.94238573, Some("lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens")), (368, 0.0029994072, Some("gibbon, Hylobates lar")), (277, 0.0016564301, Some("red fox, Vulpes vulpes")), (356, 0.0015081967, Some("weasel")), (295, 0.001427932, Some("American black bear, black bear, Ursus americanus, Euarctos americanus"))] } }

```
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,10 @@ struct Args {
}

fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
.init();
let args: Args = argh::from_env();

// build model
let options = Options::mobileone_s0()
// convnext_v2_atto()
// fastvit_sa24_distill()
// deit_tiny_distill()
// beit_base()
.with_model_dtype(args.dtype.as_str().try_into()?)
.with_model_device(args.device.as_str().try_into()?)
.commit()?;
Expand Down
4 changes: 4 additions & 0 deletions examples/rtdetr/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Quick Start


**Models exported from [RT-DETR](https://github.com/lyuwenyu/RT-DETR)**


```shell
cargo run -r --example rtdetr
```
Expand Down
4 changes: 0 additions & 4 deletions examples/rtdetr/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ use anyhow::Result;
use usls::{models::RTDETR, Annotator, DataLoader, Options};

fn main() -> Result<()> {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
.init();

// options
let options = Options::rtdetr_v2_s_coco()
// rtdetr_v1_r18vd_coco()
Expand Down
5 changes: 1 addition & 4 deletions examples/rtmo/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use anyhow::Result;
use usls::{
models::{COCO_SKELETONS_16, RTMO},
Annotator, DataLoader, Options,
};
use usls::{models::RTMO, Annotator, DataLoader, Options, COCO_SKELETONS_16};

fn main() -> Result<()> {
// build model
Expand Down
4 changes: 2 additions & 2 deletions examples/yolo/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Result;
use usls::{
models::{COCO_CLASS_NAMES_80, COCO_SKELETONS_16, IMAGENET_NAMES_1K, YOLO},
Annotator, DataLoader, Options,
models::YOLO, Annotator, DataLoader, Options, COCO_CLASS_NAMES_80, COCO_SKELETONS_16,
IMAGENET_NAMES_1K,
};

#[derive(argh::FromArgs, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ pub mod models;
mod xy;

pub use misc::*;
pub use models::{Kind, Options, Processor, ResizeMode, Scale, Task, Version};
pub use models::*;
pub use xy::*;
5 changes: 2 additions & 3 deletions src/misc/annotator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use ab_glyph::{FontArc, PxScale};
use anyhow::Result;
use image::{DynamicImage, GenericImage, Rgba, RgbaImage};
use imageproc::map::map_colors;
use log::{error, info};

use crate::{
string_now, Bbox, Color, ColorMap256, Dir, Hub, Keypoint, Mask, Mbr, Polygon, Prob, Y,
Expand Down Expand Up @@ -403,9 +402,9 @@ impl Annotator {
if save {
let saveout = self.saveout()?.join(format!("{}.png", string_now("-")));
match img_rgba.save(&saveout) {
Err(err) => error!("Saving failed: {:?}", err),
Err(err) => anyhow::bail!("Failed to save annotated image: {:?}", err),
Ok(_) => {
info!("Annotated image saved to: {:?}", saveout);
println!("Annotated image saved to: {:?}", saveout);
}
}
}
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 615f3c0

Please sign in to comment.