Swin-Transformer-Object-Detection V2.11.0环境搭建(一)

Swin-Transformer-Object-Detection环境搭建

1、 Swin-Transformer-Object-Detection系列版本

​​​​​​​​Swin-Transformer-Object-Detection
在这里插入图片描述

2 MMdetection系列版本

mmdetection
在这里插入图片描述

3、 MMDetection和MMCV兼容版本

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4、Installation(Linux系统环境安装)

1、pytorch 与cudn的匹配版本

2、创建虚拟环境并安装pytorch

conda create -n mmdetection python=3.7 -y   #创建环境
conda activate mmdetection                  #激活环境
conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch  #安装 PyTorch and torchvision (官方)

#如果网不好,可以这样安装
pip3 install torch==1.8.2+cu102 torchvision==0.9.2+cu102 torchaudio===0.8.2 -f https://download.pytorch.org/whl/lts/1.8/torch_lts.html  -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

#验证是否安装成功

>>> import torchvision
>>> import torch
>>> import.__version__
  File "<stdin>", line 1
    import.__version__
          ^
SyntaxError: invalid syntax
>>> torch.__version__
'1.8.2+cu102'

3、安装mmcv-full

下面是cuda、pytorch和mmcv-full的版本
在这里插入图片描述
查看torch1.6.0支持的mmcv-full的版本
注意:上面提供的预构建包不包括所有版本的mmcv-full,您可以单击相应的链接(查看torch1.6.0支持的mmcv-full的版本)来查看支持的版本。例如,您可以单击cu102-torch1.8.0,可以看到cu102-torch1.8.0只提供1.3.0及以上版本的mmcv-full。此外,从v1.3.17开始,我们不再提供使用PyTorch 1.3和1.4编译的完整的mmcv预构建包。你可以在这里找到用PyTorch 1.3和1.4编译的以前版本。在我们的Cl中,兼容性仍然得到保证,但我们将在明年放弃对PyTorch 1.3和1.4的支持。

#Install mmcv-full. 安装mmcv-full
pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/{cu_version}/{torch_version}/index.html

Please replace {cu_version} and {torch_version} in the url to your desired one. For example, to install the latest mmcv-full with CUDA 11.0 and PyTorch 1.7.0, use the following command:

#案例1:(安装最新版本)
pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu110/torch1.7.0/index.html
pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu102/torch1.8.0/index.html

#案例2:(安装特定版本)
pip install mmcv-full==1.3.9 -f https://download.openmmlab.com/mmcv/dist/cu111/torch1.9.0/index.html #明确mmcv-full的版本号

pip install mmcv-full==1.3.17 -f https://download.openmmlab.com/mmcv/dist/cu102/torch1.8.0/index.html -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com #(镜像加速安装)

#验证是否安装成功
import mmcv

#如果出现
>>> import mmcv
No CUDA runtime is found, using CUDA_HOME='/usr/local/cuda-10.2'

#我们去看看驱动:
nvidia-smi

如果返回NVIDIA驱动失效简单解决方案:NVIDIA-SMI has failed because it couldn‘t communicate with the NVIDIA driver.

这种情况是由于重启服务器,linux内核升级导致的,由于linux内核升级,之前的Nvidia驱动就不匹配连接了,但是此时Nvidia驱动还在,可以通过命令 nvcc -V 找到答案。



#解决方法:
查看已安装驱动的版本信息
ls /usr/src | grep nvidia
(mmdetection) lhy@thales-Super-Server:~$ ls /usr/src | grep nvidia
nvidia-440.33.01


#进行下列操作
sudo apt-get install dkms
sudo dkms install -m nvidia -v 440.33.01


#然后进行验证:
(mmdetection) lhy@thales-Super-Server:~$ nvidia-smi
Fri May  6 00:56:02 2022       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 440.33.01    Driver Version: 440.33.01    CUDA Version: 10.2     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  TITAN RTX           Off  | 00000000:02:00.0 Off |                  N/A |
|  0%   47C    P0    54W / 280W |      0MiB / 24220MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   1  TITAN RTX           Off  | 00000000:03:00.0 Off |                  N/A |
|  0%   47C    P0    65W / 280W |      0MiB / 24220MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   2  TITAN RTX           Off  | 00000000:82:00.0 Off |                  N/A |
|  0%   48C    P0    63W / 280W |      0MiB / 24220MiB |      1%      Default |
+-------------------------------+----------------------+----------------------+
|   3  TITAN RTX           Off  | 00000000:83:00.0 Off |                  N/A |
|  0%   46C    P0    42W / 280W |      0MiB / 24220MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+
(mmdetection) lhy@thales-Super-Server:~$ python
Python 3.7.13 (default, Mar 29 2022, 02:18:16) 
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import mmcv

4、MMDetection安装

#克隆mmdetection存储库
git clone https://github.com/open-mmlab/mmdetection.git
cd mmdetection

安装构建要求,然后安装mmdetection。(我们通过github repo安装pycocotools而不是pypi,因为pypi版本较旧并且与最新的numpy不兼容。)

pip install cython matplotlib opencv-python 
cython
numpy
matplotlib

pip install -r requirements.txt -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com 
#pip install -r requirements/build.txt -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
pip install "git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI"
pip install -v -e .## or "python setup.py develop"
#a.当指定-e或develop时,MMDetection被安装在dev模式下,对代码所做的任何本地修改都将生效,无需重新安装
#b.如果你想使用opencv-python-headless而不是opencv-python,你可以在安装MMCV之前安装它。

#安装完成
Using /home/lhy/anaconda3/envs/mmdetection/lib/python3.7/site-packages
Finished processing dependencies for mmdet==2.24.1

注意:
1.git commit的id将在步骤d中写入版本号,例如0.6.0 + 2e7045c。该版本还将保存在经过训练的模型中。建议你每次从github获取一些更新时都运行步骤d。如果修改了C++/CUDA代码,则此步骤为强制性的。
2.按照上述说明,mmdetection将安装在dev模式下,对代码进行的任何本地修改都将生效,而无需重新安装它(除非你提交了一些提交并希望更新版本号)。
3.如果要使用 opencv-python-headless 来代替 opencv-python,可以在安装MMCV之前先安装它。
4.一些依赖关系是可选的。只需运行 pip install -v -e .,将只会安装最低运行时间要求。要使用可选的依赖项如albumentations和imagecorruptions,并使用pip install -r requirements/optional.txt 手动安装它们,或者在调用pip时指定所需的附加项(例如pip install -v -e .[optional])。对于额外字段的有效键为:all,tests,build,和 optional。

#安装额外依赖Instaboost, Panoptic Segmentation, LVIS数据集,或Albumentations。

# for instaboost
pip install instaboostfast
# for panoptic segmentation
pip install git+https://github.com/cocodataset/panopticapi.git
# for LVIS dataset
pip install git+https://github.com/lvis-dataset/lvis-api.git
# for albumentations
pip install -r requirements/albu.txt


d.如果你想使用albumentations,我们建议使用pip install -r requirements/ albumentations或pip install -U albumentations——nobinary qudida, albumentations。如果您简单地使用pip install albumentations&gt;=0.3.2,它将同时安装opencv-python-headless(即使您已经安装了opencv-python)。我们建议在安装albumentation的产品后检查环境,以确保opencv-python和opencv-python-headless没有被同时安装,因为如果同时安装可能会导致意想不到的问题。请参阅官方文件了解更多细节。

5、apex安装

git clone https://github.com/NVIDIA/apex

#进入 apex 文件夹
执行:python setup.py install

git clone https://github.com/NVIDIA/apex
cd apex
python3 setup.py install

pip list 能看见 apex (0.1版本,只有这一个版本)
注:安装的apex会在训练模型时候有一个警告内容如下:(但实际没啥影响)
fused_weight_gradient_mlp_cuda module not found. gradient accumulation fusion with weight gradient computation disabled.

在这里插入图片描述

5、测试搭建好的环境

添加一个自己的图片在demo目录下,执行:

python demo/image_demo.py  demo/demo.jpg configs/swin/mask_rcnn_swin_tiny_patch4_window7_mstrain_480-800_adamw_3x_coco.py  weights/mask_rcnn_swin_tiny_patch4_window7.pth

在这里插入图片描述
不输出实例分割图,demo/image_demo.py 做如下修改:

demo/image_demo.py 做如下修改:

    # test a single image
    result = inference_detector(model, args.img)
    new_result = result[0]
    # show the results
    show_result_pyplot(model, args.img, new_result, score_thr=args.score_thr)

python demo/image_detection_demo.py  demo/demo.jpg configs/swin/mask_rcnn_swin_tiny_patch4_window7_mstrain_480-800_adamw_3x_coco.py  weights/mask_rcnn_swin_tiny_patch4_window7.pth

在这里插入图片描述

6、问题解决

1.AssertionError: Incompatible version of pycocotools is installed. Run pip uninstall pycocotools first. Then run pip install mmpycocotools to install open-mmlab forked pycocotools.

解决办法已经给出了,命令行中:

pip uninstall pycocotools
pip install mmpycocotools

2.KeyError: “CascadeRCNN: ‘backbone.layers.0.blocks.0.attn.relative_position_bias_table’”

预训练模型加载错误,应该使用imagenet预训练的模型,而不是在coco上微调的模型,这个错误我也很无奈啊,跟我预想的使用coco模型预训练不一样,官方github也有人提出相同问题,解决办法就是不加载预训练模型从头训练,或者在https://github.com/microsoft/Swin-Transformer上下载分类的模型。

3.import pycocotools._mask as _mask

File “pycocotools/_mask.pyx”, line 1, in init pycocotools._mask
ValueError: numpy.ndarray size changed, may indicate binary incompatibility. Expected 88 from C header, got 80 from PyObject。

numpy版本问题,使用pip install --upgrade numpy升级numpy版本

7、后续安装库

1、安装WBF库

pip install ensemble-boxes   -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值