Skip to content

Commit b2b3619

Browse files
authored
feat(yolo): integrate slsl::Tensor to accelerate preprocessing, inference, and postprocessing (#143)
* Attempt to introduce `slsl` * update: hub download and system font * Accelerate image pre-processing
1 parent 7e8b77d commit b2b3619

17 files changed

Lines changed: 2270 additions & 347 deletions

File tree

.github/dependabot.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ updates:
44
directory: "/"
55
schedule:
66
interval: "monthly"
7-
open-pull-requests-limit: 3
7+
open-pull-requests-limit: 5

Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "usls"
33
edition = "2021"
4-
version = "0.1.0"
4+
version = "0.1.1"
55
rust-version = "1.85"
66
description = "A Rust library integrated with ONNXRuntime, providing a collection of ML models."
77
repository = "https://github.com/jamjamjon/usls"
@@ -18,7 +18,7 @@ ab_glyph = { version = "0.2.29" }
1818
image = { version = "0.25" }
1919
imageproc = { version = "0.25" }
2020
ndarray = { version = "0.16.1", features = ["rayon", "serde"] }
21-
indicatif = { version = "0.17.11" }
21+
indicatif = { version = "0.18" }
2222
log = "0.4.26"
2323
minifb = { version = "0.28.0", optional = true }
2424
rand = { version = "0.9" }
@@ -30,12 +30,12 @@ rayon = { version = "1.10.0" }
3030
glob = "0.3.2"
3131
dirs = "6.0.0"
3232
natord = "1.0.9"
33-
geo = "0.30.0"
33+
geo = "0.31.0"
3434
chrono = "0.4.40"
3535
regex = "1.11.1"
3636
tempfile = "3.19.1"
3737
video-rs = { version = "0.10.3", features = ["ndarray"], optional = true }
38-
fast_image_resize = { version = "=5.1.2", features = ["image"] }
38+
fast_image_resize = { version = "=5.1.4", features = ["image", "rayon"] }
3939
ndarray-npy = "0.9.1"
4040
half = { version = "2.6.0", features = ["bytemuck", "num-traits"] }
4141
prost = "0.14.1"
@@ -47,6 +47,7 @@ ort = { version = "=2.0.0-rc.10", default-features = false, optional = true, fea
4747
hf-hub = { version = "0.4.3", default-features = false, features = ["ureq", "native-tls"] }
4848
tokenizers = { version = "0.21.1" }
4949
paste = "1.0.15"
50+
slsl = { version = "0.0.5", features = ["rayon"] }
5051

5152

5253
[dev-dependencies]

examples/clip/main.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use anyhow::Result;
2-
use ndarray::Axis;
2+
use slsl::Tensor;
33
use usls::{models::Clip, Config, DataLoader};
44

5-
#[derive(argh::FromArgs)]
65
/// CLIP Example
6+
#[derive(argh::FromArgs)]
77
struct Args {
88
/// device
99
#[argh(option, default = "String::from(\"cpu:0\")")]
@@ -44,22 +44,29 @@ fn main() -> Result<()> {
4444
"There is a doll with red hair and a clock on a table.",
4545
"Some people holding wine glasses in a restaurant.",
4646
];
47-
let feats_text = model.encode_texts(&texts)?.norm(1)?;
47+
48+
// encode texts
49+
let feats_text = model.encode_texts(&texts)?;
50+
let feats_text_norm = feats_text.norm_l2_keepdim(-1)?.to_dtype::<f32>()?;
51+
let feats_text = (feats_text / feats_text_norm).t()?;
4852

4953
// load images
5054
let dl = DataLoader::new("./examples/clip/images")?.build()?;
5155

5256
// run
5357
for images in &dl {
54-
let feats_image = model.encode_images(&images)?.norm(1)?;
58+
// encode image
59+
let feats_image: Tensor = model.encode_images(&images)?;
60+
let feats_image_norm = feats_image.norm_l2_keepdim(-1)?.to_dtype::<f32>()?;
61+
let feats_image = feats_image / feats_image_norm;
5562

5663
// use image to query texts
57-
let matrix = (feats_image * 100.).dot2(&feats_text)?.softmax(1)?;
64+
let matrix = (feats_image * 100.0f32).matmul(&feats_text)?.softmax(-1)?;
5865

5966
// Process each image's matching scores
60-
for (i, row) in matrix.axis_iter(Axis(0)).enumerate() {
67+
for (i, row) in matrix.iter_dim(0).enumerate() {
6168
let (id, &score) = row
62-
.iter()
69+
.iter::<f32>()
6370
.enumerate()
6471
.max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
6572
.unwrap();
@@ -72,6 +79,7 @@ fn main() -> Result<()> {
7279
);
7380
}
7481
}
82+
7583
usls::perf(false);
7684

7785
Ok(())

examples/yolo/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ fn main() -> Result<()> {
171171
.with_topk(args.topk)
172172
.retain_classes(&args.retain_classes)
173173
.exclude_classes(&args.exclude_classes)
174-
.with_model_num_dry_run(2);
174+
.with_model_num_dry_run(5);
175175
if args.use_coco_80_classes {
176176
config = config.with_class_names(&NAMES_COCO_80);
177177
}
@@ -234,7 +234,7 @@ fn main() -> Result<()> {
234234
// run & annotate
235235
for xs in &dl {
236236
let ys = model.forward(&xs)?;
237-
// println!("ys: {:?}", ys);
237+
println!("ys: {:?}", ys);
238238

239239
for (x, y) in xs.iter().zip(ys.iter()) {
240240
annotator.annotate(x, y)?.save(format!(

0 commit comments

Comments
 (0)