ad
ad
Topview AI logo

307 - Segment your images in python without training using Segment Anything Model (SAM)

Science & Technology


Introduction

Welcome back, everyone! In this tutorial, we will explore the Segment Anything Model (SAM), recently released by Meta AI on April 5th. Many of you have expressed interest in a specific tutorial about this model, so I will walk you through the process of importing the pre-trained model they have generously shared and segmenting your own images. I'll run the examples on my local IDE since I have a GPU; however, if you don’t, you can use Google Colab instead. You can also find more code and information on their official website.

Understanding the Segment Anything Model (SAM)

To get started, I recommend visiting the official SAM page. Performing a quick Google search for "Segment Anything" will bring you to a wealth of information about this model and its capabilities. They have a demo page where you can upload images to see how the model detects objects through box drawing or point clicking.

!SAM Demo

The basic concept of SAM involves processing an image through an encoder that converts it into embeddings. You can then provide input in various forms, such as points, boxes, or even text. SAM can identify and segment multiple objects in a single image.

Installation Setup

To install everything locally, I created a new Conda environment for segmentation tasks. Here are the key steps you need to follow:

  1. Check CUDA Version: Before installing PyTorch, ensure you know your CUDA version. For example, I checked that my CUDA version was 11.0.
  2. Install PyTorch: Based on your CUDA version, install an appropriate version of PyTorch. For CUDA 11.0, I used this command:
    pip install torch==1.7.0 torchvision==0.8.2
    
    Please ensure that your Python version is at least 3.8.
  3. Install Additional Libraries: Next, install OpenCV and Matplotlib if they are not already installed:
    pip install opencv-python matplotlib
    
  4. Get SAM: Clone the repository from GitHub or download the ZIP file. You can install it with:
    pip install -e .
    
  5. Download the Pre-trained Model: You must download the default trained model, which is approximately 2.4 gigabytes.

Now you are ready to move on to coding!

Using SAM for Image Segmentation

Code Setup

Begin by importing the necessary libraries. After checking your setup, you can read images using OpenCV and convert the color from BGR to RGB for proper visualization.

import cv2
import torch
from segment_anything import sam_model_registry

## Introduction
image = cv2.imread('path_to_your_image.jpeg')
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

Define and load the model to start segmentation.

device = 'cuda' if torch.cuda.is_available() else 'cpu'
sam = sam_model_registry['default'].to(device)

Generating Masks

You will create a mask generator that will automatically detect objects in your image.

mask_generator = sam.create_auto_mask_generator()
masks = mask_generator.generate(image_rgb)

print(f"Number of masks generated: (len(masks))")

You can adjust the parameters, such as the IOU threshold and stability score, to refine the output.

Visualizing Results

Finally, you can visualize the segmentation results on your image.

import matplotlib.pyplot as plt

## Introduction
def plot_masks(image, masks):
    # Your code to display masks on the image
    pass

plt.imshow(plot_masks(image_rgb, masks))
plt.show()

Example Outputs

I tested the model with various images, including homes and microscopic neuron images, and the results were impressive, effectively segmenting objects accurately while allowing for adjustments in sensitivity.

Conclusion

In conclusion, the Segment Anything Model serves as a powerful tool for image segmentation without the necessity of training your own model. It excels at assisting with annotations and can adapt to various datasets. Stay tuned for more explorations regarding multi-class segmentation and other advanced uses of SAM!


Keywords

Segment Anything Model, SAM, Meta AI, Image Segmentation, Pre-trained Model, Python, Object Detection, Mask Generator, GPU, Annotation Tool.


FAQ

Q1: What is the Segment Anything Model (SAM)?
A1: SAM is an image segmentation model developed by Meta AI that can segment and detect multiple objects in images without additional training.

Q2: How do I install the Segment Anything Model?
A2: You can install SAM by creating a new Conda environment, installing PyTorch, additional libraries like OpenCV and Matplotlib, and downloading the SAM repository and pre-trained model.

Q3: Can I use SAM on my CPU?
A3: Yes, SAM can be used on a CPU, but it will run slower compared to using a GPU.

Q4: What types of inputs can SAM take for segmentation?
A4: SAM can accept inputs in the form of bounding boxes, points, and text to specify what to segment in the image.

Q5: How can I visualize the segmentation results?
A5: You can visualize the segmentation results using libraries like Matplotlib to display the original image with the overlaid segmentation masks.

ad

Share

linkedin icon
twitter icon
facebook icon
email icon
ad