Skip to content

Deekshith-Dade/mlx_sam3

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

23 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🎯 MLX SAM3

Segment Anything Model 3 β€” Native Apple Silicon Implementation

MLX Python 3.13+ License HuggingFace

A high-performance MLX port of Meta's SAM3 for interactive image segmentation on Mac


Read the Blog


πŸ“– New to SAM3? Check out the accompanying blog post where I explain the SAM3 architecture, how it works, and what makes it special: Understanding SAM3 β†’

✨ Features

  • πŸš€ Native Apple Silicon β€” Optimized for M1/M2/M3/M4 chips using MLX
  • πŸ“ Text Prompts β€” Segment objects by describing them ("car", "person", "dog")
  • πŸ“¦ Box Prompts β€” Draw bounding boxes to include or exclude regions
  • 🎨 Interactive Studio β€” Beautiful web interface for real-time segmentation
  • 🐍 Python API β€” Simple programmatic access for scripting and integration
  • ⬇️ Auto Model Download β€” Weights automatically fetched from HuggingFace

πŸ–ΌοΈ Demo

SAM3 Demo - Car Detection SAM3 Demo - Coat Segmentation
Object detection with "car" prompt Semantic segmentation with "coat" prompt

SAM3 Studio β€” Interactive segmentation with text and box prompts


πŸ“‹ Prerequisites

Requirement Version Notes
macOS 13.0+ Apple Silicon required (M1/M2/M3/M4)
Python 3.13+ Required for MLX compatibility
Node.js 18+ For the web interface
uv Latest Optional but recommended β€” Install uv

⚠️ Apple Silicon Only: This project uses MLX, Apple's machine learning framework optimized exclusively for Apple Silicon.


πŸš€ Quick Start

Option 1: One-Command Launch (Recommended)

If you have uv installed:

# Clone the repository
git clone https://github.com/Deekshith-Dade/mlx-sam3.git
cd mlx-sam3

# Install project dependencies
uv sync

# Launch the app (backend + frontend)
cd app && ./run.sh

The first run will automatically download MLX weights from mlx-community/sam3-image (~3.5GB).

Access the app:

Press Ctrl+C to stop all servers.


Option 2: Manual Setup (Standard pip)

Click to expand manual setup instructions

1. Create Virtual Environment

# Clone the repository
git clone https://github.com/your-username/mlx-sam3.git
cd mlx-sam3

# Create and activate virtual environment
python3 -m venv .venv
source .venv/bin/activate

# Install the package
pip install -e .

2. Start the Backend

cd app/backend
pip install -r requirements.txt
python main.py

The backend will start on http://localhost:8000

3. Start the Frontend (new terminal)

cd app/frontend
npm install
npm run dev

The frontend will start on http://localhost:3000


🐍 Python API

Use SAM3 directly in your Python scripts:

from PIL import Image
from sam3 import build_sam3_image_model
from sam3.model.sam3_image_processor import Sam3Processor

# Load model (auto-downloads MLX weights from mlx-community/sam3-image)
model = build_sam3_image_model()
processor = Sam3Processor(model, confidence_threshold=0.5)

# Load and process an image
image = Image.open("your_image.jpg")
state = processor.set_image(image)

# Segment with text prompt
state = processor.set_text_prompt("person", state)

# Access results
masks = state["masks"]       # Binary segmentation masks
boxes = state["boxes"]       # Bounding boxes [x0, y0, x1, y1]
scores = state["scores"]     # Confidence scores

print(f"Found {len(scores)} objects")

Adding Box Prompts

# Add a box prompt (normalized coordinates: center_x, center_y, width, height)
# label=True for inclusion, label=False for exclusion
state = processor.add_geometric_prompt(
    box=[0.5, 0.5, 0.3, 0.3],  # Center of image, 30% width/height
    label=True,
    state=state
)

Reset and Try New Prompts

# Clear all prompts while keeping the image
processor.reset_all_prompts(state)

# Try a different prompt
state = processor.set_text_prompt("car", state)

πŸ—οΈ Project Structure

mlx-sam3/
β”œβ”€β”€ sam3/                    # Core MLX SAM3 implementation
β”‚   β”œβ”€β”€ model/               # Model components
β”‚   β”‚   β”œβ”€β”€ sam3_image.py    # Main model architecture
β”‚   β”‚   β”œβ”€β”€ vitdet.py        # Vision Transformer backbone
β”‚   β”‚   β”œβ”€β”€ text_encoder_ve.py # Text encoder
β”‚   β”‚   └── ...
β”‚   β”œβ”€β”€ model_builder.py     # Model construction utilities
β”‚   β”œβ”€β”€ convert.py           # Weight conversion from PyTorch
β”‚   └── utils.py             # Helper utilities
β”œβ”€β”€ app/                     # Web application
β”‚   β”œβ”€β”€ backend/             # FastAPI server
β”‚   β”œβ”€β”€ frontend/            # Next.js React app
β”‚   └── run.sh               # One-command launcher
β”œβ”€β”€ assets/                  # Static assets & test images
β”œβ”€β”€ examples/                # Jupyter notebook examples
└── pyproject.toml           # Project configuration

πŸ”Œ API Reference

Endpoint Method Description
/health GET Check if the model is loaded and ready
/upload POST Upload an image and create a session
/segment/text POST Segment using a text prompt
/segment/box POST Add a box prompt (include/exclude)
/reset POST Clear all prompts for a session
/session/{id} DELETE Delete a session and free memory

Example API Call

# Upload an image
curl -X POST "http://localhost:8000/upload" \
  -F "file=@your_image.jpg"

# Response: {"session_id": "abc-123", "width": 1920, "height": 1080, ...}

# Segment with text
curl -X POST "http://localhost:8000/segment/text" \
  -H "Content-Type: application/json" \
  -d '{"session_id": "abc-123", "prompt": "car"}'

πŸ““ Examples

Jupyter notebooks are available in the examples/ directory:

  • sam3_image_predictor_example.ipynb β€” Basic image segmentation
  • sam3_image_interactive.ipynb β€” Interactive prompting workflows

Run them with:

cd examples
jupyter notebook

πŸ› οΈ Tech Stack

Component Technology
ML Framework MLX
Backend FastAPI, Uvicorn
Frontend Next.js 16, React 19, Tailwind CSS 4
Model SAM3 MLX

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the Apache 2.0 License β€” see the LICENSE file for details.


πŸ™ Acknowledgments

  • Meta AI for the original SAM3 model
  • MLX Team at Apple for the incredible ML framework
  • The open-source community for continuous inspiration

Built with ❀️ for Apple Silicon

Report Bug Β· Request Feature

About

MLX SAM3 - MLX Port of SAM3 for interactive image segmentation with Text and Geometric Prompts

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors