Book Image

Deep Learning with Microsoft Cognitive Toolkit Quick Start Guide

By : Willem Meints
Book Image

Deep Learning with Microsoft Cognitive Toolkit Quick Start Guide

By: Willem Meints

Overview of this book

Cognitive Toolkit is a very popular and recently open sourced deep learning toolkit by Microsoft. Cognitive Toolkit is used to train fast and effective deep learning models. This book will be a quick introduction to using Cognitive Toolkit and will teach you how to train and validate different types of neural networks, such as convolutional and recurrent neural networks. This book will help you understand the basics of deep learning. You will learn how to use Microsoft Cognitive Toolkit to build deep learning models and discover what makes this framework unique so that you know when to use it. This book will be a quick, no-nonsense introduction to the library and will teach you how to train different types of neural networks, such as convolutional neural networks, recurrent neural networks, autoencoders, and more, using Cognitive Toolkit. Then we will look at two scenarios in which deep learning can be used to enhance human capabilities. The book will also demonstrate how to evaluate your models' performance to ensure it trains and runs smoothly and gives you the most accurate results. Finally, you will get a short overview of how Cognitive Toolkit fits in to a DevOps environment
Table of Contents (9 chapters)

Installing CNTK

Now that we've seen how neural networks work and what CNTK is, let's take a look at how to install it on your computer. CNTK is supported on both Windows and Linux, so we'll walk through each of them.

Installing on Windows

We will be using the Anaconda version of Python on Windows to run CNTK. Anaconda is a redistribution of Python that includes additional packages, such as SciPy and scikit-learn, which are used by CNTK to perform various calculations.

Installing Anaconda

You can download Anaconda from the public website: https://www.anaconda.com/download/.

After you've downloaded the setup files, start the installation and follow the instructions to install Anaconda on your computer. You can find the installation instructions at https://docs.anaconda.com/anaconda/install/.

Anaconda will install a number of utilities on your computer. It will install a new command prompt that will automatically include all the Anaconda executables in your PATH variable. You can quickly manage your Python environment from this command prompt, install packages and, of course, run Python scripts.

Optionally, you can install Visual Studio Code with your Anaconda installation. Visual Studio Code is a code editor similar to Sublime and Atom and contains a large number of plugins that make it easier to write program code in different programming languages, such as Python.

CNTK 2.6 supports Python 3.6 only, which means that not all distributions of Anaconda will work correctly. You can get an older version of Anaconda through the Anaconda archives at https://repo.continuum.io/archive/. Alternatively, you can downgrade the Python version in your Anaconda installation if you haven't got a version with Python 3.6 included. To install Python 3.6 in your Anaconda environment, open a new Anaconda prompt and execute the following command:

conda install python=3.6

Upgrading pip

Anaconda comes with a slightly outdated version of the python package manager, pip. This can cause problems when we try to install the CNTK package. So, before we install the CNTK package, let's upgrade the pip executable.

To upgrade the pip executable, open the Anaconda prompt and execute the following command:

python -m pip install --upgrade pip

This will remove the old pip executable and install a new version in its place.

Installing CNTK

There's a number of ways to get the CNTK package on your computer. The most common way is to install the package through the pip executable:

pip install cntk

This will download the CNTK package from the package manager website and install it on your machine. pip will automatically check for missing dependencies and install those as well.

There are several alternative methods to install CNTK on your machine. The website has a neat set of documentation that explains the other installation methods in great detail: https://docs.microsoft.com/en-us/cognitive-toolkit/Setup-CNTK-on-your-machine.

Installing on Linux

Installing CNTK on Linux is slightly different than installing it on Windows. Just as on Windows, we will use Anaconda to run the CNTK package. But instead of a graphical installer for Anaconda, there's a terminal-based installer on Linux. The installer will work on most Linux distributions. We limited the description to Ubuntu, a widely-used Linux distribution.

Installing Anaconda

Before we can install Anaconda, we need to make sure that the system is fully up to date. To check this, execute the following two commands inside a terminal:

sudo apt update 
sudo apt upgrade

Automatically Programmed Tool (APT) is used to install all sorts of packages inside Ubuntu. In the code sample, we first ask apt to update the references to the various package repositories. We then ask it to install the latest updates.

After the computer is updated, we can start the installation of Anaconda. First, navigate to https://www.anaconda.com/download/ to get the URL for the latest Anaconda installation files. You can right-click on the download link and copy the URL to your clipboard.

Now open up a terminal window and execute the following command:

wget -O anaconda-installer.sh url

Make sure to replace the url placeholder with the URL you copied from the Anaconda website. Press Enter to execute the command.

Once the installation file is download, you can install Anaconda by running the following command:

sh ./anaconda-installer.sh

This will start the installer. Follow the instructions on the screen to install Anaconda on your computer. By default, Anaconda gets installed in a folder called anaconda3 inside your home directory.

As is the case with the Windows version of CNTK 2.6, it only supports Python 3.6. You can either get an older distribution of Anaconda through their archives at https://repo.continuum.io/archive/, or downgrade your Python version by executing the following command in your terminal:

conda install python=3.6

Upgrading pip to the latest version

Once we have Anaconda installed, we need to upgrade pip to the latest version. pip is used to install packages inside Python. It is the tool we're going to use to install CNTK:

python -m pip install --upgrade pip

Installing the CNTK package

The final step in the installation process is to install CNTK. This is done through pip using the following command:

pip install cntk

Should you want to, you can also install CNTK by downloading a wheel file directly or using an installer with Anaconda included. You can find more information on alternative installation methods for CNTK at https://docs.microsoft.com/en-us/cognitive-toolkit/Setup-CNTK-on-your-machine.

Using your GPU with CNTK

We looked at how to install the basic version of CNTK for use with your CPU. While the CNTK package is fast, it will run quicker on a GPU. But not all machines support this setup, and that's why I put the description of how to use your GPU into a separate section.

Before you attempt to install CNTK for use with a GPU, make sure you have a supported graphics card. Currently, CNTK supports the NVIDIA graphics card with at least CUDA 3.0 support. CUDA is the programming API from NVIDIA that allows developers to run non-graphical programs on their graphics cards. You can check whether your graphics card supports CUDA on this website: https://developer.nvidia.com/cuda-gpus.

Enabling GPU usage on Windows

To use your graphics card with CNTK on Windows, you need to have the latest GeForce or Quadro drivers for your graphics card (depending on which one you have). Aside from the latest drivers, you need to install the CUDA toolkit Version 9.0 for Windows.

You can download the CUDA toolkit from the NVIDIA website: https://developer.nvidia.com/cuda-90-download-archive?target_os=Windows&target_arch=x86_64. Once downloaded, run the installer and follow the instructions on the screen.

CNTK uses a layer on top of CUDA, called cuDNN, for neural-network-specific primitives. You can download the cuDNN binaries from the NVIDIA website at https://developer.nvidia.com/rdp/form/cudnn-download-survey. In contrast to the CUDA toolkit, you need to register an account to the website before you can download the cuDNN binaries.

Not all cuDNN binaries work with every version of CUDA. The website mentions which version of cuDNN is compatible with which version of the CUDA toolkit. For CUDA 9.0, you need to download cuDNN 7.4.1.

Once you have downloaded the cuDNN binaries, extract the zip file into the root folder of your CUDA toolkit installation. Typically, the CUDA toolkit is located at C:\program files\NVIDIA GPU Computing Toolkit\CUDA\v9.0.

The final step to enable GPU usage inside CNTK is to install the CNTK-GPU package. Open the Anaconda prompt in Windows and execute the following command:

pip install cntk-gpu

Enabling GPU usage on Linux

Using your graphics card with CNTK on Linux requires that you run the proprietary drivers for NVIDIA. When you install the CUDA toolkit on your Linux machine, you get asked to install the latest drivers for your graphics card automatically. While you are not required to install the drivers through the CUDA toolkit installer, we strongly recommend you do, as the drivers will match the binaries of the CUDA toolkit. This reduces the risk of a failing installation or other errors later on.

You can download the CUDA toolkit from the NVIDIA website: https://developer.nvidia.com/cuda-90-download-archive?target_os=Linux&target_arch=x86_64&target_distro=Ubuntu&target_version=1604&target_type=runfilelocal.

Please make sure you select the appropriate Linux distribution and version. The link automatically selects Ubuntu 16.04 and uses a local runfile.

Once you've downloaded the binaries to disk, you can run the installer by opening a terminal and executing the following command:

sh cuda_9.0.176_384.81_linux-run

Follow the onscreen instructions to install the CUDA toolkit on your machine.

Once you have the CUDA toolkit installed, you need to modify your Bash profile script. Open the $HOME/.bashrc file in your favorite text editor and include the following lines at the end of the script:

export PATH=/usr/local/cuda-9.0/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-9.0/lib64\
${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

The first line includes the CUDA binaries in the PATH variable so CNTK can access them. The second line in the script includes the CNTK libraries in your library PATH so CNTK can load them when needed.

Save the changes to the file and close the editor. Please make sure you restart your terminal window to ensure that the new settings are loaded.

The final step is to download and install the cuDNN binaries. CNTK uses a layer on top of CUDA, called cuDNN, for neural-network-specific primitives. You can download the cuDNN binaries from the NVIDIA website here: https://developer.nvidia.com/rdp/form/cudnn-download-survey. In contrast to the CUDA toolkit, you need to register an account on the website before you can download the cuDNN binaries.

Not all cuDNN binaries work with every version of CUDA. The website mentions which version of cuDNN is compatible with which version of the CUDA toolkit. For CUDA 9.0, you need to download cuDNN 7.4.1. Download the version for Linux and extract it to the /usr/local/cuda-9.0 folder using the following command:

tar xvzf -C /usr/local/cuda-9.0/ cudnn-9.0-linux-x64-v7.4.1.5.tgz

The filename may differ slightly; change the path to the filename as needed.