Configure tensorflow-gpu+Keras under Ubuntu 16.04, install Pycharm, and build a deep learning environment

System environment: Ubuntu16.04

One, install Ubuntu16.04 dual system

1. Use UltraISO to make and install a bootable USB flash drive "see the Ubuntu 16.04 installation basics introductory tutorial to start the USB flash drive production part"

2. Partition Windows Disk to Ubuntu "See Installing Ubuntu 16.04 Dual System under Windows "

3. Successful installation

Second, install the graphics driver

1. Go to Nvidia's official website to download the driver ( https://www.nvidia.com/Download/index.aspx?lang=en-us)

The downloaded driver ×××.run is best stored in /home/your computer name/xxx.run, that is, the root directory and renamed to 0.run (you don’t need to enter a long string of characters for easy installation)

It is recommended to install the highest version of the Nvidia driver, otherwise the CUDA driver version is insufficient for CUDA runtime version may appear in the compiled code after the installation, which means that the version of the cuda runtime library is higher than the version of the driver. Either install a higher version of the driver, or Just use a lower version of the cuda runtime library. So install the latest Nvidia driver directly to avoid this problem.

2. Disable nouveau

The graphics card driver integrated in the Ubuntu system is nouveau, which is an open source driver developed by a third party for NVIDIA. We need to shield it before installing the official NVIDIA driver. 
Add the driver to the blacklist blacklist.conf, but the attributes of this file are not allowed to be modified. So you need to modify the file attributes first.

Change the blacklist-nouveau.conf file

sudo gedit /etc/modprobe.d/blacklist-nouveau.conf

Add at the end of the file

blacklist nouveau
options nouveau modeset=0

Update the configuration file

sudo update-initramfs -u

Reboot

Confirm that Nouveau has been killed by you, use the command: lsmod | grep nouveau

If there is no output, Nouveau has been killed by you. If there is output, please Baidu again!

3. Start the installation

First, we need to end the x-window service, otherwise the driver will not be installed normally

Close X-Window, it's very simple:

sudo service lightdm stop

Install the driver again 

Install the driver with parameters (this place my driver is directly in the root directory, so it is very convenient):

sudo sh 0.run -no-x-check -no-opengl-files -no-nouveau-check

During installation, you may be prompted whether to install the KDM package, click yes to confirm.

Restart X-window

sudo service lightdm start

4. Check whether the graphics card driver is installed successfully

nvidia-smi

The above picture appears, indicating that the driver is successfully installed. It can be seen that my driver version is 410.73.

Three, Anaconda installation

Most of the major deep learning frameworks support Python, and Python can be said to be the leader in scientific computing and data mining. Although there are competitive pressures from languages ​​such as R and Julia, Python's various libraries are too complete. Web development, data visualization, data preprocessing, database connection, crawlers, etc. are omnipotent and have a perfect ecological environment. Only in the data mining tool chain, Python has components such as NumPy, SciPy, Pandas, Scikit-learn, XGBoost, etc. It is very convenient to do data collection and preprocessing, and the subsequent model training phase can be compared with TensorFlow and other Python-based depth The learning framework is perfectly connected. So here we also choose Python as the development language for deep learning.
Anaconda includes python and almost all libraries developed at ordinary times, so you don't have to worry about the installation of the functional libraries needed in the development. Finally, it is the main interface of spyder. The debugger and variable viewing windows are both easy to use.

This installation chooses python3.6 version, Anaconda3-5.2.0-Linux-x86_64.sh version. This version is python3.6, about 621MB.
The download link of anaconda is https://repo.continuum.io/archive/

After the download is complete, you need to install anaconda, enter the anaconda storage directory, and use the command

bash Anaconda3-5.2.0-Linux-x86_64.sh 

After installation, Yu Du who enters the user agreement will enter yes to agree to the agreement. After agreeing, he will ask for the installation path. Press enter to agree to enter the installation. After the installation is complete, it will ask whether to add environment variables and enter yes. After the installation, it will prompt whether to install VSCode. I clicked yes and the installation was not successful anyway. The prompt is that there is a problem with the network connection. It is estimated that it needs to be downloaded from the external network. It does not matter if it is not installed here. You can configure it later.
 

After installation, remember to check whether the path of anaconda is added in the ~/.bashrc file, if it is all the way down, it will be automatically added. If not, you need to manually add:

export PATH="/anaconda3 installation path/bin:$PATH"

Remember to fill in the installation path of anaconda3 according to your actual situation. Then source it to take effect:

source ~/.bashrc

After entering python in the terminal, if the output is as shown below, the installation is successful.

Replace pip source, conda source, apt-get source (optional operation)

After anaconda is installed, it’s best to change the source, because the pip, conda, apt-get and other commands are linked to overseas mirror files when they are operated, and the speed is relatively slow. The source can be replaced with a domestic source.
1. Replace the pip source:
cd to the root directory, enter the command line: sudo mkdir .pip, create a new folder named ".pip".
Create a pip configuration file: sudo gedit ~/.pip/pip.conf, insert the following sentence in the gedit editor interface:

[global]
index-url = https://pypi.douban.com/simple

You can update the pip source to the domestic Douban mirror

2. Replacing conda source
In addition to pip installation, you can also use conda install xxx to install third-party python packages. To change to a domestic mirror, create a configuration file: sudo gedit ~/.condarc
add the following sentences:

channels:

- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

- defaults

show_channel_urls: true

3. Replace apt-get source

Refer here https://blog.csdn.net/ezreal_king/article/details/72790291

Fourth, install Tensorflow-gpu, keras through Anaconda

First, you need to create a new conda environment, named tensorflow:

conda create -n tensorflow python=3.6

After the creation is complete, you can use the following commands to manage this environment:

source activate tensorflow    #激活环境
source deactivate tensorflow  #关闭环境

When the environment is activated, enter the following command to install the gpu version and keras of tensorflow:

conda install tensorflow-gpu keras

Wait for download and install

The conda install tensorflow-gpu keras command is very powerful. It will automatically install CUDA and Cudnn for you and automatically install the matching tensorflow-gpu version, avoiding the need to manually install cuda and cudnn. Which version is installed? ?"this problem.

Test whether the installation is successful, in the case of environment activation, enter python, and then enter

import tensorflow as tf
import keras

If no error is reported, the installation was successful.

You can also try the slightly more complete code below:

import tensorflow as tf
hello = tf.constant(‘hello tensorflow!’)
sess = tf.Session()
print(sess.run(hello))

Remember to close the environment after use! ! !

Five, PyCharm installation and configuration

1) Installation of PyCharm

First download the installation package on the PyCharm official website

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

The webpage may be a bit slow to open

The selected platform is Linux, which is divided into Professional Edition and Community Edition. The difference is that the Professional Edition is charged and has more functions. I didn't understand the specific functions. I chose the Community Edition, just click DOWNLOAD to download

After the download is complete, the installation package is in the Downloads folder.

Right-click the installation package and click "Extract Here" which means to extract here, which is equivalent to decompression.

Specify to the pycharm/bin directory in the terminal, and enter "sh ./pycharm.sh"

Then there are some basic settings. After completion, you can see the following interface:

For the convenience of future use, you can add the path of pycharm to the ~/.bashrc file:

vim ~/.bashrc

Add at the end of the file:

export PATH=/pycharm的解压路径/bin:$PATH

Remember to change the path, save and exit, and then source it to make it effective. Then you can directly enter pycharm.sh in the terminal to start PyCharm.

If you can’t find the pycharm startup icon after the installation is complete, you can refer to: https://jingyan.baidu.com/article/c45ad29c030102051753e20f.html

2) PyCharm configuration

Key: proper configuration Projectof the Interpretercan

source activate tensorflow
which python

Find the installation path of python in tensorflow and write it down in a small notebook

For example, my installation path is /home/cc/anaconda3/evns/tensorflow/bin/python

Then open pycharm, create a new project, in File->setting->project->project interpreter

Click the setting in the red box on the right, select Add local, and then enter the path in the terminal before entering in the red box, and save it.

Just choose existing environment, the ok option of new environment is grayed out, it may not be modified

Then,,, there is no more then, the work is done, start your pycharm journey!

 

Attach:

Anaconda installs a Python development artifact by default, Jupyter Notebook, (previously known as IPython notebook) is an interactive notebook that supports running more than 40 programming languages. Next, we will use it to write a tensorflow helloworld to verify whether our previous installation was successful.

Activate the tensorflow environment in the terminal and enter jupyter notebook to open jupyter

You can also open the .ipynb file written by others for code debugging.

If you want to add some libraries to the python compiler in the jupyter notebook, you need to activate the tensorflow environment first. For example, if an error occurs under jupyter, ImportError: Could not import PIL.Image. The use of `array_to_img` requires PIL. is because one is missing Package, first activate the tensorflow environment source activate tensorflow, and then pip install pillow.

 

Guess you like

Origin blog.csdn.net/Xiao_Xue_Seng/article/details/83963566