summaryrefslogtreecommitdiff
path: root/candle-examples/examples
diff options
context:
space:
mode:
authorJani Monoses <jani.monoses@gmail.com>2024-08-01 12:59:22 +0300
committerGitHub <noreply@github.com>2024-08-01 11:59:22 +0200
commitac51f477eb354c319e604a5a4edc846e9ebc598f (patch)
tree507fae826e6494f576865255b47b22f2cb9cf5fb /candle-examples/examples
parentd4b6f6eef64d805e9fd678608378e1dfeb8278d2 (diff)
downloadcandle-ac51f477eb354c319e604a5a4edc846e9ebc598f.tar.gz
candle-ac51f477eb354c319e604a5a4edc846e9ebc598f.tar.bz2
candle-ac51f477eb354c319e604a5a4edc846e9ebc598f.zip
Add Hiera vision model. (#2382)
Diffstat (limited to 'candle-examples/examples')
-rw-r--r--candle-examples/examples/hiera/README.md18
-rw-r--r--candle-examples/examples/hiera/main.rs99
2 files changed, 117 insertions, 0 deletions
diff --git a/candle-examples/examples/hiera/README.md b/candle-examples/examples/hiera/README.md
new file mode 100644
index 00000000..763ce1a5
--- /dev/null
+++ b/candle-examples/examples/hiera/README.md
@@ -0,0 +1,18 @@
+# hiera
+
+[Hiera: A Hierarchical Vision Transformer without the Bells-and-Whistles](https://arxiv.org/abs/2306.00989)
+This candle implementation uses pre-trained Hiera models from timm for inference.
+The classification head has been trained on the ImageNet dataset and returns the probabilities for the top-5 classes.
+
+## Running an example
+
+```
+$ cargo run --example hiera --release -- --image candle-examples/examples/yolo-v8/assets/bike.jpg --which tiny
+loaded image Tensor[dims 3, 224, 224; f32]
+model built
+mountain bike, all-terrain bike, off-roader: 71.15%
+unicycle, monocycle : 7.11%
+knee pad : 4.26%
+crash helmet : 1.48%
+moped : 1.07%
+```
diff --git a/candle-examples/examples/hiera/main.rs b/candle-examples/examples/hiera/main.rs
new file mode 100644
index 00000000..55bb1d54
--- /dev/null
+++ b/candle-examples/examples/hiera/main.rs
@@ -0,0 +1,99 @@
+#[cfg(feature = "mkl")]
+extern crate intel_mkl_src;
+
+#[cfg(feature = "accelerate")]
+extern crate accelerate_src;
+
+use clap::{Parser, ValueEnum};
+
+use candle::{DType, IndexOp, D};
+use candle_nn::{Module, VarBuilder};
+use candle_transformers::models::hiera;
+
+#[derive(Clone, Copy, Debug, ValueEnum)]
+enum Which {
+ Tiny,
+ Small,
+ Base,
+ BasePlus,
+ Large,
+ Huge,
+}
+
+impl Which {
+ fn model_filename(&self) -> String {
+ let name = match self {
+ Self::Tiny => "tiny",
+ Self::Small => "small",
+ Self::Base => "base",
+ Self::BasePlus => "base_plus",
+ Self::Large => "large",
+ Self::Huge => "huge",
+ };
+ format!("timm/hiera_{}_224.mae_in1k_ft_in1k", name)
+ }
+
+ fn config(&self) -> hiera::Config {
+ match self {
+ Self::Tiny => hiera::Config::tiny(),
+ Self::Small => hiera::Config::small(),
+ Self::Base => hiera::Config::base(),
+ Self::BasePlus => hiera::Config::base_plus(),
+ Self::Large => hiera::Config::large(),
+ Self::Huge => hiera::Config::huge(),
+ }
+ }
+}
+
+#[derive(Parser)]
+struct Args {
+ #[arg(long)]
+ model: Option<String>,
+
+ #[arg(long)]
+ image: String,
+
+ /// Run on CPU rather than on GPU.
+ #[arg(long)]
+ cpu: bool,
+
+ #[arg(value_enum, long, default_value_t=Which::Tiny)]
+ which: Which,
+}
+
+pub fn main() -> anyhow::Result<()> {
+ let args = Args::parse();
+
+ let device = candle_examples::device(args.cpu)?;
+
+ let image = candle_examples::imagenet::load_image224(args.image)?.to_device(&device)?;
+ println!("loaded image {image:?}");
+
+ let model_file = match args.model {
+ None => {
+ let model_name = args.which.model_filename();
+ let api = hf_hub::api::sync::Api::new()?;
+ let api = api.model(model_name);
+ api.get("model.safetensors")?
+ }
+ Some(model) => model.into(),
+ };
+
+ let vb = unsafe { VarBuilder::from_mmaped_safetensors(&[model_file], DType::F32, &device)? };
+ let model = hiera::hiera(&args.which.config(), 1000, vb)?;
+ println!("model built");
+ let logits = model.forward(&image.unsqueeze(0)?)?;
+ let prs = candle_nn::ops::softmax(&logits, D::Minus1)?
+ .i(0)?
+ .to_vec1::<f32>()?;
+ let mut prs = prs.iter().enumerate().collect::<Vec<_>>();
+ prs.sort_by(|(_, p1), (_, p2)| p2.total_cmp(p1));
+ for &(category_idx, pr) in prs.iter().take(5) {
+ println!(
+ "{:24}: {:.2}%",
+ candle_examples::imagenet::CLASSES[category_idx],
+ 100. * pr
+ );
+ }
+ Ok(())
+}