Libtorch常用代码随笔

一、tensor与vector的转化

参考博客:https://blog.csdn.net/Chris_zhangrx/article/details/119044311

tensor转vector

aten::Tensor ten; // 假设 ten 里面已经有数据了
std::vector<float> v(ten.data_ptr<float>(), ten.data_ptr<float>() + ten.numel());

 

二、Libtorch引用问题

原本的引用代码是(cmake):

if( TORCH_PATH ) 
   message("TORCH_PATH set to: ${TORCH_PATH}")
   set(Torch_DIR ${TORCH_PATH})
else()
   message(FATAL_ERROR "Need to specify Torch path, e.g., pytorch/torch/share/cmake/Torch ")
endif()

find_package(Torch REQUIRED)
message(STATUS "Torch version is: ${Torch_VERSION}")
if(Torch_VERSION GREATER 1.0.1)
   message(STATUS "Torch version is newer than v1.0.1, will use new api")
   add_definitions(-DTORCH_NEW_API)
endif()

 

最近发现不知道为什么有些程序有时候会报错:

Build type: Release
-- Using flag -std=c++14.
CMake Error at CMakeLists.txt:46 (message):
  Need to specify Torch path, e.g., pytorch/torch/share/cmake/Torch


-- Configuring incomplete, errors occurred!

 

环境里不知为何找不到libtorch,因此手动在代码上添加

set(TORCH_PATH /usr/local/libtorch1.8.2/libtorch/share/cmake/Torch)

 

posted @ 2022-03-26 21:07  小咸鱼在看博客  阅读(36)  评论(0编辑  收藏  举报