Ubuntu14.04+GPU+keras机器学习环境配置

1.首先安装Ubuntu14.04,这是Ubuntu系列最稳定的版本。

安装驱动,有条件使用GPU的,采用GPU加速。

2.安装GPU驱动过程:

ctrl+Alt+F1输入账号密码

sudo stop lightdm

sudo su

sudo apt-get --purge remove nvidia-*

./NVIDIA(tab一下)......--no-opengl-files

sudo start lightdm

3.配置keras

Keras使用了下面的依赖包:

·        numpyscipy

·        pyyaml

·        HDF5, h5py(可选,仅在模型的save/load函数中使用)

·        如果使用CNN的推荐安装cuDNN

当使用TensorFlow为后端时:

·        TensorFlow

当使用Theano作为后端时:

·        Theano

后端翻译自backend,指的是Keras依赖于完成底层的张量运算的软件包。

安装Keras时,请cdKeras的文件夹中,并运行下面的安装命令:

sudopython setup.py install

你也可以使用PyPI来安装Keras

sudopip install keras

Ubuntu初始环境设置

·        安装开发包打开终端输入:

#系统升级

>>>sudo apt update

>>>sudo apt upgrade

#安装python基础开发包

>>>sudo apt install -y python-dev python-pip python-nose gcc g++ git gfortran vim

·        安装运算加速库打开终端输入:

>>>sudo apt install -y libopenblas-dev liblapack-dev libatlas-base-dev

3. CUDA开发环境的搭建(CPU加速跳过)

如果您的仅仅采用cpu加速,可跳过此步骤 -下载CUDA8.0

请采用GPU加速的NVIDA官网步骤进行GPU加速

Keras框架搭建

相关开发包安装,根据你是采用GPU还是采用CPU选择相应命令

终端中输入:

>>> sudo pip install -U --pre pip setuptools wheel

>>> sudo pip install -U --pre numpy scipymatplotlib scikit-learn scikit-image

>>> sudo pip install -U --pre tensorflow-gpu

>>>sudopip install -U --pre theano

# >>> sudo pip install -U --pre tensorflow

>>> sudo pip install -U --pre keras==1.2.2

特别说明:keras版本若不指定,将会默认最高版本,运行有些程序识别不了

 

修改keras后端,在终端输入:

>>>gedit ~/.keras/keras.json

安装完毕后,输入python,然后输入:

>>>import tensorflow

>>>import keras

无错输出即可

配置theano文件,在终端中输入:

>>>gedit ~/.theanorc

若是采用GPU加速的,写入以下

 [global]

openmp=False

device = gpu

floatX = float32

allow_input_downcast=True

[lib]

cnmem = 0.8

[blas]

ldflags= -lopenblas

[nvcc]

fastmath = True

若是采用CPU加速版本,则.theanorc文件配置如下:

[global]

openmp=True

device = cpu

floatX = float32

allow_input_downcast=True

[blas]

ldflags= -lopenblas

之后可以验证keras是否安装成功,在命令行中输入Python命令进入Python变成命令行环境:

>>>import keras

没有报错,并且会打印出关于显卡信息以及cnmem等信息(CPU版本没有),那么keras就安装成功了

加速测试

速度测试

新建一个文件 test.py,内容为:

from theano import function, config,shared, sandbox

import theano.tensor as T

import numpy

import time

vlen = 10 * 30 * 768 # 10 x #cores x #threads per core

iters = 1000

rng = numpy.random.RandomState(22)

x = shared(numpy.asarray(rng.rand(vlen),config.floatX))

f = function([], T.exp(x))

print(f.maker.fgraph.toposort())

t0 = time.time()

for i in xrange(iters):

r = f()

t1 = time.time()

print("Looping %d times took %fseconds" % (iters, t1 - t0))

print("Result is %s" % (r,))

if numpy.any([isinstance(x.op,T.Elemwise) for x in f.maker.fgraph.toposort()]):

print('Used the cpu')

else:

print('Used the gpu')

GTX 970显卡下,输出结果大概是0.21秒,在一百倍运算量下19秒,可以进行对比。理论上,相比

较主频为3.3GHzCPU,加速比应该是75倍,但不同的ssd和内存限制了IO接口传输速度。

Keras mnist数据集测试

下载Keras开发包

git clonehttps://github.com/fchollet/keras.git

cd keras/examples/

python mnist_mlp.py

程序无错进行,至此,keras安装完成。

输入:

>>>keras.__version__

注意:上面每个下划线为两个

Keras安装原作者联系方式:[email protected].

4.安装pycharm

5.安装opencv2.4.9

参照:http://www.linuxdiyf.com/linux/28899.html

https://www.douban.com/note/521352400/

1.安装ffmpeg

git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg

cd source_directory

./configure--enable-shared --disable-static

make

sudo makeinstall

检查是否安装成功

ffmpeg

2.安装opencv

安装之前要先装好一堆依赖包,参考官网http://docs.opencv.org/2.4/doc/tutorials/introduction/linux_install/linux_install.html

安装过程出现问题可以参考部分解决方案

注意:必须先安装ffmpeg

官网显示的这一步很重要:

2Enter the<cmake_binary_dir> and type

cmake [<someoptional parameters>] <path to the OpenCVsource directory>

For example

cd ~/opencv

mkdir release

cd release

cmake -D CMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local ..

也可参照以下安装opencv3.0的博客:

http://blog.csdn.net/linj_m/article/details/45048905

安装完成后,输入以下查看opencv版本:

pkg-config --modversionopencv

将opencv源码包下的example/c拷贝出来,执行./build_all.sh

结果如下:

如果打不开输入sh ./build_all.sh

 

6安装h5py

>>>sudopip install h5py

猜你喜欢

转载自blog.csdn.net/tumi678/article/details/74911019