Skip to content

Latest commit

 

History

History

segment-anything-2

Segment Anything 2

Image mode

Input

Input

(Image from https://github.com/facebookresearch/segment-anything/blob/main/notebooks/images/truck.jpg)

Output

Output

Video mode

Input

Input

(Image from https://github.com/facebookresearch/segment-anything-2/tree/main/notebooks/videos/bedroom)

Output

Output

Usage

Automatically downloads the onnx and prototxt files on the first run. It is necessary to be connected to the Internet while downloading.

For the sample image,

$ python3 segment-anything-2.py

For the sample video,

$ python3 segment-anything-2.py -v demo

For the webcam,

$ python3 segment-anything-2.py -v 0 --pos 960 540

By default, the ailia SDK is used. If you want to use ONNX Runtime, use the --onnx option.

$ python3 segment-anything-2.py --onnx

If you want to specify the input image, put the image path after the --input option.
You can use --savepath option to change the name of the output file to save.

$ python3 segment-anything-2.py --input IMAGE_PATH --savepath SAVE_IMAGE_PATH

If you want to specify the positive point, put the coordinates(x,y) after the --pos option.

$ python3 segment-anything-2.py --pos 500 375

And if you want to specify the negative point, put the coordinates after the --neg option.

$ python3 segment-anything-2.py --pos 500 375 --neg 360 405

If you want to specify the box, put the coordinates(x1,y1,x2,y2) after the --box option.

$ python3 segment-anything-2.py --box 425 600 700 875

These options can be combined.

$ python3 segment-anything-2.py --pos 500 375 --pos 1125 625
$ python3 segment-anything-2.py --box 425 600 700 875 --neg 575 750

By adding the --model_type option, you can specify model type which is selected from "hiera_l", "hiera_b+", "hiera_s", and "hiera_t". (default is hiera_l)

$ python3 segment-anything-2.py --model_type hiera_l

To improve the performance of MemoryAttention, you can also reduce the number of reference images in past frames, which is num_mask_mem.

$ python3 segment-anything-2.py -v 0 --num_mask_mem 2 --max_obj_ptrs_in_encoder 2

Reference

Framework

Pytorch

Model Format

ONNX opset=17

Netron

hiera_l

hiera_b+

hiera_s

hiera_t

Optimized model

memory_attention.onnx uses a 6-dimensional Matmul. memory_attention.opt.onnx can be implemented using a 4-dimensional Matmul instead of fixing the batch size to 1.