warpctc_pytorch 编译不成功的解决办法

最近在做字符串识别工作,需要调用warp_ctc。但是调用过程中屡屡出错。

warp_ctc源码地址为:https://github.com/SeanNaren/Warp-ctc

按照github上的指令安装warpctc_pytorch,执行到这一步时报错:

python setup.py install

错误显示如下:

generating build/warpctc_pytorch/_warp_ctc/__warp_ctc.c
(already up-to-date)
not modified: 'build/warpctc_pytorch/_warp_ctc/__warp_ctc.c'
running install
running bdist_egg
running egg_info
writing warpctc_pytorch.egg-info/PKG-INFO
writing dependency_links to warpctc_pytorch.egg-info/dependency_links.txt
writing top-level names to warpctc_pytorch.egg-info/top_level.txt
reading manifest file 'warpctc_pytorch.egg-info/SOURCES.txt'
writing manifest file 'warpctc_pytorch.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_py
copying warpctc_pytorch/_warp_ctc/__init__.py -> build/lib.linux-x86_64-3.6/warpctc_pytorch/_warp_ctc
running build_ext
building 'warpctc_pytorch._warp_ctc.__warp_ctc' extension
gcc -pthread -B /home1/fzp/anaconda3/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-pib/include -I/home1/fzp/anaconda3/lib/python3.6/site-packages/torch/utils/ffi/../../lib/include/TH -I/home1/fzp/anaconda3/li-I/home1/fzp/attention-OCR/warp-ctc-pytorch_bindings/include -I/home1/fzp/anaconda3/include/python3.6m -c build/warpctc_pyto__warp_ctc.o -std=c++11 -fPIC -std=c99 -DWARPCTC_ENABLE_GPU
cc1: warning: command line option ‘-std=c++11’ is valid for C++/ObjC++ but not for C [enabled by default]
gcc -pthread -B /home1/fzp/anaconda3/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-pib/include -I/home1/fzp/anaconda3/lib/python3.6/site-packages/torch/utils/ffi/../../lib/include/TH -I/home1/fzp/anaconda3/li-I/home1/fzp/attention-OCR/warp-ctc-pytorch_bindings/include -I/home1/fzp/anaconda3/include/python3.6m -c /home1/fzp/attenti4-3.6/home1/fzp/attention-OCR/warp-ctc-pytorch_bindings/pytorch_binding/src/binding.o -std=c++11 -fPIC -std=c99 -DWARPCTC_EN
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++ [enabled by default]
cc1plus: warning: command line option ‘-std=c99’ is valid for C/ObjC but not for C++ [enabled by default]
/home1/fzp/attention-OCR/warp-ctc-pytorch_bindings/pytorch_binding/src/binding.cpp: In function ‘int gpu_ctc(THCudaTensor*, 
/home1/fzp/attention-OCR/warp-ctc-pytorch_bindings/pytorch_binding/src/binding.cpp:92:49: error: cannot convert ‘THCudaTensotTensor*, int)’
     int probs_size = THFloatTensor_size(probs, 2);
                                                 ^
/home1/fzp/attention-OCR/warp-ctc-pytorch_bindings/pytorch_binding/src/binding.cpp:105:61: error: invalid conversion from ‘s
     void* gpu_workspace = THCudaMalloc(state, gpu_size_bytes);
                                                             ^
/home1/fzp/attention-OCR/warp-ctc-pytorch_bindings/pytorch_binding/src/binding.cpp:105:61: error: too few arguments to funct
In file included from /home1/fzp/anaconda3/lib/python3.6/site-packages/torch/utils/ffi/../../lib/include/THC/THC.h:4:0,
                 from /home1/fzp/attention-OCR/warp-ctc-pytorch_bindings/pytorch_binding/src/binding.cpp:9:
/home1/fzp/anaconda3/lib/python3.6/site-packages/torch/utils/ffi/../../lib/include/THC/THCGeneral.h:209:21: note: declared h
 THC_API cudaError_t THCudaMalloc(THCState *state, void **ptr, size_t size);
                     ^
error: command 'gcc' failed with exit status 1
 

经过无数次的查阅资料和调试,终于把坑填平。大家可按照如下步骤完成从头到尾的编译和调用:

1.下载压缩包并解压,或直接git clone源码。

2.运行如下指令进行编译:

mv warp-ctc-pytorch_bindings/ warp-ctc 
cd warp-ctc
mkdir build; cd build
cmake ..
make

(如果下载zip,解压后文件名是warp-ctc-pytorch_bindings,需要第一句的改名操作。如果git clone则不需要此操作)

3.按照如下解决方案进行pytorch_bingding/src/binding.cpp的代码段修改。这是造成上述错误的主要原因。(万分感谢jielingTang大神的答案)

1 at 92 line
int probs_size = THCudaTensor_size(state, probs, 2);
2 at l05 lines
void* gpu_workspace;
THCudaMalloc(state, &gpu_workspace, gpu_size_bytes);

4.开始安装:

cd pytorch_binding
python setup.py install

5. 安装结束后,在warp-ctc/pytorch_binding/build目录下新建一个test.py文件进行测试。目录结构如下:

文件内容为:

import torch
from warpctc_pytorch import CTCLoss
ctc_loss = CTCLoss()
# expected shape of seqLength x batchSize x alphabet_size
probs = torch.FloatTensor([[[0.1, 0.6, 0.1, 0.1, 0.1], [0.1, 0.1, 0.6, 0.1, 0.1]]]).transpose(0, 1).contiguous()
labels = torch.IntTensor([1, 2])
label_sizes = torch.IntTensor([2])
probs_sizes = torch.IntTensor([2])
probs.requires_grad_(True)  # tells autograd to compute gradients for probs
cost = ctc_loss(probs, labels, probs_sizes, label_sizes)
cost.backward()

执行python test.py,若没有报错,则证明编译及运行成功。

6.将其应用于其他项目中。若某项目存在一py格式文件,存在如下调用方式:

则将warp-ctc/pytorch_binding/build/warpctc_pytorch 目录拷贝至与该py文件同级的目录下。

cp -r ~/warp-ctc/pytorch_binding/build/warpctc_pytorch .

再执行该py文件即可成功。

猜你喜欢

转载自blog.csdn.net/abcd740181246/article/details/83058511
今日推荐