mahan_ym commited on
Commit
f305549
·
1 Parent(s): 7869d96

pushed the quantized model, edit readme.

Browse files
README.md CHANGED
@@ -1,3 +1,104 @@
1
  ---
2
  license: mit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: mit
3
+ language:
4
+ - en
5
+ model_name: BioCLIP-2 Quantized
6
+ model_description: "BioCLIP-2 Quantized is a quantized version of BioCLIP-2, a foundation model for biological organismal images. It is trained on TreeOfLife-200M on the basis of a CLIP model (ViT-14/L) pre-trained on LAION-2B. BioCLIP-2 yields state-of-the-art performance in recognizing various species. More importantly, it demonstrates emergent properties beyond species classification after extensive hierarchical contrastive training."
7
+ base_model:
8
+ - imageomics/bioclip-2
9
+ tags:
10
+ - bioclip
11
+ - bioclip-2
12
+ - biology
13
+ - CV
14
+ - images
15
+ - imageomics
16
+ - clip
17
+ - species-classification
18
+ - biological visual task
19
+ - multimodal
20
+ - animals
21
+ - species
22
+ - taxonomy
23
+ - rare species
24
+ - endangered species
25
+ - evolutionary biology
26
+ - knowledge-guided
27
+ - zero-shot-image-classification
28
+ datasets:
29
+ - imageomics/TreeOfLife-200M
30
+ - GBIF
31
+ - bioscan-ml/BIOSCAN-5M
32
+ - EOL
33
+ - FathomNet
34
  ---
35
+
36
+ # Model Card for BioCLIP-2 Quantized
37
+ BioCLIP-2 Quantized is a quantized version of BioCLIP-2, a foundation model for biological organismal images [read more about the model here](https://huggingface.co/imageomics/bioclip-2). The quantized model is designed to reduce memory usage and improve inference with cost of losing a little accuracy.
38
+
39
+ ## Model Description
40
+ BioCLIP-2 Quantized is dynamically quantized from the original BioCLIP-2 model. The Pytorch `onnx.export` function is used to convert the original model to ONNX format, and then the `onnxruntime.quantization.quantize_dynamic` function is used to perform dynamic quantization on the ONNX model with `weight_type` set to `QuantType.QInt8`.
41
+
42
+ ## Inference
43
+ To use the quantized model for inference, you can use the `onnxruntime` library. Here is an example code snippet:
44
+
45
+ ```python
46
+
47
+ import onnxruntime as ort
48
+ import torch
49
+ import torch.nn.functional as F
50
+ import numpy as np
51
+ from huggingface_hub import hf_hub_download
52
+
53
+
54
+ # Load the quantized model
55
+ ort_session = ort.InferenceSession("path/to/bioclip-2-quantized.onnx", providers=['CPUExecutionProvider'])
56
+
57
+ # only return one label
58
+ k = 1
59
+
60
+ # Preprocess image
61
+ img_tensor = preprocess_img(img).unsqueeze(0)
62
+ img_np = img_tensor.numpy()
63
+
64
+ # Run ONNX inference
65
+ input_name = session.get_inputs()[0].name
66
+ output_name = session.get_outputs()[0].name
67
+
68
+ img_features_np = session.run([output_name], {input_name: img_np})[0]
69
+
70
+ # Convert back to torch for compatibility with existing code
71
+ img_features = torch.from_numpy(img_features_np)
72
+ img_features = F.normalize(img_features, dim=-1)
73
+
74
+ # optional: for the open ended classification that you need a text embedding:
75
+ txt_emb = torch.from_numpy(
76
+ np.load(
77
+ hf_hub_download(
78
+ repo_id="imageomics/TreeOfLife-200M",
79
+ filename="embeddings/txt_emb_species.npy",
80
+ repo_type="dataset",
81
+ )
82
+ )
83
+ )
84
+ # or you can skip this and use zero-shot classification with your own text inputs
85
+
86
+ # Use the same text embeddings and logit scale from the original model (logit_scale of the main model: 100.00000762939453)
87
+ logits = (model.logit_scale.exp() * img_features @ txt_emb).squeeze()
88
+ probs = F.softmax(logits, dim=0)
89
+
90
+ topk = probs.topk(k)
91
+ prediction_dict = {
92
+ format_name(*txt_names[i]): prob
93
+ for i, prob in zip(topk.indices, topk.values)
94
+ }
95
+
96
+ print(prediction_dict)
97
+ ```
98
+
99
+ ## Tradeoff
100
+ The Model got tested on [Animals and Plants dataset by Nguyen Le Truong Thien](https://www.kaggle.com/datasets/nguyenletruongthien/animals-and-plants-dataset) for open-ended species classification and the highest probability class is selected as the predicted class and compared with the main BioCLIP-2 model. The results are as follows:
101
+ ![alt text](./doc/comparison_to_main_model.png)
102
+
103
+ ## Acknowledgements
104
+ The main model is developed by the Imageomics Institute team. And the current model is just a quantized version of the main model to reduce memory usage and improve inference speed and make the model more accessible.
doc/comparison_to_main_model.png ADDED
onnx/bioclip2_model_int8.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6932aadb7f54f33a40ce0d38acf5e867de015eded62632f984d49cd2b1431f3f
3
+ size 306917008