Skip to main content

Unofficial MobileSAMv2 and MobileSAM software package for lightweight Segment Anything and everything inference.

Project description

MobileSAM_lite

An unofficial Python package for MobileSAM and MobileSAMv2 runtime that adds support for lighter encoder models not available in the original implementation.

This package vendors the runtime code needed for inference:

  • mobilesamv2
  • tinyvit
  • efficientvit
  • ultralytics under mobilesam_lite/_vendor/ultralytics

It intentionally does not bundle model checkpoints. Download weights separately and pass the checkpoint path at runtime.

The optional mobilesamv2.promt_mobilesamv2 module now resolves its Ultralytics dependency from the vendored package in mobilesam_lite._vendor.ultralytics.

Install locally

pip install -e .

Install with pypi

pip install mobilesam-lite

Example

import torch

from mobilesam_lite.mobile_sam import SamPredictor, sam_model_registry

model = sam_model_registry["vit_t"]("./weight/mobile_sam.pt")
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)
model.eval()

predictor = SamPredictor(model)

Verify an installed wheel

After installing the wheel into a clean environment, run:

python example_inference_mobilesam.py --checkpoint /path/to/mobile_sam.pt

You can also provide a real image:

python example_inference_mobilesam.py --checkpoint /path/to/mobile_sam.pt --image /path/to/image.jpg

The script prints the installed distribution version, the imported package path, and the output tensor shapes from one prediction call.

For the MobileSAMv2 decoder path, use:

python example_inference_mobilesamv2.py \
  --checkpoint /path/to/mobile_sam.pt \
  --prompt-decoder-checkpoint /path/to/Prompt_guided_Mask_Decoder.pt \
  --object-aware-model-checkpoint /path/to/ObjectAwareModel.pt \
  --image /path/to/image.jpg \
  --output-dir wheel_verify_mobilesamv2_output

This script runs the MobileSAMv2 seg-every pipeline with ObjectAwareModel box proposals plus the prompt-guided decoder.

Inputs:

  • --checkpoint: image encoder checkpoint
  • --prompt-decoder-checkpoint: Prompt_guided_Mask_Decoder.pt
  • --object-aware-model-checkpoint: ObjectAwareModel.pt
  • --image: optional input image path. If omitted, the script uses a synthetic test image.
  • --output-dir: directory for generated visualizations
  • Optional tuning args: --encoder-type, --imgsz, --iou, --conf, --retina, --decoder-batch-size, --min-box-area-ratio, --max-box-area-ratio

Outputs:

  • Console summary with device, input image shape, detected box count, filtered box count, mask tensor shape, and saved output path
  • boxes.png: detected boxes after filtering
  • mask_union.png: binary union of all predicted masks
  • mask_union_overlay.png: union mask blended over the input image
  • mask_overlay.png: per-mask color overlay for the seg-every result

Example assets for the MobileSAMv2 seg-every flow:

Input image:

MobileSAMv2 seg-every input

Output overlay:

MobileSAMv2 seg-every output

Reference: Official MobileSAM repository

https://github.com/chaoningzhang/mobilesam

If you find this repo useful to you please consider click the button below to donate and support my work! Buy Me A Coffee

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mobilesam_lite-0.1.2.tar.gz (608.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mobilesam_lite-0.1.2-py3-none-any.whl (728.6 kB view details)

Uploaded Python 3

File details

Details for the file mobilesam_lite-0.1.2.tar.gz.

File metadata

  • Download URL: mobilesam_lite-0.1.2.tar.gz
  • Upload date:
  • Size: 608.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for mobilesam_lite-0.1.2.tar.gz
Algorithm Hash digest
SHA256 769b7e09f94f991e271345d4917a18e0b19150522ef82f4b060ed0d3da0e9dfd
MD5 8e155bcd0b9a67fc4b13c25497deeb96
BLAKE2b-256 ab54f42fa6be986fe9b2e0979b1bec6327243837b9f0011bb5a8956e484d4088

See more details on using hashes here.

File details

Details for the file mobilesam_lite-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: mobilesam_lite-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 728.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for mobilesam_lite-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1cffa3c11ee24b9ccf26a13bd946570ed4146a96e98da2edc855aa03aac60198
MD5 da356ac2d4050e448f101c92b9d151de
BLAKE2b-256 e971ea86ad08abf144a56313252fb2b86cfbc1da51293e04ad636766e78d92b8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page