Code Monkey home page Code Monkey logo

fastsam's Introduction

Fast Segment Anything

[📕Paper] [🤗HuggingFace Demo] [Colab demo] [Replicate demo & API] [OpenXLab Demo] [Model Zoo] [BibTeX] [Video Demo]

FastSAM Speed

The Fast Segment Anything Model(FastSAM) is a CNN Segment Anything Model trained using only 2% of the SA-1B dataset published by SAM authors. FastSAM achieves comparable performance with the SAM method at 50× higher run-time speed.

FastSAM design

🍇 Updates

Installation

Clone the repository locally:

git clone https://github.com/CASIA-IVA-Lab/FastSAM.git

Create the conda env. The code requires python>=3.7, as well as pytorch>=1.7 and torchvision>=0.8. Please follow the instructions here to install both PyTorch and TorchVision dependencies. Installing both PyTorch and TorchVision with CUDA support is strongly recommended.

conda create -n FastSAM python=3.9
conda activate FastSAM

Install the packages:

cd FastSAM
pip install -r requirements.txt

Install CLIP:

pip install git+https://github.com/openai/CLIP.git

Getting Started

First download a model checkpoint.

Then, you can run the scripts to try the everything mode and three prompt modes.

# Everything mode
python Inference.py --model_path ./weights/FastSAM.pt --img_path ./images/dogs.jpg
# Text prompt
python Inference.py --model_path ./weights/FastSAM.pt --img_path ./images/dogs.jpg  --text_prompt "the yellow dog"
# Box prompt (xywh)
python Inference.py --model_path ./weights/FastSAM.pt --img_path ./images/dogs.jpg --box_prompt "[[570,200,230,400]]"
# Points prompt
python Inference.py --model_path ./weights/FastSAM.pt --img_path ./images/dogs.jpg  --point_prompt "[[520,360],[620,300]]" --point_label "[1,0]"

You can use the following code to generate all masks, make mask selection based on prompts, and visualize the results.

from fastsam import FastSAM, FastSAMPrompt

model = FastSAM('./weights/FastSAM.pt')
IMAGE_PATH = './images/dogs.jpg'
DEVICE = 'cpu'
everything_results = model(IMAGE_PATH, device=DEVICE, retina_masks=True, imgsz=1024, conf=0.4, iou=0.9,)
prompt_process = FastSAMPrompt(IMAGE_PATH, everything_results, device=DEVICE)

# everything prompt
ann = prompt_process.everything_prompt()

# bbox default shape [0,0,0,0] -> [x1,y1,x2,y2]
ann = prompt_process.box_prompt(bboxes=[[200, 200, 300, 300]])

# text prompt
ann = prompt_process.text_prompt(text='a photo of a dog')

# point prompt
# points default [[0,0]] [[x1,y1],[x2,y2]]
# point_label default [0] [1,0] 0:background, 1:foreground
ann = prompt_process.point_prompt(points=[[620, 360]], pointlabel=[1])

prompt_process.plot(annotations=ann,output_path='./output/dog.jpg',)

You are also welcomed to try our Colab demo: FastSAM_example.ipynb.

Different Inference Options

We provide various options for different purposes, details are in MORE_USAGES.md.

Training or Validation

Training from scratch or validation: Training and Validation Code.

Web demo

Gradio demo

  • We also provide a UI for testing our method that is built with gradio. You can upload a custom image, select the mode and set the parameters, click the segment button, and get a satisfactory segmentation result. Currently, the UI supports interaction with the 'Everything mode' and 'points mode'. We plan to add support for additional modes in the future. Running the following command in a terminal will launch the demo:
# Download the pre-trained model in "./weights/FastSAM.pt"
python app_gradio.py

HF_Everyhting HF_Points

Replicate demo

  • Replicate demo has supported all modes, you can experience points/box/text mode.

Replicate-1 Replicate-2 Replicate-3

Model Checkpoints

Two model versions of the model are available with different sizes. Click the links below to download the checkpoint for the corresponding model type.

Results

All result were tested on a single NVIDIA GeForce RTX 3090.

1. Inference time

Running Speed under Different Point Prompt Numbers(ms).

method params 1 10 100 E(16x16) E(32x32*) E(64x64)
SAM-H 0.6G 446 464 627 852 2099 6972
SAM-B 136M 110 125 230 432 1383 5417
FastSAM 68M 40 40 40 40 40 40

2. Memory usage

Dataset Method GPU Memory (MB)
COCO 2017 FastSAM 2608
COCO 2017 SAM-H 7060
COCO 2017 SAM-B 4670

3. Zero-shot Transfer Experiments

Edge Detection

Test on the BSDB500 dataset.

method year ODS OIS AP R50
HED 2015 .788 .808 .840 .923
SAM 2023 .768 .786 .794 .928
FastSAM 2023 .750 .790 .793 .903

Object Proposals

COCO
method AR10 AR100 AR1000 AUC
SAM-H E64 15.5 45.6 67.7 32.1
SAM-H E32 18.5 49.5 62.5 33.7
SAM-B E32 11.4 39.6 59.1 27.3
FastSAM 15.7 47.3 63.7 32.2
LVIS

bbox AR@1000

method all small med. large
ViTDet-H 65.0 53.2 83.3 91.2
zero-shot transfer methods
SAM-H E64 52.1 36.6 75.1 88.2
SAM-H E32 50.3 33.1 76.2 89.8
SAM-B E32 45.0 29.3 68.7 80.6
FastSAM 57.1 44.3 77.1 85.3

Instance Segmentation On COCO 2017

method AP APS APM APL
ViTDet-H .510 .320 .543 .689
SAM .465 .308 .510 .617
FastSAM .379 .239 .434 .500

4. Performance Visualization

Several segmentation results:

Natural Images

Natural Images

Text to Mask

Text to Mask

5.Downstream tasks

The results of several downstream tasks to show the effectiveness.

Anomaly Detection

Anomaly Detection

Salient Object Detection

Salient Object Detection

Building Extracting

Building Detection

License

The model is licensed under the Apache 2.0 license.

Acknowledgement

Contributors

Our project wouldn't be possible without the contributions of these amazing people! Thank you all for making this project better.

Citing FastSAM

If you find this project useful for your research, please consider citing the following BibTeX entry.

@misc{zhao2023fast,
      title={Fast Segment Anything},
      author={Xu Zhao and Wenchao Ding and Yongqi An and Yinglong Du and Tao Yu and Min Li and Ming Tang and Jinqiao Wang},
      year={2023},
      eprint={2306.12156},
      archivePrefix={arXiv},
      primaryClass={cs.CV}
}

Star History Chart

fastsam's People

Contributors

an-yongqi avatar arjo129 avatar artyaltanzaya avatar bala-az avatar berry-ding avatar capjamesg avatar chenxwh avatar christopher5106 avatar colinator avatar gaoxinge avatar glenn-jocher avatar htwootwo avatar ivaopenlab avatar jobinsjohn avatar laclouis5 avatar msanov avatar nianjiuhuiyi avatar quantumcode-martin avatar riyanswat avatar tianjinren avatar uahic avatar vietanhdev avatar yinglongdu avatar zhangningboo avatar zhaochunhai avatar zxdeepdiver avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fastsam's Issues

strategy for training data selection?

Hi

Thanks for your work. Could you please provide the details of how the 2% of the training data was selected? Did you use any specific strategy or just random selection ?

Thanks

Fine Tuning Code

Hey,
Huge thanks for such a great repo that you have made, just wanted to know if it can be fine tuned for specific purposes, it would be great if you could provide code to fine tune the model on COCO dataset.

Minor error

Hello,

I am running the cat example given in the image folder. On MAC-M1, I get the following error:

img_array = np.frombuffer(buf, dtype=np.uint8).reshape(rows, cols, 3)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: cannot reshape array of size 24883200 into shape (1920,1080,3)

can someone help?

Mask Label

Since FastSAM uses Yolo detector, is it possible to get the mask labels?

CoreML export with variable Input Size

Hi,
Can someone help in understanding how to give variable input image sizes to FastSAM model exported in onnx format, currently it takes only (1024,1024) size image which is leading to mismatch in desired outcome

Interface compatibility

Nice work!

I'd like to suggest that you create a compatible interface to SAM itself, i.e. a drop-in replacement to SamAutomaticMaskGenerator which would make it easier for people to start using this.

Training code

Hi author,
Will you release the code so we can have a try?

RuntimeError: expected scalar type long int but found float

I get following error when I run the model

File /root/FastSAM/fastsam/utils.py:17, in adjust_bboxes_to_image_border(boxes, image_shape, threshold)
     14 h, w = image_shape
     16 # Adjust boxes
---> 17 boxes[:, 0] = torch.where(boxes[:, 0] < threshold, 0, boxes[:, 0])  # x1
     18 boxes[:, 1] = torch.where(boxes[:, 1] < threshold, 0, boxes[:, 1])  # y1
     19 boxes[:, 2] = torch.where(boxes[:, 2] > w - threshold, w, boxes[:, 2])  # x2

RuntimeError: expected scalar type long int but found float

Errro:

Hello,

I am trying the mode for the first time. I get the following message on Apple M1. Can someone help?
File "/Users/Projects/FastSAM/gitsrc/utils/tools.py", line 179, in fast_process
img_array = np.fromstring(buf, dtype=np.uint8).reshape(rows, cols, 3)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: cannot reshape array of size 7756992 into shape (603,1072,3)

License question

Thanks for you interesting work.

I have license question: How can FastSAM model be released under Apache license when the SA-1B training dataset is licensed under research only license + ultralytics yolo codebase under AGPL?

Is Yolact anchor based or anchor free ?

Thanks for the great engineering application research !

I see the Yolo-seg series models completely borrow the idea of coefficientized masks from Yolact.

In your paper 3.2 section saying about yolov8: The updated Head module embraces a decoupled structure, separating classification and detection heads, and shifts from Anchor-Based to Anchor-Free.
And I'm wondering, does the threshold crop step in Yolact makes the yolo-seg architecture back to an Anchor-Based method?

Can I use my own YOLOv8 model weights with FastSAM ?

I have tried to use a trained YOLOv8 models weights with FastSAM but it is raising an error about mismatching. I know you guys working on a fine-tune/train code right now but I was just curious about whether replacing the weights possible or not ?

Thanks for a great work!

Output from onnx format

Hi, output from onnx format is of shape ([(1, 37, 21504), (1, 32, 256, 256)]. If I post process them using below method
where conf = 0.4, iou -> 0,=.9, and agnostic_nms = False like in the FastSAM .pt model but it doesn't return masks fo same length.

Can someone explain the outputs from onnx format FastSAM model and how to postprocess them .
def postprocess(preds, conf, iou, agnostic_nms=False):
"""TODO: filter by classes."""

p = ops.non_max_suppression(preds[0],
                            conf,
                            iou,
                            agnostic_nms,
                            max_det=100,
                            nc=1)



results = []
proto = preds[1]  # second output is len 3 if pt, but only 1 if exported
for i, pred in enumerate(p):

    pred[:, :4] = ops.scale_boxes(torch.Size([1024,1024]), pred[:, :4],(1024,1024))
    masks = ops.process_mask_native(proto[i], pred[:, 6:], pred[:, :4],(1024,1024))  # HWC
    return masks

Suggestion - Integrate MobileSAM into the pipeline for lightweight and faster inference

Reference: https://github.com/ChaoningZhang/MobileSAM

Our project performs on par with the original SAM and keeps exactly the same pipeline as the original SAM except for a change on the image encode, therefore, it is easy to Integrate into any project.

MobileSAM is around 60 times smaller and around 50 times faster than original SAM, and it is around 7 times smaller and around 5 times faster than the concurrent FastSAM. The comparison of the whole pipeline is summarzed as follows:

image

image

Best Wishes,
Qiao

Why is FastSAM worse than MobileSAM with points as the prompt?

We have just released MobileSAM project https://github.com/ChaoningZhang/MobileSAM. We found that FastSAM seems to perform much worse than MobileSAM with points as the prompt, especially when the foreground point and background point as set close. Can you give thoughts on what might be the reason? Thank you for your help in advance.

Training Cost

Hi! Thanks for the great repo! I really like it.
Could you please provide more details about training costs, such as GPUs / training time? Besides, why are only 2% of images from SA-1B used for training? Is there some reason for the setting?

Output Image - Colab

Why does the example script used in the Colab notebook save the final image in a downsized version? When I use my own custom photo, the resulting image dimensions are considerably smaller.

cog是啥库?

向作者致敬,predict文件中import cog,这个cog是什么?也是pip安装的嘛

Output Image

Hi there,

I passed a 19201080 image into the model for segmenation, but the output image doesn't stay in the same resolution. The image with masks is scaled down into around 1328741 (a rough number) in equal proportions.

Any measures to keep the ourput image same size as the original image?

Thanks

about the train code

Thanks for your work, does the training code mean that I can use my labeled segmented data containing one class to train a model that segments only that one class?

感谢您的工作,请问训练代码是指可以使用我包含一个类的已标注分割数据训练一个只分割这一个类的模型吗?

Convert FastSAM to onnx and coreml format

Hi,

Has someone convert FastSAM to the onnx or coreml format?
Since, FastSam is based on YOLOV8 model and takes img.path as input, how to get an image trace for this and convert to core ml ormat?
Also, how to convert it to coreml format such that output can be of variable size ?

FigureCanvasTkAgg

Hello,

python Inference.py --model_path FastSAM-x.pt --img_path images/dogs.jpg

I got the following error when I ran it.

AttributeError: 'FigureCanvasTkAgg' object has no attribute 'renderer'

If anyone encounters this problem, they can add the following line to tools.py (line 171).

buf = fig.canvas.draw()
buf = fig.canvas.tostring_rgb()

Spliting model into Encoder and Decoder

Hello! I really like this project.
Do you plan to support splitting this model into Encoder and Decoder like the original SAM?
In that way, the Decoder part can be run very fast, and we can apply it to some applications like AnyLabeling.
I'd love to help integrate into AnyLabeling if we can find a way to split the model.
Thank you very much!

Read multiple images at once

Can I read all the pictures in the folder at one time and use only one text prompt? I tried to do this, but an opencv error occurred. Is there a mistake in my operation or other reasons, because this operation is more realistic significance

Multiple objects text prompt

Is it possible with the text prompt that it detects multiple objects ? (e.g., "people" will find all people in the image)

Install Error

I'm getting this error when I'm trying to run pip.

Installing collected packages: seaborn, ultralytics
ERROR: Could not install packages due to an OSError: [WinError 2] The system cannot find the file specified: 'C:\Python311\Scripts\ultralytics.exe' -> 'C:\Python311\Scripts\ultralytics.exe.deleteme'

TypeError: torch._VariableFunctionsClass.meshgrid() got multiple values for keyword argument 'indexing'

Run the example script.
python Inference.py --model_path FastSAM-x.pt --img_path .\examples\dogs.jpg

File "d:\ProgramData\Anaconda3\envs\fastsam\lib\site-packages\torch\functional.py", line 504, in _meshgrid
return _VF.meshgrid(tensors, **kwargs, indexing ='ij') # type: ignore[attr-defined]
TypeError: torch._VariableFunctionsClass.meshgrid() got multiple values for keyword argument 'indexing'
Any one know how to solve it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.