Instructions to use moonbox-dev/MobileSAM-MLX with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- MLX
How to use moonbox-dev/MobileSAM-MLX with MLX:
# Download the model from the Hub pip install huggingface_hub[hf_xet] hf download moonbox-dev/MobileSAM-MLX --local-dir MobileSAM-MLX
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- LM Studio
- Atomic Chat
Model card and the original license
Browse files
LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Apache License
|
| 2 |
+
Version 2.0, January 2004
|
| 3 |
+
http://www.apache.org/licenses/
|
| 4 |
+
|
| 5 |
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
| 6 |
+
|
| 7 |
+
1. Definitions.
|
| 8 |
+
|
| 9 |
+
"License" shall mean the terms and conditions for use, reproduction,
|
| 10 |
+
and distribution as defined by Sections 1 through 9 of this document.
|
| 11 |
+
|
| 12 |
+
"Licensor" shall mean the copyright owner or entity authorized by
|
| 13 |
+
the copyright owner that is granting the License.
|
| 14 |
+
|
| 15 |
+
"Legal Entity" shall mean the union of the acting entity and all
|
| 16 |
+
other entities that control, are controlled by, or are under common
|
| 17 |
+
control with that entity. For the purposes of this definition,
|
| 18 |
+
"control" means (i) the power, direct or indirect, to cause the
|
| 19 |
+
direction or management of such entity, whether by contract or
|
| 20 |
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
| 21 |
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
| 22 |
+
|
| 23 |
+
"You" (or "Your") shall mean an individual or Legal Entity
|
| 24 |
+
exercising permissions granted by this License.
|
| 25 |
+
|
| 26 |
+
"Source" form shall mean the preferred form for making modifications,
|
| 27 |
+
including but not limited to software source code, documentation
|
| 28 |
+
source, and configuration files.
|
| 29 |
+
|
| 30 |
+
"Object" form shall mean any form resulting from mechanical
|
| 31 |
+
transformation or translation of a Source form, including but
|
| 32 |
+
not limited to compiled object code, generated documentation,
|
| 33 |
+
and conversions to other media types.
|
| 34 |
+
|
| 35 |
+
"Work" shall mean the work of authorship, whether in Source or
|
| 36 |
+
Object form, made available under the License, as indicated by a
|
| 37 |
+
copyright notice that is included in or attached to the work
|
| 38 |
+
(an example is provided in the Appendix below).
|
| 39 |
+
|
| 40 |
+
"Derivative Works" shall mean any work, whether in Source or Object
|
| 41 |
+
form, that is based on (or derived from) the Work and for which the
|
| 42 |
+
editorial revisions, annotations, elaborations, or other modifications
|
| 43 |
+
represent, as a whole, an original work of authorship. For the purposes
|
| 44 |
+
of this License, Derivative Works shall not include works that remain
|
| 45 |
+
separable from, or merely link (or bind by name) to the interfaces of,
|
| 46 |
+
the Work and Derivative Works thereof.
|
| 47 |
+
|
| 48 |
+
"Contribution" shall mean any work of authorship, including
|
| 49 |
+
the original version of the Work and any modifications or additions
|
| 50 |
+
to that Work or Derivative Works thereof, that is intentionally
|
| 51 |
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
| 52 |
+
or by an individual or Legal Entity authorized to submit on behalf of
|
| 53 |
+
the copyright owner. For the purposes of this definition, "submitted"
|
| 54 |
+
means any form of electronic, verbal, or written communication sent
|
| 55 |
+
to the Licensor or its representatives, including but not limited to
|
| 56 |
+
communication on electronic mailing lists, source code control systems,
|
| 57 |
+
and issue tracking systems that are managed by, or on behalf of, the
|
| 58 |
+
Licensor for the purpose of discussing and improving the Work, but
|
| 59 |
+
excluding communication that is conspicuously marked or otherwise
|
| 60 |
+
designated in writing by the copyright owner as "Not a Contribution."
|
| 61 |
+
|
| 62 |
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
| 63 |
+
on behalf of whom a Contribution has been received by Licensor and
|
| 64 |
+
subsequently incorporated within the Work.
|
| 65 |
+
|
| 66 |
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
| 67 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 68 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 69 |
+
copyright license to reproduce, prepare Derivative Works of,
|
| 70 |
+
publicly display, publicly perform, sublicense, and distribute the
|
| 71 |
+
Work and such Derivative Works in Source or Object form.
|
| 72 |
+
|
| 73 |
+
3. Grant of Patent License. Subject to the terms and conditions of
|
| 74 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 75 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 76 |
+
(except as stated in this section) patent license to make, have made,
|
| 77 |
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
| 78 |
+
where such license applies only to those patent claims licensable
|
| 79 |
+
by such Contributor that are necessarily infringed by their
|
| 80 |
+
Contribution(s) alone or by combination of their Contribution(s)
|
| 81 |
+
with the Work to which such Contribution(s) was submitted. If You
|
| 82 |
+
institute patent litigation against any entity (including a
|
| 83 |
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
| 84 |
+
or a Contribution incorporated within the Work constitutes direct
|
| 85 |
+
or contributory patent infringement, then any patent licenses
|
| 86 |
+
granted to You under this License for that Work shall terminate
|
| 87 |
+
as of the date such litigation is filed.
|
| 88 |
+
|
| 89 |
+
4. Redistribution. You may reproduce and distribute copies of the
|
| 90 |
+
Work or Derivative Works thereof in any medium, with or without
|
| 91 |
+
modifications, and in Source or Object form, provided that You
|
| 92 |
+
meet the following conditions:
|
| 93 |
+
|
| 94 |
+
(a) You must give any other recipients of the Work or
|
| 95 |
+
Derivative Works a copy of this License; and
|
| 96 |
+
|
| 97 |
+
(b) You must cause any modified files to carry prominent notices
|
| 98 |
+
stating that You changed the files; and
|
| 99 |
+
|
| 100 |
+
(c) You must retain, in the Source form of any Derivative Works
|
| 101 |
+
that You distribute, all copyright, patent, trademark, and
|
| 102 |
+
attribution notices from the Source form of the Work,
|
| 103 |
+
excluding those notices that do not pertain to any part of
|
| 104 |
+
the Derivative Works; and
|
| 105 |
+
|
| 106 |
+
(d) If the Work includes a "NOTICE" text file as part of its
|
| 107 |
+
distribution, then any Derivative Works that You distribute must
|
| 108 |
+
include a readable copy of the attribution notices contained
|
| 109 |
+
within such NOTICE file, excluding those notices that do not
|
| 110 |
+
pertain to any part of the Derivative Works, in at least one
|
| 111 |
+
of the following places: within a NOTICE text file distributed
|
| 112 |
+
as part of the Derivative Works; within the Source form or
|
| 113 |
+
documentation, if provided along with the Derivative Works; or,
|
| 114 |
+
within a display generated by the Derivative Works, if and
|
| 115 |
+
wherever such third-party notices normally appear. The contents
|
| 116 |
+
of the NOTICE file are for informational purposes only and
|
| 117 |
+
do not modify the License. You may add Your own attribution
|
| 118 |
+
notices within Derivative Works that You distribute, alongside
|
| 119 |
+
or as an addendum to the NOTICE text from the Work, provided
|
| 120 |
+
that such additional attribution notices cannot be construed
|
| 121 |
+
as modifying the License.
|
| 122 |
+
|
| 123 |
+
You may add Your own copyright statement to Your modifications and
|
| 124 |
+
may provide additional or different license terms and conditions
|
| 125 |
+
for use, reproduction, or distribution of Your modifications, or
|
| 126 |
+
for any such Derivative Works as a whole, provided Your use,
|
| 127 |
+
reproduction, and distribution of the Work otherwise complies with
|
| 128 |
+
the conditions stated in this License.
|
| 129 |
+
|
| 130 |
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
| 131 |
+
any Contribution intentionally submitted for inclusion in the Work
|
| 132 |
+
by You to the Licensor shall be under the terms and conditions of
|
| 133 |
+
this License, without any additional terms or conditions.
|
| 134 |
+
Notwithstanding the above, nothing herein shall supersede or modify
|
| 135 |
+
the terms of any separate license agreement you may have executed
|
| 136 |
+
with Licensor regarding such Contributions.
|
| 137 |
+
|
| 138 |
+
6. Trademarks. This License does not grant permission to use the trade
|
| 139 |
+
names, trademarks, service marks, or product names of the Licensor,
|
| 140 |
+
except as required for reasonable and customary use in describing the
|
| 141 |
+
origin of the Work and reproducing the content of the NOTICE file.
|
| 142 |
+
|
| 143 |
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
| 144 |
+
agreed to in writing, Licensor provides the Work (and each
|
| 145 |
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
| 146 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 147 |
+
implied, including, without limitation, any warranties or conditions
|
| 148 |
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
| 149 |
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
| 150 |
+
appropriateness of using or redistributing the Work and assume any
|
| 151 |
+
risks associated with Your exercise of permissions under this License.
|
| 152 |
+
|
| 153 |
+
8. Limitation of Liability. In no event and under no legal theory,
|
| 154 |
+
whether in tort (including negligence), contract, or otherwise,
|
| 155 |
+
unless required by applicable law (such as deliberate and grossly
|
| 156 |
+
negligent acts) or agreed to in writing, shall any Contributor be
|
| 157 |
+
liable to You for damages, including any direct, indirect, special,
|
| 158 |
+
incidental, or consequential damages of any character arising as a
|
| 159 |
+
result of this License or out of the use or inability to use the
|
| 160 |
+
Work (including but not limited to damages for loss of goodwill,
|
| 161 |
+
work stoppage, computer failure or malfunction, or any and all
|
| 162 |
+
other commercial damages or losses), even if such Contributor
|
| 163 |
+
has been advised of the possibility of such damages.
|
| 164 |
+
|
| 165 |
+
9. Accepting Warranty or Additional Liability. While redistributing
|
| 166 |
+
the Work or Derivative Works thereof, You may choose to offer,
|
| 167 |
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
| 168 |
+
or other liability obligations and/or rights consistent with this
|
| 169 |
+
License. However, in accepting such obligations, You may act only
|
| 170 |
+
on Your own behalf and on Your sole responsibility, not on behalf
|
| 171 |
+
of any other Contributor, and only if You agree to indemnify,
|
| 172 |
+
defend, and hold each Contributor harmless for any liability
|
| 173 |
+
incurred by, or claims asserted against, such Contributor by reason
|
| 174 |
+
of your accepting any such warranty or additional liability.
|
| 175 |
+
|
| 176 |
+
END OF TERMS AND CONDITIONS
|
| 177 |
+
|
| 178 |
+
APPENDIX: How to apply the Apache License to your work.
|
| 179 |
+
|
| 180 |
+
To apply the Apache License to your work, attach the following
|
| 181 |
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
| 182 |
+
replaced with your own identifying information. (Don't include
|
| 183 |
+
the brackets!) The text should be enclosed in the appropriate
|
| 184 |
+
comment syntax for the file format. We also recommend that a
|
| 185 |
+
file or class name and description of purpose be included on the
|
| 186 |
+
same "printed page" as the copyright notice for easier
|
| 187 |
+
identification within third-party archives.
|
| 188 |
+
|
| 189 |
+
Copyright [yyyy] [name of copyright owner]
|
| 190 |
+
|
| 191 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 192 |
+
you may not use this file except in compliance with the License.
|
| 193 |
+
You may obtain a copy of the License at
|
| 194 |
+
|
| 195 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 196 |
+
|
| 197 |
+
Unless required by applicable law or agreed to in writing, software
|
| 198 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 199 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 200 |
+
See the License for the specific language governing permissions and
|
| 201 |
+
limitations under the License.
|
README.md
ADDED
|
@@ -0,0 +1,295 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
pipeline_tag: mask-generation
|
| 4 |
+
library_name: mlx
|
| 5 |
+
tags:
|
| 6 |
+
- mlx
|
| 7 |
+
- moonbox
|
| 8 |
+
- mobilesam
|
| 9 |
+
- segment-anything
|
| 10 |
+
- sam
|
| 11 |
+
- image-segmentation
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
# MobileSAM v2 MLX
|
| 15 |
+
|
| 16 |
+
MLX builds of [MobileSAM v2](https://github.com/ChaoningZhang/MobileSAM), made by Moonbox for Apple Silicon Macs. Each precision is a folder; keep a folder's
|
| 17 |
+
files together. Only the parts listed below are converted: the rest of the pipeline comes from the original
|
| 18 |
+
repository unless the section below says otherwise.
|
| 19 |
+
|
| 20 |
+
## Files
|
| 21 |
+
|
| 22 |
+
The last column is how far each build is from the original PyTorch model on the same inputs (see the section below
|
| 23 |
+
for what was measured).
|
| 24 |
+
|
| 25 |
+
| Precision | File | Size | sha256 | Checked against the original |
|
| 26 |
+
|---|---|---|---|---|
|
| 27 |
+
| fp16 | [`fp16/config.json`](https://huggingface.co/moonbox-ai/MobileSAM-MLX/blob/main/fp16/config.json) | 0.3 kB | `d6876abfe8f9` | mask IoU 0.999 mean / 0.988 worst |
|
| 28 |
+
| fp16 | [`fp16/mlx_model.py`](https://huggingface.co/moonbox-ai/MobileSAM-MLX/blob/main/fp16/mlx_model.py) | 20.7 kB | `57f29a58bfa3` | mask IoU 0.999 mean / 0.988 worst |
|
| 29 |
+
| fp16 | [`fp16/model.safetensors`](https://huggingface.co/moonbox-ai/MobileSAM-MLX/blob/main/fp16/model.safetensors) | 19.6 MB | `e8a52d0c013f` | mask IoU 0.999 mean / 0.988 worst |
|
| 30 |
+
|
| 31 |
+
## How to run
|
| 32 |
+
|
| 33 |
+
MobileSAM (TinyViT-5M image encoder + SAM's prompt encoder and mask decoder, `weights/mobile_sam.pt`) ported to
|
| 34 |
+
MLX: the point / box segmentation half of MobileSAM v2. Each folder holds `model.safetensors`, `config.json` and
|
| 35 |
+
`mlx_model.py`, the port (only `mlx`, `numpy` and `pillow` needed), with SamPredictor's interface:
|
| 36 |
+
|
| 37 |
+
```python
|
| 38 |
+
import numpy as np, mlx_model
|
| 39 |
+
from PIL import Image
|
| 40 |
+
model = mlx_model.load("fp16")
|
| 41 |
+
rgb = np.array(Image.open("in.jpg").convert("RGB"))
|
| 42 |
+
emb = mlx_model.embed(model, rgb) # once per image
|
| 43 |
+
masks, scores, low_res, logits = mlx_model.predict(model, emb, points=[[320, 240]], labels=[1], multimask=True)
|
| 44 |
+
masks, scores, _, _ = mlx_model.predict(model, emb, box=[100, 80, 400, 380], multimask=False)
|
| 45 |
+
```
|
| 46 |
+
|
| 47 |
+
**Not included:** MobileSAM v2's object-aware prompt sampler (a YOLOv8 from ultralytics, AGPL-3.0, weights on Google
|
| 48 |
+
Drive only) and its prompt-guided decoder (shipped as a pickled PyTorch module). For "segment everything", feed
|
| 49 |
+
boxes from any detector to `predict`.
|
| 50 |
+
|
| 51 |
+
**How they were checked.** Against the original in PyTorch fp32 on 8 photos, one point prompt (3 masks) and one box
|
| 52 |
+
prompt each: IoU of each of the 32 masks with PyTorch's. Pass: IoU >= 0.95 on every mask.
|
| 53 |
+
|
| 54 |
+
| Precision | Size | Mask IoU mean / worst | Status |
|
| 55 |
+
|---|---|---|---|
|
| 56 |
+
| `fp16/` | 19.7 MB | 0.999 / 0.988 | passes |
|
| 57 |
+
| `bf16/` | 19.7 MB | 0.996 / 0.972 | passes |
|
| 58 |
+
| `8bit/` | 12.7 MB | 0.989 / 0.899 | **below threshold, experimental** |
|
| 59 |
+
| `6bit/` | 10.9 MB | 0.989 / 0.914 | **below threshold, experimental** |
|
| 60 |
+
| `4bit/` | 9.0 MB | 0.922 / 0.012 | **below threshold, experimental**: some prompts give a wrong mask |
|
| 61 |
+
|
| 62 |
+
Use `fp16/`: the model is 10 M parameters, so quantizing saves 7-11 MB and costs accuracy. About 65 ms per image
|
| 63 |
+
(encoder + two prompts) on an Apple M5.
|
| 64 |
+
|
| 65 |
+
## How these were made
|
| 66 |
+
|
| 67 |
+
From [MobileSAM v2](https://github.com/ChaoningZhang/MobileSAM) at revision `f706ad9c4e`: the original weights loaded in MLX and stored as bf16 or fp16, or
|
| 68 |
+
quantized with MLX's affine quantization (`8bit`, `6bit`, `4bit`, group size 64; layers MLX can't quantize, such as
|
| 69 |
+
convolutions and norms, stay bf16). Every build was checked against the original before upload; the `sha256` column
|
| 70 |
+
above is what the files must hash to.
|
| 71 |
+
|
| 72 |
+
## License
|
| 73 |
+
|
| 74 |
+
apache-2.0. These files are a derivative of the original model and carry its license: it is copied in this
|
| 75 |
+
repository, and it governs your use of these files. The original model card follows unchanged.
|
| 76 |
+
|
| 77 |
+
---
|
| 78 |
+
|
| 79 |
+
# Original model card
|
| 80 |
+
|
| 81 |
+
<p float="center">
|
| 82 |
+
<img src="assets/logo2.png?raw=true" width="99.1%" />
|
| 83 |
+
</p>
|
| 84 |
+
|
| 85 |
+
# Faster Segment Anything (MobileSAM) and Everything (MobileSAMv2)
|
| 86 |
+
:pushpin: MobileSAMv2, available at [ResearchGate](https://www.researchgate.net/publication/376579294_MobileSAMv2_Faster_Segment_Anything_to_Everything) and [arXiv](https://arxiv.org/abs/2312.09579.pdf), replaces the grid-search prompt sampling in SAM with object-aware prompt sampling for faster **segment everything(SegEvery)**.
|
| 87 |
+
|
| 88 |
+
:pushpin: MobileSAM, available at [ResearchGate](https://www.researchgate.net/publication/371851844_Faster_Segment_Anything_Towards_Lightweight_SAM_for_Mobile_Applications) and [arXiv](https://arxiv.org/pdf/2306.14289.pdf), replaces the heavyweight image encoder in SAM with a lightweight image encoder for faster **segment anything(SegAny)**.
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
**Support for ONNX model export**. Feel free to test it on your devices and share your results with us.
|
| 92 |
+
|
| 93 |
+
**A demo of MobileSAM** running on **CPU** is open at [hugging face demo](https://huggingface.co/spaces/dhkim2810/MobileSAM). On our own Mac i5 CPU, it takes around 3s. On the hugging face demo, the interface and inferior CPUs make it slower but still works fine. Stayed tuned for a new version with more features! You can also run a demo of MobileSAM on [your local PC](https://github.com/ChaoningZhang/MobileSAM/tree/master/app).
|
| 94 |
+
|
| 95 |
+
:grapes: Media coverage and Projects that adapt from SAM to MobileSAM (Thank you all!)
|
| 96 |
+
* **2023/07/03**: [joliGEN](https://github.com/jolibrain/joliGEN) supports MobileSAM for faster and lightweight mask refinement for image inpainting with Diffusion and GAN.
|
| 97 |
+
* **2023/07/03**: [MobileSAM-in-the-Browser](https://github.com/akbartus/MobileSAM-in-the-Browser) shows a demo of running MobileSAM on the browser of your local PC or Mobile phone.
|
| 98 |
+
* **2023/07/02**: [Inpaint-Anything](https://github.com/qiaoyu1002/Inpaint-Anything) supports MobileSAM for faster and lightweight Inpaint Anything
|
| 99 |
+
* **2023/07/02**: [Personalize-SAM](https://github.com/qiaoyu1002/Personalize-SAM) supports MobileSAM for faster and lightweight Personalize Segment Anything with 1 Shot
|
| 100 |
+
* **2023/07/01**: [MobileSAM-in-the-Browser](https://github.com/akbartus/MobileSAM-in-the-Browser) makes an example implementation of MobileSAM in the browser.
|
| 101 |
+
* **2023/06/30**: [SegmentAnythingin3D](https://github.com/Jumpat/SegmentAnythingin3D) supports MobileSAM to segment anything in 3D efficiently.
|
| 102 |
+
* **2023/06/30**: MobileSAM has been featured by [AK](https://twitter.com/_akhaliq?lang=en) for the second time, see the link [AK's MobileSAM tweet](https://twitter.com/_akhaliq/status/1674410573075718145). Welcome to retweet.
|
| 103 |
+
* **2023/06/29**: [AnyLabeling](https://github.com/vietanhdev/anylabeling) supports MobileSAM for auto-labeling.
|
| 104 |
+
* **2023/06/29**: [SonarSAM](https://github.com/wangsssky/SonarSAM) supports MobileSAM for Image encoder full-finetuing.
|
| 105 |
+
* **2023/06/29**: [Stable Diffusion WebUIv](https://github.com/continue-revolution/sd-webui-segment-anything) supports MobileSAM.
|
| 106 |
+
|
| 107 |
+
* **2023/06/28**: [Grounding-SAM](https://github.com/IDEA-Research/Grounded-Segment-Anything) supports MobileSAM with [Grounded-MobileSAM](https://github.com/IDEA-Research/Grounded-Segment-Anything/tree/main/EfficientSAM).
|
| 108 |
+
|
| 109 |
+
* **2023/06/27**: MobileSAM has been featured by [AK](https://twitter.com/_akhaliq?lang=en), see the link [AK's MobileSAM tweet](https://twitter.com/_akhaliq/status/1673585099097636864). Welcome to retweet.
|
| 110 |
+

|
| 111 |
+
|
| 112 |
+
:star: **How is MobileSAM trained?** MobileSAM is trained on a single GPU with 100k datasets (1% of the original images) for less than a day. The training code will be available soon.
|
| 113 |
+
|
| 114 |
+
:star: **How to Adapt from SAM to MobileSAM?** Since MobileSAM keeps exactly the same pipeline as the original SAM, we inherit pre-processing, post-processing, and all other interfaces from the original SAM. Therefore, by assuming everything is exactly the same except for a smaller image encoder, those who use the original SAM for their projects can **adapt to MobileSAM with almost zero effort**.
|
| 115 |
+
|
| 116 |
+
:star: **MobileSAM performs on par with the original SAM (at least visually)** and keeps exactly the same pipeline as the original SAM except for a change on the image encoder. Specifically, we replace the original heavyweight ViT-H encoder (632M) with a much smaller Tiny-ViT (5M). On a single GPU, MobileSAM runs around 12ms per image: 8ms on the image encoder and 4ms on the mask decoder.
|
| 117 |
+
|
| 118 |
+
* The comparison of ViT-based image encoder is summarzed as follows:
|
| 119 |
+
|
| 120 |
+
Image Encoder | Original SAM | MobileSAM
|
| 121 |
+
:-----------------------------------------:|:---------|:-----:
|
| 122 |
+
Parameters | 611M | 5M
|
| 123 |
+
Speed | 452ms | 8ms
|
| 124 |
+
|
| 125 |
+
* Original SAM and MobileSAM have exactly the same prompt-guided mask decoder:
|
| 126 |
+
|
| 127 |
+
Mask Decoder | Original SAM | MobileSAM
|
| 128 |
+
:-----------------------------------------:|:---------|:-----:
|
| 129 |
+
Parameters | 3.876M | 3.876M
|
| 130 |
+
Speed | 4ms | 4ms
|
| 131 |
+
|
| 132 |
+
* The comparison of the whole pipeline is summarized as follows:
|
| 133 |
+
|
| 134 |
+
Whole Pipeline (Enc+Dec) | Original SAM | MobileSAM
|
| 135 |
+
:-----------------------------------------:|:---------|:-----:
|
| 136 |
+
Parameters | 615M | 9.66M
|
| 137 |
+
Speed | 456ms | 12ms
|
| 138 |
+
|
| 139 |
+
:star: **Original SAM and MobileSAM with a point as the prompt.**
|
| 140 |
+
|
| 141 |
+
<p float="left">
|
| 142 |
+
<img src="assets/mask_point.jpg?raw=true" width="99.1%" />
|
| 143 |
+
</p>
|
| 144 |
+
|
| 145 |
+
:star: **Original SAM and MobileSAM with a box as the prompt.**
|
| 146 |
+
<p float="left">
|
| 147 |
+
<img src="assets/mask_box.jpg?raw=true" width="99.1%" />
|
| 148 |
+
</p>
|
| 149 |
+
|
| 150 |
+
:muscle: **Is MobileSAM faster and smaller than FastSAM? Yes!**
|
| 151 |
+
MobileSAM is around 7 times smaller and around 5 times faster than the concurrent FastSAM.
|
| 152 |
+
The comparison of the whole pipeline is summarzed as follows:
|
| 153 |
+
Whole Pipeline (Enc+Dec) | FastSAM | MobileSAM
|
| 154 |
+
:-----------------------------------------:|:---------|:-----:
|
| 155 |
+
Parameters | 68M | 9.66M
|
| 156 |
+
Speed | 64ms |12ms
|
| 157 |
+
|
| 158 |
+
:muscle: **Does MobileSAM aign better with the original SAM than FastSAM? Yes!**
|
| 159 |
+
FastSAM is suggested to work with multiple points, thus we compare the mIoU with two prompt points (with different pixel distances) and show the resutls as follows. Higher mIoU indicates higher alignment.
|
| 160 |
+
mIoU | FastSAM | MobileSAM
|
| 161 |
+
:-----------------------------------------:|:---------|:-----:
|
| 162 |
+
100 | 0.27 | 0.73
|
| 163 |
+
200 | 0.33 |0.71
|
| 164 |
+
300 | 0.37 |0.74
|
| 165 |
+
400 | 0.41 |0.73
|
| 166 |
+
500 | 0.41 |0.73
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
## Installation
|
| 176 |
+
|
| 177 |
+
The code requires `python>=3.8`, as well as `pytorch>=1.7` and `torchvision>=0.8`. Please follow the instructions [here](https://pytorch.org/get-started/locally/) to install both PyTorch and TorchVision dependencies. Installing both PyTorch and TorchVision with CUDA support is strongly recommended.
|
| 178 |
+
|
| 179 |
+
Install Mobile Segment Anything:
|
| 180 |
+
|
| 181 |
+
```
|
| 182 |
+
pip install git+https://github.com/ChaoningZhang/MobileSAM.git
|
| 183 |
+
```
|
| 184 |
+
|
| 185 |
+
or clone the repository locally and install with
|
| 186 |
+
|
| 187 |
+
```
|
| 188 |
+
git clone git@github.com:ChaoningZhang/MobileSAM.git
|
| 189 |
+
cd MobileSAM; pip install -e .
|
| 190 |
+
```
|
| 191 |
+
|
| 192 |
+
## Demo
|
| 193 |
+
|
| 194 |
+
Once installed MobileSAM, you can run demo on your local PC or check out our [HuggingFace Demo](https://huggingface.co/spaces/dhkim2810/MobileSAM).
|
| 195 |
+
|
| 196 |
+
It requires latest version of [gradio](https://gradio.app).
|
| 197 |
+
|
| 198 |
+
```
|
| 199 |
+
cd app
|
| 200 |
+
python app.py
|
| 201 |
+
```
|
| 202 |
+
|
| 203 |
+
## <a name="GettingStarted"></a>Getting Started
|
| 204 |
+
The MobileSAM can be loaded in the following ways:
|
| 205 |
+
|
| 206 |
+
```
|
| 207 |
+
from mobile_sam import sam_model_registry, SamAutomaticMaskGenerator, SamPredictor
|
| 208 |
+
|
| 209 |
+
model_type = "vit_t"
|
| 210 |
+
sam_checkpoint = "./weights/mobile_sam.pt"
|
| 211 |
+
|
| 212 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 213 |
+
|
| 214 |
+
mobile_sam = sam_model_registry[model_type](checkpoint=sam_checkpoint)
|
| 215 |
+
mobile_sam.to(device=device)
|
| 216 |
+
mobile_sam.eval()
|
| 217 |
+
|
| 218 |
+
predictor = SamPredictor(mobile_sam)
|
| 219 |
+
predictor.set_image(<your_image>)
|
| 220 |
+
masks, _, _ = predictor.predict(<input_prompts>)
|
| 221 |
+
```
|
| 222 |
+
|
| 223 |
+
or generate masks for an entire image:
|
| 224 |
+
|
| 225 |
+
```
|
| 226 |
+
from mobile_sam import SamAutomaticMaskGenerator
|
| 227 |
+
|
| 228 |
+
mask_generator = SamAutomaticMaskGenerator(mobile_sam)
|
| 229 |
+
masks = mask_generator.generate(<your_image>)
|
| 230 |
+
```
|
| 231 |
+
## <a name="GettingStarted"></a>Getting Started (MobileSAMv2)
|
| 232 |
+
Download the model weights from the [checkpoints](https://drive.google.com/file/d/1dE-YAG-1mFCBmao2rHDp0n-PP4eH7SjE/view?usp=sharing).
|
| 233 |
+
|
| 234 |
+
After downloading the model weights, faster SegEvery with MobileSAMv2 can be simply used as follows:
|
| 235 |
+
```
|
| 236 |
+
cd MobileSAMv2
|
| 237 |
+
bash ./experiments/mobilesamv2.sh
|
| 238 |
+
```
|
| 239 |
+
## ONNX Export
|
| 240 |
+
**MobileSAM** now supports ONNX export. Export the model with
|
| 241 |
+
|
| 242 |
+
```
|
| 243 |
+
python scripts/export_onnx_model.py --checkpoint ./weights/mobile_sam.pt --model-type vit_t --output ./mobile_sam.onnx
|
| 244 |
+
```
|
| 245 |
+
|
| 246 |
+
Also check the [example notebook](https://github.com/ChaoningZhang/MobileSAM/blob/master/notebooks/onnx_model_example.ipynb) to follow detailed steps.
|
| 247 |
+
We recommend to use `onnx==1.12.0` and `onnxruntime==1.13.1` which is tested.
|
| 248 |
+
|
| 249 |
+
|
| 250 |
+
## BibTex of our MobileSAM
|
| 251 |
+
If you use MobileSAM in your research, please use the following BibTeX entry. :mega: Thank you!
|
| 252 |
+
|
| 253 |
+
```bibtex
|
| 254 |
+
@article{mobile_sam,
|
| 255 |
+
title={Faster Segment Anything: Towards Lightweight SAM for Mobile Applications},
|
| 256 |
+
author={Zhang, Chaoning and Han, Dongshen and Qiao, Yu and Kim, Jung Uk and Bae, Sung-Ho and Lee, Seungkyu and Hong, Choong Seon},
|
| 257 |
+
journal={arXiv preprint arXiv:2306.14289},
|
| 258 |
+
year={2023}
|
| 259 |
+
}
|
| 260 |
+
```
|
| 261 |
+
|
| 262 |
+
## Acknowledgement
|
| 263 |
+
|
| 264 |
+
This work was supported by Institute of Information & communications Technology Planning & Evaluation (IITP) grant funded by the Korea government(MSIT) (No.RS-2022-00155911, Artificial Intelligence Convergence Innovation Human Resources Development (Kyung Hee University)),(No.2019-0-01287, Evolvable Deep Learning Model Generation Platform for Edge Computing)
|
| 265 |
+
|
| 266 |
+
<details>
|
| 267 |
+
<summary>
|
| 268 |
+
<a href="https://github.com/facebookresearch/segment-anything">SAM</a> (Segment Anything) [<b>bib</b>]
|
| 269 |
+
</summary>
|
| 270 |
+
|
| 271 |
+
```bibtex
|
| 272 |
+
@article{kirillov2023segany,
|
| 273 |
+
title={Segment Anything},
|
| 274 |
+
author={Kirillov, Alexander and Mintun, Eric and Ravi, Nikhila and Mao, Hanzi and Rolland, Chloe and Gustafson, Laura and Xiao, Tete and Whitehead, Spencer and Berg, Alexander C. and Lo, Wan-Yen and Doll{\'a}r, Piotr and Girshick, Ross},
|
| 275 |
+
journal={arXiv:2304.02643},
|
| 276 |
+
year={2023}
|
| 277 |
+
}
|
| 278 |
+
```
|
| 279 |
+
</details>
|
| 280 |
+
|
| 281 |
+
|
| 282 |
+
|
| 283 |
+
<details>
|
| 284 |
+
<summary>
|
| 285 |
+
<a href="https://github.com/microsoft/Cream/tree/main/TinyViT">TinyViT</a> (TinyViT: Fast Pretraining Distillation for Small Vision Transformers) [<b>bib</b>]
|
| 286 |
+
</summary>
|
| 287 |
+
|
| 288 |
+
```bibtex
|
| 289 |
+
@InProceedings{tiny_vit,
|
| 290 |
+
title={TinyViT: Fast Pretraining Distillation for Small Vision Transformers},
|
| 291 |
+
author={Wu, Kan and Zhang, Jinnian and Peng, Houwen and Liu, Mengchen and Xiao, Bin and Fu, Jianlong and Yuan, Lu},
|
| 292 |
+
booktitle={European conference on computer vision (ECCV)},
|
| 293 |
+
year={2022}
|
| 294 |
+
```
|
| 295 |
+
</details>
|