天天看点

ubuntu16.04+tensorflow1.8 + python3.5 +other packages 环境搭建(monopsr)ubuntu 16.04+tensorflow-gpu 1.8 + python 3.5 +other packages 环境搭建(monopsr)

@liuhaojie

ubuntu 16.04+tensorflow-gpu 1.8 + python 3.5 +other packages 环境搭建(monopsr)

创建一个Python3.5环境

#创建一个Python3.5环境
	conda create --name mono3 python=3.5
#查看已有环境
	conda info -e
#删除已有环境
	conda remove -n mono3 --all 
#复制环境
	conda create -n mono3copy --clone torch 
#激活pytorch环境
	source activate mono3
#退出环境
	deactivate mono3
           

修改conda镜像源

在终端中运行以下命令修改镜像源,可以明显加速安装.

conda config --prepend channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/  #优先使用清华conda镜像
	conda config --prepend channels http://mirrors.ustc.edu.cn/anaconda/pkgs/free/   #也可选用科大conda镜像
	conda config --prepend channels https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
           

要查看镜像源是否安装成功的话,建议终端中运行以下命令:

conda config --set show_channel_urls yes
           

会生成一个~/.condarc文件,运行cat命令查看文件内容

cat ~/.condarc #查看
gedit ~/.condarc #修改
           

显示内容为

ubuntu16.04+tensorflow1.8 + python3.5 +other packages 环境搭建(monopsr)ubuntu 16.04+tensorflow-gpu 1.8 + python 3.5 +other packages 环境搭建(monopsr)

安装tensorflow 1.8 gpu

coverage
matplotlib
nose
numpy
opencv-python
pandas
pillow
pypng
pyyaml
scipy
scikit-image
sklearn
tensorflow-gpu==1.8
           

package安装

#conda方式
	conda install tensorflow-gpu==1.8
#打印tensorflow 版本	
	pip list | grep tensorflow

#pip方式
pip install coverage nose numpy opencv-python
pip3 install pandas pillow pypng pyyaml scipy scikit-image sklearn
pip install pyswarms 'matplotlib<3.0'  # matplotlib>3不支持python3+
	#安装skimage时 直接使用 pip install skimage 报错,使用下面代替
	pip install scikit-image
	pip install opencv-contrib-python==3.4.2.16  #opencv新增的contrib模块
	
#查看环境已经安装的 package
	conda list
           

测试tensorflow:

import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
a = tf.constant(10)
b = tf.constant(32)
print(sess.run(a + b))
           

结果图

ubuntu16.04+tensorflow1.8 + python3.5 +other packages 环境搭建(monopsr)ubuntu 16.04+tensorflow-gpu 1.8 + python 3.5 +other packages 环境搭建(monopsr)

# 2.服务器上+(GPU)的环境搭建

查看cuda的版本

nvidia-smi  #查看显卡
nvcc -V  #查看cuda
           

关闭某个进程

kill -9 pid #用来强制终止指定pid进程(推荐使用此方法)

我的cuda的版本是8.0

ok!!!

monospr 编译tf_op 遇到的问题

cuda8 +tensorflow-gpu1.4 编译没有问题 OK

cuda8 +tensorflow-gpu1.8 编译有问题

使用

locate tensorflow_frameework

查看 tensorflow_frameework的存在性:否则会报:

error:nn_distance/tf_nndistance_so.so: undefined symbol: _ZN10tensorflow8internal21CheckOpMessageBuilder9NewStringEv
           
locate tensorflow_frameework
           
ubuntu16.04+tensorflow1.8 + python3.5 +other packages 环境搭建(monopsr)ubuntu 16.04+tensorflow-gpu 1.8 + python 3.5 +other packages 环境搭建(monopsr)

Anconda3 的pkgs 包的存在会影响一些库的安装,例如

Compile the two custom TF ops src/tf_ops/nn_distance and src/tf_ops/approxmatch by running the shell scripts found in the respective folders. The location of your TensorFlow python package is passed as an argument.

sh src/tf_ops/approxmatch/tf_approxmatch_compile.sh 
sh src/tf_ops/nn_distance/tf_nndistance_compile.sh
           

tf_nndistance_compile.sh

TF_PATH=/home/lhj/anaconda3/envs/mono3/lib/python3.5/site-packages/tensorflow
/usr/local/cuda-8.0/bin/nvcc tf_nndistance_g.cu -o tf_nndistance_g.cu.o -c -O2 -DGOOGLE_CUDA=1 -x cu -Xcompiler -fPIC
g++ -std=c++11 tf_nndistance.cpp tf_nndistance_g.cu.o -o tf_nndistance_so.so -shared -fPIC -I $TF_PATH/include -I /usr/local/cuda-8.0/include -I $TF_PATH/include/external/nsync/public -lcudart -L /usr/local/cuda-8.0/lib64/ -L/$TF_PATH -ltensorflow_framework -O2 -D_GLIBCXX_USE_CXX11_ABI=0
           

tf_approxmatch_compile.sh

TF_PATH=/home/lhj/anaconda3/envs/mono3/lib/python3.5/site-packages/tensorflow
/usr/local/cuda-8.0/bin/nvcc tf_approxmatch_g.cu -o tf_approxmatch_g.cu.o -c -O2 -DGOOGLE_CUDA=1 -x cu -Xcompiler -fPIC
g++ -std=c++11 tf_approxmatch.cpp tf_approxmatch_g.cu.o -o tf_approxmatch_so.so -shared -fPIC -I $TF_PATH/include -I /usr/local/cuda-8.0/include -I $TF_PATH/include/external/nsync/public -lcudart -L /usr/local/cuda-8.0/lib64/ -L/$TF_PATH -ltensorflow_framework -O2 -D_GLIBCXX_USE_CXX11_ABI=0

           

if TensorFlow > 1.7.0:

keepdims

else:

keep_dims

升级gcc g++版本

Ubuntu (https://askubuntu.com/questions/466651/how-do-i-use-the-latest-gcc-on-ubuntu/581497#581497)

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-4.9 g++-4.9
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.9  #报错,使用下面的相应命令


           

1.)

sudo add-apt-repository ppa:ubuntu-toolchain-r/test

2.) Then install gcc 4.8 and g++ 4.8:

sudo apt-get update; sudo apt-get install gcc-4.8 g++-4.8
           

3.) Once installed, run following commands one by one to use gcc 4.8 instead of previous version.

sudo update-alternatives --remove-all gcc 

sudo update-alternatives --remove-all g++

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 20

sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 20
           
sudo update-alternatives --config gcc

sudo update-alternatives --config g++
           

Now you have the gcc 4.8 with c++11 complete feature in your system. Check out by:

gcc --version