点击小眼睛开启蜘蛛网特效

教你30分钟安装cuda环境下的torch(非Pytorch)

《教你30分钟安装cuda环境下的torch(非Pytorch)》

前言

Pytorch我们都熟悉,是一个优秀的深度学习的运行库,但我们可能也知道Pytorch的前身torchTorch也是一个优秀的深度学习库,运行语言是lua语言。既然我们有了Pytorch,为什么还要装torch呢?

很简单:

  • Torch框架和Pytorch框架类似,熟悉了Pytorch学习torch轻而易举
  • Torch框架的设计也很优秀,自定义化相比Pytorch更灵活
  • 很多优秀的项目,或者说很多最近的项目很多都是使用Torch编写的

说了这么多,开始安装吧!

安装

以下安装环境在Ubuntu16.04。

因为我们安装torch需要运行在GPU上,首先应该安装好Cuda9.1和cudnn。

相关安装cudacudnn教程:
https://oldpan.me/archives/ubuntu16-04-install-cuda9-1-solution
https://oldpan.me/archives/pytorch-gpu-ubuntu-nvidia-cuda90

安装搭配好环境后就可以进行安装torch了。

首先自己创建一个文件夹叫做torch,一般我创建的地址为:home/prototype/torch/

cd到当前的文件夹,从github下载源代码:

git clone https://github.com/torch/distro.git . --recursive

下载好后,安装依赖包然后,安装源文件:

./install-deps
./install.sh

因为我们使用的是cuda9.0以上,如果直接安装torch的话需要遇到这种问题:

...
[ 15%] Building NVCC (Device) object lib/THC/CMakeFiles/THC.dir/THC_generated_THCTensorMathReduce.cu.o
2 errors detected in the compilation of "/tmp/tmpxft_00002141_00000000-4_THCTensorMath.cpp4.ii".
CMake Error at THC_generated_THCTensorMath.cu.o.cmake:267 (message):
  Error generating file
  /home/ubuntu/torch/extra/cutorch/build/lib/THC/CMakeFiles/THC.dir//./THC_generated_THCTensorMath.cu.o


lib/THC/CMakeFiles/THC.dir/build.make:112: recipe for target 'lib/THC/CMakeFiles/THC.dir/THC_generated_THCTensorMath.cu.o' failed
make[2]: *** [lib/THC/CMakeFiles/THC.dir/THC_generated_THCTensorMath.cu.o] Error 1
make[2]: *** Waiting for unfinished jobs....
^Clib/THC/CMakeFiles/THC.dir/build.make:105: recipe for target 'lib/THC/CMakeFiles/THC.dir/THC_generated_THCTensorCopy.cu.o' failed
make[2]: *** [lib/THC/CMakeFiles/THC.dir/THC_generated_THCTensorCopy.cu.o] Interrupt
lib/THC/CMakeFiles/THC.dir/build.make:140: recipe for target 'lib/THC/CMakeFiles/THC.dir/THC_generated_THCTensorMathPairwise.cu.o' failed
make[2]: *** [lib/THC/CMakeFiles/THC.dir/THC_generated_THCTensorMathPairwise.cu.o] Interrupt
CMakeFiles/Makefile2:172: recipe for target 'lib/THC/CMakeFiles/THC.dir/all' failed
make[1]: *** [lib/THC/CMakeFiles/THC.dir/all] Interrupt
Makefile:127: recipe for target 'all' failed
make: *** [all] Interrupt

遇到这种错误是因为最新版的cuda和torch不兼容的问题,我们需要设置一下环境变量:

export TORCH_NVCC_FLAGS="-D__CUDA_NO_HALF_OPERATORS__"

设置好之后,执行:

 ./clean.sh 

清除之前的安装文件。
继续使用命令:./install.sh安装。

安装编译需要一会时间。

安装好后,会提示设置环境变量,输入yes就行。

然后执行source ~/.bashrc激活相应的环境变量。

这是在终端输入th就可以呼出torch的控制界面:

prototype@prototype-X299-UD4-Pro:~$ th
 
  ______             __   |  Torch7 
 /_  __/__  ________/ /   |  Scientific computing for Lua. 
  / / / _ \/ __/ __/ _ \  |  Type ? for help 
 /_/  \___/_/  \__/_//_/  |  https://github.com/torch 
                          |  http://torch.ch 
	
th> 

相关组件

配套torch的相关组件我们一般是Image图像处理库和Loadcaffe这个读取caffe预训练model的库。

安装Image图像处理库很简单:

luarocks install image

安装loadcaffe的时候可能会需要问题:

luarocks install loadcaffe

会遇到:

-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- 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
-- Found Torch7 in /home/adityac/torch/install
-- Could NOT find PROTOBUF (missing: PROTOBUF_LIBRARY PROTOBUF_INCLUDE_DIR)
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
PROTOBUF_LIBRARY (ADVANCED)
linked by target "loadcaffe" in directory /tmp/luarocks_loadcaffe-1.0-0-7097/loadcaffe

-- Configuring incomplete, errors occurred!
make: *** No targets specified and no makefile found. Stop.

Error: Build error: Failed building.

这是我们缺少相应的环境包:

sudo apt-get install libprotobuf-dev protobuf-compiler

安装好这个再安装loadcaffe就没问题了。

这时就可以愉快地使用torch了。

下篇我会讲解如何已经学会Pytorch的基础上如何快速入门torch

参考资料

https://github.com/szagoruyko/loadcaffe/issues/76
https://github.com/torch/cutorch/issues/797

  点赞
本篇文章采用 署名-非商业性使用-禁止演绎 4.0 国际 进行许可
转载请务必注明来源: https://oldpan.me/archives/torch-install

   关注Oldpan博客微信公众号,你最需要的及时推送给你。


  1. S
    Su说道:

    请问
    CUDA_cublas_device_LIBRARY(ADVANCED)
    linked by target "THC" in directory /home/torch/extra/cutorch/lib/THC
    如何解决,和您上面说描述的linked by target "loadcaffe" in directory /tmp/luarocks_loadcaffe-1.0-0-7097/loadcaffe十分相似

    1. x
      xiayang说道:

      解决了么,我也遇到了

    2. d
      dd说道:

      Su
      我也一样出现该问题!!!! 请教怎么解决的?

  2. 董佳辉说道:

    谢啦兄弟!!困扰了好几天,终于解决了!感谢!

  3. c
    chen zj说道:

    我装torch的时候,执行了bash install-deps命令后出现了 “E:软件包 python-software-properties 没有可安装候选 ” 的错误,网上找了一些办法并没有用

    1. c
      chen zj说道:

      我是ubuntu 16.04的系统

chen zj进行回复 取消回复

邮箱地址不会被公开。 必填项已用*标注

评论审核已启用。您的评论可能需要一段时间后才能被显示。