diff --git a/Cargo.toml b/Cargo.toml index 5beb404..867f42d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "usls" -version = "0.0.15" +version = "0.0.16" edition = "2021" description = "A Rust library integrated with ONNXRuntime, providing a collection of ML models." repository = "https://github.com/jamjamjon/usls" @@ -38,6 +38,7 @@ video-rs = { version = "0.9.0", features = ["ndarray"] } natord = "1.0.9" tracing = "0.1.40" tracing-subscriber = "0.3.18" +minifb = "0.27.0" [features] diff --git a/benches/yolo.rs b/benches/yolo.rs index 5a9e806..9ee3196 100644 --- a/benches/yolo.rs +++ b/benches/yolo.rs @@ -56,9 +56,8 @@ pub fn benchmark_cuda(c: &mut Criterion, h: isize, w: isize) -> Result<()> { .with_cuda(0) // .with_cpu() .with_dry_run(0) - .with_i00((1, 1, 4).into()) - .with_i02((320, h, 1280).into()) - .with_i03((320, w, 1280).into()) + .with_ixx(0, 2, (320, h, 1280).into()) + .with_ixx(0, 3, (320, w, 1280).into()) .with_confs(&[0.2, 0.15]); let mut model = YOLO::new(options)?; diff --git a/examples/blip/main.rs b/examples/blip/main.rs index b0cc929..da7fc89 100644 --- a/examples/blip/main.rs +++ b/examples/blip/main.rs @@ -4,19 +4,14 @@ fn main() -> Result<(), Box> { // visual let options_visual = Options::default() .with_model("blip/visual-base.onnx")? - .with_i00((1, 1, 4).into()) + // .with_ixx(0, 2, 384.into()) + // .with_ixx(0, 3, 384.into()) .with_profile(false); // textual let options_textual = Options::default() .with_model("blip/textual-base.onnx")? .with_tokenizer("blip/tokenizer.json")? - .with_i00((1, 1, 4).into()) // input_id: batch - .with_i01((1, 1, 4).into()) // input_id: seq_len - .with_i10((1, 1, 4).into()) // attention_mask: batch - .with_i11((1, 1, 4).into()) // attention_mask: seq_len - .with_i20((1, 1, 4).into()) // encoder_hidden_states: batch - .with_i30((1, 1, 4).into()) // encoder_attention_mask: batch .with_profile(false); // build model diff --git a/examples/clip/main.rs b/examples/clip/main.rs index b9c1cc9..0fd03ce 100644 --- a/examples/clip/main.rs +++ b/examples/clip/main.rs @@ -2,17 +2,12 @@ use usls::{models::Clip, DataLoader, Options}; fn main() -> Result<(), Box> { // visual - let options_visual = Options::default() - .with_model("clip/visual-base-dyn.onnx")? - .with_i00((1, 1, 4).into()) - .with_profile(false); + let options_visual = Options::default().with_model("clip/visual-base-dyn.onnx")?; // textual let options_textual = Options::default() .with_model("clip/textual-base-dyn.onnx")? - .with_tokenizer("clip/tokenizer.json")? - .with_i00((1, 1, 4).into()) - .with_profile(false); + .with_tokenizer("clip/tokenizer.json")?; // build model let mut model = Clip::new(options_visual, options_textual)?; diff --git a/examples/dataloader/main.rs b/examples/dataloader/main.rs index 40484f2..dbc27fe 100644 --- a/examples/dataloader/main.rs +++ b/examples/dataloader/main.rs @@ -1,4 +1,6 @@ -use usls::{models::YOLO, Annotator, DataLoader, Options, Vision, YOLOTask, YOLOVersion}; +use usls::{ + models::YOLO, Annotator, DataLoader, Device, Options, Viewer, Vision, YOLOTask, YOLOVersion, +}; fn main() -> anyhow::Result<()> { tracing_subscriber::fmt() @@ -6,40 +8,57 @@ fn main() -> anyhow::Result<()> { .init(); let options = Options::new() - .with_cuda(0) + .with_device(Device::Cuda(0)) .with_model("yolo/v8-m-dyn.onnx")? .with_yolo_version(YOLOVersion::V8) .with_yolo_task(YOLOTask::Detect) - .with_i00((1, 1, 4).into()) - .with_i02((0, 640, 640).into()) - .with_i03((0, 640, 640).into()) + .with_batch(2) + .with_ixx(0, 2, (416, 640, 800).into()) + .with_ixx(0, 3, (416, 640, 800).into()) .with_confs(&[0.2]); let mut model = YOLO::new(options)?; + // build annotator + let annotator = Annotator::new() + .with_bboxes_thickness(4) + .with_saveout("YOLO-DataLoader"); + // build dataloader let dl = DataLoader::new( - // "images/bus.jpg", // remote image + // "images/bus.jpg", // remote image // "../images", // image folder // "../demo.mp4", // local video // "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4", // remote video // "rtsp://admin:xyz@192.168.2.217:554/h265/ch1/", // rtsp h264 stream - "./assets/bus.jpg", // local image + // "./assets/bus.jpg", // local image + "../7.mp4", )? .with_batch(1) .build()?; - // build annotator - let annotator = Annotator::new() - .with_bboxes_thickness(4) - .with_saveout("YOLO-DataLoader"); + let mut viewer = Viewer::new().with_delay(10).with_scale(1.).resizable(true); - // run + // iteration for (xs, _) in dl { - // std::thread::sleep(std::time::Duration::from_millis(100)); - let ys = model.forward(&xs, false)?; - annotator.annotate(&xs, &ys); + // inference & annotate + let ys = model.run(&xs)?; + let images_plotted = annotator.plot(&xs, &ys, false)?; + + // show image + viewer.imshow(&images_plotted)?; + + // check out window and key event + if !viewer.is_open() || viewer.is_key_pressed(usls::Key::Escape) { + break; + } + + // write video + viewer.write_batch(&images_plotted)?; } + // finish video write + viewer.finish_write()?; + // images -> video // DataLoader::is2v("runs/YOLO-DataLoader", &["runs", "is2v"], 24)?; diff --git a/examples/db/main.rs b/examples/db/main.rs index b252653..b133216 100644 --- a/examples/db/main.rs +++ b/examples/db/main.rs @@ -3,9 +3,9 @@ use usls::{models::DB, Annotator, DataLoader, Options}; fn main() -> Result<(), Box> { // build model let options = Options::default() - .with_i00((1, 4, 8).into()) - .with_i02((608, 960, 1280).into()) - .with_i03((608, 960, 1280).into()) + .with_ixx(0, 0, (1, 4, 8).into()) + .with_ixx(0, 2, (608, 960, 1280).into()) + .with_ixx(0, 3, (608, 960, 1280).into()) // .with_trt(0) .with_confs(&[0.4]) .with_min_width(5.0) diff --git a/examples/depth-anything/main.rs b/examples/depth-anything/main.rs index 20c3163..d339ff3 100644 --- a/examples/depth-anything/main.rs +++ b/examples/depth-anything/main.rs @@ -5,13 +5,12 @@ fn main() -> Result<(), Box> { let options = Options::default() // .with_model("depth-anything/v1-s-dyn.onnx")? .with_model("depth-anything/v2-s.onnx")? - .with_i00((1, 1, 8).into()) - .with_i02((384, 512, 1024).into()) - .with_i03((384, 512, 1024).into()); + .with_ixx(0, 2, (384, 512, 1024).into()) + .with_ixx(0, 3, (384, 512, 1024).into()); let mut model = DepthAnything::new(options)?; // load - let x = [DataLoader::try_read("images/2.jpg")?]; + let x = [DataLoader::try_read("images/street.jpg")?]; // run let y = model.run(&x)?; diff --git a/examples/dinov2/main.rs b/examples/dinov2/main.rs index 7186959..4cc7732 100644 --- a/examples/dinov2/main.rs +++ b/examples/dinov2/main.rs @@ -4,9 +4,8 @@ fn main() -> Result<(), Box> { // build model let options = Options::default() .with_model("dinov2/s-dyn.onnx")? - .with_i00((1, 1, 1).into()) - .with_i02((224, 224, 224).into()) - .with_i03((224, 224, 224).into()); + .with_ixx(0, 2, 224.into()) + .with_ixx(0, 3, 224.into()); let mut model = Dinov2::new(options)?; let x = [DataLoader::try_read("images/bus.jpg")?]; let y = model.run(&x)?; diff --git a/examples/florence2/main.rs b/examples/florence2/main.rs index 546ebb9..07cc7d1 100644 --- a/examples/florence2/main.rs +++ b/examples/florence2/main.rs @@ -1,130 +1,35 @@ use usls::{models::Florence2, Annotator, DataLoader, Options, Task}; fn main() -> Result<(), Box> { + let batch_size = 3; + // vision encoder let options_vision_encoder = Options::default() - .with_model("florence2/base-vision-encoder.onnx")? - .with_i00((1, 2, 4).into()) - .with_i02((512, 768, 800).into()) - .with_i03((512, 768, 800).into()) - .with_profile(false) - .with_cuda(0); + .with_model("florence2/base-vision-encoder-f16.onnx")? + .with_ixx(0, 2, (512, 768, 800).into()) + .with_ixx(0, 3, 768.into()) + .with_ixx(0, 0, (1, batch_size as _, 8).into()); // text embed let options_text_embed = Options::default() - .with_model("florence2/base-embed-tokens.onnx")? - .with_i00((1, 2, 4).into()) - .with_i01((1, 2, 20).into()) // seq_length + .with_model("florence2/base-embed-tokens-f16.onnx")? .with_tokenizer("florence2/tokenizer.json")? - .with_profile(false); + .with_batch(batch_size); // transformer encoder let options_encoder = Options::default() - .with_model("florence2/base-encoder.onnx")? - .with_i00((1, 2, 4).into()) - .with_i01((1, 2, 300).into()) // encoder_sequence_length - .with_i10((1, 2, 4).into()) - .with_i11((1, 2, 300).into()) // encoder_sequence_length - .with_profile(false); + .with_model("florence2/base-encoder-f16.onnx")? + .with_batch(batch_size); // transformer decoder let options_decoder = Options::default() - .with_model("florence2/base-decoder.onnx")? - .with_i00((1, 2, 4).into()) - .with_i01((1, 2, 300).into()) // encoder_sequence_length - .with_i10((1, 2, 4).into()) - .with_i11((1, 2, 300).into()) // encoder_sequence_length - .with_i20((1, 2, 4).into()) - .with_i21((1, 2, 300).into()) // encoder_sequence_length - .with_profile(false); + .with_model("florence2/base-decoder-f16.onnx")? + .with_batch(batch_size); // transformer decoder merged let options_decoder_merged = Options::default() - .with_model("florence2/base-decoder-merged.onnx")? - // encoder_attention_mask - .with_i00((1, 2, 4).into()) - .with_i01((1, 2, 300).into()) // encoder_sequence_length - // encoder_hidden_states - .with_i10((1, 2, 4).into()) - .with_i11((1, 2, 300).into()) // encoder_sequence_length - // inputs_embeds - .with_i20((1, 2, 4).into()) - .with_i21((1, 2, 300).into()) // encoder_sequence_length - // past_key_values.0.decoder.key - .with_i30((1, 2, 4).into()) - .with_i32_((1, 2, 1).into()) - // past_key_values.0.decoder.value - .with_i40((1, 2, 4).into()) - .with_i42((1, 2, 1).into()) - // past_key_values.0.encoder.key - .with_i50((1, 2, 4).into()) - .with_i52((1, 2, 1).into()) - // past_key_values.0.decoder.value - .with_i60((1, 2, 4).into()) - .with_i62((1, 2, 1).into()) - // past_key_values.1.decoder.key - .with_i70((1, 2, 4).into()) - .with_i72((1, 2, 1).into()) - // past_key_values.1.decoder.value - .with_i80((1, 2, 4).into()) - .with_i82((1, 2, 1).into()) - // past_key_values.1.encoder.key - .with_i90((1, 2, 4).into()) - .with_i92((1, 2, 1).into()) - // past_key_values.1.decoder.value - .with_i100((1, 2, 4).into()) - .with_i102((1, 2, 1).into()) - // past_key_values.2.decoder.key - .with_i110((1, 2, 4).into()) - .with_i112((1, 2, 1).into()) - // past_key_values.2.decoder.value - .with_i120((1, 2, 4).into()) - .with_i122((1, 2, 1).into()) - // past_key_values.2.encoder.key - .with_i130((1, 2, 4).into()) - .with_i132((1, 2, 1).into()) - // past_key_values.2.decoder.value - .with_i140((1, 2, 4).into()) - .with_i142((1, 2, 1).into()) - // past_key_values.3.decoder.key - .with_i150((1, 2, 4).into()) - .with_i152((1, 2, 1).into()) - // past_key_values.3.decoder.value - .with_i160((1, 2, 4).into()) - .with_i162((1, 2, 1).into()) - // past_key_values.3.encoder.key - .with_i170((1, 2, 4).into()) - .with_i172((1, 2, 1).into()) - // past_key_values.3.decoder.value - .with_i180((1, 2, 4).into()) - .with_i182((1, 2, 1).into()) - // past_key_values.4.decoder.key - .with_i190((1, 2, 4).into()) - .with_i192((1, 2, 1).into()) - // past_key_values.4.decoder.value - .with_i200((1, 2, 4).into()) - .with_i202((1, 2, 1).into()) - // past_key_values.4.encoder.key - .with_i210((1, 2, 4).into()) - .with_i212((1, 2, 1).into()) - // past_key_values.4.decoder.value - .with_i220((1, 2, 4).into()) - .with_i222((1, 2, 1).into()) - // past_key_values.5.decoder.key - .with_i230((1, 2, 4).into()) - .with_i232((1, 2, 1).into()) - // past_key_values.5.decoder.value - .with_i240((1, 2, 4).into()) - .with_i242((1, 2, 1).into()) - // past_key_values.5.encoder.key - .with_i250((1, 2, 4).into()) - .with_i252((1, 2, 1).into()) - // past_key_values.5.decoder.value - .with_i260((1, 2, 4).into()) - .with_i262((1, 2, 1).into()) - //use_cache_branch - .with_i270((1, 2, 1).into()) - .with_profile(false); + .with_model("florence2/base-decoder-merged-f16.onnx")? + .with_batch(batch_size); // build model let mut model = Florence2::new( diff --git a/examples/grounding-dino/main.rs b/examples/grounding-dino/main.rs index a9446f6..2ceb61c 100644 --- a/examples/grounding-dino/main.rs +++ b/examples/grounding-dino/main.rs @@ -2,20 +2,20 @@ use usls::{models::GroundingDINO, Annotator, DataLoader, Options}; fn main() -> Result<(), Box> { let opts = Options::default() - .with_i00((1, 1, 4).into()) - .with_i02((640, 800, 1200).into()) - .with_i03((640, 1200, 1200).into()) - .with_i10((1, 1, 4).into()) - .with_i11((256, 256, 512).into()) - .with_i20((1, 1, 4).into()) - .with_i21((256, 256, 512).into()) - .with_i30((1, 1, 4).into()) - .with_i31((256, 256, 512).into()) - .with_i40((1, 1, 4).into()) - .with_i41((256, 256, 512).into()) - .with_i50((1, 1, 4).into()) - .with_i51((256, 256, 512).into()) - .with_i52((256, 256, 512).into()) + .with_ixx(0, 0, (1, 1, 4).into()) + .with_ixx(0, 2, (640, 800, 1200).into()) + .with_ixx(0, 3, (640, 1200, 1200).into()) + // .with_i10((1, 1, 4).into()) + // .with_i11((256, 256, 512).into()) + // .with_i20((1, 1, 4).into()) + // .with_i21((256, 256, 512).into()) + // .with_i30((1, 1, 4).into()) + // .with_i31((256, 256, 512).into()) + // .with_i40((1, 1, 4).into()) + // .with_i41((256, 256, 512).into()) + // .with_i50((1, 1, 4).into()) + // .with_i51((256, 256, 512).into()) + // .with_i52((256, 256, 512).into()) .with_model("grounding-dino/swint-ogc-dyn-u8.onnx")? // TODO: current onnx model does not support bs > 1 // .with_model("grounding-dino/swint-ogc-dyn-f32.onnx")? .with_tokenizer("grounding-dino/tokenizer.json")? diff --git a/examples/modnet/main.rs b/examples/modnet/main.rs index 697b8b5..660ded5 100644 --- a/examples/modnet/main.rs +++ b/examples/modnet/main.rs @@ -4,9 +4,8 @@ fn main() -> Result<(), Box> { // build model let options = Options::default() .with_model("modnet/dyn-f32.onnx")? - .with_i00((1, 1, 4).into()) - .with_i02((416, 512, 800).into()) - .with_i03((416, 512, 800).into()); + .with_ixx(0, 2, (416, 512, 800).into()) + .with_ixx(0, 3, (416, 512, 800).into()); let mut model = MODNet::new(options)?; // load image diff --git a/examples/rtmo/main.rs b/examples/rtmo/main.rs index 3c2f052..aae1706 100644 --- a/examples/rtmo/main.rs +++ b/examples/rtmo/main.rs @@ -4,7 +4,6 @@ fn main() -> Result<(), Box> { // build model let options = Options::default() .with_model("rtmo/s-dyn.onnx")? - .with_i00((1, 1, 8).into()) .with_nk(17) .with_confs(&[0.3]) .with_kconfs(&[0.5]); diff --git a/examples/sam/main.rs b/examples/sam/main.rs index 91b74d4..72ed218 100644 --- a/examples/sam/main.rs +++ b/examples/sam/main.rs @@ -29,9 +29,6 @@ fn main() -> Result<(), Box> { .with_model("sam/sam-vit-b-encoder-u8.onnx")?; let options_decoder = Options::default() - .with_i00((1, 1, 1).into()) - .with_i11((1, 1, 1).into()) - .with_i21((1, 1, 1).into()) .with_sam_kind(SamKind::Sam) // .with_model("sam/sam-vit-b-decoder.onnx")?; // .with_model("sam/sam-vit-b-decoder-singlemask.onnx")?; @@ -44,8 +41,6 @@ fn main() -> Result<(), Box> { // .with_model("sam/sam2-hiera-small-encoder.onnx")?; .with_model("sam/sam2-hiera-base-plus-encoder.onnx")?; let options_decoder = Options::default() - .with_i31((1, 1, 1).into()) - .with_i41((1, 1, 1).into()) .with_sam_kind(SamKind::Sam2) // .with_model("sam/sam2-hiera-tiny-decoder.onnx")?; // .with_model("sam/sam2-hiera-small-decoder.onnx")?; @@ -57,9 +52,6 @@ fn main() -> Result<(), Box> { Options::default().with_model("sam/mobile-sam-vit-t-encoder.onnx")?; let options_decoder = Options::default() - .with_i00((1, 1, 1).into()) - .with_i11((1, 1, 1).into()) - .with_i21((1, 1, 1).into()) .with_sam_kind(SamKind::MobileSam) .with_model("sam/mobile-sam-vit-t-decoder.onnx")?; (options_encoder, options_decoder, "Mobile-SAM") @@ -68,9 +60,6 @@ fn main() -> Result<(), Box> { let options_encoder = Options::default().with_model("sam/sam-hq-vit-t-encoder.onnx")?; let options_decoder = Options::default() - .with_i00((1, 1, 1).into()) - .with_i21((1, 1, 1).into()) - .with_i31((1, 1, 1).into()) .with_sam_kind(SamKind::SamHq) .with_model("sam/sam-hq-vit-t-decoder.onnx")?; (options_encoder, options_decoder, "SAM-HQ") @@ -78,9 +67,6 @@ fn main() -> Result<(), Box> { SamKind::EdgeSam => { let options_encoder = Options::default().with_model("sam/edge-sam-3x-encoder.onnx")?; let options_decoder = Options::default() - .with_i00((1, 1, 1).into()) - .with_i11((1, 1, 1).into()) - .with_i21((1, 1, 1).into()) .with_sam_kind(SamKind::EdgeSam) .with_model("sam/edge-sam-3x-decoder.onnx")?; (options_encoder, options_decoder, "Edge-SAM") @@ -88,9 +74,8 @@ fn main() -> Result<(), Box> { }; let options_encoder = options_encoder .with_cuda(args.device_id) - .with_i00((1, 1, 1).into()) - .with_i02((800, 1024, 1024).into()) - .with_i03((800, 1024, 1024).into()); + .with_ixx(0, 2, (800, 1024, 1024).into()) + .with_ixx(0, 3, (800, 1024, 1024).into()); let options_decoder = options_decoder .with_cuda(args.device_id) .use_low_res_mask(args.use_low_res_mask) diff --git a/examples/sapiens/main.rs b/examples/sapiens/main.rs index 06f04c8..111d90f 100644 --- a/examples/sapiens/main.rs +++ b/examples/sapiens/main.rs @@ -8,9 +8,7 @@ fn main() -> Result<(), Box> { let options = Options::default() .with_model("sapiens/seg-0.3b-dyn.onnx")? .with_sapiens_task(SapiensTask::Seg) - .with_names(&BODY_PARTS_28) - .with_profile(false) - .with_i00((1, 1, 8).into()); + .with_names(&BODY_PARTS_28); let mut model = Sapiens::new(options)?; // load diff --git a/examples/svtr/main.rs b/examples/svtr/main.rs index 35fa7aa..43562c1 100644 --- a/examples/svtr/main.rs +++ b/examples/svtr/main.rs @@ -3,8 +3,9 @@ use usls::{models::SVTR, DataLoader, Options}; fn main() -> Result<(), Box> { // build model let options = Options::default() - .with_i00((1, 2, 8).into()) - .with_i03((320, 960, 1600).into()) + .with_ixx(0, 0, (1, 2, 8).into()) + .with_ixx(0, 2, (320, 960, 1600).into()) + .with_ixx(0, 3, (320, 960, 1600).into()) .with_confs(&[0.2]) .with_vocab("svtr/ppocr_rec_vocab.txt")? .with_model("svtr/ppocr-v4-svtr-ch-dyn.onnx")?; diff --git a/examples/yolo-sam/main.rs b/examples/yolo-sam/main.rs index 8a9e0a5..3b51ace 100644 --- a/examples/yolo-sam/main.rs +++ b/examples/yolo-sam/main.rs @@ -5,12 +5,8 @@ use usls::{ fn main() -> Result<(), Box> { // build SAM - let options_encoder = Options::default() - .with_i00((1, 1, 1).into()) - .with_model("sam/mobile-sam-vit-t-encoder.onnx")?; + let options_encoder = Options::default().with_model("sam/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) .with_sam_kind(SamKind::Sam) .with_model("sam/mobile-sam-vit-t-decoder.onnx")?; @@ -22,9 +18,8 @@ fn main() -> Result<(), Box> { .with_yolo_task(YOLOTask::Detect) .with_model("yolo/v8-m-dyn.onnx")? .with_cuda(0) - .with_i00((1, 1, 4).into()) - .with_i02((416, 640, 800).into()) - .with_i03((416, 640, 800).into()) + .with_ixx(0, 2, (416, 640, 800).into()) + .with_ixx(0, 3, (416, 640, 800).into()) .with_find_contours(false) .with_confs(&[0.45]); let mut yolo = YOLO::new(options_yolo)?; diff --git a/examples/yolo/main.rs b/examples/yolo/main.rs index 41e4365..e2b4462 100644 --- a/examples/yolo/main.rs +++ b/examples/yolo/main.rs @@ -21,6 +21,9 @@ pub struct Args { #[arg(long, value_enum, default_value_t = YOLOVersion::V8)] pub ver: YOLOVersion, + #[arg(long, default_value_t = 1)] + pub batch_size: usize, + #[arg(long, default_value_t = 224)] pub width_min: isize, @@ -163,7 +166,7 @@ fn main() -> Result<()> { } else if args.trt { let options = options.with_trt(args.device_id); if args.half { - options.with_fp16(true) + options.with_trt_fp16(true) } else { options } @@ -173,9 +176,9 @@ fn main() -> Result<()> { options.with_cpu() }; let options = options - .with_i00((1, 1, 4).into()) - .with_i02((args.height_min, args.height, args.height_max).into()) - .with_i03((args.width_min, args.width, args.width_max).into()) + .with_ixx(0, 0, (1, args.batch_size as _, 4).into()) + .with_ixx(0, 2, (args.height_min, args.height, args.height_max).into()) + .with_ixx(0, 3, (args.width_min, args.width, args.width_max).into()) .with_confs(&[0.2, 0.15]) // class_0: 0.4, others: 0.15 // .with_names(&COCO_CLASS_NAMES_80) .with_names2(&COCO_KEYPOINTS_17) diff --git a/examples/yolop/main.rs b/examples/yolop/main.rs index 865fa5c..2e338cc 100644 --- a/examples/yolop/main.rs +++ b/examples/yolop/main.rs @@ -4,7 +4,6 @@ fn main() -> Result<(), Box> { // build model let options = Options::default() .with_model("yolop/v2-dyn-480x800.onnx")? - .with_i00((1, 1, 8).into()) .with_confs(&[0.3]); let mut model = YOLOPv2::new(options)?; diff --git a/src/core/annotator.rs b/src/core/annotator.rs index af8c14d..694cb7e 100644 --- a/src/core/annotator.rs +++ b/src/core/annotator.rs @@ -358,17 +358,17 @@ impl Annotator { Dir::Currnet.raw_path_with_subs(&subs) } - /// Annotate images, and no return + /// Annotate images, save, and no return pub fn annotate(&self, imgs: &[DynamicImage], ys: &[Y]) { - let _ = self.plot(imgs, ys); + let _ = self.plot(imgs, ys, true); } - /// Plot images and return plotted images(RGBA8) - pub fn plot(&self, imgs: &[DynamicImage], ys: &[Y]) -> Result> { + /// Plot images and return plotted images + pub fn plot(&self, imgs: &[DynamicImage], ys: &[Y], save: bool) -> Result> { let span = tracing::span!(tracing::Level::INFO, "Annotator-plot"); let _guard = span.enter(); - let mut vs: Vec = Vec::new(); + let mut vs: Vec = Vec::new(); // annotate for (img, y) in imgs.iter().zip(ys.iter()) { @@ -414,16 +414,19 @@ impl Annotator { self.plot_probs(&mut img_rgba, xs); } - // save - let saveout = self.saveout()?.join(format!("{}.png", string_now("-"))); - match img_rgba.save(&saveout) { - Err(err) => tracing::error!("{} Saving failed: {:?}", CROSS_MARK, err), - Ok(_) => { - tracing::info!("{} Annotated image saved to: {:?}", CHECK_MARK, saveout); + // save or not + if save { + let saveout = self.saveout()?.join(format!("{}.png", string_now("-"))); + match img_rgba.save(&saveout) { + Err(err) => tracing::error!("{} Saving failed: {:?}", CROSS_MARK, err), + Ok(_) => { + tracing::info!("{} Annotated image saved to: {:?}", CHECK_MARK, saveout); + } } } - vs.push(img_rgba); + // RgbaImage -> DynamicImage + vs.push(image::DynamicImage::from(img_rgba)); } Ok(vs) } diff --git a/src/core/dataloader.rs b/src/core/dataloader.rs index 502ce92..5c35e54 100644 --- a/src/core/dataloader.rs +++ b/src/core/dataloader.rs @@ -19,6 +19,7 @@ type TempReturnType = (Vec, Vec); pub struct DataLoaderIterator { receiver: mpsc::Receiver, progress_bar: Option, + batch_size: u64, } impl Iterator for DataLoaderIterator { @@ -29,7 +30,7 @@ impl Iterator for DataLoaderIterator { None => self.receiver.recv().ok(), Some(progress_bar) => match self.receiver.recv().ok() { Some(item) => { - progress_bar.inc(1); + progress_bar.inc(self.batch_size); Some(item) } None => { @@ -53,7 +54,7 @@ impl IntoIterator for DataLoader { fn into_iter(self) -> Self::IntoIter { let progress_bar = if self.with_pb { build_progress_bar( - self.nf / self.batch_size as u64, + self.nf, " Iterating", Some(&format!("{:?}", self.media_type)), crate::PROGRESS_BAR_STYLE_CYAN_2, @@ -66,6 +67,7 @@ impl IntoIterator for DataLoader { DataLoaderIterator { receiver: self.receiver, progress_bar, + batch_size: self.batch_size as _, } } } @@ -164,7 +166,7 @@ impl DataLoader { } // summary - tracing::info!("{} Found {:?} x{}", CHECK_MARK, media_type, nf,); + tracing::info!("{} Found {:?} x{}", CHECK_MARK, media_type, nf); Ok(DataLoader { paths, diff --git a/src/core/device.rs b/src/core/device.rs index 2dda3a6..583df16 100644 --- a/src/core/device.rs +++ b/src/core/device.rs @@ -1,6 +1,6 @@ #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] pub enum Device { - Auto(usize), // TODO + Auto(usize), Cpu(usize), Cuda(usize), Trt(usize), diff --git a/src/core/dir.rs b/src/core/dir.rs index 1e525a7..7c36c56 100644 --- a/src/core/dir.rs +++ b/src/core/dir.rs @@ -14,6 +14,10 @@ pub enum Dir { } impl Dir { + pub fn saveout(subs: &[&str]) -> anyhow::Result { + Self::Currnet.raw_path_with_subs(subs) + } + /// Retrieves the base path for the specified directory type, optionally appending the `usls` subdirectory. /// /// # Arguments diff --git a/src/core/min_opt_max.rs b/src/core/min_opt_max.rs index 803d4d2..c1c5b45 100644 --- a/src/core/min_opt_max.rs +++ b/src/core/min_opt_max.rs @@ -1,63 +1,421 @@ /// A value composed of Min-Opt-Max -#[derive(Clone)] +#[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord)] pub struct MinOptMax { - pub min: isize, - pub opt: isize, - pub max: isize, + min: usize, + opt: usize, + max: usize, } impl Default for MinOptMax { fn default() -> Self { Self { - min: -1, - opt: -1, - max: -1, + min: 1, + opt: 1, + max: 1, } } } -impl std::fmt::Debug for MinOptMax { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("") - .field("Min", &self.min) - .field("Opt", &self.opt) - .field("Max", &self.max) - .finish() +impl MinOptMax { + pub fn min(&self) -> usize { + self.min + } + + pub fn opt(&self) -> usize { + self.opt + } + + pub fn max(&self) -> usize { + self.max + } + + pub fn ones() -> Self { + Default::default() + } + + pub fn zeros() -> Self { + Self { + min: 0, + opt: 0, + max: 0, + } + } + + pub fn update_opt(&mut self, opt: usize) { + // `opt` can be any valid usize number, even if it is smaller than `self.min` or greater than `self.max`. + self.opt = opt; + self.auto_tune(); + } + + pub fn try_update_min(&mut self, x: usize) -> anyhow::Result<()> { + if x > self.opt { + anyhow::bail!( + "Newly assigned `min`: {} must be smaller than the current `self.opt`: {}", + x, + self.opt + ); + } else { + self.min = x; + Ok(()) + } + } + + pub fn try_update_max(&mut self, x: usize) -> anyhow::Result<()> { + if x < self.opt { + anyhow::bail!( + "Newly assigned `max`: {} must be greater than the current `self.opt`: {}", + x, + self.opt + ); + } else { + self.max = x; + Ok(()) + } + } + + pub fn auto_tune(&mut self) { + // Rule 1: min <= opt <= max + // Rule 2: opt is unchangeable here + self.min = self.min.min(self.opt); + self.max = self.max.max(self.opt); + } + + pub fn auto_tuned(mut self) -> Self { + self.auto_tune(); + self + } + + pub fn is_dyn(&self) -> bool { + self.min + self.max + self.opt == 0 } } -impl From<(isize, isize, isize)> for MinOptMax { - fn from((min, opt, max): (isize, isize, isize)) -> Self { - let min = min.min(opt); - let max = max.max(opt); +impl From for MinOptMax { + fn from(opt: i32) -> Self { + let opt = opt.max(0) as usize; + let min = opt; + let max = opt; Self { min, opt, max } } } -impl From<[isize; 3]> for MinOptMax { - fn from([min, opt, max]: [isize; 3]) -> Self { - let min = min.min(opt); - let max = max.max(opt); +impl From for MinOptMax { + fn from(opt: i64) -> Self { + let opt = opt.max(0) as usize; + let min = opt; + let max = opt; Self { min, opt, max } } } -impl MinOptMax { - pub fn new(opt: isize) -> Self { - Self { - min: opt, - opt, - max: opt, - } +impl From for MinOptMax { + fn from(opt: u32) -> Self { + let opt = opt as usize; + let min = opt; + let max = opt; + Self { min, opt, max } } +} - pub fn update(&mut self, opt: isize) { - self.opt = opt; - if self.min > opt { - self.min = opt; - } - if self.max < opt { - self.max = opt; +impl From for MinOptMax { + fn from(opt: u64) -> Self { + let opt = opt as usize; + let min = opt; + let max = opt; + Self { min, opt, max } + } +} + +impl From for MinOptMax { + fn from(opt: usize) -> Self { + let min = opt; + let max = opt; + Self { min, opt, max } + } +} + +impl From for MinOptMax { + fn from(opt: isize) -> Self { + let opt = opt.max(0) as usize; + let min = opt; + let max = opt; + Self { min, opt, max } + } +} + +impl From for MinOptMax { + fn from(opt: f32) -> Self { + let opt = opt.max(0.).round() as usize; + let min = opt; + let max = opt; + Self { min, opt, max } + } +} + +impl From for MinOptMax { + fn from(opt: f64) -> Self { + let opt = opt.max(0.).round() as usize; + let min = opt; + let max = opt; + Self { min, opt, max } + } +} + +impl From<(T, T, T)> for MinOptMax +where + T: Into, +{ + fn from((min, opt, max): (T, T, T)) -> Self { + let min = min.into().min; + let opt = opt.into().opt; + let max = max.into().max; + Self { min, opt, max }.auto_tuned() + } +} + +impl From<[T; 3]> for MinOptMax +where + T: Into, +{ + fn from([min, opt, max]: [T; 3]) -> Self { + Self::from((min, opt, max)) + } +} + +#[cfg(test)] +mod tests_minoptmax { + use super::MinOptMax; + + #[test] + fn test_default() { + let default_mom = MinOptMax::default(); + assert_eq!(default_mom.min, 1); + assert_eq!(default_mom.opt, 1); + assert_eq!(default_mom.max, 1); + } + + #[test] + fn test_ones() { + let ones_mom = MinOptMax::ones(); + assert_eq!(ones_mom.min, 1); + assert_eq!(ones_mom.opt, 1); + assert_eq!(ones_mom.max, 1); + } + + #[test] + fn test_zeros() { + let zeros_mom = MinOptMax::zeros(); + assert_eq!(zeros_mom.min, 0); + assert_eq!(zeros_mom.opt, 0); + assert_eq!(zeros_mom.max, 0); + } + + #[test] + fn test_update_opt() { + let mut mom = MinOptMax::default(); + mom.update_opt(5); + assert_eq!(mom.opt, 5); + assert_eq!(mom.min, 1); + assert_eq!(mom.max, 5); + + let mut mom = MinOptMax::from((5, 6, 7)); + mom.update_opt(2); + assert_eq!(mom.opt, 2); + assert_eq!(mom.min, 2); + assert_eq!(mom.max, 7); + } + + #[test] + fn test_try_update_min_success() { + let mut mom = MinOptMax::default(); + let result = mom.try_update_min(0); + assert!(result.is_ok()); + assert_eq!(mom.min, 0); + + let result = mom.try_update_min(1); + assert!(result.is_ok()); + assert_eq!(mom.min, 1); + } + + #[test] + fn test_try_update_min_failure() { + let mut mom = MinOptMax::default(); // 1 + let result = mom.try_update_min(6); + assert!(result.is_err()); + assert_eq!(mom.min, 1); + assert_eq!(mom.opt, 1); + assert_eq!(mom.max, 1); + } + + #[test] + fn test_try_update_max_success() { + let mut mom = MinOptMax::default(); + let result = mom.try_update_max(20); + assert!(result.is_ok()); + assert_eq!(mom.max, 20); + assert_eq!(mom.opt, 1); + assert_eq!(mom.min, 1); + } + + #[test] + fn test_try_update_max_failure() { + let mut mom = MinOptMax::default(); + mom.update_opt(5); + let result = mom.try_update_max(4); + assert!(result.is_err()); + assert_eq!(mom.max, 5); + assert_eq!(mom.opt, 5); + assert_eq!(mom.min, 1); + } + + #[test] + fn test_combined_updates() { + let mut mom = MinOptMax::default(); + mom.update_opt(5); + assert_eq!(mom.max, 5); + assert_eq!(mom.opt, 5); + assert_eq!(mom.min, 1); + + assert!(mom.try_update_min(3).is_ok()); + assert_eq!(mom.min, 3); + assert_eq!(mom.opt, 5); + assert_eq!(mom.max, 5); + + assert!(mom.try_update_max(6).is_ok()); + assert_eq!(mom.max, 6); + assert_eq!(mom.opt, 5); + assert_eq!(mom.min, 3); + + // unchanged + assert!(mom.try_update_min(7).is_err()); + assert_eq!(mom.max, 6); + assert_eq!(mom.opt, 5); + assert_eq!(mom.min, 3); + + // unchanged + assert!(mom.try_update_max(4).is_err()); + assert_eq!(mom.max, 6); + assert_eq!(mom.opt, 5); + assert_eq!(mom.min, 3); + } + + #[test] + fn test_auto_tune() { + let mut mom = MinOptMax { + min: 5, + opt: 3, + max: 7, + }; + mom.auto_tune(); + assert_eq!(mom.min, 3); + assert_eq!(mom.max, 7); + assert_eq!(mom.opt, 3); + } + + #[test] + fn test_auto_tuned() { + let mom = MinOptMax { + min: 5, + opt: 3, + max: 7, } + .auto_tuned(); + assert_eq!(mom.min, 3); + assert_eq!(mom.max, 7); + assert_eq!(mom.opt, 3); + } + + #[test] + fn test_is_dyn() { + let dyn_mom = MinOptMax::zeros(); + assert!(dyn_mom.is_dyn()); + + let non_dyn_mom = MinOptMax { + min: 1, + opt: 1, + max: 1, + }; + assert!(!non_dyn_mom.is_dyn()); + } + + #[test] + fn test_from_integer_types() { + let from_i32: MinOptMax = MinOptMax::from(-5); + assert_eq!(from_i32, MinOptMax::zeros()); + + let from_i64: MinOptMax = MinOptMax::from(-3); + assert_eq!(from_i64, MinOptMax::zeros()); + + let from_u32: MinOptMax = MinOptMax::from(4u32); + assert_eq!( + from_u32, + MinOptMax { + min: 4, + opt: 4, + max: 4 + } + ); + + let from_u64: MinOptMax = MinOptMax::from(7u64); + assert_eq!( + from_u64, + MinOptMax { + min: 7, + opt: 7, + max: 7 + } + ); + + let from_usize: MinOptMax = MinOptMax::from(10); + assert_eq!( + from_usize, + MinOptMax { + min: 10, + opt: 10, + max: 10 + } + ); + + let from_isize: MinOptMax = MinOptMax::from(-1isize); + assert_eq!(from_isize, MinOptMax::zeros()); + + let from_f32: MinOptMax = MinOptMax::from(-2.0); + assert_eq!(from_f32, MinOptMax::zeros()); + + let from_f64: MinOptMax = MinOptMax::from(3.9); + assert_eq!( + from_f64, + MinOptMax { + min: 4, + opt: 4, + max: 4 + } + ); + } + + #[test] + fn test_from_tuple() { + let tuple_mom: MinOptMax = MinOptMax::from((1, 2, 3)); + assert_eq!( + tuple_mom, + MinOptMax { + min: 1, + opt: 2, + max: 3 + } + ); + } + + #[test] + fn test_from_array() { + let array_mom: MinOptMax = [1, 2, 3].into(); + assert_eq!( + array_mom, + MinOptMax { + min: 1, + opt: 2, + max: 3 + } + ); } } diff --git a/src/core/mod.rs b/src/core/mod.rs index 7225f8f..0b0c2f1 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -15,6 +15,7 @@ mod ort_engine; mod task; mod tokenizer_stream; mod ts; +mod viewer; mod vision; mod x; mod xs; @@ -31,10 +32,14 @@ pub use metric::Metric; pub use min_opt_max::MinOptMax; pub use ops::Ops; pub use options::Options; -pub use ort_engine::OrtEngine; +pub use ort_engine::*; pub use task::Task; pub use tokenizer_stream::TokenizerStream; pub use ts::Ts; +pub use viewer::Viewer; pub use vision::Vision; pub use x::X; pub use xs::Xs; + +// re-export +pub use minifb::Key; diff --git a/src/core/options.rs b/src/core/options.rs index 3bd8691..5ea92ea 100644 --- a/src/core/options.rs +++ b/src/core/options.rs @@ -4,7 +4,7 @@ use anyhow::Result; use crate::{ models::{SamKind, SapiensTask, YOLOPreds, YOLOTask, YOLOVersion}, - Device, Hub, MinOptMax, Task, + Device, Hub, Iiix, MinOptMax, Task, }; /// Options for building models @@ -13,176 +13,11 @@ pub struct Options { pub onnx_path: String, pub task: Task, pub device: Device, + pub batch_size: usize, + pub iiixs: Vec, pub profile: bool, pub num_dry_run: usize, - pub i00: Option, // the 1st input, axis 0, batch usually - pub i01: Option, // the 1st input, axis 1 - pub i02: Option, - pub i03: Option, - pub i04: Option, - pub i05: Option, - pub i10: Option, // the 2nd input, axis 0 - pub i11: Option, // the 2nd input, axis 1 - pub i12: Option, - pub i13: Option, - pub i14: Option, - pub i15: Option, - pub i20: Option, - pub i21: Option, - pub i22: Option, - pub i23: Option, - pub i24: Option, - pub i25: Option, - pub i30: Option, - pub i31: Option, - pub i32_: Option, - pub i33: Option, - pub i34: Option, - pub i35: Option, - pub i40: Option, - pub i41: Option, - pub i42: Option, - pub i43: Option, - pub i44: Option, - pub i45: Option, - pub i50: Option, - pub i51: Option, - pub i52: Option, - pub i53: Option, - pub i54: Option, - pub i55: Option, - pub i60: Option, - pub i61: Option, - pub i62: Option, - pub i63: Option, - pub i64_: Option, - pub i65: Option, - pub i70: Option, - pub i71: Option, - pub i72: Option, - pub i73: Option, - pub i74: Option, - pub i75: Option, - pub i80: Option, - pub i81: Option, - pub i82: Option, - pub i83: Option, - pub i84: Option, - pub i85: Option, - pub i90: Option, - pub i91: Option, - pub i92: Option, - pub i93: Option, - pub i94: Option, - pub i95: Option, - pub i100: Option, - pub i101: Option, - pub i102: Option, - pub i103: Option, - pub i104: Option, - pub i105: Option, - pub i110: Option, - pub i111: Option, - pub i112: Option, - pub i113: Option, - pub i114: Option, - pub i115: Option, - pub i120: Option, - pub i121: Option, - pub i122: Option, - pub i123: Option, - pub i124: Option, - pub i125: Option, - pub i130: Option, - pub i131: Option, - pub i132: Option, - pub i133: Option, - pub i134: Option, - pub i135: Option, - pub i140: Option, - pub i141: Option, - pub i142: Option, - pub i143: Option, - pub i144: Option, - pub i145: Option, - pub i150: Option, - pub i151: Option, - pub i152: Option, - pub i153: Option, - pub i154: Option, - pub i155: Option, - pub i160: Option, - pub i161: Option, - pub i162: Option, - pub i163: Option, - pub i164: Option, - pub i165: Option, - pub i170: Option, - pub i171: Option, - pub i172: Option, - pub i173: Option, - pub i174: Option, - pub i175: Option, - pub i180: Option, - pub i181: Option, - pub i182: Option, - pub i183: Option, - pub i184: Option, - pub i185: Option, - pub i190: Option, - pub i191: Option, - pub i192: Option, - pub i193: Option, - pub i194: Option, - pub i195: Option, - pub i200: Option, - pub i201: Option, - pub i202: Option, - pub i203: Option, - pub i204: Option, - pub i205: Option, - pub i210: Option, - pub i211: Option, - pub i212: Option, - pub i213: Option, - pub i214: Option, - pub i215: Option, - pub i220: Option, - pub i221: Option, - pub i222: Option, - pub i223: Option, - pub i224: Option, - pub i225: Option, - pub i230: Option, - pub i231: Option, - pub i232: Option, - pub i233: Option, - pub i234: Option, - pub i235: Option, - pub i240: Option, - pub i241: Option, - pub i242: Option, - pub i243: Option, - pub i244: Option, - pub i245: Option, - pub i250: Option, - pub i251: Option, - pub i252: Option, - pub i253: Option, - pub i254: Option, - pub i255: Option, - pub i260: Option, - pub i261: Option, - pub i262: Option, - pub i263: Option, - pub i264: Option, - pub i265: Option, - pub i270: Option, - pub i271: Option, - pub i272: Option, - pub i273: Option, - pub i274: Option, - pub i275: Option, + // trt related pub trt_engine_cache_enable: bool, pub trt_int8_enable: bool, @@ -221,175 +56,10 @@ impl Default for Options { onnx_path: String::new(), device: Device::Cuda(0), profile: false, + batch_size: 1, + iiixs: vec![], num_dry_run: 3, - i00: None, - i01: None, - i02: None, - i03: None, - i04: None, - i05: None, - i10: None, - i11: None, - i12: None, - i13: None, - i14: None, - i15: None, - i20: None, - i21: None, - i22: None, - i23: None, - i24: None, - i25: None, - i30: None, - i31: None, - i32_: None, - i33: None, - i34: None, - i35: None, - i40: None, - i41: None, - i42: None, - i43: None, - i44: None, - i45: None, - i50: None, - i51: None, - i52: None, - i53: None, - i54: None, - i55: None, - i60: None, - i61: None, - i62: None, - i63: None, - i64_: None, - i65: None, - i70: None, - i71: None, - i72: None, - i73: None, - i74: None, - i75: None, - i80: None, - i81: None, - i82: None, - i83: None, - i84: None, - i85: None, - i90: None, - i91: None, - i92: None, - i93: None, - i94: None, - i95: None, - i100: None, - i101: None, - i102: None, - i103: None, - i104: None, - i105: None, - i110: None, - i111: None, - i112: None, - i113: None, - i114: None, - i115: None, - i120: None, - i121: None, - i122: None, - i123: None, - i124: None, - i125: None, - i130: None, - i131: None, - i132: None, - i133: None, - i134: None, - i135: None, - i140: None, - i141: None, - i142: None, - i143: None, - i144: None, - i145: None, - i150: None, - i151: None, - i152: None, - i153: None, - i154: None, - i155: None, - i160: None, - i161: None, - i162: None, - i163: None, - i164: None, - i165: None, - i170: None, - i171: None, - i172: None, - i173: None, - i174: None, - i175: None, - i180: None, - i181: None, - i182: None, - i183: None, - i184: None, - i185: None, - i190: None, - i191: None, - i192: None, - i193: None, - i194: None, - i195: None, - i200: None, - i201: None, - i202: None, - i203: None, - i204: None, - i205: None, - i210: None, - i211: None, - i212: None, - i213: None, - i214: None, - i215: None, - i220: None, - i221: None, - i222: None, - i223: None, - i224: None, - i225: None, - i230: None, - i231: None, - i232: None, - i233: None, - i234: None, - i235: None, - i240: None, - i241: None, - i242: None, - i243: None, - i244: None, - i245: None, - i250: None, - i251: None, - i252: None, - i253: None, - i254: None, - i255: None, - i260: None, - i261: None, - i262: None, - i263: None, - i264: None, - i265: None, - i270: None, - i271: None, - i272: None, - i273: None, - i274: None, - i275: None, + trt_engine_cache_enable: true, trt_int8_enable: false, trt_fp16_enable: false, @@ -437,11 +107,26 @@ impl Options { Ok(self) } + pub fn with_batch_size(mut self, n: usize) -> Self { + self.batch_size = n; + self + } + + pub fn with_batch(mut self, n: usize) -> Self { + self.batch_size = n; + self + } + pub fn with_dry_run(mut self, n: usize) -> Self { self.num_dry_run = n; self } + pub fn with_device(mut self, device: Device) -> Self { + self.device = device; + self + } + pub fn with_cuda(mut self, id: usize) -> Self { self.device = Device::Cuda(id); self @@ -462,7 +147,7 @@ impl Options { self } - pub fn with_fp16(mut self, x: bool) -> Self { + pub fn with_trt_fp16(mut self, x: bool) -> Self { self.trt_fp16_enable = x; self } @@ -587,842 +272,8 @@ impl Options { self } - pub fn with_i00(mut self, x: MinOptMax) -> Self { - self.i00 = Some(x); - self - } - - pub fn with_i01(mut self, x: MinOptMax) -> Self { - self.i01 = Some(x); - self - } - - pub fn with_i02(mut self, x: MinOptMax) -> Self { - self.i02 = Some(x); - self - } - - pub fn with_i03(mut self, x: MinOptMax) -> Self { - self.i03 = Some(x); - self - } - - pub fn with_i04(mut self, x: MinOptMax) -> Self { - self.i04 = Some(x); - self - } - - pub fn with_i05(mut self, x: MinOptMax) -> Self { - self.i05 = Some(x); - self - } - - pub fn with_i10(mut self, x: MinOptMax) -> Self { - self.i10 = Some(x); - self - } - - pub fn with_i11(mut self, x: MinOptMax) -> Self { - self.i11 = Some(x); - self - } - - pub fn with_i12(mut self, x: MinOptMax) -> Self { - self.i12 = Some(x); - self - } - - pub fn with_i13(mut self, x: MinOptMax) -> Self { - self.i13 = Some(x); - self - } - - pub fn with_i14(mut self, x: MinOptMax) -> Self { - self.i14 = Some(x); - self - } - - pub fn with_i15(mut self, x: MinOptMax) -> Self { - self.i15 = Some(x); - self - } - - pub fn with_i20(mut self, x: MinOptMax) -> Self { - self.i20 = Some(x); - self - } - - pub fn with_i21(mut self, x: MinOptMax) -> Self { - self.i21 = Some(x); - self - } - - pub fn with_i22(mut self, x: MinOptMax) -> Self { - self.i22 = Some(x); - self - } - - pub fn with_i23(mut self, x: MinOptMax) -> Self { - self.i23 = Some(x); - self - } - - pub fn with_i24(mut self, x: MinOptMax) -> Self { - self.i24 = Some(x); - self - } - - pub fn with_i25(mut self, x: MinOptMax) -> Self { - self.i25 = Some(x); - self - } - - pub fn with_i30(mut self, x: MinOptMax) -> Self { - self.i30 = Some(x); - self - } - - pub fn with_i31(mut self, x: MinOptMax) -> Self { - self.i31 = Some(x); - self - } - - pub fn with_i32_(mut self, x: MinOptMax) -> Self { - self.i32_ = Some(x); - self - } - - pub fn with_i33(mut self, x: MinOptMax) -> Self { - self.i33 = Some(x); - self - } - - pub fn with_i34(mut self, x: MinOptMax) -> Self { - self.i34 = Some(x); - self - } - - pub fn with_i35(mut self, x: MinOptMax) -> Self { - self.i35 = Some(x); - self - } - - pub fn with_i40(mut self, x: MinOptMax) -> Self { - self.i40 = Some(x); - self - } - - pub fn with_i41(mut self, x: MinOptMax) -> Self { - self.i41 = Some(x); - self - } - - pub fn with_i42(mut self, x: MinOptMax) -> Self { - self.i42 = Some(x); - self - } - - pub fn with_i43(mut self, x: MinOptMax) -> Self { - self.i43 = Some(x); - self - } - - pub fn with_i44(mut self, x: MinOptMax) -> Self { - self.i44 = Some(x); - self - } - - pub fn with_i45(mut self, x: MinOptMax) -> Self { - self.i45 = Some(x); - self - } - - pub fn with_i50(mut self, x: MinOptMax) -> Self { - self.i50 = Some(x); - self - } - - pub fn with_i51(mut self, x: MinOptMax) -> Self { - self.i51 = Some(x); - self - } - - pub fn with_i52(mut self, x: MinOptMax) -> Self { - self.i52 = Some(x); - self - } - - pub fn with_i53(mut self, x: MinOptMax) -> Self { - self.i53 = Some(x); - self - } - - pub fn with_i54(mut self, x: MinOptMax) -> Self { - self.i54 = Some(x); - self - } - - pub fn with_i55(mut self, x: MinOptMax) -> Self { - self.i55 = Some(x); - self - } - - pub fn with_i60(mut self, x: MinOptMax) -> Self { - self.i60 = Some(x); - self - } - - pub fn with_i61(mut self, x: MinOptMax) -> Self { - self.i61 = Some(x); - self - } - - pub fn with_i62(mut self, x: MinOptMax) -> Self { - self.i62 = Some(x); - self - } - - pub fn with_i63(mut self, x: MinOptMax) -> Self { - self.i63 = Some(x); - self - } - - pub fn with_i64(mut self, x: MinOptMax) -> Self { - self.i64_ = Some(x); - self - } - - pub fn with_i65(mut self, x: MinOptMax) -> Self { - self.i65 = Some(x); - self - } - - pub fn with_i70(mut self, x: MinOptMax) -> Self { - self.i70 = Some(x); - self - } - - pub fn with_i71(mut self, x: MinOptMax) -> Self { - self.i71 = Some(x); - self - } - - pub fn with_i72(mut self, x: MinOptMax) -> Self { - self.i72 = Some(x); - self - } - - pub fn with_i73(mut self, x: MinOptMax) -> Self { - self.i73 = Some(x); - self - } - - pub fn with_i74(mut self, x: MinOptMax) -> Self { - self.i74 = Some(x); - self - } - - pub fn with_i75(mut self, x: MinOptMax) -> Self { - self.i75 = Some(x); - self - } - - pub fn with_i80(mut self, x: MinOptMax) -> Self { - self.i80 = Some(x); - self - } - - pub fn with_i81(mut self, x: MinOptMax) -> Self { - self.i81 = Some(x); - self - } - - pub fn with_i82(mut self, x: MinOptMax) -> Self { - self.i82 = Some(x); - self - } - - pub fn with_i83(mut self, x: MinOptMax) -> Self { - self.i83 = Some(x); - self - } - - pub fn with_i84(mut self, x: MinOptMax) -> Self { - self.i84 = Some(x); - self - } - - pub fn with_i85(mut self, x: MinOptMax) -> Self { - self.i85 = Some(x); - self - } - - pub fn with_i90(mut self, x: MinOptMax) -> Self { - self.i90 = Some(x); - self - } - - pub fn with_i91(mut self, x: MinOptMax) -> Self { - self.i91 = Some(x); - self - } - - pub fn with_i92(mut self, x: MinOptMax) -> Self { - self.i92 = Some(x); - self - } - - pub fn with_i93(mut self, x: MinOptMax) -> Self { - self.i93 = Some(x); - self - } - - pub fn with_i94(mut self, x: MinOptMax) -> Self { - self.i94 = Some(x); - self - } - - pub fn with_i95(mut self, x: MinOptMax) -> Self { - self.i95 = Some(x); - self - } - - pub fn with_i100(mut self, x: MinOptMax) -> Self { - self.i100 = Some(x); - self - } - - pub fn with_i101(mut self, x: MinOptMax) -> Self { - self.i101 = Some(x); - self - } - - pub fn with_i102(mut self, x: MinOptMax) -> Self { - self.i102 = Some(x); - self - } - - pub fn with_i103(mut self, x: MinOptMax) -> Self { - self.i103 = Some(x); - self - } - - pub fn with_i104(mut self, x: MinOptMax) -> Self { - self.i104 = Some(x); - self - } - - pub fn with_i105(mut self, x: MinOptMax) -> Self { - self.i105 = Some(x); - self - } - - pub fn with_i110(mut self, x: MinOptMax) -> Self { - self.i110 = Some(x); - self - } - - pub fn with_i111(mut self, x: MinOptMax) -> Self { - self.i111 = Some(x); - self - } - - pub fn with_i112(mut self, x: MinOptMax) -> Self { - self.i112 = Some(x); - self - } - - pub fn with_i113(mut self, x: MinOptMax) -> Self { - self.i113 = Some(x); - self - } - - pub fn with_i114(mut self, x: MinOptMax) -> Self { - self.i114 = Some(x); - self - } - - pub fn with_i115(mut self, x: MinOptMax) -> Self { - self.i115 = Some(x); - self - } - - pub fn with_i120(mut self, x: MinOptMax) -> Self { - self.i120 = Some(x); - self - } - - pub fn with_i121(mut self, x: MinOptMax) -> Self { - self.i121 = Some(x); - self - } - - pub fn with_i122(mut self, x: MinOptMax) -> Self { - self.i122 = Some(x); - self - } - - pub fn with_i123(mut self, x: MinOptMax) -> Self { - self.i123 = Some(x); - self - } - - pub fn with_i124(mut self, x: MinOptMax) -> Self { - self.i124 = Some(x); - self - } - - pub fn with_i125(mut self, x: MinOptMax) -> Self { - self.i125 = Some(x); - self - } - - pub fn with_i130(mut self, x: MinOptMax) -> Self { - self.i130 = Some(x); - self - } - - pub fn with_i131(mut self, x: MinOptMax) -> Self { - self.i131 = Some(x); - self - } - - pub fn with_i132(mut self, x: MinOptMax) -> Self { - self.i132 = Some(x); - self - } - - pub fn with_i133(mut self, x: MinOptMax) -> Self { - self.i133 = Some(x); - self - } - - pub fn with_i134(mut self, x: MinOptMax) -> Self { - self.i134 = Some(x); - self - } - - pub fn with_i135(mut self, x: MinOptMax) -> Self { - self.i135 = Some(x); - self - } - - pub fn with_i140(mut self, x: MinOptMax) -> Self { - self.i140 = Some(x); - self - } - - pub fn with_i141(mut self, x: MinOptMax) -> Self { - self.i141 = Some(x); - self - } - - pub fn with_i142(mut self, x: MinOptMax) -> Self { - self.i142 = Some(x); - self - } - - pub fn with_i143(mut self, x: MinOptMax) -> Self { - self.i143 = Some(x); - self - } - - pub fn with_i144(mut self, x: MinOptMax) -> Self { - self.i144 = Some(x); - self - } - - pub fn with_i145(mut self, x: MinOptMax) -> Self { - self.i145 = Some(x); - self - } - - pub fn with_i150(mut self, x: MinOptMax) -> Self { - self.i150 = Some(x); - self - } - - pub fn with_i151(mut self, x: MinOptMax) -> Self { - self.i151 = Some(x); - self - } - - pub fn with_i152(mut self, x: MinOptMax) -> Self { - self.i152 = Some(x); - self - } - - pub fn with_i153(mut self, x: MinOptMax) -> Self { - self.i153 = Some(x); - self - } - - pub fn with_i154(mut self, x: MinOptMax) -> Self { - self.i154 = Some(x); - self - } - - pub fn with_i155(mut self, x: MinOptMax) -> Self { - self.i155 = Some(x); - self - } - - pub fn with_i160(mut self, x: MinOptMax) -> Self { - self.i160 = Some(x); - self - } - - pub fn with_i161(mut self, x: MinOptMax) -> Self { - self.i161 = Some(x); - self - } - - pub fn with_i162(mut self, x: MinOptMax) -> Self { - self.i162 = Some(x); - self - } - - pub fn with_i163(mut self, x: MinOptMax) -> Self { - self.i163 = Some(x); - self - } - - pub fn with_i164(mut self, x: MinOptMax) -> Self { - self.i164 = Some(x); - self - } - - pub fn with_i165(mut self, x: MinOptMax) -> Self { - self.i165 = Some(x); - self - } - - pub fn with_i170(mut self, x: MinOptMax) -> Self { - self.i170 = Some(x); - self - } - - pub fn with_i171(mut self, x: MinOptMax) -> Self { - self.i171 = Some(x); - self - } - - pub fn with_i172(mut self, x: MinOptMax) -> Self { - self.i172 = Some(x); - self - } - - pub fn with_i173(mut self, x: MinOptMax) -> Self { - self.i173 = Some(x); - self - } - - pub fn with_i174(mut self, x: MinOptMax) -> Self { - self.i174 = Some(x); - self - } - - pub fn with_i175(mut self, x: MinOptMax) -> Self { - self.i175 = Some(x); - self - } - - pub fn with_i180(mut self, x: MinOptMax) -> Self { - self.i180 = Some(x); - self - } - - pub fn with_i181(mut self, x: MinOptMax) -> Self { - self.i181 = Some(x); - self - } - - pub fn with_i182(mut self, x: MinOptMax) -> Self { - self.i182 = Some(x); - self - } - - pub fn with_i183(mut self, x: MinOptMax) -> Self { - self.i183 = Some(x); - self - } - - pub fn with_i184(mut self, x: MinOptMax) -> Self { - self.i184 = Some(x); - self - } - - pub fn with_i185(mut self, x: MinOptMax) -> Self { - self.i185 = Some(x); - self - } - - pub fn with_i190(mut self, x: MinOptMax) -> Self { - self.i190 = Some(x); - self - } - - pub fn with_i191(mut self, x: MinOptMax) -> Self { - self.i191 = Some(x); - self - } - - pub fn with_i192(mut self, x: MinOptMax) -> Self { - self.i192 = Some(x); - self - } - - pub fn with_i193(mut self, x: MinOptMax) -> Self { - self.i193 = Some(x); - self - } - - pub fn with_i194(mut self, x: MinOptMax) -> Self { - self.i194 = Some(x); - self - } - - pub fn with_i195(mut self, x: MinOptMax) -> Self { - self.i195 = Some(x); - self - } - - pub fn with_i200(mut self, x: MinOptMax) -> Self { - self.i200 = Some(x); - self - } - - pub fn with_i201(mut self, x: MinOptMax) -> Self { - self.i201 = Some(x); - self - } - - pub fn with_i202(mut self, x: MinOptMax) -> Self { - self.i202 = Some(x); - self - } - - pub fn with_i203(mut self, x: MinOptMax) -> Self { - self.i203 = Some(x); - self - } - - pub fn with_i204(mut self, x: MinOptMax) -> Self { - self.i204 = Some(x); - self - } - - pub fn with_i205(mut self, x: MinOptMax) -> Self { - self.i205 = Some(x); - self - } - - pub fn with_i210(mut self, x: MinOptMax) -> Self { - self.i210 = Some(x); - self - } - - pub fn with_i211(mut self, x: MinOptMax) -> Self { - self.i211 = Some(x); - self - } - - pub fn with_i212(mut self, x: MinOptMax) -> Self { - self.i212 = Some(x); - self - } - - pub fn with_i213(mut self, x: MinOptMax) -> Self { - self.i213 = Some(x); - self - } - - pub fn with_i214(mut self, x: MinOptMax) -> Self { - self.i214 = Some(x); - self - } - - pub fn with_i215(mut self, x: MinOptMax) -> Self { - self.i215 = Some(x); - self - } - - pub fn with_i220(mut self, x: MinOptMax) -> Self { - self.i220 = Some(x); - self - } - - pub fn with_i221(mut self, x: MinOptMax) -> Self { - self.i221 = Some(x); - self - } - - pub fn with_i222(mut self, x: MinOptMax) -> Self { - self.i222 = Some(x); - self - } - - pub fn with_i223(mut self, x: MinOptMax) -> Self { - self.i223 = Some(x); - self - } - - pub fn with_i224(mut self, x: MinOptMax) -> Self { - self.i224 = Some(x); - self - } - - pub fn with_i225(mut self, x: MinOptMax) -> Self { - self.i225 = Some(x); - self - } - - pub fn with_i230(mut self, x: MinOptMax) -> Self { - self.i230 = Some(x); - self - } - - pub fn with_i231(mut self, x: MinOptMax) -> Self { - self.i231 = Some(x); - self - } - - pub fn with_i232(mut self, x: MinOptMax) -> Self { - self.i232 = Some(x); - self - } - - pub fn with_i233(mut self, x: MinOptMax) -> Self { - self.i233 = Some(x); - self - } - - pub fn with_i234(mut self, x: MinOptMax) -> Self { - self.i234 = Some(x); - self - } - - pub fn with_i235(mut self, x: MinOptMax) -> Self { - self.i235 = Some(x); - self - } - - pub fn with_i240(mut self, x: MinOptMax) -> Self { - self.i240 = Some(x); - self - } - - pub fn with_i241(mut self, x: MinOptMax) -> Self { - self.i241 = Some(x); - self - } - - pub fn with_i242(mut self, x: MinOptMax) -> Self { - self.i242 = Some(x); - self - } - - pub fn with_i243(mut self, x: MinOptMax) -> Self { - self.i243 = Some(x); - self - } - - pub fn with_i244(mut self, x: MinOptMax) -> Self { - self.i244 = Some(x); - self - } - - pub fn with_i245(mut self, x: MinOptMax) -> Self { - self.i245 = Some(x); - self - } - - pub fn with_i250(mut self, x: MinOptMax) -> Self { - self.i250 = Some(x); - self - } - - pub fn with_i251(mut self, x: MinOptMax) -> Self { - self.i251 = Some(x); - self - } - - pub fn with_i252(mut self, x: MinOptMax) -> Self { - self.i252 = Some(x); - self - } - - pub fn with_i253(mut self, x: MinOptMax) -> Self { - self.i253 = Some(x); - self - } - - pub fn with_i254(mut self, x: MinOptMax) -> Self { - self.i254 = Some(x); - self - } - - pub fn with_i255(mut self, x: MinOptMax) -> Self { - self.i255 = Some(x); - self - } - pub fn with_i260(mut self, x: MinOptMax) -> Self { - self.i260 = Some(x); - self - } - - pub fn with_i261(mut self, x: MinOptMax) -> Self { - self.i261 = Some(x); - self - } - - pub fn with_i262(mut self, x: MinOptMax) -> Self { - self.i262 = Some(x); - self - } - - pub fn with_i263(mut self, x: MinOptMax) -> Self { - self.i263 = Some(x); - self - } - - pub fn with_i264(mut self, x: MinOptMax) -> Self { - self.i264 = Some(x); - self - } - - pub fn with_i265(mut self, x: MinOptMax) -> Self { - self.i265 = Some(x); - self - } - - pub fn with_i270(mut self, x: MinOptMax) -> Self { - self.i270 = Some(x); - self - } - - pub fn with_i271(mut self, x: MinOptMax) -> Self { - self.i271 = Some(x); - self - } - - pub fn with_i272(mut self, x: MinOptMax) -> Self { - self.i272 = Some(x); - self - } - - pub fn with_i273(mut self, x: MinOptMax) -> Self { - self.i273 = Some(x); - self - } - - pub fn with_i274(mut self, x: MinOptMax) -> Self { - self.i274 = Some(x); - self - } - - pub fn with_i275(mut self, x: MinOptMax) -> Self { - self.i275 = Some(x); + pub fn with_ixx(mut self, i: usize, ii: usize, x: MinOptMax) -> Self { + self.iiixs.push(Iiix::from((i, ii, x))); self } } diff --git a/src/core/ort_engine.rs b/src/core/ort_engine.rs index 255a0ae..9aa5f29 100644 --- a/src/core/ort_engine.rs +++ b/src/core/ort_engine.rs @@ -12,12 +12,26 @@ use crate::{ CHECK_MARK, CROSS_MARK, X, }; -/// Ort Tensor Attrs: name, data_type, dims +/// A struct for input composed of the i-th input, the ii-th dimension, and the value. +#[derive(Clone, Debug, Default)] +pub struct Iiix { + pub i: usize, + pub ii: usize, + pub x: MinOptMax, +} + +impl From<(usize, usize, MinOptMax)> for Iiix { + fn from((i, ii, x): (usize, usize, MinOptMax)) -> Self { + Self { i, ii, x } + } +} + +/// A struct for tensor attrs composed of the names, the dtypes, and the dimensions. #[derive(Debug)] pub struct OrtTensorAttr { pub names: Vec, - pub dtypes: Vec, - pub dimss: Vec>, + pub dtypes: Vec, + pub dimss: Vec>, } /// ONNXRuntime Backend @@ -69,193 +83,8 @@ impl OrtEngine { // inputs & outputs let inputs_attrs = Self::io_from_onnx_value_info(&initializer_names, &graph.input)?; let outputs_attrs = Self::io_from_onnx_value_info(&initializer_names, &graph.output)?; - - // inputs minoptmax - let mut inputs_minoptmax: Vec> = Vec::new(); - for (i, dims) in inputs_attrs.dimss.iter().enumerate() { - let mut v_: Vec = Vec::new(); - for (ii, &x) in dims.iter().enumerate() { - let x_default: MinOptMax = ( - inputs_attrs.dimss[i][ii], - inputs_attrs.dimss[i][ii], - inputs_attrs.dimss[i][ii], - ) - .into(); - let x: MinOptMax = match (i, ii) { - (0, 0) => Self::_set_ixx(x, &config.i00, i, ii).unwrap_or(x_default), - (0, 1) => Self::_set_ixx(x, &config.i01, i, ii).unwrap_or(x_default), - (0, 2) => Self::_set_ixx(x, &config.i02, i, ii).unwrap_or(x_default), - (0, 3) => Self::_set_ixx(x, &config.i03, i, ii).unwrap_or(x_default), - (0, 4) => Self::_set_ixx(x, &config.i04, i, ii).unwrap_or(x_default), - (0, 5) => Self::_set_ixx(x, &config.i05, i, ii).unwrap_or(x_default), - (1, 0) => Self::_set_ixx(x, &config.i10, i, ii).unwrap_or(x_default), - (1, 1) => Self::_set_ixx(x, &config.i11, i, ii).unwrap_or(x_default), - (1, 2) => Self::_set_ixx(x, &config.i12, i, ii).unwrap_or(x_default), - (1, 3) => Self::_set_ixx(x, &config.i13, i, ii).unwrap_or(x_default), - (1, 4) => Self::_set_ixx(x, &config.i14, i, ii).unwrap_or(x_default), - (1, 5) => Self::_set_ixx(x, &config.i15, i, ii).unwrap_or(x_default), - (2, 0) => Self::_set_ixx(x, &config.i20, i, ii).unwrap_or(x_default), - (2, 1) => Self::_set_ixx(x, &config.i21, i, ii).unwrap_or(x_default), - (2, 2) => Self::_set_ixx(x, &config.i22, i, ii).unwrap_or(x_default), - (2, 3) => Self::_set_ixx(x, &config.i23, i, ii).unwrap_or(x_default), - (2, 4) => Self::_set_ixx(x, &config.i24, i, ii).unwrap_or(x_default), - (2, 5) => Self::_set_ixx(x, &config.i25, i, ii).unwrap_or(x_default), - (3, 0) => Self::_set_ixx(x, &config.i30, i, ii).unwrap_or(x_default), - (3, 1) => Self::_set_ixx(x, &config.i31, i, ii).unwrap_or(x_default), - (3, 2) => Self::_set_ixx(x, &config.i32_, i, ii).unwrap_or(x_default), - (3, 3) => Self::_set_ixx(x, &config.i33, i, ii).unwrap_or(x_default), - (3, 4) => Self::_set_ixx(x, &config.i34, i, ii).unwrap_or(x_default), - (3, 5) => Self::_set_ixx(x, &config.i35, i, ii).unwrap_or(x_default), - (4, 0) => Self::_set_ixx(x, &config.i40, i, ii).unwrap_or(x_default), - (4, 1) => Self::_set_ixx(x, &config.i41, i, ii).unwrap_or(x_default), - (4, 2) => Self::_set_ixx(x, &config.i42, i, ii).unwrap_or(x_default), - (4, 3) => Self::_set_ixx(x, &config.i43, i, ii).unwrap_or(x_default), - (4, 4) => Self::_set_ixx(x, &config.i44, i, ii).unwrap_or(x_default), - (4, 5) => Self::_set_ixx(x, &config.i45, i, ii).unwrap_or(x_default), - (5, 0) => Self::_set_ixx(x, &config.i50, i, ii).unwrap_or(x_default), - (5, 1) => Self::_set_ixx(x, &config.i51, i, ii).unwrap_or(x_default), - (5, 2) => Self::_set_ixx(x, &config.i52, i, ii).unwrap_or(x_default), - (5, 3) => Self::_set_ixx(x, &config.i53, i, ii).unwrap_or(x_default), - (5, 4) => Self::_set_ixx(x, &config.i54, i, ii).unwrap_or(x_default), - (5, 5) => Self::_set_ixx(x, &config.i55, i, ii).unwrap_or(x_default), - (6, 0) => Self::_set_ixx(x, &config.i60, i, ii).unwrap_or(x_default), - (6, 1) => Self::_set_ixx(x, &config.i61, i, ii).unwrap_or(x_default), - (6, 2) => Self::_set_ixx(x, &config.i62, i, ii).unwrap_or(x_default), - (6, 3) => Self::_set_ixx(x, &config.i63, i, ii).unwrap_or(x_default), - (6, 4) => Self::_set_ixx(x, &config.i64_, i, ii).unwrap_or(x_default), - (6, 5) => Self::_set_ixx(x, &config.i65, i, ii).unwrap_or(x_default), - (7, 0) => Self::_set_ixx(x, &config.i70, i, ii).unwrap_or(x_default), - (7, 1) => Self::_set_ixx(x, &config.i71, i, ii).unwrap_or(x_default), - (7, 2) => Self::_set_ixx(x, &config.i72, i, ii).unwrap_or(x_default), - (7, 3) => Self::_set_ixx(x, &config.i73, i, ii).unwrap_or(x_default), - (7, 4) => Self::_set_ixx(x, &config.i74, i, ii).unwrap_or(x_default), - (7, 5) => Self::_set_ixx(x, &config.i75, i, ii).unwrap_or(x_default), - (8, 0) => Self::_set_ixx(x, &config.i80, i, ii).unwrap_or(x_default), - (8, 1) => Self::_set_ixx(x, &config.i81, i, ii).unwrap_or(x_default), - (8, 2) => Self::_set_ixx(x, &config.i82, i, ii).unwrap_or(x_default), - (8, 3) => Self::_set_ixx(x, &config.i83, i, ii).unwrap_or(x_default), - (8, 4) => Self::_set_ixx(x, &config.i84, i, ii).unwrap_or(x_default), - (8, 5) => Self::_set_ixx(x, &config.i85, i, ii).unwrap_or(x_default), - (9, 0) => Self::_set_ixx(x, &config.i90, i, ii).unwrap_or(x_default), - (9, 1) => Self::_set_ixx(x, &config.i91, i, ii).unwrap_or(x_default), - (9, 2) => Self::_set_ixx(x, &config.i92, i, ii).unwrap_or(x_default), - (9, 3) => Self::_set_ixx(x, &config.i93, i, ii).unwrap_or(x_default), - (9, 4) => Self::_set_ixx(x, &config.i94, i, ii).unwrap_or(x_default), - (9, 5) => Self::_set_ixx(x, &config.i95, i, ii).unwrap_or(x_default), - (10, 0) => Self::_set_ixx(x, &config.i100, i, ii).unwrap_or(x_default), - (10, 1) => Self::_set_ixx(x, &config.i101, i, ii).unwrap_or(x_default), - (10, 2) => Self::_set_ixx(x, &config.i102, i, ii).unwrap_or(x_default), - (10, 3) => Self::_set_ixx(x, &config.i103, i, ii).unwrap_or(x_default), - (10, 4) => Self::_set_ixx(x, &config.i104, i, ii).unwrap_or(x_default), - (10, 5) => Self::_set_ixx(x, &config.i105, i, ii).unwrap_or(x_default), - (11, 0) => Self::_set_ixx(x, &config.i110, i, ii).unwrap_or(x_default), - (11, 1) => Self::_set_ixx(x, &config.i111, i, ii).unwrap_or(x_default), - (11, 2) => Self::_set_ixx(x, &config.i112, i, ii).unwrap_or(x_default), - (11, 3) => Self::_set_ixx(x, &config.i113, i, ii).unwrap_or(x_default), - (11, 4) => Self::_set_ixx(x, &config.i114, i, ii).unwrap_or(x_default), - (11, 5) => Self::_set_ixx(x, &config.i115, i, ii).unwrap_or(x_default), - (12, 0) => Self::_set_ixx(x, &config.i120, i, ii).unwrap_or(x_default), - (12, 1) => Self::_set_ixx(x, &config.i121, i, ii).unwrap_or(x_default), - (12, 2) => Self::_set_ixx(x, &config.i122, i, ii).unwrap_or(x_default), - (12, 3) => Self::_set_ixx(x, &config.i123, i, ii).unwrap_or(x_default), - (12, 4) => Self::_set_ixx(x, &config.i124, i, ii).unwrap_or(x_default), - (12, 5) => Self::_set_ixx(x, &config.i125, i, ii).unwrap_or(x_default), - (13, 0) => Self::_set_ixx(x, &config.i130, i, ii).unwrap_or(x_default), - (13, 1) => Self::_set_ixx(x, &config.i131, i, ii).unwrap_or(x_default), - (13, 2) => Self::_set_ixx(x, &config.i132, i, ii).unwrap_or(x_default), - (13, 3) => Self::_set_ixx(x, &config.i133, i, ii).unwrap_or(x_default), - (13, 4) => Self::_set_ixx(x, &config.i134, i, ii).unwrap_or(x_default), - (13, 5) => Self::_set_ixx(x, &config.i135, i, ii).unwrap_or(x_default), - (14, 0) => Self::_set_ixx(x, &config.i140, i, ii).unwrap_or(x_default), - (14, 1) => Self::_set_ixx(x, &config.i141, i, ii).unwrap_or(x_default), - (14, 2) => Self::_set_ixx(x, &config.i142, i, ii).unwrap_or(x_default), - (14, 3) => Self::_set_ixx(x, &config.i143, i, ii).unwrap_or(x_default), - (14, 4) => Self::_set_ixx(x, &config.i144, i, ii).unwrap_or(x_default), - (14, 5) => Self::_set_ixx(x, &config.i145, i, ii).unwrap_or(x_default), - (15, 0) => Self::_set_ixx(x, &config.i150, i, ii).unwrap_or(x_default), - (15, 1) => Self::_set_ixx(x, &config.i151, i, ii).unwrap_or(x_default), - (15, 2) => Self::_set_ixx(x, &config.i152, i, ii).unwrap_or(x_default), - (15, 3) => Self::_set_ixx(x, &config.i153, i, ii).unwrap_or(x_default), - (15, 4) => Self::_set_ixx(x, &config.i154, i, ii).unwrap_or(x_default), - (15, 5) => Self::_set_ixx(x, &config.i155, i, ii).unwrap_or(x_default), - (16, 0) => Self::_set_ixx(x, &config.i160, i, ii).unwrap_or(x_default), - (16, 1) => Self::_set_ixx(x, &config.i161, i, ii).unwrap_or(x_default), - (16, 2) => Self::_set_ixx(x, &config.i162, i, ii).unwrap_or(x_default), - (16, 3) => Self::_set_ixx(x, &config.i163, i, ii).unwrap_or(x_default), - (16, 4) => Self::_set_ixx(x, &config.i164, i, ii).unwrap_or(x_default), - (16, 5) => Self::_set_ixx(x, &config.i165, i, ii).unwrap_or(x_default), - (17, 0) => Self::_set_ixx(x, &config.i170, i, ii).unwrap_or(x_default), - (17, 1) => Self::_set_ixx(x, &config.i171, i, ii).unwrap_or(x_default), - (17, 2) => Self::_set_ixx(x, &config.i172, i, ii).unwrap_or(x_default), - (17, 3) => Self::_set_ixx(x, &config.i173, i, ii).unwrap_or(x_default), - (17, 4) => Self::_set_ixx(x, &config.i174, i, ii).unwrap_or(x_default), - (17, 5) => Self::_set_ixx(x, &config.i175, i, ii).unwrap_or(x_default), - (18, 0) => Self::_set_ixx(x, &config.i180, i, ii).unwrap_or(x_default), - (18, 1) => Self::_set_ixx(x, &config.i181, i, ii).unwrap_or(x_default), - (18, 2) => Self::_set_ixx(x, &config.i182, i, ii).unwrap_or(x_default), - (18, 3) => Self::_set_ixx(x, &config.i183, i, ii).unwrap_or(x_default), - (18, 4) => Self::_set_ixx(x, &config.i184, i, ii).unwrap_or(x_default), - (18, 5) => Self::_set_ixx(x, &config.i185, i, ii).unwrap_or(x_default), - (19, 0) => Self::_set_ixx(x, &config.i190, i, ii).unwrap_or(x_default), - (19, 1) => Self::_set_ixx(x, &config.i191, i, ii).unwrap_or(x_default), - (19, 2) => Self::_set_ixx(x, &config.i192, i, ii).unwrap_or(x_default), - (19, 3) => Self::_set_ixx(x, &config.i193, i, ii).unwrap_or(x_default), - (19, 4) => Self::_set_ixx(x, &config.i194, i, ii).unwrap_or(x_default), - (19, 5) => Self::_set_ixx(x, &config.i195, i, ii).unwrap_or(x_default), - (20, 0) => Self::_set_ixx(x, &config.i200, i, ii).unwrap_or(x_default), - (20, 1) => Self::_set_ixx(x, &config.i201, i, ii).unwrap_or(x_default), - (20, 2) => Self::_set_ixx(x, &config.i202, i, ii).unwrap_or(x_default), - (20, 3) => Self::_set_ixx(x, &config.i203, i, ii).unwrap_or(x_default), - (20, 4) => Self::_set_ixx(x, &config.i204, i, ii).unwrap_or(x_default), - (20, 5) => Self::_set_ixx(x, &config.i205, i, ii).unwrap_or(x_default), - (21, 0) => Self::_set_ixx(x, &config.i210, i, ii).unwrap_or(x_default), - (21, 1) => Self::_set_ixx(x, &config.i211, i, ii).unwrap_or(x_default), - (21, 2) => Self::_set_ixx(x, &config.i212, i, ii).unwrap_or(x_default), - (21, 3) => Self::_set_ixx(x, &config.i213, i, ii).unwrap_or(x_default), - (21, 4) => Self::_set_ixx(x, &config.i214, i, ii).unwrap_or(x_default), - (21, 5) => Self::_set_ixx(x, &config.i215, i, ii).unwrap_or(x_default), - (22, 0) => Self::_set_ixx(x, &config.i220, i, ii).unwrap_or(x_default), - (22, 1) => Self::_set_ixx(x, &config.i221, i, ii).unwrap_or(x_default), - (22, 2) => Self::_set_ixx(x, &config.i222, i, ii).unwrap_or(x_default), - (22, 3) => Self::_set_ixx(x, &config.i223, i, ii).unwrap_or(x_default), - (22, 4) => Self::_set_ixx(x, &config.i224, i, ii).unwrap_or(x_default), - (22, 5) => Self::_set_ixx(x, &config.i225, i, ii).unwrap_or(x_default), - (23, 0) => Self::_set_ixx(x, &config.i230, i, ii).unwrap_or(x_default), - (23, 1) => Self::_set_ixx(x, &config.i231, i, ii).unwrap_or(x_default), - (23, 2) => Self::_set_ixx(x, &config.i232, i, ii).unwrap_or(x_default), - (23, 3) => Self::_set_ixx(x, &config.i233, i, ii).unwrap_or(x_default), - (23, 4) => Self::_set_ixx(x, &config.i234, i, ii).unwrap_or(x_default), - (23, 5) => Self::_set_ixx(x, &config.i235, i, ii).unwrap_or(x_default), - (24, 0) => Self::_set_ixx(x, &config.i240, i, ii).unwrap_or(x_default), - (24, 1) => Self::_set_ixx(x, &config.i241, i, ii).unwrap_or(x_default), - (24, 2) => Self::_set_ixx(x, &config.i242, i, ii).unwrap_or(x_default), - (24, 3) => Self::_set_ixx(x, &config.i243, i, ii).unwrap_or(x_default), - (24, 4) => Self::_set_ixx(x, &config.i244, i, ii).unwrap_or(x_default), - (24, 5) => Self::_set_ixx(x, &config.i245, i, ii).unwrap_or(x_default), - (25, 0) => Self::_set_ixx(x, &config.i250, i, ii).unwrap_or(x_default), - (25, 1) => Self::_set_ixx(x, &config.i251, i, ii).unwrap_or(x_default), - (25, 2) => Self::_set_ixx(x, &config.i252, i, ii).unwrap_or(x_default), - (25, 3) => Self::_set_ixx(x, &config.i253, i, ii).unwrap_or(x_default), - (25, 4) => Self::_set_ixx(x, &config.i254, i, ii).unwrap_or(x_default), - (25, 5) => Self::_set_ixx(x, &config.i255, i, ii).unwrap_or(x_default), - (26, 0) => Self::_set_ixx(x, &config.i260, i, ii).unwrap_or(x_default), - (26, 1) => Self::_set_ixx(x, &config.i261, i, ii).unwrap_or(x_default), - (26, 2) => Self::_set_ixx(x, &config.i262, i, ii).unwrap_or(x_default), - (26, 3) => Self::_set_ixx(x, &config.i263, i, ii).unwrap_or(x_default), - (26, 4) => Self::_set_ixx(x, &config.i264, i, ii).unwrap_or(x_default), - (26, 5) => Self::_set_ixx(x, &config.i265, i, ii).unwrap_or(x_default), - (27, 0) => Self::_set_ixx(x, &config.i270, i, ii).unwrap_or(x_default), - (27, 1) => Self::_set_ixx(x, &config.i271, i, ii).unwrap_or(x_default), - (27, 2) => Self::_set_ixx(x, &config.i272, i, ii).unwrap_or(x_default), - (27, 3) => Self::_set_ixx(x, &config.i273, i, ii).unwrap_or(x_default), - (27, 4) => Self::_set_ixx(x, &config.i274, i, ii).unwrap_or(x_default), - (27, 5) => Self::_set_ixx(x, &config.i275, i, ii).unwrap_or(x_default), - _ => todo!(), - }; - v_.push(x); - } - inputs_minoptmax.push(v_); - } + let inputs_minoptmax = + Self::build_inputs_minoptmax(&inputs_attrs, &config.iiixs, config.batch_size)?; // build ort::init().commit()?; @@ -343,9 +172,9 @@ impl OrtEngine { let mut s_opt = format!("{}:", name); let mut s_max = format!("{}:", name); for d in inputs_minoptmax[i].iter() { - let min_ = &format!("{}x", d.min); - let opt_ = &format!("{}x", d.opt); - let max_ = &format!("{}x", d.max); + let min_ = &format!("{}x", d.min()); + let opt_ = &format!("{}x", d.opt()); + let max_ = &format!("{}x", d.max()); s_min += min_; s_opt += opt_; s_max += max_; @@ -423,7 +252,7 @@ impl OrtEngine { for i in self.inputs_minoptmax.iter() { let mut x: Vec = Vec::new(); for i_ in i.iter() { - x.push(i_.opt as usize); + x.push(i_.opt()); } let x: Array = Array::ones(x).into_dyn(); xs.push(X::from(x)); @@ -555,20 +384,58 @@ impl OrtEngine { Ok(ys) } - fn _set_ixx(x: isize, ixx: &Option, i: usize, ii: usize) -> Option { - match x { - -1 => { - match ixx { - None => panic!( - "{CROSS_MARK} Using dynamic shapes in inputs without specifying it: the {}-th input, the {}-th dimension.", - i + 1, - ii + 1 - ), - Some(ixx) => Some(ixx.to_owned()), // customized + fn build_inputs_minoptmax( + inputs_attrs: &OrtTensorAttr, + iiixs: &[Iiix], + batch_size: usize, + ) -> Result>> { + let span = tracing::span!(tracing::Level::INFO, "OrtEngine-build_inputs_minoptmax"); + let _guard = span.enter(); + + // init + let mut ys: Vec> = inputs_attrs + .dimss + .iter() + .map(|dims| dims.iter().map(|&x| MinOptMax::from(x)).collect()) + .collect(); + + // update from customized + for iiix in iiixs.iter() { + if let Some(x) = inputs_attrs + .dimss + .get(iiix.i) + .and_then(|dims| dims.get(iiix.ii)) + { + // dynamic + if *x == 0 { + ys[iiix.i][iiix.ii] = iiix.x.clone(); } + } else { + anyhow::bail!( + "Cannot retrieve the {}-th dimension of the {}-th input.", + iiix.ii, + iiix.i, + ); } - _ => Some((x, x, x).into()), // customized, but not dynamic } + + // deal with the dynamic axis + ys.iter_mut().enumerate().for_each(|(i, xs)| { + xs.iter_mut().enumerate().for_each(|(ii, x)| { + if x.is_dyn() { + let n = if ii == 0 { batch_size } else { 1 }; + let y = MinOptMax::from(n); + tracing::warn!( + "Using dynamic shapes in inputs without specifying it: the {}-th input, the {}-th dimension. \ + Using {:?} by default. You should make it clear when using TensorRT.", + i + 1, ii + 1, y + ); + *x = y; + } + }); + }); + + Ok(ys) } #[allow(dead_code)] @@ -627,57 +494,11 @@ impl OrtEngine { } } - #[allow(dead_code)] - fn i_from_session(session: &ort::Session) -> Result { - let mut dimss = Vec::new(); - let mut dtypes = Vec::new(); - let mut names = Vec::new(); - for x in session.inputs.iter() { - names.push(x.name.to_owned()); - if let ort::ValueType::Tensor { ty, dimensions } = &x.input_type { - dimss.push(dimensions.iter().map(|x| *x as isize).collect::>()); - dtypes.push(*ty); - } else { - dimss.push(vec![-1_isize]); - dtypes.push(ort::TensorElementType::Float32); - } - } - - Ok(OrtTensorAttr { - names, - dimss, - dtypes, - }) - } - - #[allow(dead_code)] - fn o_from_session(session: &ort::Session) -> Result { - let mut dimss = Vec::new(); - let mut dtypes = Vec::new(); - let mut names = Vec::new(); - for x in session.outputs.iter() { - names.push(x.name.to_owned()); - if let ort::ValueType::Tensor { ty, dimensions } = &x.output_type { - dimss.push(dimensions.iter().map(|x| *x as isize).collect::>()); - dtypes.push(*ty); - } else { - dimss.push(vec![-1_isize]); - dtypes.push(ort::TensorElementType::Float32); - } - } - - Ok(OrtTensorAttr { - names, - dimss, - dtypes, - }) - } - fn io_from_onnx_value_info( initializer_names: &HashSet<&str>, value_info: &[onnx::ValueInfoProto], ) -> Result { - let mut dimss: Vec> = Vec::new(); + let mut dimss: Vec> = Vec::new(); let mut dtypes: Vec = Vec::new(); let mut names: Vec = Vec::new(); for v in value_info.iter() { @@ -708,16 +529,16 @@ impl OrtEngine { Some(shapes) => shapes, None => continue, }; - let mut shape_: Vec = Vec::new(); + let mut shape_: Vec = Vec::new(); for shape in shapes.dim.iter() { match &shape.value { None => continue, Some(value) => match value { onnx::tensor_shape_proto::dimension::Value::DimValue(x) => { - shape_.push(*x as isize); + shape_.push(*x as _); } onnx::tensor_shape_proto::dimension::Value::DimParam(_) => { - shape_.push(-1isize); + shape_.push(0); } }, } @@ -736,11 +557,11 @@ impl OrtEngine { Ok(onnx::ModelProto::decode(f.as_slice())?) } - pub fn oshapes(&self) -> &Vec> { + pub fn oshapes(&self) -> &Vec> { &self.outputs_attrs.dimss } - pub fn odimss(&self) -> &Vec> { + pub fn odimss(&self) -> &Vec> { &self.outputs_attrs.dimss } @@ -752,11 +573,11 @@ impl OrtEngine { &self.outputs_attrs.dtypes } - pub fn ishapes(&self) -> &Vec> { + pub fn ishapes(&self) -> &Vec> { &self.inputs_attrs.dimss } - pub fn idimss(&self) -> &Vec> { + pub fn idimss(&self) -> &Vec> { &self.inputs_attrs.dimss } @@ -780,6 +601,14 @@ impl OrtEngine { &self.inputs_minoptmax[0][0] } + pub fn try_height(&self) -> Option<&MinOptMax> { + self.inputs_minoptmax.first().and_then(|x| x.get(2)) + } + + pub fn try_width(&self) -> Option<&MinOptMax> { + self.inputs_minoptmax.first().and_then(|x| x.get(3)) + } + pub fn height(&self) -> &MinOptMax { &self.inputs_minoptmax[0][2] } @@ -789,7 +618,7 @@ impl OrtEngine { } pub fn is_batch_dyn(&self) -> bool { - self.ishapes()[0][0] == -1 + self.ishapes()[0][0] == 0 } pub fn try_fetch(&self, key: &str) -> Option { diff --git a/src/core/viewer.rs b/src/core/viewer.rs new file mode 100644 index 0000000..982fc8a --- /dev/null +++ b/src/core/viewer.rs @@ -0,0 +1,195 @@ +use anyhow::Result; +use image::DynamicImage; +use minifb::{Window, WindowOptions}; +use video_rs::{ + encode::{Encoder, Settings}, + time::Time, +}; + +use crate::{string_now, Dir, Key}; + +pub struct Viewer<'a> { + name: &'a str, + window: Option, + window_scale: f32, + window_resizable: bool, + fps_poll: usize, + fps: usize, + writer: Option, + position: Time, +} + +impl Default for Viewer<'_> { + fn default() -> Self { + Self { + name: "usls-viewer", + window: None, + window_scale: 0.5, + window_resizable: true, + fps_poll: 100, + fps: 25, + writer: None, + position: Time::zero(), + } + } +} + +impl Viewer<'_> { + pub fn new() -> Self { + Default::default() + } + + pub fn imshow(&mut self, xs: &[DynamicImage]) -> Result<()> { + for x in xs.iter() { + let rgb = x.to_rgb8(); + let (w, h) = (rgb.width() as usize, rgb.height() as usize); + let (w_scale, h_scale) = ( + (w as f32 * self.window_scale) as usize, + (h as f32 * self.window_scale) as usize, + ); + + // should reload? + let should_reload = match &self.window { + None => true, + Some(window) => { + if self.window_resizable { + false + } else { + window.get_size() != (w_scale, h_scale) + } + } + }; + + // create window + if should_reload { + self.window = Window::new( + self.name, + w_scale, + h_scale, + WindowOptions { + resize: true, + topmost: true, + borderless: false, + scale: minifb::Scale::X1, + ..WindowOptions::default() + }, + ) + .ok() + .map(|mut x| { + x.set_target_fps(self.fps_poll); + x + }); + } + + // build buffer + let mut buffer: Vec = Vec::with_capacity(w * h); + for pixel in rgb.pixels() { + let r = pixel[0]; + let g = pixel[1]; + let b = pixel[2]; + let p = Self::rgb8_to_u32(r, g, b); + buffer.push(p); + } + + // update buffer + self.window + .as_mut() + .unwrap() + .update_with_buffer(&buffer, w, h)?; + } + + Ok(()) + } + + pub fn write(&mut self, frame: &image::DynamicImage) -> Result<()> { + // build writer at the 1st time + let frame = frame.to_rgb8(); + let (w, h) = frame.dimensions(); + if self.writer.is_none() { + let settings = Settings::preset_h264_yuv420p(w as _, h as _, false); + let saveout = Dir::saveout(&["runs"])?.join(format!("{}.mp4", string_now("-"))); + tracing::info!("Video will be save to: {:?}", saveout); + self.writer = Some(Encoder::new(saveout, settings)?); + } + + // write video + if let Some(writer) = self.writer.as_mut() { + let raw_data = frame.to_vec(); + let frame = ndarray::Array3::from_shape_vec((h as usize, w as usize, 3), raw_data)?; + + // encode and update + writer.encode(&frame, self.position)?; + self.position = self + .position + .aligned_with(Time::from_nth_of_a_second(self.fps)) + .add(); + } + Ok(()) + } + + pub fn write_batch(&mut self, frames: &[image::DynamicImage]) -> Result<()> { + for frame in frames.iter() { + self.write(frame)? + } + Ok(()) + } + + pub fn finish_write(&mut self) -> Result<()> { + match &mut self.writer { + Some(writer) => writer.finish()?, + None => { + tracing::info!("Found no video writer. No need to release."); + } + } + Ok(()) + } + + pub fn is_open(&self) -> bool { + if let Some(window) = &self.window { + window.is_open() + } else { + false + } + } + + pub fn is_key_pressed(&self, key: Key) -> bool { + if let Some(window) = &self.window { + window.is_key_down(key) + } else { + false + } + } + + pub fn is_esc_pressed(&self) -> bool { + self.is_key_pressed(Key::Escape) + } + + pub fn resizable(mut self, x: bool) -> Self { + self.window_resizable = x; + self + } + + pub fn with_scale(mut self, x: f32) -> Self { + self.window_scale = x; + self + } + + pub fn with_fps(mut self, x: usize) -> Self { + self.fps = x; + self + } + + pub fn with_delay(mut self, x: usize) -> Self { + self.fps_poll = 1000 / x; + self + } + + pub fn wh(&self) -> Option<(usize, usize)> { + self.window.as_ref().map(|x| x.get_size()) + } + + fn rgb8_to_u32(r: u8, g: u8, b: u8) -> u32 { + let (r, g, b) = (r as u32, g as u32, b as u32); + (r << 16) | (g << 8) | b + } +} diff --git a/src/models/blip.rs b/src/models/blip.rs index b6d74f9..72c8925 100644 --- a/src/models/blip.rs +++ b/src/models/blip.rs @@ -56,8 +56,8 @@ impl Blip { let xs_ = X::apply(&[ Ops::Resize( xs, - self.height.opt as u32, - self.width.opt as u32, + self.height.opt() as u32, + self.width.opt() as u32, "Bilinear", ), Ops::Normalize(0., 255.), @@ -146,10 +146,10 @@ impl Blip { } pub fn batch_visual(&self) -> usize { - self.batch_visual.opt as usize + self.batch_visual.opt() } pub fn batch_textual(&self) -> usize { - self.batch_textual.opt as usize + self.batch_textual.opt() } } diff --git a/src/models/clip.rs b/src/models/clip.rs index e2aaaba..f9bdee6 100644 --- a/src/models/clip.rs +++ b/src/models/clip.rs @@ -66,8 +66,8 @@ impl Clip { let xs_ = X::apply(&[ Ops::Resize( xs, - self.height.opt as u32, - self.width.opt as u32, + self.height.opt() as u32, + self.width.opt() as u32, "Bilinear", ), Ops::Normalize(0., 255.), @@ -98,10 +98,10 @@ impl Clip { } pub fn batch_visual(&self) -> usize { - self.batch_visual.opt as usize + self.batch_visual.opt() } pub fn batch_textual(&self) -> usize { - self.batch_textual.opt as usize + self.batch_textual.opt() } } diff --git a/src/models/db.rs b/src/models/db.rs index c4ba46b..aefa620 100644 --- a/src/models/db.rs +++ b/src/models/db.rs @@ -162,14 +162,14 @@ impl DB { } pub fn batch(&self) -> isize { - self.batch.opt + self.batch.opt() as _ } pub fn width(&self) -> isize { - self.width.opt + self.width.opt() as _ } pub fn height(&self) -> isize { - self.height.opt + self.height.opt() as _ } } diff --git a/src/models/depth_anything.rs b/src/models/depth_anything.rs index 1b97ad7..4573dfb 100644 --- a/src/models/depth_anything.rs +++ b/src/models/depth_anything.rs @@ -33,8 +33,8 @@ impl DepthAnything { let xs_ = X::apply(&[ Ops::Resize( xs, - self.height.opt as u32, - self.width.opt as u32, + self.height.opt() as u32, + self.width.opt() as u32, "Lanczos3", ), Ops::Normalize(0., 255.), @@ -77,14 +77,14 @@ impl DepthAnything { } pub fn batch(&self) -> isize { - self.batch.opt + self.batch.opt() as _ } pub fn width(&self) -> isize { - self.width.opt + self.width.opt() as _ } pub fn height(&self) -> isize { - self.height.opt + self.height.opt() as _ } } diff --git a/src/models/dinov2.rs b/src/models/dinov2.rs index 49e1e3b..1facfa9 100644 --- a/src/models/dinov2.rs +++ b/src/models/dinov2.rs @@ -51,8 +51,8 @@ impl Dinov2 { let xs_ = X::apply(&[ Ops::Resize( xs, - self.height.opt as u32, - self.width.opt as u32, + self.height.opt() as u32, + self.width.opt() as u32, "Lanczos3", ), Ops::Normalize(0., 255.), diff --git a/src/models/florence2.rs b/src/models/florence2.rs index 66eb497..06d1674 100644 --- a/src/models/florence2.rs +++ b/src/models/florence2.rs @@ -80,8 +80,8 @@ impl Florence2 { let xs_ = X::apply(&[ Ops::Resize( xs, - self.height.opt as u32, - self.width.opt as u32, + self.height.opt() as u32, + self.width.opt() as u32, "Bilinear", ), Ops::Normalize(0., 255.), @@ -103,7 +103,7 @@ impl Florence2 { let image_embeddings = self.encode_images(xs)?; // note: the length of xs is not always equal to batch size - self.batch.update(xs.len() as isize); + self.batch.update_opt(xs.len() as _); // build pb let pb = build_progress_bar( @@ -454,6 +454,6 @@ impl Florence2 { } pub fn batch(&self) -> usize { - self.batch.opt as usize + self.batch.opt() } } diff --git a/src/models/grounding_dino.rs b/src/models/grounding_dino.rs index a732be5..8700c91 100644 --- a/src/models/grounding_dino.rs +++ b/src/models/grounding_dino.rs @@ -232,14 +232,14 @@ impl GroundingDINO { } pub fn batch(&self) -> isize { - self.batch.opt + self.batch.opt() as _ } pub fn width(&self) -> isize { - self.width.opt + self.width.opt() as _ } pub fn height(&self) -> isize { - self.height.opt + self.height.opt() as _ } } diff --git a/src/models/modnet.rs b/src/models/modnet.rs index f7dfd01..4f87cbd 100644 --- a/src/models/modnet.rs +++ b/src/models/modnet.rs @@ -34,8 +34,8 @@ impl MODNet { let xs_ = X::apply(&[ Ops::Resize( xs, - self.height.opt as u32, - self.width.opt as u32, + self.height.opt() as u32, + self.width.opt() as u32, "Lanczos3", ), Ops::Normalize(0., 255.), @@ -71,14 +71,14 @@ impl MODNet { } pub fn batch(&self) -> isize { - self.batch.opt + self.batch.opt() as _ } pub fn width(&self) -> isize { - self.width.opt + self.width.opt() as _ } pub fn height(&self) -> isize { - self.height.opt + self.height.opt() as _ } } diff --git a/src/models/rtmo.rs b/src/models/rtmo.rs index b3b5569..1ae4b4d 100644 --- a/src/models/rtmo.rs +++ b/src/models/rtmo.rs @@ -128,14 +128,14 @@ impl RTMO { } pub fn batch(&self) -> isize { - self.batch.opt + self.batch.opt() as _ } pub fn width(&self) -> isize { - self.width.opt + self.width.opt() as _ } pub fn height(&self) -> isize { - self.height.opt + self.height.opt() as _ } } diff --git a/src/models/sam.rs b/src/models/sam.rs index 94a7e7b..bcd12bd 100644 --- a/src/models/sam.rs +++ b/src/models/sam.rs @@ -322,14 +322,14 @@ impl SAM { } pub fn batch(&self) -> isize { - self.batch.opt + self.batch.opt() as _ } pub fn width(&self) -> isize { - self.width.opt + self.width.opt() as _ } pub fn height(&self) -> isize { - self.height.opt + self.height.opt() as _ } } diff --git a/src/models/sapiens.rs b/src/models/sapiens.rs index 26f3d3a..c43a6b9 100644 --- a/src/models/sapiens.rs +++ b/src/models/sapiens.rs @@ -146,14 +146,14 @@ impl Sapiens { } pub fn batch(&self) -> isize { - self.batch.opt + self.batch.opt() as _ } pub fn width(&self) -> isize { - self.width.opt + self.width.opt() as _ } pub fn height(&self) -> isize { - self.height.opt + self.height.opt() as _ } } diff --git a/src/models/svtr.rs b/src/models/svtr.rs index 7af5023..8f25880 100644 --- a/src/models/svtr.rs +++ b/src/models/svtr.rs @@ -46,8 +46,8 @@ impl SVTR { let xs_ = X::apply(&[ Ops::Letterbox( xs, - self.height.opt as u32, - self.width.opt as u32, + self.height.opt() as u32, + self.width.opt() as u32, "Bilinear", 0, "auto", diff --git a/src/models/yolo.rs b/src/models/yolo.rs index 7e7c9e6..635e229 100644 --- a/src/models/yolo.rs +++ b/src/models/yolo.rs @@ -506,15 +506,15 @@ impl Vision for YOLO { impl YOLO { pub fn batch(&self) -> isize { - self.batch.opt + self.batch.opt() as _ } pub fn width(&self) -> isize { - self.width.opt + self.width.opt() as _ } pub fn height(&self) -> isize { - self.height.opt + self.height.opt() as _ } pub fn version(&self) -> Option<&YOLOVersion> { diff --git a/src/models/yolop.rs b/src/models/yolop.rs index 2f02860..05adbba 100644 --- a/src/models/yolop.rs +++ b/src/models/yolop.rs @@ -172,15 +172,15 @@ impl YOLOPv2 { } pub fn batch(&self) -> isize { - self.batch.opt + self.batch.opt() as _ } pub fn width(&self) -> isize { - self.width.opt + self.width.opt() as _ } pub fn height(&self) -> isize { - self.height.opt + self.height.opt() as _ } fn get_contours_from_mask( diff --git a/src/ys/bbox.rs b/src/ys/bbox.rs index a19fdfc..5d068b8 100644 --- a/src/ys/bbox.rs +++ b/src/ys/bbox.rs @@ -15,6 +15,7 @@ pub struct Bbox { name: Option, id_born: isize, } + impl Nms for Bbox { /// Returns the confidence score of the bounding box. fn confidence(&self) -> f32 {