Contents
General information
Segment-anything model (SAM) is developed by
Meta AI Research, FAIR
research team. It can be used to segment individual objects or the whole image using a single mouse click.
Details of the research is available here:
https://segment-anything.com
Implementation of SAM in MIB is done via utilization of an external Python interpreter, please follow the following steps
to configure local python environment to work in MIB.
Important! even though SAM can work on CPU, GPU is highly recommended as it is x30-60 faster.
Reference
-
Alexander Kirillov, Eric Mintun, Nikhila Ravi, Hanzi Mao, Chloe Rolland, Laura Gustafson, Tete Xiao,
Spencer Whitehead, Alexander C. Berg, Wan-Yen Lo, Piotr Dollar, Ross Girshick
Segment Anything
arXiv:2304.02643, https://doi.org/10.48550/arXiv.2304.02643
Requirements
- MATLAB R2022a or newer (tested on R2022a, R2022b, R2023a)
- Python 3.8, 3.9, 3.10; tested on 3.9
- List of Python versions compatible with various MATLAB releases
- CUDA-compatible GPU is highly recommended, CPU can also be used but it is significantly slower
Installation on Linux
SAM can also be used under Linux, for details please check the dedicated page:
https://mib.helsinki.fi/downloads_systemreq_sam_linux.html
Python installation
- Install Miniconda
(tested on python 3.9, version 23.1.0, Miniconda3-py39_23.1.0-1-Windows-x86_64.exe)
https://docs.conda.io/en/latest/miniconda.html
Archive of miniconda releases: https://repo.anaconda.com/miniconda/
If you have admin account, you can install Python for all users, otherwise it is possible to install Python
only for the current user.
- With Admin rights
Example of the installation directory: D:\Python\Miniconda39\
Installation for all users
Miniconda configuration
Optional note!
If installation was done for All Users, change permission of Miniconda's
envs
directory
(
e.g. d:\Python\Miniconda39\envs\
) or the whole Miniconda39 directory to be accessible for all users.
This makes things a bit more organized, otherwise the python environment will be created in
C:\Users\[USERNAME]\.conda\envs\
Set permissions to python directory
Set permissions to python directory
- Without Admin rights
Example of the installation directory: C:\Users\[USERNAME]\AppData\Local\miniconda39\
Installation for the current user
Miniconda installation directory
Miniconda configuration
Install segment-anything model
Install required python packages
- Create a new environment for Python with SAM
- Start "Anaconda Prompt"
Start->Miniconda3->Anaconda prompt (Miniconda39)
- Create a new environment, specify location for the environment and the version of Python:
>> conda create --prefix d:\Python\Miniconda39\envs\sam4mib python=3.9
- Activate the environment:
>> activate sam4mib
- The code requires python>=3.8, as well as pytorch>=1.7 and torchvision>=0.8.
the instructions here to install both PyTorch and TorchVision dependencies:
https://pytorch.org/get-started/locally
The forked distribution has
"requirements.txt"
that can be used to install
required dependencies at once for Windows with CUDA 11.8 supported GPU:
- Make sure the the environment is created and activated (steps above)
- In the terminal/anaconda prompt run the command to install required packages
(make sure that the correct path to requirements.txt is specified):
>> pip3 install -r requirements.txt
or
>> pip3 install -r d:\Python\Examples\segment-anything_230414\requirements.txt
- Proceed to the MIB configuration section below
- using the possible options configure the command to install the packages:
Configure pytorch
- In the command window type the generated command to install pytorch; the tested command:
>> pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
- The following dependencies are necessary for mask post-processing, saving masks in COCO format,
the example notebooks, and exporting the model in ONNX format:
>> pip3 install opencv-python matplotlib onnxruntime onnx
>> pip3 install pycocotools
if there is an error see below
- Install "onnxruntime-gpu" to make prediction on GPU:
>> pip3 install onnxruntime-gpu==1.14.1
- Optionally, install Jupyter notebook:
>> pip install notebook
MIB configuration
Troubleshooting
error: Microsoft Visual C++ 14.0 or greater is required
This error may appear during
>> pip install opencv-python pycocotools matplotlib onnxruntime onnx
Solution 1:
- Try to solve the issue by installing the required package via conda using this command:
>> conda install -c conda-forge pycocotools
Solution 2:
Remove sam4mib environment
If you do not need mib4sam environment, you can follow the following steps to uninstall it from your system.
- Start "Anaconda Prompt"
Start->Miniconda3->Anaconda prompt (Miniconda39)
- List the environments installed on your system by running the command:
>> conda env list
- Remove sam4mib environment:
>> conda remove --name sam4mib --all