pineapple_data/data/
weights.rs

1// Copyright (c) 2025, Tom Ouellette
2// Licensed under the BSD 3-Clause License
3
4use crate::get_pineapple_cache;
5use crate::request;
6
7// NOTE: This download scheme isn't really good practice as any
8// new dataset additions will require a new release of the library.
9// BUT, if there's request, then I'll move the identifiers outside
10// the library to allow for arbitrary data/dataset additions.
11
12/// Available neural network models in the pineapple library.
13pub enum Weights {
14    DinoVitSmall,
15    DinoVitBase,
16    DinobloomVitBase,
17    ScdinoVitSmall,
18    SubcellVitBase,
19}
20
21impl Weights {
22    /// Select a weights from the available weights.
23    pub fn select(weights_name: &str) -> Self {
24        match weights_name {
25            "dino_vit_small" => Weights::DinoVitSmall,
26            "dino_vit_base" => Weights::DinoVitBase,
27            "dinobloom_vit_base" => Weights::DinobloomVitBase,
28            "scdino_vit_small" => Weights::ScdinoVitSmall,
29            "subcell_vit_base" => Weights::SubcellVitBase,
30            _ => {
31                let msg = format!(
32                    "[pineapple::data::weights] Weights {} not found. Avalaible weights include: {}",
33                    weights_name,
34                    "dino_vit_small, dino_vit_base, dinobloom_vit_base, scdino_vit_small, subcell_vit_base."
35                );
36                eprintln!("{}", msg);
37                std::process::exit(1);
38            }
39        }
40    }
41
42    /// Return an iterator over the enum members.
43    pub fn iter() -> impl Iterator<Item = &'static Weights> {
44        static WEIGHTS: [Weights; 5] = [
45            Weights::DinoVitSmall,
46            Weights::DinoVitBase,
47            Weights::DinobloomVitBase,
48            Weights::ScdinoVitSmall,
49            Weights::SubcellVitBase,
50        ];
51
52        WEIGHTS.iter()
53    }
54
55    /// Get the name of the model.
56    pub fn model_name(&self) -> &str {
57        match self {
58            Weights::DinoVitSmall => "dino_vit_small",
59            Weights::DinoVitBase => "dino_vit_base",
60            Weights::DinobloomVitBase => "dinobloom_vit_base",
61            Weights::ScdinoVitSmall => "scdino_vit_small",
62            Weights::SubcellVitBase => "subcell_vit_base",
63        }
64    }
65
66    /// Get the file name of the model saved on Google drive.
67    fn file_name(&self) -> &str {
68        match self {
69            Weights::DinoVitSmall => "dinov2_vits14_imagenet.safetensors",
70            Weights::DinoVitBase => "dinov2_vitb14_imagenet.safetensors",
71            Weights::DinobloomVitBase => "dinov2_vitb14_dinobloom.safetensors",
72            Weights::ScdinoVitSmall => "scdino_vit_small.safetensors",
73            Weights::SubcellVitBase => "subcell_vit_base.safetensors",
74        }
75    }
76
77    /// Get the Google drive file identifier for the saved model.
78    pub fn file_id(&self) -> &str {
79        match self {
80            Weights::DinoVitSmall => "1xuyTyPsuPiDtec8ojZwAyXSDq9AzyPQX",
81            Weights::DinoVitBase => "19vy-A-KTaaF3vsWKxu0JpA0gaATU52Gh",
82            Weights::DinobloomVitBase => "1XhzSiO2IDKppr2UCTAio_niLSk5QA6hG",
83            Weights::ScdinoVitSmall => "1omwQNJVMkrbYCstSF11p5HsErHzINTz6",
84            Weights::SubcellVitBase => "1LZn3xlgVVd2jQIpXst4CMCYN58F-VG-x",
85        }
86    }
87
88    /// Get the usage license for a model.
89    pub fn license(&self) -> &str {
90        match self {
91            Weights::DinoVitSmall => "Apache License 2.0",
92            Weights::DinoVitBase => "Apache License 2.0",
93            Weights::DinobloomVitBase => "Apache License 2.0",
94            Weights::ScdinoVitSmall => "Apache License 2.0",
95            Weights::SubcellVitBase => "MIT License",
96        }
97    }
98
99    /// Get the authors of the model weights.
100    pub fn data_authors(&self) -> &str {
101        match self {
102            Weights::DinoVitSmall => "Huggingface/candle",
103            Weights::DinoVitBase => "Huggingface/candle",
104            Weights::DinobloomVitBase => "Marr Lab",
105            Weights::ScdinoVitSmall => "Snijder Lab",
106            Weights::SubcellVitBase => "Lundberg Lab",
107        }
108    }
109
110    /// Get the size of the model in GB.
111    pub fn data_size(&self) -> &str {
112        match self {
113            Weights::DinoVitSmall => "0.097",
114            Weights::DinoVitBase => "0.330",
115            Weights::DinobloomVitBase => "0.330",
116            Weights::SubcellVitBase => "0.330",
117            Weights::ScdinoVitSmall => "0.097",
118        }
119    }
120
121    /// Download the model to the pineapple cache.
122    pub fn download(&self, verbose: bool) {
123        let cache = get_pineapple_cache();
124        let model_name = cache.join(self.file_name());
125        if !model_name.exists() {
126            request::download_file(self.file_id(), cache.as_path(), self.file_name(), !verbose)
127                .unwrap();
128
129            if !model_name.exists() {
130                eprintln!("[pineapple::data::weights] Failed to download model weights.");
131                std::process::exit(1);
132            }
133        }
134    }
135
136    /// Get path to model weights.
137    pub fn path(&self) -> std::path::PathBuf {
138        let cache = get_pineapple_cache();
139        cache.join(self.file_name())
140    }
141}