1. 程式人生 > >ubuntu16.04 ananconda tensorflow

ubuntu16.04 ananconda tensorflow


ABSTRACT

descripe :
date     : 2018.06.06
platform : NVIDIA GeForce 1080Ti
version  : ubuntu        - 16.04 LTS(自帶的是python3.5,所以在安裝tensorflow時,需要注意版本)
           NVIDIA-driver - 384.13
           CUDA          - 9.0
           cuDNN         - 7.1.2
           tensorflow    - 1.8
      

 ==========================

首先在search your computer中輸入upd,進入software&updates,更改下載源(推薦清華或阿里作為下載源。)

 首先sudo apt update,然後sudo apt upgrade


==========================

STEP 1 : install nvidia driver

reference : https://blog.csdn.net/u012759136/article/details/53355781

 

修改驅動檔案許可權

sudo chmod 777 NVIDIA-Linux-x86_64-410.78.run


    1.  sudo apt-get remove –purge nvidia*
    2.  sudo vim /etc/modprobe.d/blacklist.conf (sudo apt-get install vim-gtk)
    3.  add "blacklist nouveau" at the end of file
    4.  sudo update-initramfs -u
    5.  sudo reboot
    6.  Ctrl+Alt+F1
    7.  lsmod | grep nouveau
    8.  sudo /etc/init.d/lightdm stop
    9.  cd ${nvidia_driver_path}
    10. sudo sh NVIDIA-Linux-x86_64-384.130.run –no-x-check –no-nouveau-check –no-opengl-files

安裝(注意 引數)


  sudo ./NVIDIA-Linux-x86_64-384.13.run –no-opengl-files

–no-opengl-files 只安裝驅動檔案,不安裝OpenGL檔案。這個引數最重要
–no-x-check 安裝驅動時不檢查X服務
–no-nouveau-check 安裝驅動時不檢查nouveau 
後面兩個引數可不加。

在安裝驅動的時候,有一步問你”Would you like to run the nvidia-xconfig utility to automatically update your X configuration file…”什麼的,選擇 No

    11. sudo reboot

    
    Please run the commands, if the desktop can not login after reboot.
    1. cd /usr/share/X11/xorg.conf.d/
    2. sudo mv nvidia-drm-outputclass.conf nvidia-drm-outputclass.conf.bak
    3. sudo reboot
    
==========================

STEP 2: install CUDA

    1. Download CUDA 9.0 at https://developer.nvidia.com/cuda-90-download-archive
       select Linux, x86_64, Ubuntu, 16.04, runfile(local) and download base installer,(the download file name is "cuda_9.0.176_384.81_linux.run")
    2. cd ${cuda_9.0_path}
    3. sudo sh cuda_9.0.176_384.81_linux.run
    4. enter n, y, '\n', y, y, '\n' step by step, like this. ('\n' means enter)

有一個關鍵是會讓你選擇是否安裝Graphics Driver for Linux-x86_64:XXXX版本,這個地方必須選擇no!否則會覆蓋之前的驅動。
    
        Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 384.81?
        (y)es/(n)o/(q)uit: n

        Install the CUDA 9.0 Toolkit?
        (y)es/(n)o/(q)uit: y

        Enter Toolkit Location
         [ default is /usr/local/cuda-9.0 ]:

        Do you want to install a symbolic link at /usr/local/cuda?
        (y)es/(n)o/(q)uit: y

        Install the CUDA 9.0 Samples?
        (y)es/(n)o/(q)uit: y

        Enter CUDA Samples Location
         [ default is /home/ai_group ]:

        Installing the CUDA Toolkit in /usr/local/cuda-9.0 ...
        Missing recommended library: libGLU.so
        Missing recommended library: libX11.so
        Missing recommended library: libXi.so
        Missing recommended library: libXmu.so
        Missing recommended library: libGL.so

        Installing the CUDA Samples in /home/ai_group ...
        Copying samples to /home/ai_group/NVIDIA_CUDA-9.0_Samples now...
        Finished copying samples.

        ===========
        = Summary =
        ===========

        Driver:   Not Selected
        Toolkit:  Installed in /usr/local/cuda-9.0
        Samples:  Installed in /home/ai_group, but missing recommended libraries

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 384.00 is required for CUDA 9.0 functionality to work.
To install the driver using this installer, run the following command, replacing <CudaInstaller> with the name of this run file:
    sudo <CudaInstaller>.run -silent -driver

Logfile is /tmp/cuda_install_3795.log

[email protected]:~/Downloads/install required package$ sudo sh cuda_9.0.176_384.81_linux.run -silent -driver

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~``

 

 


    5.1 gedit ./.bashrc

 install CUDA 的 5. gedit ./.bashrc,請注意bashrc檔案在home路徑下,所以需要進入home路徑,開啟bashrc檔案進行編輯。

    5.2 source ~/.bashrc

    5.3 關閉終端,然後再重啟一個新的終端,這一步很重要

    6. add environment path at the end
       export PATH=/usr/local/cuda/bin:$PATH
       export LD_LIBRARY_PATH=/usr/local/cuda/lib64$:LD_LIBRARY_PATH
       
==========================

STEP 3: install cuDNN

    1. Download cuDNN for CUDA 9.0 at https://developer.nvidia.com/rdp/cudnn-archive
    2. Extract the cuDNN
    3. cd ${cuDNN_extract_folder_path}
    4. sudo cp ./include/cudnn.h /usr/local/cuda/include
    5. sudo cp ./lib64/libcudnn* /usr/local/cuda/lib64
    6. sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
    
==========================

STEP 4: install Anaconda (我安裝的對應於python3.6,注意tensorflow對應python的版本)

1、先從官網https://www.anaconda.com/download/#linux或者https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/下載對應的anaconda版本

2、下載完成後進入下載目錄執行以下語句

    $ bash ./Anaconda3-5.2.0-Linux-x86_64.sh
    #此處將檔名替換成你自己的檔名

3、接下來的安裝提示按回車進入下一步,按q跳過License文件,最後輸入yes確認
接下來讓我們輸入安裝路徑,沒特殊情況直接回車預設路徑就好
4、安裝完成後程式提示我們是否把Anaconda3的binary路徑加入到.bashrc,建議新增,這樣以後python和ipython命令就會自動使用anaconda Python3.6環境了(全程Yes,預設安裝在當前使用者的home目錄下,注意,最後一步,是問你要不要新增環境變數,預設是NO,最好選擇YES.)
5、可以在終端輸入$ conda list檢視是否安裝成功

 

=========================

STEP 5: 使用anaconda安裝tensorflow

1、建立conda計算環境

    conda create -n tensorflow python=3.6

過程中輸入y繼續進行
2、啟用環境,使用 conda 安裝 TensorFlow

    source activate tensorflow

3、安裝tensorflow

pip install --ignore-installed --upgrade https://mirrors.tuna.tsinghua.edu.cn/tensorflow/linux/gpu/tensorflow_gpu-1.5.0rc1-cp36-cp36m-linux_x86_64.whl


4、測試tensorflow
按照圖中命令輸入

=========================

STEP 6: pycharm安裝

1、pycharm下載

https://www.jetbrains.com/pycharm/download/#section=linux

社群版對於我們這些學生大體夠用,當然你也可以下載社群版,然後用學校郵箱進行註冊,可免費使用
2、解壓下載檔案
進入下載目錄,執行以下解壓命令

    tar -xzvf pycharm-professional-2018.1.tar.gz

解壓後,會生成一個檔案
這裡寫圖片描述
3、進入檔案bin目錄,進行安裝

    cd pycharm-2018.1/bin
    sh ./pycharm.sh
5、pycharm匯入tensorflow
File->Settings->Project: 專案名->Project Interpreter

點選右上角齒輪圖形

選擇Conda Enviroment,再選擇Existing enviroment
選擇你的tensorflow下的python3.6,例如我的:/home/lee/anaconda3/envs/tensorflow/bin/python3.6

——————————————————————————————————

測試是否安裝成功

import tensorflow as tf

hello=tf.constant('Hello, Tensorflow')

sess = tf.Session()

print(sess.run(hello))

 

=========================

STEP 7: tensorflow環境下安裝python包

1、開啟終端,啟用tensorflow

    source activate tensorflow

2、繼續安裝numpy、pandas、matplotlib,安裝方法很簡單

    conda install numpy

過程中選擇yes
 

=============================================

STEP 8:https://blog.csdn.net/weixin_40362972/article/details/79838662

八、mnist測試

1、下載mnist資料集
進入http://yann.lecun.com/exdb/mnist/,下載如下圖四個檔案
這裡寫圖片描述
注意不要去解壓下載過來的檔案,然後將檔案複製到你的python的Project的目錄下
在pycharm輸入以下程式碼進行測試

# load MNIST data
import tensorflow.examples.tutorials.mnist.input_data as input_data

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

# start tensorflow interactiveSession
import tensorflow as tf

sess = tf.InteractiveSession()


# weight initialization
def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)


def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)


# convolution
def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')


# pooling
def max_pool_2x2(x):
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')


# Create the model
# placeholder
x = tf.placeholder("float", [None, 784])
y_ = tf.placeholder("float", [None, 10])
# variables
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

y = tf.nn.softmax(tf.matmul(x, W) + b)

# first convolutinal layer
w_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])

x_image = tf.reshape(x, [-1, 28, 28, 1])

h_conv1 = tf.nn.relu(conv2d(x_image, w_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)

# second convolutional layer
w_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])

h_conv2 = tf.nn.relu(conv2d(h_pool1, w_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)

# densely connected layer
w_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])

h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, w_fc1) + b_fc1)

# dropout
keep_prob = tf.placeholder("float")
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

# readout layer
w_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])

y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, w_fc2) + b_fc2)

# train and evaluate the model
cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv))
# train_step = tf.train.AdagradOptimizer(1e-4).minimize(cross_entropy)
train_step = tf.train.GradientDescentOptimizer(1e-3).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
sess.run(tf.global_variables_initializer())
for i in range(20000):
    batch = mnist.train.next_batch(50)
    if i % 100 == 0:
        train_accuracy = accuracy.eval(feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0})
        print("step %d, train accuracy %g" % (i, train_accuracy))
    train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})

# 視訊記憶體不足錯誤
# print("test accuracy %g" % accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
a = 10
b = 50
sum = 0
for i in range(a):
    testSet = mnist.test.next_batch(b)
    c = accuracy.eval(feed_dict={x: testSet[0], y_: testSet[1], keep_prob: 1.0})
    sum += c * b
    #print("test accuracy %g" %  c)
print("test accuracy %g" %  (sum / (b * a)))
 

=================================================

anaconda安裝opencv-python

在Linux下,配置Anaconda的環境變數,請參考我的另一篇文章。

以上工作都完成之後,接下來開始輸入命令了。

conda install --channel https://conda.anaconda.org/menpo opencv3

如果不行試試這個

conda install -c https://conda.anaconda.org/menpo opencv3

樓主親測,第一種方法就行得通。

接著就是等待安裝了。Linux下詢問yes/no的時候,輸入yes。


記憶兩個比較實用的conda的命令,安裝和更新包:

conda install numpy=1.9.3

conda update numpy=1.9.3

 

========================

conda install -c menpo opencv

最後測試一下,沒問題。

import cv2

img = cv2.imread(r'F:\Pictures\tiger.jpg')
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()