onnxruntime_ng/download/vision/
image_classification.rs

1//! Module defining image classification models available to download.
2//!
3//! See [https://github.com/onnx/models#image_classification](https://github.com/onnx/models#image_classification)
4
5// Acronyms are specific ONNX model names and contains upper cases
6#![allow(clippy::upper_case_acronyms)]
7
8use crate::download::{vision::Vision, AvailableOnnxModel, ModelUrl};
9
10/// Image classification model
11///
12/// > This collection of models take images as input, then classifies the major objects in the images
13/// > into 1000 object categories such as keyboard, mouse, pencil, and many animals.
14///
15/// Source: [https://github.com/onnx/models#image-classification-](https://github.com/onnx/models#image-classification-)
16#[derive(Debug, Clone)]
17pub enum ImageClassification {
18    /// Image classification aimed for mobile targets.
19    ///
20    /// > MobileNet models perform image classification - they take images as input and classify the major
21    /// > object in the image into a set of pre-defined classes. They are trained on ImageNet dataset which
22    /// > contains images from 1000 classes. MobileNet models are also very efficient in terms of speed and
23    /// > size and hence are ideal for embedded and mobile applications.
24    ///
25    /// Source: [https://github.com/onnx/models/tree/master/vision/classification/mobilenet](https://github.com/onnx/models/tree/master/vision/classification/mobilenet)
26    ///
27    /// Variant downloaded: ONNX Version 1.2.1 with Opset Version 7.
28    MobileNet,
29    /// Image classification, trained on ImageNet with 1000 classes.
30    ///
31    /// > ResNet models provide very high accuracies with affordable model sizes. They are ideal for cases when
32    /// > high accuracy of classification is required.
33    ///
34    /// Source: [https://github.com/onnx/models/tree/master/vision/classification/resnet](https://github.com/onnx/models/tree/master/vision/classification/resnet)
35    ResNet(ResNet),
36    /// A small CNN with AlexNet level accuracy on ImageNet with 50x fewer parameters.
37    ///
38    /// > SqueezeNet is a small CNN which achieves AlexNet level accuracy on ImageNet with 50x fewer parameters.
39    /// > SqueezeNet requires less communication across servers during distributed training, less bandwidth to
40    /// > export a new model from the cloud to an autonomous car and more feasible to deploy on FPGAs and other
41    /// > hardware with limited memory.
42    ///
43    /// Source: [https://github.com/onnx/models/tree/master/vision/classification/squeezenet](https://github.com/onnx/models/tree/master/vision/classification/squeezenet)
44    ///
45    /// Variant downloaded: SqueezeNet v1.1, ONNX Version 1.2.1 with Opset Version 7.
46    SqueezeNet,
47    /// Image classification, trained on ImageNet with 1000 classes.
48    ///
49    /// > VGG models provide very high accuracies but at the cost of increased model sizes. They are ideal for
50    /// > cases when high accuracy of classification is essential and there are limited constraints on model sizes.
51    ///
52    /// Source: [https://github.com/onnx/models/tree/master/vision/classification/vgg](https://github.com/onnx/models/tree/master/vision/classification/vgg)
53    Vgg(Vgg),
54    /// Convolutional neural network for classification, which competed in the ImageNet Large Scale Visual Recognition Challenge in 2012.
55    ///
56    /// Source: [https://github.com/onnx/models/tree/master/vision/classification/alexnet](https://github.com/onnx/models/tree/master/vision/classification/alexnet)
57    ///
58    /// Variant downloaded: ONNX Version 1.4 with Opset Version 9.
59    AlexNet,
60    /// Convolutional neural network for classification, which competed in the ImageNet Large Scale Visual Recognition Challenge in 2014.
61    ///
62    /// Source: [https://github.com/onnx/models/tree/master/vision/classification/inception_and_googlenet/googlenet](https://github.com/onnx/models/tree/master/vision/classification/inception_and_googlenet/googlenet)
63    ///
64    /// Variant downloaded: ONNX Version 1.4 with Opset Version 9.
65    GoogleNet,
66    /// Variant of AlexNet, it's the name of a convolutional neural network for classification, which competed in the ImageNet Large Scale Visual Recognition Challenge in 2012.
67    ///
68    /// Source: [https://github.com/onnx/models/tree/master/vision/classification/caffenet](https://github.com/onnx/models/tree/master/vision/classification/caffenet)
69    ///
70    /// Variant downloaded: ONNX Version 1.4 with Opset Version 9.
71    CaffeNet,
72    /// Convolutional neural network for detection.
73    ///
74    /// > This model was made by transplanting the R-CNN SVM classifiers into a fc-rcnn classification layer.
75    ///
76    /// Source: [https://github.com/onnx/models/tree/master/vision/classification/rcnn_ilsvrc13](https://github.com/onnx/models/tree/master/vision/classification/rcnn_ilsvrc13)
77    ///
78    /// Variant downloaded: ONNX Version 1.4 with Opset Version 9.
79    RcnnIlsvrc13,
80    /// Convolutional neural network for classification.
81    ///
82    /// Source: [https://github.com/onnx/models/tree/master/vision/classification/rcnn_ilsvrc13](https://github.com/onnx/models/tree/master/vision/classification/rcnn_ilsvrc13)
83    ///
84    /// Variant downloaded: ONNX Version 1.4 with Opset Version 9.
85    DenseNet121,
86    /// Google's Inception
87    Inception(InceptionVersion),
88    /// Computationally efficient CNN architecture designed specifically for mobile devices with very limited computing power.
89    ///
90    /// Source: [https://github.com/onnx/models/tree/master/vision/classification/shufflenet](https://github.com/onnx/models/tree/master/vision/classification/shufflenet)
91    ShuffleNet(ShuffleNetVersion),
92    /// Deep convolutional networks for classification.
93    ///
94    /// > This model's 4th layer has 512 maps instead of 1024 maps mentioned in the paper.
95    ///
96    /// Source: [https://github.com/onnx/models/tree/master/vision/classification/zfnet-512](https://github.com/onnx/models/tree/master/vision/classification/zfnet-512)
97    ZFNet512,
98    /// Image classification model that achieves state-of-the-art accuracy.
99    ///
100    /// >  It is designed to run on mobile CPU, GPU, and EdgeTPU devices, allowing for applications on mobile and loT, where computational resources are limited.
101    ///
102    /// Source: [https://github.com/onnx/models/tree/master/vision/classification/efficientnet-lite4](https://github.com/onnx/models/tree/master/vision/classification/efficientnet-lite4)
103    ///
104    /// Variant downloaded: ONNX Version 1.7.0 with Opset Version 11.
105    EfficientNetLite4,
106}
107
108/// Google's Inception
109#[derive(Debug, Clone)]
110pub enum InceptionVersion {
111    /// Google's Inception v1
112    ///
113    /// Source: [https://github.com/onnx/models/tree/master/vision/classification/inception_and_googlenet/inception_v1](https://github.com/onnx/models/tree/master/vision/classification/inception_and_googlenet/inception_v1)
114    ///
115    /// Variant downloaded: ONNX Version 1.4 with Opset Version 9.
116    V1,
117    /// Google's Inception v2
118    ///
119    /// Source: [https://github.com/onnx/models/tree/master/vision/classification/inception_and_googlenet/inception_v2](https://github.com/onnx/models/tree/master/vision/classification/inception_and_googlenet/inception_v2)
120    ///
121    /// Variant downloaded: ONNX Version 1.4 with Opset Version 9.
122    V2,
123}
124
125/// ResNet
126///
127/// Source: [https://github.com/onnx/models/tree/master/vision/classification/resnet](https://github.com/onnx/models/tree/master/vision/classification/resnet)
128#[derive(Debug, Clone)]
129pub enum ResNet {
130    /// ResNet v1
131    V1(ResNetV1),
132    /// ResNet v2
133    V2(ResNetV2),
134}
135/// ResNet v1
136///
137/// Source: [https://github.com/onnx/models/tree/master/vision/classification/resnet](https://github.com/onnx/models/tree/master/vision/classification/resnet)
138#[derive(Debug, Clone)]
139pub enum ResNetV1 {
140    /// ResNet18
141    ///
142    /// Variant downloaded: ONNX Version 1.2.1 with Opset Version 7.
143    ResNet18,
144    /// ResNet34
145    ///
146    /// Variant downloaded: ONNX Version 1.2.1 with Opset Version 7.
147    ResNet34,
148    /// ResNet50
149    ///
150    /// Variant downloaded: ONNX Version 1.2.1 with Opset Version 7.
151    ResNet50,
152    /// ResNet101
153    ///
154    /// Variant downloaded: ONNX Version 1.2.1 with Opset Version 7.
155    ResNet101,
156    /// ResNet152
157    ///
158    /// Variant downloaded: ONNX Version 1.2.1 with Opset Version 7.
159    ResNet152,
160}
161/// ResNet v2
162///
163/// Source: [https://github.com/onnx/models/tree/master/vision/classification/resnet](https://github.com/onnx/models/tree/master/vision/classification/resnet)
164#[derive(Debug, Clone)]
165pub enum ResNetV2 {
166    /// ResNet18
167    ///
168    /// Variant downloaded: ONNX Version 1.2.1 with Opset Version 7.
169    ResNet18,
170    /// ResNet34
171    ///
172    /// Variant downloaded: ONNX Version 1.2.1 with Opset Version 7.
173    ResNet34,
174    /// ResNet50
175    ///
176    /// Variant downloaded: ONNX Version 1.2.1 with Opset Version 7.
177    ResNet50,
178    /// ResNet101
179    ///
180    /// Variant downloaded: ONNX Version 1.2.1 with Opset Version 7.
181    ResNet101,
182    /// ResNet152
183    ///
184    /// Variant downloaded: ONNX Version 1.2.1 with Opset Version 7.
185    ResNet152,
186}
187
188/// ResNet
189///
190/// Source: [https://github.com/onnx/models/tree/master/vision/classification/resnet](https://github.com/onnx/models/tree/master/vision/classification/resnet)
191#[derive(Debug, Clone)]
192pub enum Vgg {
193    /// VGG with 16 convolutional layers
194    ///
195    /// Variant downloaded: ONNX Version 1.2.1 with Opset Version 7.
196    Vgg16,
197    /// VGG with 16 convolutional layers, with batch normalization applied after each convolutional layer.
198    ///
199    /// The batch normalization leads to better convergence and slightly better accuracies.
200    ///
201    /// Variant downloaded: ONNX Version 1.2.1 with Opset Version 7.
202    Vgg16Bn,
203    /// VGG with 19 convolutional layers
204    ///
205    /// Variant downloaded: ONNX Version 1.2.1 with Opset Version 7.
206    Vgg19,
207    /// VGG with 19 convolutional layers, with batch normalization applied after each convolutional layer.
208    ///
209    /// The batch normalization leads to better convergence and slightly better accuracies.
210    ///
211    /// Variant downloaded: ONNX Version 1.2.1 with Opset Version 7.
212    Vgg19Bn,
213}
214
215/// Computationally efficient CNN architecture designed specifically for mobile devices with very limited computing power.
216///
217/// Source: [https://github.com/onnx/models/tree/master/vision/classification/shufflenet](https://github.com/onnx/models/tree/master/vision/classification/shufflenet)
218#[derive(Debug, Clone)]
219pub enum ShuffleNetVersion {
220    /// Source: [https://github.com/onnx/models/tree/master/vision/classification/shufflenet](https://github.com/onnx/models/tree/master/vision/classification/shufflenet)
221    ///
222    /// Variant downloaded: ONNX Version 1.4 with Opset Version 9.
223    V1,
224    /// ShuffleNetV2 is an improved architecture that is the state-of-the-art in terms of speed and accuracy tradeoff used for image classification.
225    ///
226    /// Source: [https://github.com/onnx/models/tree/master/vision/classification/shufflenet](https://github.com/onnx/models/tree/master/vision/classification/shufflenet)
227    ///
228    /// Variant downloaded: ONNX Version 1.6 with Opset Version 10.
229    V2,
230}
231
232impl ModelUrl for ImageClassification {
233    fn fetch_url(&self) -> &'static str {
234        match self {
235            ImageClassification::MobileNet => "https://github.com/onnx/models/raw/master/vision/classification/mobilenet/model/mobilenetv2-7.onnx",
236            ImageClassification::SqueezeNet => "https://github.com/onnx/models/raw/master/vision/classification/squeezenet/model/squeezenet1.1-7.onnx",
237            ImageClassification::Inception(version) => version.fetch_url(),
238            ImageClassification::ResNet(version) => version.fetch_url(),
239            ImageClassification::Vgg(variant) => variant.fetch_url(),
240            ImageClassification::AlexNet => "https://github.com/onnx/models/raw/master/vision/classification/alexnet/model/bvlcalexnet-9.onnx",
241            ImageClassification::GoogleNet => "https://github.com/onnx/models/raw/master/vision/classification/inception_and_googlenet/googlenet/model/googlenet-9.onnx",
242            ImageClassification::CaffeNet => "https://github.com/onnx/models/raw/master/vision/classification/caffenet/model/caffenet-9.onnx",
243            ImageClassification::RcnnIlsvrc13 => "https://github.com/onnx/models/raw/master/vision/classification/rcnn_ilsvrc13/model/rcnn-ilsvrc13-9.onnx",
244            ImageClassification::DenseNet121 => "https://github.com/onnx/models/raw/master/vision/classification/densenet-121/model/densenet-9.onnx",
245            ImageClassification::ShuffleNet(version) => version.fetch_url(),
246            ImageClassification::ZFNet512 => "https://github.com/onnx/models/raw/master/vision/classification/zfnet-512/model/zfnet512-9.onnx",
247            ImageClassification::EfficientNetLite4 => "https://github.com/onnx/models/raw/master/vision/classification/efficientnet-lite4/model/efficientnet-lite4.onnx"
248        }
249    }
250}
251
252impl ModelUrl for InceptionVersion {
253    fn fetch_url(&self) -> &'static str {
254        match self {
255            InceptionVersion::V1 => "https://github.com/onnx/models/raw/master/vision/classification/inception_and_googlenet/inception_v1/model/inception-v1-9.onnx",
256            InceptionVersion::V2 => "https://github.com/onnx/models/raw/master/vision/classification/inception_and_googlenet/inception_v2/model/inception-v2-9.onnx",
257        }
258    }
259}
260
261impl ModelUrl for ResNet {
262    fn fetch_url(&self) -> &'static str {
263        match self {
264            ResNet::V1(variant) => variant.fetch_url(),
265            ResNet::V2(variant) => variant.fetch_url(),
266        }
267    }
268}
269
270impl ModelUrl for ResNetV1 {
271    fn fetch_url(&self) -> &'static str {
272        match self {
273            ResNetV1::ResNet18 => "https://github.com/onnx/models/raw/master/vision/classification/resnet/model/resnet18-v1-7.onnx",
274            ResNetV1::ResNet34 => "https://github.com/onnx/models/raw/master/vision/classification/resnet/model/resnet34-v1-7.onnx",
275            ResNetV1::ResNet50 => "https://github.com/onnx/models/raw/master/vision/classification/resnet/model/resnet50-v1-7.onnx",
276            ResNetV1::ResNet101 => "https://github.com/onnx/models/raw/master/vision/classification/resnet/model/resnet101-v1-7.onnx",
277            ResNetV1::ResNet152 => "https://github.com/onnx/models/raw/master/vision/classification/resnet/model/resnet152-v1-7.onnx",
278        }
279    }
280}
281
282impl ModelUrl for ResNetV2 {
283    fn fetch_url(&self) -> &'static str {
284        match self {
285            ResNetV2::ResNet18 => "https://github.com/onnx/models/raw/master/vision/classification/resnet/model/resnet18-v2-7.onnx",
286            ResNetV2::ResNet34 => "https://github.com/onnx/models/raw/master/vision/classification/resnet/model/resnet34-v2-7.onnx",
287            ResNetV2::ResNet50 => "https://github.com/onnx/models/raw/master/vision/classification/resnet/model/resnet50-v2-7.onnx",
288            ResNetV2::ResNet101 => "https://github.com/onnx/models/raw/master/vision/classification/resnet/model/resnet101-v2-7.onnx",
289            ResNetV2::ResNet152 => "https://github.com/onnx/models/raw/master/vision/classification/resnet/model/resnet152-v2-7.onnx",
290        }
291    }
292}
293
294impl ModelUrl for Vgg {
295    fn fetch_url(&self) -> &'static str {
296        match self {
297            Vgg::Vgg16 => "https://github.com/onnx/models/raw/master/vision/classification/vgg/model/vgg16-7.onnx",
298            Vgg::Vgg16Bn => "https://github.com/onnx/models/raw/master/vision/classification/vgg/model/vgg16-bn-7.onnx",
299            Vgg::Vgg19 => "https://github.com/onnx/models/raw/master/vision/classification/vgg/model/vgg19-7.onnx",
300            Vgg::Vgg19Bn => "https://github.com/onnx/models/raw/master/vision/classification/vgg/model/vgg19-bn-7.onnx",
301        }
302    }
303}
304
305impl ModelUrl for ShuffleNetVersion {
306    fn fetch_url(&self) -> &'static str {
307        match self {
308            ShuffleNetVersion::V1 => "https://github.com/onnx/models/raw/master/vision/classification/shufflenet/model/shufflenet-9.onnx",
309            ShuffleNetVersion::V2 => "https://github.com/onnx/models/raw/master/vision/classification/shufflenet/model/shufflenet-v2-10.onnx",
310        }
311    }
312}
313
314impl From<ImageClassification> for AvailableOnnxModel {
315    fn from(model: ImageClassification) -> Self {
316        AvailableOnnxModel::Vision(Vision::ImageClassification(model))
317    }
318}
319
320impl From<ResNet> for AvailableOnnxModel {
321    fn from(variant: ResNet) -> Self {
322        AvailableOnnxModel::Vision(Vision::ImageClassification(ImageClassification::ResNet(
323            variant,
324        )))
325    }
326}
327
328impl From<Vgg> for AvailableOnnxModel {
329    fn from(variant: Vgg) -> Self {
330        AvailableOnnxModel::Vision(Vision::ImageClassification(ImageClassification::Vgg(
331            variant,
332        )))
333    }
334}
335
336impl From<InceptionVersion> for AvailableOnnxModel {
337    fn from(variant: InceptionVersion) -> Self {
338        AvailableOnnxModel::Vision(Vision::ImageClassification(ImageClassification::Inception(
339            variant,
340        )))
341    }
342}
343
344impl From<ShuffleNetVersion> for AvailableOnnxModel {
345    fn from(variant: ShuffleNetVersion) -> Self {
346        AvailableOnnxModel::Vision(Vision::ImageClassification(
347            ImageClassification::ShuffleNet(variant),
348        ))
349    }
350}