日常填坑-DCNv2+pytorch1.7+vs2017编译

由于换工作已经很久没写文章了,今天写换工作后的第一篇文章 在win10+pytorch1.7下编译DCNv2这个东东。
去github上下载最新的DCNv2源码(如果你的初心是为了运行CenterNet,辣么用新下载的替换掉CenterNet里自带的DCNv2文件夹)

开始折腾:

  1. 定位到你的 ‘torch/utils/cpp_extension.py’文件(一般在你的python安装目录site-packages文件夹下)
  2. 定位到1374行,将源码
    command = [‘ninja’, ‘-v’] 修改成 command = [‘ninja’, ‘–v’] (这里是两个英文的-,显示出来的成一条横线了,晕)
  3. 定位到233行
    compiler_info.decode().strip() 修改成 compiler_info.decode(‘gbk’).strip()
  4. 回到DCNv2的目录下,用Nodepad++ 打开 DCNv2-master/src/cuda/dcn_v2_cuda.cu文件
    使用替换功能,将所有的"THCState_getCurrentStream(state)“替换成” c10::cuda::getCurrentCUDAStream() "
    这里注意,网上很多地方说的如下修改,在新版本里源码已经更改过了 不必再做修改!!
//extern THCState *state;                           
THCState *state = at::globalContext().lazyInitCUDA();   // Modified

上面的修改不用做了!!!
5. 把上述的修改全部保存后就可以编译了

python setup.py build develop

如果你的 cuda cudnn等配置正确,到这里就可以正常编译啦!
但是编译完成后去运行测试文件的时候又会报错,一波三折啊!
类似于下面的错误:

RuntimeError: Jacobian mismatch for output 0 with respect to input 0

这是因为torch的gradcheck需要double,而输入的都是float类型。不过dcnv的readme.md里也有提到:

Known Issues:

  • Gradient check w.r.t offset (solved)
  • Backward is not reentrant (minor)

This is an adaption of the official Deformable-ConvNets.

Update: all gradient check passes with double precision.

Another issue is that it raises RuntimeError: Backward is not reentrant. However, the error is very small (<1e-7 for
float <1e-15 for double),
so it may not be a serious problem (?)

Please post an issue or PR if you have any comments.
基本上你不用管也行,如果有强迫症必须把test文件运行通过,那就继续魔改。
6. 打开testcpu.py,找到gradcheck这个函数,将里面的参数改成如下:(主要把精度调小)

print('check_gradient_dconv: ',
          gradcheck(dcn_v2_conv, (input, offset, mask, weight, bias,
                    stride, padding, dilation, deformable_groups),
                    eps=1e-2, atol=1e-2, rtol=1e-3))

然后就可以看到正确的结果了:

python .\testcpu.py
torch.Size([2, 64, 128, 128])
torch.Size([20, 32, 7, 7])
torch.Size([20, 32, 7, 7])
torch.Size([20, 32, 7, 7])
0.971507, 1.943013
0.971507, 1.943013
Zero offset passed
C:\Users\zzubq\Anaconda3\lib\site-packages\torch\autograd\gradcheck.py:244: UserWarning: At least one of the inputs that requires gradient is not of double precision floating point. This check will likely fail if all the inputs are not of double precision floating point.
  'At least one of the inputs that requires gradient '
check_gradient_dpooling: True
check_gradient_dconv:  True

搞定,收工!

  • 9
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 20
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值