SigLIP 2 base/16 224 for Core AI (pix)
Browse files- .gitattributes +1 -0
- LICENSE +202 -0
- NOTICE +17 -0
- README.md +79 -0
- export_siglip2.py +129 -0
- pix-siglip2-base-224.aimodel/main.hash +1 -0
- pix-siglip2-base-224.aimodel/main.mlirb +3 -0
- pix-siglip2-base-224.aimodel/metadata.json +8 -0
- pix-siglip2-support/config.json +18 -0
- pix-siglip2-support/merges.bin +3 -0
- pix-siglip2-support/vocab.bin +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
pix-siglip2-base-224.aimodel/main.mlirb filter=lfs diff=lfs merge=lfs -text
|
LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
Apache License
|
| 3 |
+
Version 2.0, January 2004
|
| 4 |
+
http://www.apache.org/licenses/
|
| 5 |
+
|
| 6 |
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
| 7 |
+
|
| 8 |
+
1. Definitions.
|
| 9 |
+
|
| 10 |
+
"License" shall mean the terms and conditions for use, reproduction,
|
| 11 |
+
and distribution as defined by Sections 1 through 9 of this document.
|
| 12 |
+
|
| 13 |
+
"Licensor" shall mean the copyright owner or entity authorized by
|
| 14 |
+
the copyright owner that is granting the License.
|
| 15 |
+
|
| 16 |
+
"Legal Entity" shall mean the union of the acting entity and all
|
| 17 |
+
other entities that control, are controlled by, or are under common
|
| 18 |
+
control with that entity. For the purposes of this definition,
|
| 19 |
+
"control" means (i) the power, direct or indirect, to cause the
|
| 20 |
+
direction or management of such entity, whether by contract or
|
| 21 |
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
| 22 |
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
| 23 |
+
|
| 24 |
+
"You" (or "Your") shall mean an individual or Legal Entity
|
| 25 |
+
exercising permissions granted by this License.
|
| 26 |
+
|
| 27 |
+
"Source" form shall mean the preferred form for making modifications,
|
| 28 |
+
including but not limited to software source code, documentation
|
| 29 |
+
source, and configuration files.
|
| 30 |
+
|
| 31 |
+
"Object" form shall mean any form resulting from mechanical
|
| 32 |
+
transformation or translation of a Source form, including but
|
| 33 |
+
not limited to compiled object code, generated documentation,
|
| 34 |
+
and conversions to other media types.
|
| 35 |
+
|
| 36 |
+
"Work" shall mean the work of authorship, whether in Source or
|
| 37 |
+
Object form, made available under the License, as indicated by a
|
| 38 |
+
copyright notice that is included in or attached to the work
|
| 39 |
+
(an example is provided in the Appendix below).
|
| 40 |
+
|
| 41 |
+
"Derivative Works" shall mean any work, whether in Source or Object
|
| 42 |
+
form, that is based on (or derived from) the Work and for which the
|
| 43 |
+
editorial revisions, annotations, elaborations, or other modifications
|
| 44 |
+
represent, as a whole, an original work of authorship. For the purposes
|
| 45 |
+
of this License, Derivative Works shall not include works that remain
|
| 46 |
+
separable from, or merely link (or bind by name) to the interfaces of,
|
| 47 |
+
the Work and Derivative Works thereof.
|
| 48 |
+
|
| 49 |
+
"Contribution" shall mean any work of authorship, including
|
| 50 |
+
the original version of the Work and any modifications or additions
|
| 51 |
+
to that Work or Derivative Works thereof, that is intentionally
|
| 52 |
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
| 53 |
+
or by an individual or Legal Entity authorized to submit on behalf of
|
| 54 |
+
the copyright owner. For the purposes of this definition, "submitted"
|
| 55 |
+
means any form of electronic, verbal, or written communication sent
|
| 56 |
+
to the Licensor or its representatives, including but not limited to
|
| 57 |
+
communication on electronic mailing lists, source code control systems,
|
| 58 |
+
and issue tracking systems that are managed by, or on behalf of, the
|
| 59 |
+
Licensor for the purpose of discussing and improving the Work, but
|
| 60 |
+
excluding communication that is conspicuously marked or otherwise
|
| 61 |
+
designated in writing by the copyright owner as "Not a Contribution."
|
| 62 |
+
|
| 63 |
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
| 64 |
+
on behalf of whom a Contribution has been received by Licensor and
|
| 65 |
+
subsequently incorporated within the Work.
|
| 66 |
+
|
| 67 |
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
| 68 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 69 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 70 |
+
copyright license to reproduce, prepare Derivative Works of,
|
| 71 |
+
publicly display, publicly perform, sublicense, and distribute the
|
| 72 |
+
Work and such Derivative Works in Source or Object form.
|
| 73 |
+
|
| 74 |
+
3. Grant of Patent License. Subject to the terms and conditions of
|
| 75 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 76 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 77 |
+
(except as stated in this section) patent license to make, have made,
|
| 78 |
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
| 79 |
+
where such license applies only to those patent claims licensable
|
| 80 |
+
by such Contributor that are necessarily infringed by their
|
| 81 |
+
Contribution(s) alone or by combination of their Contribution(s)
|
| 82 |
+
with the Work to which such Contribution(s) was submitted. If You
|
| 83 |
+
institute patent litigation against any entity (including a
|
| 84 |
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
| 85 |
+
or a Contribution incorporated within the Work constitutes direct
|
| 86 |
+
or contributory patent infringement, then any patent licenses
|
| 87 |
+
granted to You under this License for that Work shall terminate
|
| 88 |
+
as of the date such litigation is filed.
|
| 89 |
+
|
| 90 |
+
4. Redistribution. You may reproduce and distribute copies of the
|
| 91 |
+
Work or Derivative Works thereof in any medium, with or without
|
| 92 |
+
modifications, and in Source or Object form, provided that You
|
| 93 |
+
meet the following conditions:
|
| 94 |
+
|
| 95 |
+
(a) You must give any other recipients of the Work or
|
| 96 |
+
Derivative Works a copy of this License; and
|
| 97 |
+
|
| 98 |
+
(b) You must cause any modified files to carry prominent notices
|
| 99 |
+
stating that You changed the files; and
|
| 100 |
+
|
| 101 |
+
(c) You must retain, in the Source form of any Derivative Works
|
| 102 |
+
that You distribute, all copyright, patent, trademark, and
|
| 103 |
+
attribution notices from the Source form of the Work,
|
| 104 |
+
excluding those notices that do not pertain to any part of
|
| 105 |
+
the Derivative Works; and
|
| 106 |
+
|
| 107 |
+
(d) If the Work includes a "NOTICE" text file as part of its
|
| 108 |
+
distribution, then any Derivative Works that You distribute must
|
| 109 |
+
include a readable copy of the attribution notices contained
|
| 110 |
+
within such NOTICE file, excluding those notices that do not
|
| 111 |
+
pertain to any part of the Derivative Works, in at least one
|
| 112 |
+
of the following places: within a NOTICE text file distributed
|
| 113 |
+
as part of the Derivative Works; within the Source form or
|
| 114 |
+
documentation, if provided along with the Derivative Works; or,
|
| 115 |
+
within a display generated by the Derivative Works, if and
|
| 116 |
+
wherever such third-party notices normally appear. The contents
|
| 117 |
+
of the NOTICE file are for informational purposes only and
|
| 118 |
+
do not modify the License. You may add Your own attribution
|
| 119 |
+
notices within Derivative Works that You distribute, alongside
|
| 120 |
+
or as an addendum to the NOTICE text from the Work, provided
|
| 121 |
+
that such additional attribution notices cannot be construed
|
| 122 |
+
as modifying the License.
|
| 123 |
+
|
| 124 |
+
You may add Your own copyright statement to Your modifications and
|
| 125 |
+
may provide additional or different license terms and conditions
|
| 126 |
+
for use, reproduction, or distribution of Your modifications, or
|
| 127 |
+
for any such Derivative Works as a whole, provided Your use,
|
| 128 |
+
reproduction, and distribution of the Work otherwise complies with
|
| 129 |
+
the conditions stated in this License.
|
| 130 |
+
|
| 131 |
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
| 132 |
+
any Contribution intentionally submitted for inclusion in the Work
|
| 133 |
+
by You to the Licensor shall be under the terms and conditions of
|
| 134 |
+
this License, without any additional terms or conditions.
|
| 135 |
+
Notwithstanding the above, nothing herein shall supersede or modify
|
| 136 |
+
the terms of any separate license agreement you may have executed
|
| 137 |
+
with Licensor regarding such Contributions.
|
| 138 |
+
|
| 139 |
+
6. Trademarks. This License does not grant permission to use the trade
|
| 140 |
+
names, trademarks, service marks, or product names of the Licensor,
|
| 141 |
+
except as required for reasonable and customary use in describing the
|
| 142 |
+
origin of the Work and reproducing the content of the NOTICE file.
|
| 143 |
+
|
| 144 |
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
| 145 |
+
agreed to in writing, Licensor provides the Work (and each
|
| 146 |
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
| 147 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 148 |
+
implied, including, without limitation, any warranties or conditions
|
| 149 |
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
| 150 |
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
| 151 |
+
appropriateness of using or redistributing the Work and assume any
|
| 152 |
+
risks associated with Your exercise of permissions under this License.
|
| 153 |
+
|
| 154 |
+
8. Limitation of Liability. In no event and under no legal theory,
|
| 155 |
+
whether in tort (including negligence), contract, or otherwise,
|
| 156 |
+
unless required by applicable law (such as deliberate and grossly
|
| 157 |
+
negligent acts) or agreed to in writing, shall any Contributor be
|
| 158 |
+
liable to You for damages, including any direct, indirect, special,
|
| 159 |
+
incidental, or consequential damages of any character arising as a
|
| 160 |
+
result of this License or out of the use or inability to use the
|
| 161 |
+
Work (including but not limited to damages for loss of goodwill,
|
| 162 |
+
work stoppage, computer failure or malfunction, or any and all
|
| 163 |
+
other commercial damages or losses), even if such Contributor
|
| 164 |
+
has been advised of the possibility of such damages.
|
| 165 |
+
|
| 166 |
+
9. Accepting Warranty or Additional Liability. While redistributing
|
| 167 |
+
the Work or Derivative Works thereof, You may choose to offer,
|
| 168 |
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
| 169 |
+
or other liability obligations and/or rights consistent with this
|
| 170 |
+
License. However, in accepting such obligations, You may act only
|
| 171 |
+
on Your own behalf and on Your sole responsibility, not on behalf
|
| 172 |
+
of any other Contributor, and only if You agree to indemnify,
|
| 173 |
+
defend, and hold each Contributor harmless for any liability
|
| 174 |
+
incurred by, or claims asserted against, such Contributor by reason
|
| 175 |
+
of your accepting any such warranty or additional liability.
|
| 176 |
+
|
| 177 |
+
END OF TERMS AND CONDITIONS
|
| 178 |
+
|
| 179 |
+
APPENDIX: How to apply the Apache License to your work.
|
| 180 |
+
|
| 181 |
+
To apply the Apache License to your work, attach the following
|
| 182 |
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
| 183 |
+
replaced with your own identifying information. (Don't include
|
| 184 |
+
the brackets!) The text should be enclosed in the appropriate
|
| 185 |
+
comment syntax for the file format. We also recommend that a
|
| 186 |
+
file or class name and description of purpose be included on the
|
| 187 |
+
same "printed page" as the copyright notice for easier
|
| 188 |
+
identification within third-party archives.
|
| 189 |
+
|
| 190 |
+
Copyright [yyyy] [name of copyright owner]
|
| 191 |
+
|
| 192 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 193 |
+
you may not use this file except in compliance with the License.
|
| 194 |
+
You may obtain a copy of the License at
|
| 195 |
+
|
| 196 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 197 |
+
|
| 198 |
+
Unless required by applicable law or agreed to in writing, software
|
| 199 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 200 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 201 |
+
See the License for the specific language governing permissions and
|
| 202 |
+
limitations under the License.
|
NOTICE
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pix-siglip2-base-224
|
| 2 |
+
Copyright 2025 Google LLC (SigLIP 2 model weights and tokenizer)
|
| 3 |
+
|
| 4 |
+
This repository redistributes Google's SigLIP 2 base/16 at 224
|
| 5 |
+
(https://huggingface.co/google/siglip2-base-patch16-224, commit
|
| 6 |
+
75de2d55ec2d0b4efc50b3e9ad70dba96a7b2fa2), licensed under the Apache License,
|
| 7 |
+
Version 2.0 (see LICENSE).
|
| 8 |
+
|
| 9 |
+
Modifications (made for swift-photo-index, https://github.com/arraypress/swift-photo-index):
|
| 10 |
+
- The vision and text towers were exported, unchanged in architecture, to an
|
| 11 |
+
Apple Core AI asset (pix-siglip2-base-224.aimodel) with coreai-torch 0.4.3,
|
| 12 |
+
weights cast from float32 to float16, as two entry points (image, text) that
|
| 13 |
+
return the pooled embeddings (get_image_features / get_text_features).
|
| 14 |
+
- The Gemma tokenizer from the same commit was rewritten into two flat binary
|
| 15 |
+
files (pix-siglip2-support/vocab.bin, merges.bin) with the same ids and
|
| 16 |
+
merge order.
|
| 17 |
+
The export script is included as export_siglip2.py.
|
README.md
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
base_model: google/siglip2-base-patch16-224
|
| 4 |
+
tags:
|
| 5 |
+
- vision
|
| 6 |
+
- siglip2
|
| 7 |
+
- image-text-retrieval
|
| 8 |
+
- core-ai
|
| 9 |
+
- apple-silicon
|
| 10 |
+
- macos
|
| 11 |
+
library_name: swift-photo-index
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
# pix-siglip2-base-224: SigLIP 2 for Core AI (macOS 27)
|
| 15 |
+
|
| 16 |
+
Google's [SigLIP 2 base/16 at 224](https://huggingface.co/google/siglip2-base-patch16-224)
|
| 17 |
+
(Tschannen et al., *SigLIP 2: Multilingual Vision-Language Encoders with Improved Semantic
|
| 18 |
+
Understanding, Localization, and Dense Features*, 2025; Apache-2.0) exported to an Apple Core AI
|
| 19 |
+
`.aimodel`. It puts pictures and sentences in one 768-dimensional space, so a photo of a dog on a
|
| 20 |
+
beach lands near the words "dog on a beach".
|
| 21 |
+
|
| 22 |
+
This is what meaning search in [`pix`](https://github.com/arraypress/swift-pix-cli) runs on, through
|
| 23 |
+
[swift-photo-index](https://github.com/arraypress/swift-photo-index). `pix index` stores one vector per
|
| 24 |
+
image (one per video: the mean of its sampled frames). `pix "dog on a beach"` embeds the query and
|
| 25 |
+
ranks by cosine. Words that are literally there (text read off the picture, speech, the file name)
|
| 26 |
+
still rank first, so an exact invoice number beats anything that only matches by meaning. pix
|
| 27 |
+
downloads this repository by itself the first time it needs it, at a pinned commit, and checks every
|
| 28 |
+
file against its published hash. Everything runs on device.
|
| 29 |
+
|
| 30 |
+
## Files
|
| 31 |
+
|
| 32 |
+
| Path | What |
|
| 33 |
+
|---|---|
|
| 34 |
+
| `pix-siglip2-base-224.aimodel/` | Two entry points. `image`: `pixel_values` `[1,3,224,224]` float32 → `embedding` `[1,768]`. `text`: `ids` `[1,64]` int32 → `embedding` `[1,768]`. Float16 weights, 750 MB. |
|
| 35 |
+
| `pix-siglip2-support/vocab.bin`, `merges.bin` | The checkpoint's Gemma tokenizer (256,000 tokens) as flat binaries: `u32` count, then per id `u32` length + UTF-8; merges as `u32` (left, right, merged) id triples in rank order. |
|
| 36 |
+
| `pix-siglip2-support/config.json` | Source model and revision, sizes, normalisation, special-token ids. |
|
| 37 |
+
| `export_siglip2.py` | The export script (also writes the parity fixtures). |
|
| 38 |
+
|
| 39 |
+
The host does the rest, as `transformers` does. Images: resize to 224×224 without keeping the
|
| 40 |
+
aspect ratio, RGB, `(x/255 − 0.5)/0.5`, channel-first. Text: lower-case, spaces to `▁`, byte-fallback
|
| 41 |
+
BPE, append `<eos>`=1, right-pad with `<pad>`=0 to 64. Then L2-normalise both outputs and take
|
| 42 |
+
dot products.
|
| 43 |
+
|
| 44 |
+
## Fidelity
|
| 45 |
+
|
| 46 |
+
Measured from Swift through Core AI against `transformers` (float32) on the same inputs. Tokenizer
|
| 47 |
+
ids are identical on 10 test strings (accents, emoji, CJK, Arabic, tabs, truncation at 64). Text
|
| 48 |
+
embeddings: worst cosine 0.9999993. Image embeddings: cosine 0.99999 when no resize is needed, and
|
| 49 |
+
0.997 on a 400×300 image, where Core Graphics' resampling differs slightly from PIL's.
|
| 50 |
+
|
| 51 |
+
## Retrieval
|
| 52 |
+
|
| 53 |
+
1,000 COCO val2017 images (the first 1,000 by id) against their 5,003 captions, embedded through
|
| 54 |
+
this asset in Swift:
|
| 55 |
+
|
| 56 |
+
- Text → image recall: R@1 **0.720**, R@5 **0.916**, R@10 **0.959**.
|
| 57 |
+
- Through pix's own search (meaning blended with words, 0.09 cut-off): R@1 0.719, R@5 0.911,
|
| 58 |
+
R@10 0.948. Words alone find none of the captions (0/0/0), because full sentences never appear
|
| 59 |
+
in OCR text or labels.
|
| 60 |
+
- Cosine of true pairs: 5th percentile 0.101, median 0.150. Wrong pairs: 99th percentile 0.087,
|
| 61 |
+
99.9th 0.135. pix's 0.09 floor keeps 97.5% of true pairs and drops 99.1% of wrong ones.
|
| 62 |
+
- Speed (Apple silicon, GPU): 12 ms per image, 4 ms per sentence, 2 s cold model load. Scoring a
|
| 63 |
+
query against 21,000 stored float16 vectors takes about 25–35 ms.
|
| 64 |
+
|
| 65 |
+
## Use
|
| 66 |
+
|
| 67 |
+
```sh
|
| 68 |
+
pix index ~/Pictures # fetches this model on first use
|
| 69 |
+
pix "a red car at the beach at sunset"
|
| 70 |
+
```
|
| 71 |
+
|
| 72 |
+
To install it by hand, run `hf download arraypress/pix-siglip2-base-224 --local-dir models` and then
|
| 73 |
+
`pix model install models`. Requires macOS 27 (Core AI) on Apple silicon. Converted with coreai-torch
|
| 74 |
+
0.4.3, coreai-core 1.0.0b3, torch 2.9.0 and transformers 5.12.1.
|
| 75 |
+
|
| 76 |
+
## Licence
|
| 77 |
+
|
| 78 |
+
Apache-2.0, the same as the original. The weights and tokenizer are Google's; see `NOTICE` for what
|
| 79 |
+
was changed.
|
export_siglip2.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# /// script
|
| 2 |
+
# requires-python = ">=3.11,<3.13"
|
| 3 |
+
# dependencies = ["torch==2.9.0", "numpy", "pillow", "coreai-torch==0.4.3", "coreai-core==1.0.0b3", "transformers==5.12.1"]
|
| 4 |
+
# ///
|
| 5 |
+
"""SigLIP 2 base/16 at 224 (Google, Apache-2.0) → pix-siglip2-base-224.aimodel, its tokenizer and fixtures.
|
| 6 |
+
|
| 7 |
+
uv run Tools/export_siglip2.py [--out Tools/exports] [--dtype float16]
|
| 8 |
+
|
| 9 |
+
Two entry points in one asset:
|
| 10 |
+
image: pixel_values [1,3,224,224] float32, already resized and normalised ((x/255 - 0.5)/0.5) → embedding [1,768]
|
| 11 |
+
text: ids [1,64] int32 (lower-cased, Gemma BPE, <eos>=1 appended, right-padded with <pad>=0) → embedding [1,768]
|
| 12 |
+
Both are the model's pooled outputs (what get_image_features / get_text_features return); the host
|
| 13 |
+
normalises to unit length. The tokenizer is written as two flat binary files the Swift side reads
|
| 14 |
+
without a JSON parse: the vocabulary and the merges as id triples in rank order.
|
| 15 |
+
Fixtures: texts with their ids and normalised embeddings, and two synthetic images with theirs, so
|
| 16 |
+
the Swift tokenizer, preprocessing and asset are each checked against transformers."""
|
| 17 |
+
import argparse, json, shutil, struct, time
|
| 18 |
+
from pathlib import Path
|
| 19 |
+
import numpy as np, torch, torch.nn as nn
|
| 20 |
+
from PIL import Image
|
| 21 |
+
from transformers import AutoModel, AutoProcessor, AutoTokenizer
|
| 22 |
+
import coreai_torch
|
| 23 |
+
from coreai.runtime import AIModelAssetMetadata
|
| 24 |
+
|
| 25 |
+
HERE = Path(__file__).resolve().parent
|
| 26 |
+
ap = argparse.ArgumentParser()
|
| 27 |
+
ap.add_argument("--name", default="google/siglip2-base-patch16-224")
|
| 28 |
+
ap.add_argument("--revision", default="75de2d55ec2d0b4efc50b3e9ad70dba96a7b2fa2")
|
| 29 |
+
ap.add_argument("--asset", default="pix-siglip2-base-224.aimodel")
|
| 30 |
+
ap.add_argument("--dtype", default="float16", choices=["float16", "float32"])
|
| 31 |
+
ap.add_argument("--out", default=str(HERE / "exports"))
|
| 32 |
+
ap.add_argument("--fixtures", default=str(HERE.parent / "Tests/PhotoIndexTests/Fixtures/siglip"))
|
| 33 |
+
args = ap.parse_args()
|
| 34 |
+
LENGTH, SIZE = 64, 224
|
| 35 |
+
|
| 36 |
+
model = AutoModel.from_pretrained(args.name, revision=args.revision, dtype=torch.float32).eval()
|
| 37 |
+
proc = AutoProcessor.from_pretrained(args.name, revision=args.revision)
|
| 38 |
+
tok = AutoTokenizer.from_pretrained(args.name, revision=args.revision)
|
| 39 |
+
|
| 40 |
+
class Image_(nn.Module):
|
| 41 |
+
def __init__(self, m): super().__init__(); self.v = m.vision_model
|
| 42 |
+
def forward(self, pixel_values): return self.v(pixel_values=pixel_values).pooler_output
|
| 43 |
+
class Text_(nn.Module):
|
| 44 |
+
def __init__(self, m): super().__init__(); self.t = m.text_model
|
| 45 |
+
def forward(self, ids): return self.t(input_ids=ids).pooler_output
|
| 46 |
+
image_net, text_net = Image_(model).eval(), Text_(model).eval()
|
| 47 |
+
|
| 48 |
+
def ids_for(texts):
|
| 49 |
+
return tok([t.lower() for t in texts], padding="max_length", max_length=LENGTH, truncation=True, return_tensors="pt")["input_ids"].to(torch.int32)
|
| 50 |
+
|
| 51 |
+
out = Path(args.out); out.mkdir(parents=True, exist_ok=True)
|
| 52 |
+
support = out / "pix-siglip2-support"; support.mkdir(exist_ok=True)
|
| 53 |
+
fx = Path(args.fixtures); fx.mkdir(parents=True, exist_ok=True)
|
| 54 |
+
|
| 55 |
+
# Tokenizer → vocab.bin (u32 count, then u32 length + UTF-8 bytes per id) and merges.bin (u32 count, then u32 left, right, merged per rank).
|
| 56 |
+
tj = json.loads(tok.backend_tokenizer.to_str())
|
| 57 |
+
vocab = tj["model"]["vocab"]; inv = sorted(vocab.items(), key=lambda kv: kv[1])
|
| 58 |
+
assert [i for _, i in inv] == list(range(len(inv)))
|
| 59 |
+
with open(support / "vocab.bin", "wb") as f:
|
| 60 |
+
f.write(struct.pack("<I", len(inv)))
|
| 61 |
+
for s, _ in inv:
|
| 62 |
+
b = s.encode("utf-8"); f.write(struct.pack("<I", len(b))); f.write(b)
|
| 63 |
+
merges = tj["model"]["merges"]
|
| 64 |
+
with open(support / "merges.bin", "wb") as f:
|
| 65 |
+
f.write(struct.pack("<I", len(merges)))
|
| 66 |
+
for m in merges:
|
| 67 |
+
a, b = (m if isinstance(m, list) else m.split(" ", 1))
|
| 68 |
+
f.write(struct.pack("<III", vocab[a], vocab[b], vocab[a + b]))
|
| 69 |
+
config = {"model": args.name, "revision": args.revision, "dimension": model.config.vision_config.hidden_size,
|
| 70 |
+
"image_size": SIZE, "mean": 0.5, "std": 0.5, "text_length": LENGTH, "pad": tok.pad_token_id, "eos": tok.eos_token_id,
|
| 71 |
+
"unk": tok.unk_token_id, "lowercase": True, "space": "▁", "entry_points": {"image": "pixel_values", "text": "ids"}}
|
| 72 |
+
json.dump(config, open(support / "config.json", "w"), indent=1, ensure_ascii=False)
|
| 73 |
+
shutil.copy(support / "config.json", fx / "config.json")
|
| 74 |
+
print(f"tokenizer: {len(inv)} tokens, {len(merges)} merges", flush=True)
|
| 75 |
+
|
| 76 |
+
# Fixtures.
|
| 77 |
+
texts = ["a dog running on the beach", "The red car at the beach at sunset", "ERROR 517 EXCLUSIVITY VIOLATION",
|
| 78 |
+
" leading and trailing spaces ", "two spaces\tand a tab\nand a newline", "café résumé naïve — punctuation!!",
|
| 79 |
+
"emoji 🎹🎛️ and CJK 漢字 and Arabic عربى", "x", "1234567890 numbers 3.14",
|
| 80 |
+
" ".join(f"word{i}" for i in range(80))]
|
| 81 |
+
ids = ids_for(texts)
|
| 82 |
+
with torch.no_grad():
|
| 83 |
+
temb = nn.functional.normalize(text_net(ids.long()), dim=-1)
|
| 84 |
+
ref = nn.functional.normalize(model.get_text_features(input_ids=ids.long()).pooler_output if hasattr(model.get_text_features(input_ids=ids.long()), "pooler_output") else model.get_text_features(input_ids=ids.long()), dim=-1)
|
| 85 |
+
print(f"text wrapper vs get_text_features max |diff| {(temb - ref).abs().max():.2e}", flush=True)
|
| 86 |
+
temb.numpy().astype(np.float32).tofile(fx / "text_embeddings.f32")
|
| 87 |
+
json.dump({"texts": texts, "ids": ids.tolist()}, open(fx / "text_fixture.json", "w"), ensure_ascii=False)
|
| 88 |
+
|
| 89 |
+
ys, xs = np.mgrid[0:SIZE, 0:SIZE]
|
| 90 |
+
square = np.stack([xs * 255 // (SIZE - 1), ys * 255 // (SIZE - 1), (255 - xs * 255 // (SIZE - 1))], -1).astype(np.uint8)
|
| 91 |
+
Image.fromarray(square).save(fx / "gradient-224.png")
|
| 92 |
+
yw, xw = np.mgrid[0:300, 0:400]
|
| 93 |
+
wide = np.stack([((xw // 40 + yw // 40) % 2) * 200 + 30, xw * 255 // 399, yw * 255 // 299], -1).astype(np.uint8)
|
| 94 |
+
Image.fromarray(wide).save(fx / "checker-400x300.png")
|
| 95 |
+
with torch.no_grad():
|
| 96 |
+
embs = []
|
| 97 |
+
for name in ["gradient-224.png", "checker-400x300.png"]:
|
| 98 |
+
px = proc(images=Image.open(fx / name).convert("RGB"), return_tensors="pt")["pixel_values"]
|
| 99 |
+
embs.append(nn.functional.normalize(image_net(px), dim=-1)[0])
|
| 100 |
+
torch.stack(embs).numpy().astype(np.float32).tofile(fx / "image_embeddings.f32")
|
| 101 |
+
px = proc(images=Image.open(fx / "gradient-224.png").convert("RGB"), return_tensors="pt")["pixel_values"]
|
| 102 |
+
manual = torch.from_numpy(((square.astype(np.float32) / 255 - 0.5) / 0.5).transpose(2, 0, 1)[None])
|
| 103 |
+
print(f"processor vs manual normalise at 224 (no resize): max |diff| {(px - manual).abs().max():.2e}", flush=True)
|
| 104 |
+
|
| 105 |
+
# Export.
|
| 106 |
+
dtype = torch.float16 if args.dtype == "float16" else torch.float32
|
| 107 |
+
t0 = time.time()
|
| 108 |
+
conv = coreai_torch.TorchConverter(mode=coreai_torch.TorchConverter.Mode.RELEASE)
|
| 109 |
+
inet, tnet = Image_(model.to(dtype)).eval(), Text_(model.to(dtype)).eval()
|
| 110 |
+
class Cast(nn.Module):
|
| 111 |
+
def __init__(self, inner, castin): super().__init__(); self.inner, self.castin = inner, castin
|
| 112 |
+
def forward(self, x): return self.inner(x.to(dtype) if self.castin else x).to(torch.float32)
|
| 113 |
+
with torch.no_grad():
|
| 114 |
+
ex = torch.export.export(Cast(inet, True), (torch.zeros(1, 3, SIZE, SIZE),)).run_decompositions(coreai_torch.get_decomp_table())
|
| 115 |
+
conv.add_exported_program(ex, input_names=["pixel_values"], output_names=["embedding"], entrypoint_name="image")
|
| 116 |
+
ex = torch.export.export(Cast(tnet, False), (torch.ones(1, LENGTH, dtype=torch.int32),)).run_decompositions(coreai_torch.get_decomp_table())
|
| 117 |
+
conv.add_exported_program(ex, input_names=["ids"], output_names=["embedding"], entrypoint_name="text")
|
| 118 |
+
program = conv.to_coreai()
|
| 119 |
+
meta = AIModelAssetMetadata()
|
| 120 |
+
meta.author = "Google — SigLIP 2 base/16 224; Core AI export by swift-photo-index"
|
| 121 |
+
meta.license = "Apache-2.0"
|
| 122 |
+
meta.model_description = (f"SigLIP 2 ({args.name}@{args.revision[:7]}), {args.dtype}. Entry 'image': pixel_values [1,3,224,224] float32 "
|
| 123 |
+
"((x/255-0.5)/0.5, RGB, resized to 224x224) → embedding [1,768]. Entry 'text': ids [1,64] int32 (lower-cased Gemma BPE, "
|
| 124 |
+
"<eos>=1 appended, right-padded with 0) → embedding [1,768]. Normalise both in the host; cosine ranks.")
|
| 125 |
+
meta.creation_date = int(time.time())
|
| 126 |
+
asset = out / args.asset
|
| 127 |
+
if asset.exists(): shutil.rmtree(asset)
|
| 128 |
+
program.save_asset(asset, meta)
|
| 129 |
+
print(f"saved {asset} ({sum(f.stat().st_size for f in asset.rglob('*'))/1e6:.0f} MB) in {time.time()-t0:.0f} s", flush=True)
|
pix-siglip2-base-224.aimodel/main.hash
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
|N+E��/�oi���>��,n�w
|
pix-siglip2-base-224.aimodel/main.mlirb
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7c4e2b0c45e6c42fcf1e1f6f69c10bc2c13ec4ec17102c6e11e9770df09a9b35
|
| 3 |
+
size 750505678
|
pix-siglip2-base-224.aimodel/metadata.json
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"creationDate" : "20261013T183249Z",
|
| 3 |
+
"author" : "Google — SigLIP 2 base\/16 224; Core AI export by swift-photo-index",
|
| 4 |
+
"license" : "Apache-2.0",
|
| 5 |
+
"assetVersion" : "2.0",
|
| 6 |
+
"description" : "SigLIP 2 (google\/siglip2-base-patch16-224@75de2d5), float16. Entry 'image': pixel_values [1,3,224,224] float32 ((x\/255-0.5)\/0.5, RGB, resized to 224x224) → embedding [1,768]. Entry 'text': ids [1,64] int32 (lower-cased Gemma BPE, <eos>=1 appended, right-padded with 0) → embedding [1,768]. Normalise both in the host; cosine ranks.",
|
| 7 |
+
"producer" : "coreai-core 1.0.0b3"
|
| 8 |
+
}
|
pix-siglip2-support/config.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"model": "google/siglip2-base-patch16-224",
|
| 3 |
+
"revision": "75de2d55ec2d0b4efc50b3e9ad70dba96a7b2fa2",
|
| 4 |
+
"dimension": 768,
|
| 5 |
+
"image_size": 224,
|
| 6 |
+
"mean": 0.5,
|
| 7 |
+
"std": 0.5,
|
| 8 |
+
"text_length": 64,
|
| 9 |
+
"pad": 0,
|
| 10 |
+
"eos": 1,
|
| 11 |
+
"unk": 3,
|
| 12 |
+
"lowercase": true,
|
| 13 |
+
"space": "▁",
|
| 14 |
+
"entry_points": {
|
| 15 |
+
"image": "pixel_values",
|
| 16 |
+
"text": "ids"
|
| 17 |
+
}
|
| 18 |
+
}
|
pix-siglip2-support/merges.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:219b2dbc1e645054ef8adacb4ed96a70d27b7b38e24fa848818c424bdf477f69
|
| 3 |
+
size 6967252
|
pix-siglip2-support/vocab.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:22aa6308f573f129d84205a72132a5f369247b7e4cd6bcd51dc4115d2be83a86
|
| 3 |
+
size 2955906
|