Skip to content

Commit

Permalink
Add yolo-sam demo
Browse files Browse the repository at this point in the history
  • Loading branch information
jamjamjon committed Jul 28, 2024
1 parent 42026c8 commit c9c39d2
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 7 deletions.
Binary file added assets/dog.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 2 additions & 3 deletions benches/yolo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ fn yolo_stage_bench(

let t = std::time::Instant::now();
let _ys = black_box(model.postprocess(xs, x).unwrap());
let t1 = t.elapsed();
t_post += t1;
t_pipeline = t1;
t_post += t.elapsed();
t_pipeline += t0.elapsed();
}
match stage {
Stage::Pre => t_pre,
Expand Down
Binary file added examples/sam/demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions examples/sam/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

// build annotator
let annotator = Annotator::default()
.with_bboxes_thickness(7)
.without_bboxes_name(true)
.without_bboxes_conf(true)
.without_mbrs_name(true)
.without_mbrs_conf(true)
.without_mbrs(true)
.with_saveout("SAM");

// run & annotate
Expand Down
Binary file added examples/yolo-sam/demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
62 changes: 62 additions & 0 deletions examples/yolo-sam/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use usls::{
models::{SamPrompt, YOLOTask, YOLOVersion, SAM, YOLO},
Annotator, DataLoader, Options, Vision,
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
// build SAM
let options_encoder = Options::default()
.with_i00((1, 1, 1).into())
.with_model("mobile-sam-vit-t-encoder.onnx")?;
let options_decoder = Options::default()
.with_i11((1, 1, 1).into())
.with_i21((1, 1, 1).into())
.with_find_contours(true) // find contours or not
.with_model("mobile-sam-vit-t-decoder.onnx")?;
let mut sam = SAM::new(options_encoder, options_decoder)?;

// build YOLOv8-Det
let options_yolo = Options::default()
.with_yolo_version(YOLOVersion::V8)
.with_yolo_task(YOLOTask::Detect)
.with_model("yolov8m-dyn.onnx")?
.with_cuda(0)
.with_i00((1, 1, 4).into())
.with_i02((416, 640, 800).into())
.with_i03((416, 640, 800).into())
.with_find_contours(false)
.with_confs(&[0.45]);
let mut yolo = YOLO::new(options_yolo)?;

// load one image
let xs = vec![DataLoader::try_read("./assets/dog.jpg")?];

// build annotator
let annotator = Annotator::default()
.with_bboxes_thickness(7)
.without_bboxes_name(true)
.without_bboxes_conf(true)
.without_mbrs(true)
.with_saveout("YOLO+SAM");

// run & annotate
let ys_det = yolo.run(&xs)?;
for y_det in ys_det {
if let Some(bboxes) = y_det.bboxes() {
for bbox in bboxes {
let ys_sam = sam.run(
&xs,
&[SamPrompt::default().with_bbox(
bbox.xmin(),
bbox.ymin(),
bbox.xmax(),
bbox.ymax(),
)],
)?;
annotator.annotate(&xs, &ys_sam);
}
}
}

Ok(())
}
2 changes: 1 addition & 1 deletion src/core/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl OrtEngine {

// summary
println!(
"{CHECK_MARK} ONNX | OpSet: {} | EP: {:?} | DType: {:?} | Params: {}",
"{CHECK_MARK} Backend: ONNXRuntime | OpSet: {} | EP: {:?} | DType: {:?} | Params: {}",
model_proto.opset_import[0].version,
device,
inputs_attrs.dtypes,
Expand Down
2 changes: 1 addition & 1 deletion src/models/sam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl SAM {

// contours
let mut rng = thread_rng();
let id = rng.gen_range(0..=255);
let id = rng.gen_range(0..20);
if self.find_contours {
let contours: Vec<imageproc::contours::Contour<i32>> =
imageproc::contours::find_contours_with_threshold(&luma, 0);
Expand Down

0 comments on commit c9c39d2

Please sign in to comment.