8000 GitHub - manojlovska/segment-anything: The repository provides code for running inference with the SegmentAnything Model (SAM), links for downloading the trained model checkpoints, and example notebooks that show how to use the model.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

The repository provides code for running inference with the SegmentAnything Model (SAM), links for downloading the trained model checkpoints, and example notebooks that show how to use the model.

License

Notifications You must be signed in to change notification settings

manojlovska/segment-anything

 
 

Repository files navigation

Analysis of NPL TEM images using SAM

Introduction

This project focuses on determining the diameter distribution of NPLs with optimized magnetic properties colloidally stabilized in a solvent or soft matrix, utilizing the Segment Anything Model. The original README file of the SAM project can be found under README_SAM.md.

Steps to reproduce

Step 1: Clone the repository

git clone git@github.com:manojlovska/segment-anything.git

Step 2: Create virtual environment with conda and activate it

conda create -n env_name python=3.8.5
conda activate env_name

Step 3: Install the sam module

cd segment-anything
pip install git+https://github.com/facebookresearch/segment-anything.git

Step 4: Install pytorch

Install PyTorch 1.7.1 (or later) and torchvision, as well as small additional dependencies. On a CUDA GPU machine, the following will do the trick:

pip install torch==2.2.1 torchvision==0.17.1 torchaudio==2.2.1 --index-url https://download.pytorch.org/whl/cu118

Or if you don't have GPU 9230 s:

pip install torch==2.2.1 torchvision==0.17.1 torchaudio==2.2.1 --index-url https://download.pytorch.org/whl/cpu

Here you can find the pytorch distribution that suits your hardware requirements.

Step 5: Install other requirements

pip install -r requirements.txt

Step 6: Download SAM weights inside a weights/ directory

mkdir weights
cd weights
wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_b_01ec64.pth
cd ..

Step 7: Obtain images' masks

Run the SAM-zero-shot.py script to obtain the masks. An example of the following command:

python SAM-zero-shot.py --img_path "/path/to/Porazdelitev delcev original" --model-type "vit_b" --device "cuda" --convert-to-rle --img-extension "tif" --checkpoint weights/sam_vit_b_01ec64.pth 
  • Notes:
    • change --img_path "/path/to/Porazdelitev delcev original" to the path to your dataset, or an image file you want segmented
    • --convert-to-rle flag must be present to save the masks in coco RLE format
    • the masks will be saved in "./output" by default, add --output "path/to/desired/output/directory" if you want to change this
    • change --device 'cuda' to --device 'cpu' if you do not want to run the program on a GPU

Run

python SAM-zero-shot.py --help

for additional information on how to use the command.

Step 8: Generate histograms out of predicted diameters

If you don't have ground truth diameters distributions, you can plot only the predicted diameter distributions. For example, to generate diameters distribution out of one image, run the following command:

python generate_histograms_main.py --images-path '/path/to/Porazdelitev delcev original/BSHF-DBSA-210325/BSHF-DBSA-210325_0001.tif' --masks-path "./output/BSHF-DBSA-210325/BSHF-DBSA-210325_0001.json" --save-hist --ratio 0.2410

Or if you want to plot the diameter distribution of an experimental analysis consisted of multiple images:

python generate_histograms_main.py --images-path '/path/to/Porazdelitev delcev original/BSHF-DBSA-210325' --masks-path "./output/BSHF-DBSA-210325" --save-hist --ratio 0.2410

Generated histograms and predicted diameters in nm are saved in "./results" directory, you can change this by specifying different --save-path.

Here you must specify the masks path and the ratio for converting the diameters in nm.

Step 9: Compare generated and ground truth histograms

Run

python compare_histograms_main.py --images-path '/path/to/Porazdelitev delcev original' --save-hist

to compare the predicted and ground truth histograms for the whole dataset or

python compare_histograms_main.py --images-path '/path/to/Porazdelitev delcev original/BSHF-DBSA-210325' --save-hist

for a particular experimental analysis.

  • Notes:
    • Run
    python compare_histograms_main.py --help 
    for additional information on how to use this command.
    • If you wish you can choose different size constraints --t_min and --t_max, default are 10 nm and 150 nm
    • Add --filter-edge to filter out the edge particles (Extremely slow on non-GPU machine!)
    • If you chose different path to save the output masks of the SAM model, you have to specify that path here by using --masks-path

Generated histograms and predicted diameters in nm are saved in "./results" directory, you can change this by specifying different --save-path.

Step 10: Visualize the masks

If you want to check if the obtained masks for an image are well generated, run the command:

python visualize_masks.py --image-path '/path/to/Porazdelitev delcev original/BSHF-DBSA-210325/BSHF-DBSA-210325_0001.tif' --save-plot --ratio 0.2410

Change the size constraints by specifying --tmin and --tmax. Specify different --save-path to save the plot in different directory.

For all the scripts you can run the following command for additional information:

python script-name.py --help

About

The repository provides code for running inference with the SegmentAnything Model (SAM), links for downloading the trained model checkpoints, and example notebooks that show how to use the model.

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Jupyter Notebook 98.5%
  • Python 1.5%
0