Google Colab is a cloud-based Jupyter notebook environment that allows you to write, run, and share Python code through your browser. It's like Google Docs, but for code.
With the free version of Google Colab you get an Nvidia Tesla T4 GPU with about 16GPU VRAM, which is pretty good for what we're doing.
Recommendation: Use NSDT Designer to quickly build programmable 3D scenes.
1. Visit Google Colab
To start using Google Colab and take advantage of GPU acceleration, follow these steps:
- Visit Colab and log in with your Google account.
- Click File > New notebook to create a new notebook.
- Click Runtime > Change runtime type to change the runtime to use GPU. In the Hardware accelerator drop-down list, select GPU, and click Save.
Now we are ready to use Google Colab with GPU enabled.
2. Install Metaseg
First, install the Metaseg library by running the following command in a new code cell:
!pip install metaseg
The !pip command is used in Google Colab to install Python packages that are not pre-installed in the environment. We installed the Metaseg library in the Colab environment by running !pip install metaseg.
Next, upload the image to your Google Colab environment using the file browser on the left. In this example we will use an image called smudge.png
To display uploaded images, run the following code:
from IPython.display import Image
Image("smudge.png")
Now, import the SegAutoMaskPredictor class from the metaseg library:
from metaseg import SegAutoMaskPredictor
Then, create an instance of the SegAutoMaskPredictor class and use it to segment the image, the code is as follows:
results = SegAutoMaskPredictor().image_predict(
source="smudge.png",
model_type="vit_l", # vit_l, vit_h, vit_b
points_per_side=16,
points_per_batch=64,
min_area=0,
output_path="output.png",
show=False,
save=True,
)
Note:
1. I set show=False because it doesn't work in Google Colab. This will immediately display the output image. In our case, we will display it by itself.
- Make sure to set save=True if you want to save the image.
Finally, display the segmented image with the mask by running:
from IPython.display import Image
Image("output.jpg")
That's it! You have successfully segmented images using Facebook's SAM and the Metaseg library in Google Colab.
Feel free to explore further and experiment with different images and settings.
Original Link: SAM Segmentation Large Model Concise Tutorial—BimANt