libtorch下载地址:https://download.pytorch.org/libtorch/cpu/libtorch-shared-with-deps-1.5.1%2Bcpu.zip
后来使用中发现libtorch1.5.1版本有内存泄漏问题,具体是openmp的内存泄漏造成的,所以后来实际使用的是libtorch1.6.0.
在linux系统下新建一个文件夹如:pytorch_test
文件夹下新建一个文件:main.cpp,文件内容如下:
#include
#include
using namespace std;
int main()
{
torch::Tensor tensor = torch::eye(3);
std::cout << tensor << std::endl;
cout << "Hello World!" << endl;
return 0;
}
文件夹下新建一个文件:CMakeLists.txt文件内容如下:(因为我把下载的libtorch1.5.1解压到共享文件夹下的vmsharefolders文件夹下,所以下面配置Torch_DIR是:/mnt/hgfs/vmsharefolders/libtorch/share/cmake/Torch)
cmake_minimum_required(VERSION 3.5)
project(libtorch_demo LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(Torch_DIR /mnt/hgfs/vmsharefolders/libtorch/share/cmake/Torch)
find_package(Torch REQUIRED)
add_executable(libtorch_demo main.cpp)
target_link_libraries(libtorch_demo "${TORCH_LIBRARIES}")
set_property(TARGET libtorch_demo PROPERTY CXX_STANDARD 17)
在文件夹下开启终端,执行如下命令:
mkdir build
cd build
cmake ..
make
分别执行上述四条命令,就会编译完程序,执行效果如下:
wmz@ubuntu:~/Desktop/mypractice/pytorch_test/build$ cmake ..
-- The CXX compiler identification is GNU 5.4.0
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for C++ include pthread.h
-- Looking for C++ include pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Found torch: /mnt/hgfs/vmsharefolders/libtorch/lib/libtorch.so
-- Configuring done
-- Generating done
-- Build files have been written to: /home/wmz/Desktop/mypractice/pytorch_test/build
wmz@ubuntu:~/Desktop/mypractice/pytorch_test/build$ ls
CMakeCache.txt CMakeFiles cmake_install.cmake Makefile
wmz@ubuntu:~/Desktop/mypractice/pytorch_test/build$ make
Scanning dependencies of target libtorch_demo
[ 50%] Building CXX object CMakeFiles/libtorch_demo.dir/main.cpp.o
[100%] Linking CXX executable libtorch_demo
[100%] Built target libtorch_demo
wmz@ubuntu:~/Desktop/mypractice/pytorch_test/build$ ls
CMakeCache.txt CMakeFiles cmake_install.cmake libtorch_demo Makefile
wmz@ubuntu:~/Desktop/mypractice/pytorch_test/build$ ./libtorch_demo
1 0 0
0 1 0
0 0 1
[ CPUFloatType{3,3} ]
Hello World!
参考:UBUNTU 18.04 INSTALL QT 5.12.9和LIBTORCH 1.5.1
参考:CMakeLists.txt 语法介绍与实例演练
问题:RuntimeError: [enforce fail at CPUAllocator.cpp:64] . DefaultCPUAllocator: can’t allocate memory: you tried to allocate
参考:我与导师的聊天记录
首先感谢参考博主的热心贡献,我遇到的问题与他的不同,但是同样是内存不足的问题,我是用的是libtorch1.5.1-cpu的c++ inference部分。因为要模拟部署,所以在一台电脑上编译,在另一台电脑上执行。编译的虚拟机配置2核4G,测试的虚拟机1核2G,然后就报错内存不足。
当我把测试虚拟机的内存调到2核4G时程序就可以正常运行了。
libtorch GPU版本编译时报错:
/usr/bin/ld: cannot find -lCUDA_cublas_device_LIBRARY-NOTFOUND
collect2: error: ld returned 1 exit status
查找原因,说是cmake版本太低,我用的是cmake3.10,然后就卸载了cmake3.10,安装了cmake3.18.2.
参考:NOTFOUND CUDA_cublas_device_LIBRARY,Ubuntu16.04安装torch遇到的错误
参考:ubuntu卸载/更新Cmake
通过升级cmake版本上面的问题解决了,但是还有报错:
In function `main':
test_db.cpp:(.text+0x39ef): undefined reference to `cv::imread(std::string const&, int)'
test_db.cpp:(.text+0x4aec): undefined reference to `cv::imwrite(std::string const&, cv::_InputArray const&, std::vector > const&)'
CMakeFiles/dbnet_demo.dir/test_db.cpp.o: In function `cv::Mat::Mat(cv::Size_, int, void*, unsigned long)':
test_db.cpp:(.text._ZN2cv3MatC2ENS_5Size_IiEEiPvm[_ZN2cv3MatC5ENS_5Size_IiEEiPvm]+0x14b): undefined reference to `cv::error(int, std::string const&, char const*, char const*, int)'
test_db.cpp:(.text._ZN2cv3MatC2ENS_5Size_IiEEiPvm[_ZN2cv3MatC5ENS_5Size_IiEEiPvm]+0x219): undefined reference to `cv::error(int, std::string const&, char const*, char const*, int)'
原因及分析参考:Opencv4 和 libtorch不兼容的问题
参考:Pytorch_2: libtorch导致OPENCV错误:对‘cv::imread(std::string const&, int)’未定义的引用
解决办法:
方法一、降低opencv版本,比如opencv3.4就可以通过。
方法二、源码编译libtorch(推荐)。
已经测试通过:使用官方libtorch1.6.0-cu102+opencv3.4.2静态库编译测试程序通过(系统ubuntu18.04,gcc 7.4)。
看执行时间确认使用了GPU。
$ ./dbnet_demo
ok
file_names.size():5
0001004.jpg
./data/0001004.jpg
[W TensorIterator.cpp:918] Warning: Mixed memory format inputs detected while calling the operator. The operator will output contiguous tensor even if some of the inputs are in channels_last format. (function operator())
The run time is: 0.440735s
CLOCKS_PER_SEC is: 1000000
0001001.jpg
./data/0001001.jpg
The run time is: 0.103798s
CLOCKS_PER_SEC is: 1000000
0001002.jpg
./data/0001002.jpg
The run time is: 0.101153s
CLOCKS_PER_SEC is: 1000000
0001003.jpg
./data/0001003.jpg
The run time is: 0.100296s
CLOCKS_PER_SEC is: 1000000
0000000.jpg
./data/0000000.jpg
The run time is: 0.098885s
CLOCKS_PER_SEC is: 1000000
在第二台GPU电脑上编译报错(系统centos7.8,gcc4.8.5):
原因是gcc版本太低。
-- Caffe2: CUDA detected: 10.1
-- Caffe2: CUDA nvcc is: /usr/local/cuda-10.1/bin/nvcc
-- Caffe2: CUDA toolkit directory: /usr/local/cuda-10.1
-- Caffe2: Header version is: 10.1
-- Found cuDNN: v7.6.4 (include: /usr/local/cuda-10.1/include, library: /usr/local/cuda-10.1/lib64/libcudnn.so)
-- Autodetected CUDA architecture(s): 7.5
-- Added CUDA NVCC flags for: -gencode;arch=compute_75,code=sm_75
-- Configuring done
-- Generating done
-- Build files have been written to: /home/wmz/Documents/dbnet/dbnet/build2
[wintone@localhost build2]$ make
[ 25%] Building CXX object CMakeFiles/dbnet.dir/DBnetAPI.cpp.o
c++: error: unrecognized command line option ‘-std=c++14’
make[2]: *** [CMakeFiles/dbnet.dir/DBnetAPI.cpp.o] Error 1
make[1]: *** [CMakeFiles/dbnet.dir/all] Error 2
make: *** [all] Error 2
在第三台GPU电脑上测试通过(系统centos7.2, gcc6.3)
中间遇到过的问题是,这台电脑上没有安装cudnn,报错:
Could NOT find CUDNN(missing: CUDNN_LIBRARY_PATH CUDNN_INCLUDE_PATH)
原因是官方Caffe2链接了cudnn,所以编译时需要本地有cudnn安装,或者编译器可以找到cudnn。
我的解决办法是直接在命令行解决(因为我下载的linux cudnn库直接解压到test目录下,默认文件夹名为cuda):
cmake .. -DCUDNN_INCLUDE_DIR=/home/wmz/test/cuda/include -DCUDNN_LIBRARY=/home/wmz/test/cuda/lib64