|
| 1 | +use anyhow::Result; |
| 2 | +use usls::{ |
| 3 | + models::{Sam3Prompt, SAM3}, |
| 4 | + Annotator, Config, DataLoader, |
| 5 | +}; |
| 6 | + |
| 7 | +#[derive(argh::FromArgs)] |
| 8 | +/// SAM3 - Segment Anything Model 3 |
| 9 | +struct Args { |
| 10 | + /// device (cpu:0, cuda:0, etc.) |
| 11 | + #[argh(option, default = "String::from(\"cpu:0\")")] |
| 12 | + device: String, |
| 13 | + |
| 14 | + /// source image paths (can specify multiple) |
| 15 | + #[argh( |
| 16 | + option, |
| 17 | + default = "vec![ |
| 18 | + String::from(\"./assets/sam3-demo.jpg\"), |
| 19 | + // String::from(\"./assets/bus.jpg\") |
| 20 | + ]" |
| 21 | + )] |
| 22 | + source: Vec<String>, |
| 23 | + |
| 24 | + /// prompts: "text;pos:x,y,w,h;neg:x,y,w,h" (can specify multiple) |
| 25 | + #[argh(option, short = 'p')] |
| 26 | + prompt: Vec<String>, |
| 27 | + |
| 28 | + /// confidence threshold (default: 0.5) |
| 29 | + #[argh(option, default = "0.5")] |
| 30 | + conf: f32, |
| 31 | + |
| 32 | + /// batch size min (default: 1) |
| 33 | + #[argh(option, default = "1")] |
| 34 | + batch_min: usize, |
| 35 | + |
| 36 | + /// batch size (default: 1) |
| 37 | + #[argh(option, default = "1")] |
| 38 | + batch: usize, |
| 39 | + |
| 40 | + /// batch size max (default: 4) |
| 41 | + #[argh(option, default = "4")] |
| 42 | + batch_max: usize, |
| 43 | + |
| 44 | + /// dtype |
| 45 | + #[argh(option, default = "String::from(\"q4f16\")")] |
| 46 | + dtype: String, |
| 47 | + |
| 48 | + /// show mask |
| 49 | + #[argh(switch)] |
| 50 | + show_mask: bool, |
| 51 | +} |
| 52 | + |
| 53 | +fn main() -> Result<()> { |
| 54 | + tracing_subscriber::fmt() |
| 55 | + .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) |
| 56 | + .with_timer(tracing_subscriber::fmt::time::ChronoLocal::rfc_3339()) |
| 57 | + .init(); |
| 58 | + |
| 59 | + let args: Args = argh::from_env(); |
| 60 | + |
| 61 | + // Parse prompts |
| 62 | + if args.prompt.is_empty() { |
| 63 | + anyhow::bail!("No prompt. Use -p \"text\" or -p \"visual;pos:x,y,w,h\""); |
| 64 | + } |
| 65 | + let prompts: Vec<Sam3Prompt> = args |
| 66 | + .prompt |
| 67 | + .iter() |
| 68 | + .map(|s| s.parse()) |
| 69 | + .collect::<std::result::Result<Vec<_>, _>>() |
| 70 | + .map_err(|e| anyhow::anyhow!("{}", e))?; |
| 71 | + |
| 72 | + // Build model |
| 73 | + let config = Config::sam3_image_predictor() |
| 74 | + .with_batch_size_all_min_opt_max(args.batch_min, args.batch, args.batch_max) |
| 75 | + .with_device_all(args.device.parse()?) |
| 76 | + .with_dtype_all(args.dtype.parse()?) |
| 77 | + .with_class_confs(&[args.conf]) |
| 78 | + .with_num_dry_run_all(1) |
| 79 | + .commit()?; |
| 80 | + let mut model = SAM3::new(config)?; |
| 81 | + |
| 82 | + // Annotator |
| 83 | + let annotator = Annotator::default().with_mask_style( |
| 84 | + usls::Style::mask() |
| 85 | + .with_draw_mask_polygon_largest(true) |
| 86 | + .with_visible(args.show_mask), |
| 87 | + ); |
| 88 | + let output_dir = usls::Dir::Current.base_dir_with_subs(&["runs", model.spec()])?; |
| 89 | + |
| 90 | + // DataLoader with batch iteration |
| 91 | + let dataloader = DataLoader::from_paths(&args.source)? |
| 92 | + .with_batch(args.batch) |
| 93 | + .with_progress_bar(true) |
| 94 | + .build()?; |
| 95 | + |
| 96 | + // Process in batches |
| 97 | + for batch in dataloader { |
| 98 | + let ys = model.forward(&batch, &prompts)?; |
| 99 | + println!("ys: {:?}", ys); |
| 100 | + |
| 101 | + for (img, y) in batch.iter().zip(ys.iter()) { |
| 102 | + annotator |
| 103 | + .annotate(img, y)? |
| 104 | + .save(output_dir.join(format!("{}.jpg", usls::timestamp(None))))?; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + usls::perf(false); |
| 109 | + Ok(()) |
| 110 | +} |
0 commit comments