由于换工作已经很久没写文章了,今天写换工作后的第一篇文章 在win10+pytorch1.7下编译DCNv2这个东东。
去github上下载最新的DCNv2源码(如果你的初心是为了运行CenterNet,辣么用新下载的替换掉CenterNet里自带的DCNv2文件夹)
//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里也有提到:
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
搞定,收工!