├── README.md └── images ├── img1.png └── img2.png /README.md: -------------------------------------------------------------------------------- 1 | # Computer Vision and Deep Learning Setup 2 | 3 | Tutorial on how to setup your system with a NVIDIA GPU and to install Deep Learning Frameworks like TensorFlow, PyTorch, Darknet for YOLO, Theano, and Keras; build OpenCV; and also setting up NVIDIA drivers, CUDA, cuDNN, TensorRT libraries on Ubuntu 16, 17 and 18. 4 | 5 | ## Contents 6 | 1. [Install Prerequisites](https://github.com/heethesh/Computer-Vision-and-Deep-Learning-Setup/blob/master/README.md#1-install-prerequisites) 7 | 2. [Setup NVIDIA Driver for your GPU](https://github.com/heethesh/Computer-Vision-and-Deep-Learning-Setup/blob/master/README.md#2-install-nvidia-driver-for-your-gpu) 8 | 3. [Install CUDA](https://github.com/heethesh/Computer-Vision-and-Deep-Learning-Setup/blob/master/README.md#3-install-cuda) 9 | 4. [Install cuDNN](https://github.com/heethesh/Computer-Vision-and-Deep-Learning-Setup/blob/master/README.md#4-install-cudnn) 10 | 5. [Install TensorRT](https://github.com/heethesh/Computer-Vision-and-Deep-Learning-Setup/blob/master/README.md#5-install-tensorrt) 11 | 6. [Python and Other Dependencies](https://github.com/heethesh/Computer-Vision-and-Deep-Learning-Setup/blob/master/README.md#6-python-and-other-dependencies) 12 | 7. [OpenCV and Contrib Modules](https://github.com/heethesh/Computer-Vision-and-Deep-Learning-Setup/blob/master/README.md#7-install-opencv-and-contrib-modules) 13 | 8. [Deep Learning Frameworks](https://github.com/heethesh/Computer-Vision-and-Deep-Learning-Setup/blob/master/README.md#8-install-deep-learning-frameworks) 14 | - [PyTorch](https://github.com/heethesh/Computer-Vision-and-Deep-Learning-Setup/blob/master/README.md#pytorch) 15 | - [TensorFlow](https://github.com/heethesh/Computer-Vision-and-Deep-Learning-Setup/blob/master/README.md#tensorflow) 16 | - [Keras](https://github.com/heethesh/Computer-Vision-and-Deep-Learning-Setup/blob/master/README.md#keras) 17 | - [Theano](https://github.com/heethesh/Computer-Vision-and-Deep-Learning-Setup/blob/master/README.md#theano) 18 | - [Darknet for YOLO](https://github.com/heethesh/Computer-Vision-and-Deep-Learning-Setup/blob/master/README.md#darknet-for-yolo) 19 | 20 | ## 1. Install Prerequisites 21 | Before installing anything, let us first update the information about the packages stored on the computer and upgrade the already installed packages to their latest versions. 22 | 23 | sudo apt-get update 24 | sudo apt-get upgrade 25 | 26 | Next, we will install some basic packages which we might need during the installation process as well in future. 27 | 28 | sudo apt-get install -y build-essential cmake gfortran git pkg-config 29 | 30 | ## 2. Install NVIDIA Driver for your GPU 31 | Before installing the NVIDIA driver, make sure **Secure Boot** is **Disabled** in BIOS and **Legacy Boot** is selected and **UEFI Boot** is disabled. 32 | 33 | **NOTE (FOR UEFI BOOT ONLY)**: If you still intend to install the driver along with UEFI boot enabled, follow the steps below to enroll the MOK keys and only then proceed with the next driver installation section. If these steps are not followed, it is likely that you might face the login loop issue. 34 | 35 | ``` 36 | sudo openssl req -new -x509 -newkey rsa:2048 -keyout UEFI.key -outform DER -out UEFI.der -nodes -days 36500 -subj "/CN=rambou_nvidia/" 37 | sudo mokutil --import UEFI.der 38 | ``` 39 | 40 | At this step after reboot you will be prompted to select your certificate to import in in key database. If you have inserted a password at certificate creation you'll be prompted to insert it. If you are not prompted, you may have to enter the BIOS by using function keys at boot time. 41 | 42 | ### Driver Installation 43 | The NVIDIA drivers will be automatically detected by Ubuntu in *Software and Updates* under *Additional drivers*. Select the driver for your GPU and click apply changes and reboot your system. *You may also select and apply Intel Microcode drivers in this window.* If they are not displayed, run the following commands from your terminal and refresh the window. 44 | 45 | ``` 46 | sudo add-apt-repository -y ppa:graphics-drivers 47 | sudo apt-get update 48 | ``` 49 | 50 | *At the time of writing this document, the latest stable driver version is 418*. 51 | 52 | Run the following command to check whether the driver has installed successfully by running NVIDIA’s System Management Interface (*nvidia-smi*). It is a tool used for monitoring the state of the GPU. 53 | 54 | nvidia-smi 55 | 56 | In case the above mentioned steps fail or you run into any other issues and have access only to a shell, run the following set of commands to reinstall the driver. 57 | 58 | ``` 59 | sudo apt-get purge -y nvidia* 60 | sudo add-apt-repository -y ppa:graphics-drivers 61 | sudo apt-get update 62 | sudo apt-get install -y nvidia-418 63 | ``` 64 | 65 | ## 3. Install CUDA 66 | CUDA (Compute Unified Device Architecture) is a parallel computing platform and API developed by NVIDIA which utilizes the parallel computing capabilities of the GPUs. In order to use the graphics card, we need to have CUDA libraries installed on our system. 67 | 68 | Download the CUDA driver from the [official nvidia website here](https://developer.nvidia.com/cuda-downloads?target_os=Linux). We recommend you download the *deb (local)* version from installer type as shown in the screen-shot below. 69 | 70 | *At the time of writing this document, the latest stable version is CUDA 10.0*. 71 | 72 | ![](https://github.com/heethesh/Computer-Vision-and-Deep-Learning-Setup/blob/master/images/img1.png) 73 | 74 | After downloading the file, go to the folder where you have downloaded the file and run the following commands from the terminal to install the CUDA drivers. Please make sure that the filename used in the command below is the same as the downloaded file and replace the `` number. 75 | 76 | sudo dpkg -i cuda-repo-ubuntu1604-10-0-local-10.0.130-410.48_1.0-1_amd64.deb 77 | sudo apt-key add /var/cuda-repo-/7fa2af80.pub 78 | sudo apt-get update 79 | sudo apt-get install cuda 80 | 81 | Next, update the paths for CUDA library and executables. 82 | 83 | echo 'export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda-10.0/lib64:/usr/local/cuda-10.0/extras/CUPTI/lib64"' >> ~/.bashrc 84 | echo 'export CUDA_HOME=/usr/local/cuda-10.0' >> ~/.bashrc 85 | echo 'export PATH="/usr/local/cuda-10.0/bin:$PATH"' >> ~/.bashrc 86 | source ~/.bashrc 87 | 88 | You can verify the installation of CUDA version by running: 89 | 90 | nvcc -V 91 | 92 | ## 4. Install cuDNN 93 | CUDA Deep Neural Network (cuDNN) is a library used for further optimizing neural network computations. It is written using the CUDA API. 94 | 95 | Go to official cuDNN website [official cuDNN website](https://developer.nvidia.com/cudnn) and fill out the form for downloading the cuDNN library. 96 | 97 | *At the time of writing this document, the latest stable version is cuDNN 7.4*. 98 | 99 | **Make sure you download the correct cuDNN version which matches with you CUDA version.** 100 | 101 | ![](https://github.com/heethesh/Computer-Vision-and-Deep-Learning-Setup/blob/master/images/img2.png) 102 | 103 | ### Installing from TAR file (Recommended Method) 104 | For cuDNN downloaded using _cuDNN Library for Linux_ method, go to the folder where you have downloaded the “.tgz” file and from the command line execute the following (update the filename). 105 | 106 | tar -xzvf cudnn-10.0-linux-x64-v7.4.2.24.tgz 107 | sudo cp cuda/include/cudnn.h /usr/local/cuda/include 108 | sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64 109 | sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn* 110 | 111 | ### Installing from Debian Package 112 | Install the downloaded packages (Runtime Library, Developer Library and Code Samples) as follows. 113 | 114 | sudo dpkg -i libcudnn7_7.4.2.24-1+cuda10.0_amd64.deb 115 | sudo dpkg -i libcudnn7-dev_7.4.2.24-1+cuda10.0_amd64.deb 116 | sudo dpkg -i libcudnn7-doc_7.4.2.24-1+cuda10.0_amd64.deb 117 | 118 | To check installation of cuDNN, run this in your terminal: 119 | 120 | dpkg -l | grep cudnn 121 | 122 | ### Fixing Broken Symbolic Links 123 | If you have issues with broken symbolic links when you run `sudo ldconfig`, follow the steps below to fix them. **Note the minor version number, which may differ on your system (shown for 7.4.2 here)** 124 | 125 | cd /usr/local/cuda/lib64 126 | sudo rm libcudnn.so 127 | sudo rm libcudnn.so.7 128 | sudo ln libcudnn.so.7.4.2 libcudnn.so.7 129 | sudo ln libcudnn.so.7 libcudnn.so 130 | sudo ldconfig 131 | 132 | ## 5. Install TensorRT 133 | The core of NVIDIA TensorRT facilitates high performance inference on NVIDIA graphics processing units (GPUs). TensorRT takes a trained network, which consists of a network definition and a set of trained parameters, and produces a highly optimized runtime engine which performs inference for that network. 134 | 135 | *At the time of writing this document, the latest stable version is TensorRT 5.0.4*. 136 | 137 | Download the TensorRT local repo file [from here](https://developer.nvidia.com/tensorrt) and run the following commands. You'll need to replace ubuntu1x04, cudax.x, trt4.x.x.x and yyyymmdd with your specific OS version, CUDA version, TensorRT version and package date (refer the downloaded filename). 138 | 139 | sudo dpkg -i nv-tensorrt-repo-ubuntu1x04-cudax.x-trt5.x.x.x-ga-yyyymmdd_1-1_amd64.deb 140 | sudo apt-key add /var/nv-tensorrt-repo-cudax.x-trt5.x.x.x-ga-yyyymmdd/7fa2af80.pub 141 | sudo apt-get update 142 | sudo apt-get install tensorrt 143 | 144 | For Python and TensorFlow support, run the following commands. 145 | 146 | sudo apt-get install libnvinfer5 python-libnvinfer-dev python3-libnvinfer-dev 147 | sudo apt-get install uff-converter-tf 148 | 149 | To check installation of TensorRT, run this in your terminal: 150 | 151 | dpkg -l | grep TensorRT 152 | 153 | ## 6. Python and Other Dependencies 154 | 155 | Install dependencies of deep learning frameworks: 156 | 157 | sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler libopencv-dev 158 | 159 | Next, we install Python 2 and 3 along with other important packages like boost, lmdb, glog, blas etc. 160 | 161 | sudo apt-get install -y --no-install-recommends libboost-all-dev doxygen 162 | sudo apt-get install -y libgflags-dev libgoogle-glog-dev liblmdb-dev libblas-dev 163 | sudo apt-get install -y libatlas-base-dev libopenblas-dev libgphoto2-dev libeigen3-dev libhdf5-dev 164 | 165 | sudo apt-get install -y python-dev python-pip python-nose python-numpy python-scipy python-wheel python-six 166 | sudo apt-get install -y python3-dev python3-pip python3-nose python3-numpy python3-scipy python3-wheel python3-six 167 | 168 | **NOTE: If you want to use Python2, replace the following pip commands with pip2.** 169 | 170 | Before we use pip, make sure you have the latest version of pip. 171 | 172 | sudo pip3 install --upgrade pip 173 | 174 | Now, we can install all the required python packages for deep learning frameworks: 175 | 176 | sudo pip3 install numpy matplotlib ipython protobuf jupyter mock 177 | sudo pip3 install scipy scikit-image scikit-learn 178 | sudo pip3 install keras_applications==1.0.6 --no-deps 179 | sudo pip3 install keras_preprocessing==1.0.5 --no-deps 180 | 181 | Upgrade numpy to the latest version: 182 | 183 | sudo pip3 install --upgrade numpy 184 | 185 | ## 7. Install OpenCV and Contrib Modules 186 | First we will install the dependencies: 187 | 188 | sudo apt-get remove -y x264 libx264-dev 189 | sudo apt-get install -y checkinstall yasm 190 | sudo apt-get install -y libjpeg8-dev libjasper-dev libpng12-dev 191 | 192 | sudo apt-get install -y libtiff5-dev 193 | sudo apt-get install -y libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev 194 | 195 | sudo apt-get install -y libxine2-dev libv4l-dev 196 | sudo apt-get install -y libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev 197 | sudo apt-get install -y libqt4-dev libgtk2.0-dev libtbb-dev 198 | sudo apt-get install -y libfaac-dev libmp3lame-dev libtheora-dev 199 | sudo apt-get install -y libvorbis-dev libxvidcore-dev 200 | sudo apt-get install -y libopencore-amrnb-dev libopencore-amrwb-dev 201 | sudo apt-get install -y x264 v4l-utils 202 | 203 | #### NOTE: Checkout to the latest version of OpenCV. 3.4.5 is used here 204 | 205 | Download OpenCV 3.4.5: 206 | 207 | git clone https://github.com/opencv/opencv.git 208 | cd opencv 209 | git checkout 3.4.5 210 | cd .. 211 | 212 | Download OpenCV-contrib 3.4.5: 213 | 214 | git clone https://github.com/opencv/opencv_contrib.git 215 | cd opencv_contrib 216 | git checkout 3.4.5 217 | cd .. 218 | 219 | #### NOTE: Keep the build folder in the same location as it may be required in future to upgrade or uninstall OpenCV 220 | 221 | Configure and generate the MakeFile in */opencv/build* folder (make sure to specify paths to downloaded OpenCV-contrib modules correctly): 222 | 223 | cd opencv 224 | mkdir build 225 | cd build 226 | 227 | cmake -D CMAKE_BUILD_TYPE=RELEASE \ 228 | -D CMAKE_INSTALL_PREFIX=/usr/local \ 229 | -D INSTALL_C_EXAMPLES=ON \ 230 | -D INSTALL_PYTHON_EXAMPLES=ON \ 231 | -D WITH_TBB=ON \ 232 | -D WITH_V4L=ON \ 233 | -D WITH_QT=ON \ 234 | -D WITH_OPENGL=ON \ 235 | -D WITH_CUDA=ON \ 236 | -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \ 237 | -D BUILD_EXAMPLES=ON .. 238 | 239 | #### NOTE: If you are using Python3, you must add the following flag as well 240 | 241 | -D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3 \ 242 | 243 | #### NOTE: If you are using Ubuntu 17 or 18, you must add the following flags as well 244 | 245 | -D CMAKE_CXX_COMPILER:FILEPATH=/usr/bin/g++-5 \ 246 | -D CMAKE_C_COMPILER:FILEPATH=/usr/bin/gcc-5 \ 247 | 248 | Compile and install: 249 | 250 | make -j$(nproc) 251 | sudo make install 252 | sudo ldconfig 253 | 254 | Retain the build folder in the same location. This will be required if you want to uninstall OpenCV or upgrade in the future or else the uninstall process might become very tedious. 255 | 256 | Check installation of OpenCV: 257 | 258 | python 259 | >>> import cv2 260 | >>> cv2.__version__ 261 | 262 | #### NOTE: If you get any errors with `import cv2`, make sure the `PYTHONPATH` points to the location of `cv2.so` file correctly in your `~/.bashrc` file as follows. 263 | 264 | export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH 265 | export PYTHONPATH=/usr/local/lib/python3.5/site-packages:$PYTHONPATH 266 | 267 | To uninstall OpenCV: 268 | 269 | cd /opencv/build 270 | sudo make uninstall 271 | 272 | ## 8. Install Deep Learning Frameworks 273 | 274 | ### PyTorch 275 | 276 | You can run the commands for installing pip packages `torch` and `torchvision` from [the Quick Start section here](https://pytorch.org/). 277 | 278 | ### TensorFlow 279 | 280 | #### Quick Install (Not Recommended) 281 | 282 | A quick way to install TensorFlow using pip without building is as follows. However this is not recomended as we have several specific versions of GPU libraries to improve performance, which may not be available with the pip builds. 283 | 284 | sudo pip3 install tensorflow-gpu 285 | 286 | #### Building TensorFlow from Source 287 | 288 | Now we will download the TensorFlow repository from GitHub in the */home* folder. Checkout to the latest version of TensorFlow (`r1.13` is used here). 289 | 290 | cd ~ 291 | git clone https://github.com/tensorflow/tensorflow.git 292 | cd tensorflow 293 | git checkout r1.13 294 | 295 | Next we need to install Bazel along with its dependencies 296 | 297 | sudo apt-get install pkg-config zip zlib1g-dev unzip 298 | wget https://github.com/bazelbuild/bazel/releases/download/0.21.0/bazel-0.21.0-installer-linux-x86_64.sh 299 | chmod +x bazel-0.21.0-installer-linux-x86_64.sh 300 | ./bazel-0.21.0-installer-linux-x86_64.sh --user 301 | 302 | export PATH="$PATH:$HOME/bin" 303 | source ~/.bashrc 304 | 305 | To verify installation of Bazel run: 306 | 307 | bazel version 308 | 309 | Now install brew on your system: 310 | 311 | sudo apt-get install linuxbrew-wrapper 312 | brew doctor 313 | brew install coreutils 314 | 315 | The root of the *tensorflow* folder contains a bash script named configure. This script asks you to identify the pathname of all relevant TensorFlow dependencies and specify other build configuration options such as compiler flags. You must run this script prior to creating the pip package and installing TensorFlow. 316 | 317 | cd ~/tensorflow 318 | ./configure 319 | 320 | **NOTE: Enter the your correct CUDA and CuDNN version below. CUDA 10.0 and CuDNN 7.4 is used here** 321 | 322 | >Select Python 3, no to all additional packages, gcc as compiler (GCC 5.4). 323 | > 324 | >For CUDA, enter 10.0 325 | > 326 | >For cuDNN, enter 7.4 327 | > 328 | > Select yes for TensorRT support 329 | > 330 | >Enter your GPU Compute Capability (Eg: 3.0 or 6.1). Find yout GPU Compute Capability from [here](https://en.wikipedia.org/wiki/CUDA#GPUs_supported). 331 | > 332 | >Use nvcc as the CUDA compiler. 333 | 334 | Finally, build the pip package: 335 | 336 | bazel build --config=opt --config=cuda --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0" //tensorflow/tools/pip_package:build_pip_package 337 | 338 | The build might take upto an hour. If it fails to build, you must clean your build using the following command and configure the build once again. 339 | 340 | bazel clean --expunge 341 | ./configure 342 | 343 | The bazel build command builds a script named build_pip_package. Running this script as follows will build a .whl file within the /tmp/tensorflow_pkg directory: 344 | 345 | bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg 346 | 347 | Once the build is complete, invoke pip install to install that pip package. The filename of the .whl file depends on your platform. Use tab completion to find your package. If you get an error saying package is not supported for the current platform, run pip explicity (as pip2 for Python 2.7). 348 | 349 | sudo pip3 install /tmp/tensorflow_pkg/tensorflow (*.whl) 350 | 351 | You can make a backup of this built .whl file. 352 | 353 | cp /tmp/tensorflow_pkg/tensorflow (*.whl) 354 | 355 | Verify that TensorFlow is using the GPU for computation by running the following python script. 356 | 357 | **NOTE: Running a script from the */tensorflow* root directory might show some errors. Change to any other directory and run the following python script.** 358 | 359 | import tensorflow as tf 360 | with tf.device('/gpu:0'): 361 | a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a') 362 | b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b') 363 | c = tf.matmul(a, b) 364 | 365 | with tf.Session() as sess: 366 | print (sess.run(c)) 367 | 368 | Here, 369 | 370 | - "*/cpu:0*": The CPU of your machine. 371 | - "*/gpu:0*": The GPU of your machine, if you have one. 372 | 373 | If you have a gpu and can use it, you will see the result. Otherwise you will see an error with a long stacktrace. 374 | 375 | ### Keras 376 | 377 | sudo pip3 install keras 378 | 379 | ### Theano 380 | 381 | sudo pip3 install Theano 382 | 383 | ### Darknet for YOLO 384 | 385 | First clone the Darknet git repository. 386 | 387 | git clone https://github.com/pjreddie/darknet.git 388 | 389 | Now, to compile Darknet with CUDA, CuDNN and OpenCV support, open the `Makefile` from the `darknet` folder and make the changes as following in the beginning of this file. Also make sure to select the right architecture based on your GPU's compute capibility. For Pascal architecture you may want to use [this version of Darknet by AlexeyAB](https://github.com/AlexeyAB/darknet) and compile with the `CUDNN_HALF=1` flag for 3x speed improvement. 390 | 391 | GPU=1 392 | CUDNN=1 393 | OPENCV=1 394 | 395 | Once done, just run make from the darknet folder. 396 | 397 | cd darknet 398 | make 399 | 400 | Refer [here](https://pjreddie.com/darknet/yolo/) for more details on running YOLO and training the network. 401 | -------------------------------------------------------------------------------- /images/img1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heethesh/Computer-Vision-and-Deep-Learning-Setup/4f2711db89ac9c2c845b774504bf9ee68d735ccc/images/img1.png -------------------------------------------------------------------------------- /images/img2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heethesh/Computer-Vision-and-Deep-Learning-Setup/4f2711db89ac9c2c845b774504bf9ee68d735ccc/images/img2.png --------------------------------------------------------------------------------