qqc1989 commited on
Commit
f752476
·
verified ·
1 Parent(s): 436956c

Upload 11 files

Browse files
.gitattributes CHANGED
@@ -34,3 +34,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *.axmodel filter=lfs diff=lfs merge=lfs -text
36
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *.axmodel filter=lfs diff=lfs merge=lfs -text
36
  *tfevents* filter=lfs diff=lfs merge=lfs -text
37
+ 000000039769.jpg filter=lfs diff=lfs merge=lfs -text
000000039769.jpg ADDED

Git LFS Details

  • SHA256: dea9e7ef97386345f7cff32f9055da4982da5471c48d575146c796ab4563b04e
  • Pointer size: 131 Bytes
  • Size of remote file: 173 kB
config.json ADDED
File without changes
python/inference_axmodel.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ from PIL import Image
3
+ import numpy as np
4
+ import torch
5
+ from transformers import AutoProcessor, AutoModel
6
+ import axengine as ort
7
+
8
+ # Set random seed
9
+ np.random.seed(42)
10
+
11
+ def load_model(model_path):
12
+ """
13
+ Load an ONNX model and measure loading time.
14
+
15
+ Args:
16
+ model_path (str): Path to the ONNX model file.
17
+
18
+ Returns:
19
+ ort.InferenceSession: Loaded ONNX model session.
20
+ """
21
+ start_time = time.time()
22
+ session = ort.InferenceSession(model_path)
23
+ elapsed_time = time.time() - start_time
24
+ print(f"Model loading time: {elapsed_time:.2f} seconds")
25
+ return session
26
+
27
+ def model_inference(sess, inputs):
28
+ """
29
+ Perform inference using the given ONNX session and measure inference time.
30
+
31
+ Args:
32
+ sess (ort.InferenceSession): ONNX model session.
33
+ inputs (list or np.ndarray): Input data.
34
+
35
+ Returns:
36
+ np.ndarray: Inference results.
37
+ """
38
+ input_name = sess.get_inputs()[0].name
39
+ start_time = time.time()
40
+ outputs = [sess.run(None, {input_name: np.array([i])})[1] for i in inputs]
41
+ outputs = np.array(outputs).reshape(-1, 1152) # Adjust output shape if needed
42
+ elapsed_time = time.time() - start_time
43
+ print(f"Model inference time: {elapsed_time:.2f} seconds")
44
+ return outputs
45
+
46
+
47
+ # Load processor and prepare inputs
48
+ processor = AutoProcessor.from_pretrained("./tokenizer")
49
+ image = Image.open("./000000039769.jpg")
50
+ texts = ["a photo of 2 cats", "a photo of 2 dogs"]
51
+ inputs = processor(text=texts, images=image, padding="max_length", return_tensors="pt")
52
+
53
+ # Load vision and text models
54
+ start_time = time.time()
55
+ vision_sess = load_model("./ax650/siglip_vision_u16_fcu8.axmodel")
56
+ text_sess = load_model("./ax650/siglip_text_u16.axmodel")
57
+ print(f"Total model loading time: {time.time() - start_time:.2f} seconds")
58
+
59
+ # Run inference on both models
60
+ start_time = time.time()
61
+ vision_outputs = model_inference(vision_sess, inputs['pixel_values'].numpy())
62
+ text_outputs = model_inference(text_sess, inputs['input_ids'].numpy().astype(np.int32))
63
+ print(f"Total inference time: {time.time() - start_time:.2f} seconds")
64
+
65
+ # Post-processing
66
+ image_embeds = torch.tensor(vision_outputs)
67
+ text_embeds = torch.tensor(text_outputs)
68
+
69
+ # Normalize features
70
+ image_embeds = image_embeds / image_embeds.norm(p=2, dim=-1, keepdim=True)
71
+ text_embeds = text_embeds / text_embeds.norm(p=2, dim=-1, keepdim=True)
72
+
73
+ # Compute similarity logits
74
+ logit_scale = np.random.randn(1)[0] # Replace with trained value if available
75
+ logit_bias = np.random.randn(1)[0]
76
+
77
+ logits_per_text = (
78
+ np.matmul(text_embeds, image_embeds.t().numpy()) * np.exp(logit_scale)
79
+ + logit_bias
80
+ )
81
+ logits_per_image = logits_per_text.T
82
+
83
+ def sigmoid(x):
84
+ return 1 / (1 + np.exp(-x))
85
+
86
+ probs = sigmoid(logits_per_image) # Get probabilities
87
+ print(f"{probs[0][0]:.1%} that image 0 is '{texts[0]}'")
python/inference_onnx.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ from PIL import Image
3
+ import numpy as np
4
+ import torch
5
+ from transformers import AutoProcessor, AutoModel
6
+ import onnxruntime as ort
7
+
8
+ # Set random seed
9
+ np.random.seed(42)
10
+
11
+ def load_model(model_path):
12
+ """
13
+ Load an ONNX model and measure loading time.
14
+
15
+ Args:
16
+ model_path (str): Path to the ONNX model file.
17
+
18
+ Returns:
19
+ ort.InferenceSession: Loaded ONNX model session.
20
+ """
21
+ start_time = time.time()
22
+ session = ort.InferenceSession(model_path)
23
+ elapsed_time = time.time() - start_time
24
+ print(f"Model loading time: {elapsed_time:.2f} seconds")
25
+ return session
26
+
27
+ def model_inference(sess, inputs):
28
+ """
29
+ Perform inference using the given ONNX session and measure inference time.
30
+
31
+ Args:
32
+ sess (ort.InferenceSession): ONNX model session.
33
+ inputs (list or np.ndarray): Input data.
34
+
35
+ Returns:
36
+ np.ndarray: Inference results.
37
+ """
38
+ input_name = sess.get_inputs()[0].name
39
+ start_time = time.time()
40
+ outputs = [sess.run(None, {input_name: np.array([i])})[1] for i in inputs]
41
+ outputs = np.array(outputs).reshape(-1, 1152) # Adjust output shape if needed
42
+ elapsed_time = time.time() - start_time
43
+ print(f"Model inference time: {elapsed_time:.2f} seconds")
44
+ return outputs
45
+
46
+
47
+ # Load processor and prepare inputs
48
+ processor = AutoProcessor.from_pretrained("./tokenizer")
49
+ image = Image.open("./000000039769.jpg")
50
+ texts = ["a photo of 2 cats", "a photo of 2 dogs"]
51
+ inputs = processor(text=texts, images=image, padding="max_length", return_tensors="pt")
52
+
53
+ # Load vision and text models
54
+ start_time = time.time()
55
+ vision_sess = load_model("./onnx/siglip-so400m-patch14-384_vision.onnx")
56
+ text_sess = load_model("./onnx/siglip-so400m-patch14-384_text.onnx")
57
+ print(f"Total model loading time: {time.time() - start_time:.2f} seconds")
58
+
59
+ # Run inference on both models
60
+ start_time = time.time()
61
+ vision_outputs = model_inference(vision_sess, inputs['pixel_values'].numpy())
62
+ text_outputs = model_inference(text_sess, inputs['input_ids'].numpy())
63
+ print(f"Total inference time: {time.time() - start_time:.2f} seconds")
64
+
65
+ # Post-processing
66
+ image_embeds = torch.tensor(vision_outputs)
67
+ text_embeds = torch.tensor(text_outputs)
68
+
69
+ # Normalize features
70
+ image_embeds = image_embeds / image_embeds.norm(p=2, dim=-1, keepdim=True)
71
+ text_embeds = text_embeds / text_embeds.norm(p=2, dim=-1, keepdim=True)
72
+
73
+ # Compute similarity logits
74
+ logit_scale = np.random.randn(1)[0] # Replace with trained value if available
75
+ logit_bias = np.random.randn(1)[0]
76
+
77
+ logits_per_text = (
78
+ np.matmul(text_embeds, image_embeds.t().numpy()) * np.exp(logit_scale)
79
+ + logit_bias
80
+ )
81
+ logits_per_image = logits_per_text.T
82
+
83
+ def sigmoid(x):
84
+ return 1 / (1 + np.exp(-x))
85
+
86
+ probs = sigmoid(logits_per_image) # Get probabilities
87
+ print(f"{probs[0][0]:.1%} that image 0 is '{texts[0]}'")
python/requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ Pillow
2
+ requests
3
+ transformers
4
+ sentencepiece
5
+ torch
tokenizer/config.json ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "SiglipModel"
4
+ ],
5
+ "initializer_factor": 1.0,
6
+ "model_type": "siglip",
7
+ "text_config": {
8
+ "hidden_size": 1152,
9
+ "intermediate_size": 4304,
10
+ "model_type": "siglip_text_model",
11
+ "num_attention_heads": 16,
12
+ "num_hidden_layers": 27
13
+ },
14
+ "torch_dtype": "float32",
15
+ "transformers_version": "4.37.0.dev0",
16
+ "vision_config": {
17
+ "hidden_size": 1152,
18
+ "image_size": 384,
19
+ "intermediate_size": 4304,
20
+ "model_type": "siglip_vision_model",
21
+ "num_attention_heads": 16,
22
+ "num_hidden_layers": 27,
23
+ "patch_size": 14
24
+ }
25
+ }
tokenizer/preprocessor_config.json ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "do_normalize": true,
3
+ "do_rescale": true,
4
+ "do_resize": true,
5
+ "image_mean": [
6
+ 0.5,
7
+ 0.5,
8
+ 0.5
9
+ ],
10
+ "image_processor_type": "SiglipImageProcessor",
11
+ "image_std": [
12
+ 0.5,
13
+ 0.5,
14
+ 0.5
15
+ ],
16
+ "processor_class": "SiglipProcessor",
17
+ "resample": 3,
18
+ "rescale_factor": 0.00392156862745098,
19
+ "size": {
20
+ "height": 384,
21
+ "width": 384
22
+ }
23
+ }
tokenizer/special_tokens_map.json ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "eos_token": {
3
+ "content": "</s>",
4
+ "lstrip": true,
5
+ "normalized": false,
6
+ "rstrip": true,
7
+ "single_word": false
8
+ },
9
+ "pad_token": {
10
+ "content": "</s>",
11
+ "lstrip": true,
12
+ "normalized": false,
13
+ "rstrip": true,
14
+ "single_word": false
15
+ },
16
+ "unk_token": {
17
+ "content": "<unk>",
18
+ "lstrip": true,
19
+ "normalized": false,
20
+ "rstrip": true,
21
+ "single_word": false
22
+ }
23
+ }
tokenizer/spiece.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1e5036bed065526c3c212dfbe288752391797c4bb1a284aa18c9a0b23fcaf8ec
3
+ size 798330
tokenizer/tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer/tokenizer_config.json ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "added_tokens_decoder": {
3
+ "1": {
4
+ "content": "</s>",
5
+ "lstrip": true,
6
+ "normalized": false,
7
+ "rstrip": true,
8
+ "single_word": false,
9
+ "special": true
10
+ },
11
+ "2": {
12
+ "content": "<unk>",
13
+ "lstrip": true,
14
+ "normalized": false,
15
+ "rstrip": true,
16
+ "single_word": false,
17
+ "special": true
18
+ }
19
+ },
20
+ "additional_special_tokens": [],
21
+ "clean_up_tokenization_spaces": true,
22
+ "do_lower_case": true,
23
+ "eos_token": "</s>",
24
+ "model_input_names": [
25
+ "input_ids"
26
+ ],
27
+ "model_max_length": 64,
28
+ "pad_token": "</s>",
29
+ "processor_class": "SiglipProcessor",
30
+ "sp_model_kwargs": {},
31
+ "tokenizer_class": "SiglipTokenizer",
32
+ "unk_token": "<unk>"
33
+ }