Tensorflow
Tensorflow r2.1 with Python 3.7 Installation Guide
Introduction
TensorFlow (TF) is a powerful library for expressing and evaluating numerical computations in a flexible and highly versatile graph-like way. It is developed in C++ and Python and maintained by Google. TF was open-sourced on November 2015 and now it is also backed up by the programming community. It originated for Machine Learning purposes, although now its applications are extended to any problem that can be expressed as a graph of fundamental computation blocks. Its success stems from its architecture and clean API, which allows computations to be deployed and distributed easily to a variety of hardware platforms and systems, like CPUs or GPUs in desktop, server or mobile devices.
The goal of this guide is to install TF in a fast and easy way, and it is not intended to cover exhaustively any possible way to install it (extended instructions can be found in the official installation page: TensorFlow Installation). Although TF supports a wide range of platforms (Linux, Windows, Android, Clusters, ...) and computation devices (CPUs, GPUs, ...), this guide describes installation on Ubuntu 16.04, 64 bit, with CPUs and NVidia GPUs.
TF can be installed from source or from the provided compiled binary packages. Quick installation uses the binary packages, while Advanced Installation installs it from source. To leverage the full power of TF GPUs must be used, and since binary packages are compiled against a predefined configurations (e.g. CUDA driver), your machine might not support the binary packages, thus installation from source is needed.
Pre-installation steps
TF strongest API is written in Python, thus we will install Python in an isolated directory so it does not interfer with operating system's version and other installations. The following lines download Python 3.7 (which is stable) and all dependencies, install it in /opt/python3.7.6 and create a virtualenv installing everything needed in it. A Virtualenv creates an isolated Python environment, where everything needed by TF and other python programs can be installed to a directory, without affecting the rest of the system.
Important notes:
- If you have already Python v3.7.x or newer installed, jump to Create and activate virtual environment and provide, in the first command, to option -p the path of Python.
- If building from source, go in Software and Updates and tick the Source Code box from the Ubuntu Software tab. This is needed to run build-dep.
Build and install Python
cd ~/Downloads
wget https://www.python.org/ftp/python/3.7.6/Python-3.7.6.tar.xz
tar xfvJ Python-3.7.6.tar.xz
cd Python-3.7.6
sudo apt-get update
sudo apt-get build-dep python3.5
./configure --prefix=/opt/python3.7.6 --enable-optimizations
make -j
sudo -H make install
sudo /opt/python3.7.6/bin/pip3 install virtualenv
Create and activate virtual environment
These lines create a Virtual environment named tensorflow_r2.1_py3.7.6
at ~/tensorflow
using Python from /opt/python3.7.6/bin/python3.7
and activate it. At the end we also install required packages by TF. keras_*
modules are required due to a bug in the building process of r2.1.
/opt/python3.7.6/bin/virtualenv -p /opt/python3.7.6/bin/python3.7 ~/tensorflow/tensorflow_r2.1_py3.7.6
source ~/tensorflow/tensorflow_r2.1_py3.7.6/bin/activate
pip install numpy mock six keras_applications keras_preprocessing keras_datasets
Quick Installation (CPU only)
pip install tensorflow
Now you can train the first network at the Test Installation section!
Quick Installation (CPU/GPU)
The Quick Installation assumes that Nvidia GPU Driver >=396.xx (for which TF r2.1 is precompiled), CUDA 10.1 and cuDNN >7.5 are installed and $PATH and $LD_LIBRARY_PATH are configured accordingly (change "cuda-10.1" to "cuda" if paths don't exist). Example:
export PATH=/usr/local/cuda-10.1/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-10.1/lib64:/usr/local/cuda-10.1/extras/CUPTI/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
Now we just need to pip install TF.
pip install tensorflow-gpu
Now you can train the first network at the Test Installation section!
Advanced Installation (CPU only)
[To be added...]
Advanced Installation (CPU/GPU)
These instructions were tested on Ubuntu 16.04, 64-bit, 64 GB RAM, 32 CPU virtual cores, TitanX GPU (Volta), CUDA 10.1, cudnn 7.5, nccl 2.4.
-
Install Bazel. Follow instructions at bazel.build for bazel version 0.27.1.
-
Install CUDA and NVidia driver provided that:
-
your GPU is CUDA-capable
-
your Linux version is supported by NVidia
-
gcc is installed
-
the system has the correct kernel headers and development packages Installed: verify kernel packages
-
previous CUDA is not installed
sudo apt-get update sudo apt-get upgrade sudo apt-get install cuda-10-1 sudo apt-get install cuda-command-line-tools
- Install cuDNN and nccl (optional) and add paths. Download cuDNN from NVidia website and nccl from here, extract the folder and copy all the files to respective directories at /usr/local/cuda-10.1. Add CUDA environment variables $PATH and $LD_LIBRARY_PATH by running exports to the bash everytime or configure the .bashrc file (after saving .bashrc restart terminal).
export PATH=/usr/local/cuda-10.1/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-10.1/lib64:/usr/local/cuda-10.1/extras/CUPTI/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
- Download TF source and configure
source ~/tensorflow/tensorflow_r2.1_py3.7.6/bin/activate
cd ~/Downloads
git clone https://github.com/tensorflow/tensorflow
cd tensorflow
git checkout r2.1
./configure
- Build TF and pip install it
# select only which instructions (MAVX, MAVX2, MAVX512F, MFMA, MSSE4.1, MSSE4.2) are available for your machine, if build with mkl add the flag --config=mkl
# disable any of the following (AWS S3 filesystem, GCP, HDFS): in the next command all of them are disabled
# bazel may need up to 12 GB of RAM
bazel build -c opt --copt=-mavx --copt=-mavx2 --copt=-mfma --copt=-mfpmath=both --copt=-msse4.1 --copt=-msse4.2 --config=noaws --config=nogcp --config=nohdfs //tensorflow/tools/pip_package:build_pip_package
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
# type next line and press tab to find the wheel that is build specifically for your machine
pip install /tmp/tensorflow_pkg/tensorflow-
Post-installation steps
Optionally install most commonly used packages.
-
Media input/output, Machine learning and scientific computing, Other useful packages, Plotting packages
pip install scikit-image Pillow moviepy imageio pip install scipy sklearn pandas pandas_ml opencv-python pip install cython PyQt5 requests joblib pylint h5py packaging pip install matplotlib mpldatacursor cairocffi
-
Other plotting packages
pip install PyGTK pip install http://ftp.gnome.org/pub/GNOME/sources/pygobject/3.25/pygobject-3.25.1.tar.xz
Test Installation
In order to test the installation, we will train a simple Convolutional Network on MNIST Dataset. Open a terminal and type and following lines.
source ~/tensorflow/tensorflow_r2.1_py3.7.6/bin/activate
git clone https://github.com/tensorflow/models.git
cd models/tutorials/image/mnist
python convolutional.py
This code should train a CNN for MNIST digit classification, that converges in ~1 minute (on GPU) and achieves 0.8% error (only 8 in 1000 digits are misclassified)!
Uninstall
To uninstall Python and Tensorflow just delete the Python directory in /opt and the TF virtual environment directory.
Appendix A: Suggested way to install latest nvidia driver
Update system ppa (unsupported packages) and then install latest driver version (can be found here). It should automatically disable nouveau linux drivers and install nvidia drivers over it.
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt-get update
sudo apt-get install nvidia-410