记录一次安装x-anylabeling并配置segment anything2进行自动标注任务踩坑的全过程

首先十分十分感谢x-anylabeling官方免费开源(链接放在了最后)!

目的:使用x-anylabeling和segment anything 2实现auto-labeling和auto-tracking的标注

软硬件配置:windows11平台,3060GPU;

安装前已经装好的环境:miniconda,pytorch>=2.3.1

由于之前已经在miniconda中安装过pytorch其他版本(没有安装CUDA),并且可以正常跑深度学习模型,所以一直就没有安装CUDA。

安装x-anylabeling-sam2的大致步骤跟官方给的repo一致,只是安装完成后出现了以下的问题:

1. 安装完成后不能够导入segment anything 2的追踪模型
2. 解决问题1,即导入segment anything 2的模型之后虽然可以进行auto labeling,但是不能够实现视频的追踪任务的自动标注

在解决以上两个问题的过程中也出现了其他很多问题(CUDA未装报错,win平台pytorch c++混编报错),会在下面对其进行说明。

问题1解决步骤:

在遵循官方的repo安装完segment-anything-2(安装指南)和x-anylabeling(安装指南)之后,进软件发现并没有sam2的模型,如下图。

尝试了很多方法之后发现不如自己手动导入模型,官方其实也有给出如何手动导入模型的方法,就是添加相关模型的Yaml文件,这些在官方的repo中也有给出(官方链接)。

对于如何导入,如下图,直接点击Load custom model,然后找到你需要导入的Yaml文件(可以通过上面的官方链接下载),然后点击导入就会下载模型,对于下载模型失败和下载模型速度慢的问题,官方也有给解决方案:改变下载源(官方链接);直接自己下载(需要手动下载后放在指定地址之下,下面会讲,并不麻烦)

在这里插入图片描述

我使用的是手动下载模型,官方也都给了这些模型的地址(model zoo地址),我本人下载了两个模型sam2_hiera_large.pt(配置文件)和sam2_hiera_base_plus.pt(配置文件);以sam2_hiera_large.pt为例,对于windows用户,下载好这些模型之后之后必须放在该地址下(如果没有地址下的某一个文件夹,直接创建):C:\Users${User}\xanylabeling_data\models_custom_sam2_hiera_large_video-r20240901\sam2_hiera_large.pt

如果是sam2_hiera_base_plus.pt,则放在C:\Users${User}\xanylabeling_data\models_custom_sam2_hiera_base_video-r20240901\sam2_hiera_large.pt

然后打开软件,像上面那样直接点击Load custom model,点击sam2_hiera_large.pt对应的yaml文件,这个时候就可以不用联网下载直接导入了,省去了一些下载的烦恼。

至此问题1,解决,导入后如下图,可以进行图像分割自动标注,但仍然无法利用sam2进行tracking 的工作

在这里插入图片描述

问题2解决步骤:

在进行问题2之前,在github官方repo的相关的issue里其实有给解决方案(官方链接官方issue相关链接),只不过我在使用此解决方案时出现了另外的问题,特此补充:

我在完成问题1后想使用tracking功能,发现terminal中报错:

ImportError: cannot import name '_C' from 'sam2'

我和该官方issue的问题相同,解决方案就是在安装segment anything 2的目录下重新安装,并使用如下命令:

python setup.py build_ext --inplace

但是我在使用该命令时直接报错:

(x-anylabeling-sam2) PS D:\X-AnyLabeling\segment-anything-2-x-anylabeling> python setup.py build_ext --inplace

CUDA_HOME environment variable is not set. Please set it to your CUDA install root.

Failed to build the SAM 2 CUDA extension due to the error above.
 You can still use SAM 2 and it's OK to ignore the error above, although some post-processing functionality may be limited (which doesn't affect the results in most cases;
  (see https://github.com/facebookresearch/segment-anything-2/blob/main/INSTALL.md).

running build_ext
C:\Users\Administrator\miniconda3\envs\x-anylabeling-sam2\lib\site-packages\torch\utils\cpp_extension.py:497: UserWarning: Attempted to use ninja as the BuildExtension backend but we could not find ninja.. 
Falling back to using the slow distutils backend.
  warnings.warn(msg.format('we could not find ninja.'))

是CUDA没有安装导致的,在windows下安装cuda即可,可以参考该教程,很详细(链接

安装完之后,当我兴致勃勃打开时,继续报错:

(x-anylabeling-sam2) PS D:\X-AnyLabeling\segment-anything-2-x-anylabeling> python setup.py build_ext --inplace
running build_ext

C:\Users\Administrator\miniconda3\envs\x-anylabeling-sam2\lib\site-packages\torch\utils\cpp_extension.py:497: UserWarning: Attempted to use ninja as the BuildExtension backend but we could not find ninja.. Falling back to using the slow distutils backend.
  warnings.warn(msg.format('we could not find ninja.'))
C:\Users\Administrator\miniconda3\envs\x-anylabeling-sam2\lib\site-packages\torch\utils\cpp_extension.py:382: UserWarning: Error checking compiler version for cl: [WinError 2] 系统找不到指定的文件。
  warnings.warn(f'Error checking compiler version for {compiler}: {error}')
building 'sam2._C' extension
Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/

Failed to build the SAM 2 CUDA extension due to the error above. You can still use SAM 2 and it's OK to ignore the error above, although some post-processing functionality may be limited (which doesn't affect the results in most cases; (see https://github.com/facebookresearch/segment-anything-2/blob/main/INSTALL.md).

好吧,一个一个解决,首先安装ninja:

pip install ninja

其次,提示Microsoft Visual C++ 14.0 or greater is required. ,搜索后找到了相关问题,原来是没有安装Visual studio及其组件Microsoft Visual C++ 14.0以上版本,接下来安装这两个就好,官方链接,记得在安装Visual studio的时候要选中MSVC 的选项,如下图;不会安装的可以参考其他链接或自行搜索如何安装,有很多教程,而且这两个安装起来很方便。

在这里插入图片描述

安装完成之后在安装的终端里先切换到下载SAM2的根目录安装SAM 2(不知道这个指令干嘛的,但是我也使用了,不影响):

pip install -e ".[demo]"

然后运行(过程中报了一个错,忘记是什么了,不过不影响安装过程,最终安装完成):

python setup.py build_ext --inplace

然后重新打开软件,即可使用tracking功能,关于如何使用tracking功能,请看官方的操作指南
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/d4c15968d1af4cebab20ead01f695c5f.png在这里插入图片描述

再次十分十分感谢x-anylabeling官方免费开源!感谢官方耐心解决issue中的问题!

官方项目链接:https://github.com/CVHub520/X-AnyLabeling

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值