Running SegmentAnything Locally

Hopefully, Made Easy

Introduction


Welcome to this tutorial on running SegmentAnything locally. I created this post because I was struggling to get it running myself, and I noticed that there were minimal resources available to help with the process. My goal is to provide you with a step-by-step guide that will make it easier for you to set up and run SegmentAnything on your machine. So, let's get started!

SegmentAnything is a promptable segmentation system created by Meta with zero-shot generalization to unfamiliar objects and images, without the need for additional training. You can try out the demo here.

Here's an example:

Frog
Image of frog before segmentation
Segmented Frog
Frog image after segmentation

Now this segmentation is fantastic! Segmentation algorithms have been a central portion of my research for a while, and this algorithm really blows the rest out of the water. Not only can it create this detailed segmentation, but it can also be informed on which objects in the image to segment, or even which parts of the image to leave out of the segmentation. It also has the capability to prompt the algorithm to inform the segmentation! So now let's get to running this on our own computer.

Dependencies


  1. First, check your version of Python (Command: python --version) to make sure it fits with the requirement python>=3.8. You can always download a more recent version here.

  2. If you do not already have openCV and matplotlib downloaded, do so with the command pip install opencv-python matplotlib.

  3. Visit https://pytorch.org/get-started/locally/ to determine the appropriate version of PyTorch for your system and install it. You can check your current version of CUDA with the command nvcc --version

  4. Next, clone the entire SegmentAnything repository from https://github.com/facebookresearch/segment-anything. You can also do the following command in terminal: pip install 'git+https://github.com/facebookresearch/segment-anything.git'

  5. Finally, download the SegmentAnything model checkpoints from https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth. Make sure to download the vit_h version.

Now that we have finished with the dependencies, we can start coding.

Now go off and segment with your newfound skills! You can also download the Jupyter Notebook following from my Github.

Please make sure to check out DigitalSreeni's Youtube video on SAM as it helped me a lot when writing this tutorial. He also has a bunch of other great videos on running different cutting-edge ML algorithms on your local machine. Check out his channel here.

If you found this site useful, (or if you didn't) I'd love to hear feedback. Enjoy.

  Thanks for the visit!