MobileSAM
repository·master·Indexed 27 days ago
https://github.com/chaoningzhang/mobilesamLightweight versions of the Segment Anything Model (SAM) optimized for mobile and edge applications. It includes MobileSAM and MobileSAMv2, which replace the heavy ViT-H encoder with a smaller Tiny-ViT encoder to achieve faster inference. The library supports prompt-guided mask prediction via SamPredictor, automatic mask generation via SamAutomaticMaskGenerator, and ONNX export. MobileSAMv2 integrates with the ultralytics package for object tracking using BoT-SORT and ByteTracker.
What's inside MobileSAM
- MobileSAM is a lightweight version of the Segment Anything Model (SAM). It maintains the same pipeline as the original SAM but replaces the heavyweight ViT-H encoder (632M) with a much smaller Tiny-ViT (5M). This allows it to perform on par with the original SAM visually while being significantly faster, running approximately 12ms per image on a single GPU (8ms for the image encoder and 4ms for the mask decoder).
Get Started with MobileSAMv2
masterMobileSAMv2 provides faster 'segment everything' (SegEvery) capabilities using object-aware prompt sampling.
- Download the model weights from the provided Google Drive link.
- Run the experiment script to start using MobileSAMv2.
cd MobileSAMv2 bash ./experiments/mobilesamv2.shExport MobileSAM to ONNX
masterMobileSAM supports ONNX export. Use the
export_onnx_model.pyscript to convert your checkpoint.Recommended versions for testing:
onnx==1.12.0onnxruntime==1.13.1
python scripts/export_onnx_model.py --checkpoint ./weights/mobile_sam.pt --model-type vit_t --output ./mobile_sam.onnxTrack objects using the Python interface
masterUse the
model.track()method from theultralyticsYOLO model to perform object tracking on video streams or files.Supported trackers include:
botsort.yaml(BoT-SORT)bytetrack.yaml(ByteTracker)
When processing a sequence of frames manually (e.g., in a loop or from a folder of images), you must set
persist=True. This ensures the model maintains object IDs across frames instead of treating each frame as a new tracking session.from ultralytics import YOLO # Initialize model (detection or segmentation) model = YOLO("yolov8n.pt") # Basic tracking usage model.track( source="video/streams", stream=True, tracker="botsort.yaml", # or 'bytetrack.yaml' show=True, ) # Accessing tracked object IDs for result in model.track(source="video.mp4"): if result.boxes.id is not None: print(result.boxes.id.cpu().numpy().astype(int))Install MobileSAM
masterMobileSAM requires
python>=3.8,pytorch>=1.7, andtorchvision>=0.8. It is highly recommended to install PyTorch and TorchVision with CUDA support.You can install MobileSAM directly via pip or by cloning the repository.
pip install git+https://github.com/ChaoningZhang/MobileSAM.git # Or via local clone git clone git@github.com:ChaoningZhang/MobileSAM.git cd MobileSAM; pip install -e .Run MobileSAM Demo locally
masterTo run the MobileSAM demo on your local machine, you must first install the
mobile_sampackage following the official installation instructions. Once installed, you can launch the Gradio application by runningapp.py.python app.pyConfigure tracker parameters
masterTracker parameters can be modified by editing the.yamlconfiguration files located in theultralytics/tracker/cfgdirectory. You can provide a path to a custom modified tracker config file when calling the tracking methods.Run the MobileSAM Demo
masterTo run a local demo using Gradio, navigate to the
appdirectory and executeapp.py. Ensure you have the latest version ofgradioinstalled.cd app python app.pyTrack objects manually in a video loop
masterWhen iterating through video frames using OpenCV, use
model.track(frame, persist=True)to ensure object IDs remain consistent across the sequence.import cv2 from ultralytics import YOLO cap = cv2.VideoCapture("video.mp4") model = YOLO("yolov8n.pt") while True: ret, frame = cap.read() if not ret: break # persist=True is critical for maintaining IDs across frames results = model.track(frame, persist=True) if results[0].boxes.id is not None: boxes = results[0].boxes.xyxy.cpu().numpy().astype(int) ids = results[0].boxes.id.cpu().numpy().astype(int) for box, id in zip(boxes, ids): cv2.rectangle(frame, (box[0], box[1]), (box[2], box[3]), (0, 255, 0), 2) cv2.putText( frame, f"Id {id}", (box[0], box[1]), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, ) cv2.imshow("frame", frame) if cv2.waitKey(1) & 0xFF == ord("q"): breakUse MobileSAM for Prompt-Guided Mask Prediction
masterYou can use
SamPredictorto predict masks based on specific input prompts (like points or boxes).- Load the model using
sam_model_registrywith thevit_tmodel type. - Initialize
SamPredictorwith the loaded model. - Set the image using
.set_image(). - Predict masks using
.predict().
from mobile_sam import sam_model_registry, SamAutomaticMaskGenerator, SamPredictor import torch model_type = "vit_t" sam_checkpoint = "./weights/mobile_sam.pt" device = "cuda" if torch.cuda.is_available() else "cpu" mobile_sam = sam_model_registry[model_type](checkpoint=sam_checkpoint) mobile_sam.to(device=device) mobile_sam.eval() predictor = SamPredictor(mobile_sam) predictor.set_image(<your_image>) masks, _, _ = predictor.predict(<input_prompts>)- Load the model using
Use MobileSAM for Automatic Mask Generation
masterTo generate masks for an entire image automatically, use the
SamAutomaticMaskGeneratorclass.from mobile_sam import sam_model_registry, SamAutomaticMaskGenerator # Assuming mobile_sam is already loaded as shown in the predictor example mask_generator = SamAutomaticMaskGenerator(mobile_sam) masks = mask_generator.generate(<your_image>)Use YOLO models via Python API
masterYou can interact with YOLO models in a Python environment using the
ultralyticspackage. You can build a model from a configuration file (.yaml) to train from scratch, or load a pre-trained checkpoint (.pt) for transfer learning or inference. TheYOLOclass accepts the same arguments as the CLI.from ultralytics import YOLO model = YOLO("model.yaml") # build a YOLOv8n model from scratch # YOLO("model.pt") use pre-trained model if available model.info() # display model information model.train(data="coco128.yaml", epochs=100) # train the model