segment-everything

安装segment-everything

项目来源
https://github.com/facebookresearch/segment-anything
本地环境

​ shell + jupyter 中搭建以下环境
​ pyTorch 1.21
​ torchvision 0.131

模型

因为电脑配置,选用 vit_b 占用内存最小的模型:
https://dl.fbaipublicfiles.com/segment_anything/sam_vit_b_01ec64.pth

项目效果

官方文件中给的文件效果如下:

load fail

分割之后的效果如下:

truckafter

分割后的层数:47

问题和改进

​ 项目搭建在macos系统上,pytorch官方没有提供cuda加速(gpu加速)的相关包,所以在原文档中注释掉了相关代码:

1
2
3
4
5
6
7
8
9
if using_colab:
import torch
import torchvision
print("PyTorch version:", torch.__version__)
print("Torchvision version:", torchvision.__version__)
print("CUDA is available:", torch.cuda.is_available())
import sys
!{sys.executable} -m pip install opencv-python matplotlib
!{sys.executable} -m pip install 'git+https://github.com/facebookresearch/segment-anything.git'

​ 注释后变成:

1
2
3
4
5
6
7
8
9
if using_colab:
import torch
import torchvision
print("PyTorch version:", torch.__version__)
print("Torchvision version:", torchvision.__version__)
# print("CUDA is available:", torch.cuda.is_available())
import sys
!{sys.executable} -m pip install opencv-python matplotlib
!{sys.executable} -m pip install 'git+https://github.com/facebookresearch/segment-anything.git'

​ 类似地,其他有cuda出现的地方都注释掉。

​ 而在segment-everthing的官方文档中,强烈建议使用带有cuda加速的pytorch版本。因此在mask生成时候,没有用到mac的gpu,速度相对较慢(下图可见gpu: 0.0%)。有机会的话可以部署到其他服务器或者使用windows系统运行,比较一下运行速度。

monitor