Win10+Pytorch1.0编译并训练语义分割网络DANet(双注意力机制)

最近研究语义分割,阅读了dual attention的文章,于是想尝试跑跑源码。结果过程异常艰辛,辛辛苦苦花了好几天时间编译,踩过的坑不计其数,不过还好最后编译成功。这里记录下几个主要的坑,希望能够帮助相关研究的同志们。
先说一下自己的平台和配置:
Win10, CUDA10, Pytorch1.0, python:3.7, Anaconda3, VS2017

DANet地址:https://github.com/junfu1115/DANet
作者经过多次更新,明确下我下载调试的时间2019年5月28日,此时作者使用的encoding模块版本是torch_encoding-0.4.5-py3.7。需要注意的是,encoding模块自身已经更新到1.0版本以上,我同时下载了encoding模块https://github.com/zhanghang1989/PyTorch-Encoding的代码,以备参考。注意不能直接替换DANet中的encoding模块版本,因为DANet作者进行了修改。

我是在Git Bash中运行命令行,首先根据Readme指示,在代码文件夹路径下进行python setup.py install操作。

1.报错Subprocess.CalledProcessError:Command ‘[‘ninja’,’-v’]’ returned non-zero exit status 1.
参考:https://zhuanlan.zhihu.com/p/53418563
解决方法是:修改…Anaconda3/Lib/site-packages/torch/utils/cpp_extension.py文件
将[‘ninja’,’-v’]改成[‘ninja’,’- -v’](改成双短线)

2.subprocess.CalledProcessError: Command ‘[‘where’, ‘cl’]’ returned non-zero exit status 1.
这个问题是因为电脑安装的VS2017,没有将编译器路径加入环境变量。解决方法参考链接为https://blog.csdn.net/qing666888/article/details/83246992

3.错误1:no module named ‘enclib_cpu’;错误2:no module named ‘enclib_gpu’
这两个错误类似,所以放在一起说。
出现这个问题,需要进行的操作就是分别cd到…encoding/lib/cpu和…encoding/lib/gpu中,执行python setup.py install操作。此时会报各种错误导致编译失败,主要的原因就是torch版本不断更新,encoding版本也不断更新,会导致一些代码语句发生了变化,这时需要对代码进行修改。
主要修改地方包括:
(1)参考https://zhuanlan.zhihu.com/p/53418563和https://github.com/zhanghang1989/PyTorch-Encoding/issues/161。

将encodoing_kernel.cu nms_kernel.cu activation_kernel.cu 的‘#include’ 
换成‘#include ’
在operation.h中添加 ‘#include 

I followed this suggestion (I think…), but did the same in all cu files in “encoding/lib/cpu” 和 “encoding/lib/gpu”. The error seemed to go away.
注意:有些cu/cpp文件替换掉#include 反而会报额外错误,所以我的选择是在这些文件中让#include 和#include 共存。目前共存的文件包括cpu文件夹中的“roi_align_cpu.cpp”和"roi_align.cpp"。gpu文件夹中的"operator.h"。
(2)编译报错“ error: class “at::Context” has no member “getCurrentCUDAStream””。这是版本不同导致,将gpu文件夹下各代码文件中的at::globalContext().getCurrentCUDAStream() 修改为at::cuda::getCurrentCUDAStream。并增加头文件#include “ATen/cuda/CUDAContext.h”。
这个根据报错位置很容易定位并且修改。
参考链接:https://github.com/zhanghang1989/PyTorch-Encoding/issues/70
具体地:

The main problem for me is enclib_gpu cannot be installed, 
class "at::Context" has no member "getCurrentCUDAStream". 
Then I search for the sorece code in pytorch-master, finding that the API has been change 
to at::cuda::getCurrentCUDAStream, so I replace all the at::globalContext.getCurrentCUDAStream() 
to at::cuda::getCurrentCUDAStream 
and add the header file #include "ATen/cuda/CUDAContext.h" , it is work for me.

(3)修改gpu文件夹中部分cu文件中的初始化tensor的代码,举例如下

at::Tensor sum_ = input_.type().tensor({input_.size(1)}).zero_();
修改为
at::Tensor sum_ = torch::zeros({ input_.size(1) }, input_.options());

这个也是根据编译错误报错位置定位修改。
(4)Including torch/torch.h for C++ extensions is deprecated. Please include torch/extension.h"这是从torch源码中看到的注释。

4.anaconda: import numpy报错:ImportError: DLL load failed: 找不到指定的模块。
还是环境变量的问题。参考链接:https://blog.csdn.net/zhangpeterx/article/details/84872125
解决方法2:将“C:\Users\MSIK\Anaconda3\Library\bin”添加到环境变量Path中。

5.编译后执行训练时,报错:ret = input.log_softmax(dim) RuntimeError: cuda runtime error (77) : an illegal memory access was encountered。或者报错:RuntimeError: CUDA error: an illegal memory access was encountered。或者报错:RuntimeError: cuda runtime error (4) : unspecified launch failure。
以上错误的原因均是显存不够,我的电脑配置是2个1080ti,单个显卡显存8G,按照作者提供的参数设置是无法运行的。我最后成功执行的命令,如下:

CUDA_VISIBLE_DEVICES=0,1 python train.py --dataset cityscapes --model  danet --backbone resnet101 --checkname danet101  --base-size 256 --crop-size 192 --epochs 240 --batch-size 2 --lr 0.003 --workers 2 --multi-grid --multi-dilation 4 8 16

比较关键的是 --base-size 256 --crop-size 192 --batch-size 2

祝大家成功!

你可能感兴趣的:(语义分割)