[PYTHON] Set up AWS (Ubuntu 14.04) for Deep Learning (install CUDA, cuDNN)

1. Prepare a machine for Deep Learning on AWS

A Linux machine with a GPU is essential for deep learning.

However, it takes time and money to prepare GPU machines by yourself, so Let's easily prepare a high-performance Linux machine with AWS (Amazon Web Service).

1.1. Instance requirements

For the instance (computer on AWS), adopt the following conditions.

In AMI, you can select Amazon Linux etc. in addition to Ubuntu, It is recommended to use normal Ubuntu because troubles frequently occur related to packages.

1.2. Security group settings

By default it only allows SSH connections, so I can't access even if I set up a server on AWS. Ping does not fly either.

Configure the security group to access TCP port 8888.

1.3. Elastic IP settings

AWS requires you to stop the instance for a reboot, The public IP changes every time I stop the instance.

Therefore, in order to fix the public IP, register the Elastic IP and assign it to the instance.

1.4. SSH connection to the instance

Download the SSH client software Tera Term.

Connect to the instance's public IP and If you specify the key that can be downloaded when creating an instance with the user name "ubuntu", Put it in the console of your instance.

Now operate as Ubuntu.

2. Make the GPU available

At this point, you have an instance on AWS.

But the instance g2.2xlarge has a GPU, GPU is not enabled by default.

Therefore, install the following to enable Deep Learning on the GPU.

2.1. Driver installation

2.1.1. Package update

First, update your package management system. You can also use apt-get instead of aptitude.

bash


$ sudo aptitude update
$ sudo aptitude -y full-upgrade
$ sudo aptitude install -y build-essential cmake

2.1.2. Stop nouveau

Next, I would like to update the graphics driver. First, stop the default graphics driver (nouveau).

After creating the following configuration file

bash:/etc/modprobe.d/blacklist-nouveau.conf


blacklist nouveau
blacklist lbm-nouveau
options nouveau modeset=0
alias nouveau off
alias lbm-nouveau off

bash:/etc/modprobe.d/nouveau-kms.conf


options nouveau modeset=0

Hit the stop command from bash.

bash


$ sudo update-initramfs -u

Now restart the instance. In AWS, you can not reboot with the reboot command, so Stop and restart the instance from the AWS console.

2.1.3. Installing the graphics driver

Download the driver from NVIDIA official page.

You can wget directly to the instance from the download destination, or you can send the downloaded driver via tera term etc.

To install the driver, use the following command.

bash


$ sudo aptitude install -y linux-image-extra-virtual linux-source linux-headers-`uname -r`
$wget "driver".run
$sudo sh "driver".run -a --disable-nouveau

Now restart the instance again to complete the driver installation.

2.2. Installing CUDA-toolkit

Download the repository for cuda from the CUDA official page.

After enabling the downloaded repository, cuda can be installed from aptitude (apt-get).

bash


$wget "repository".deb
$ sudo dpkg -i "Repository"
$ sudo aptitude update
$ sudo aptitude install -y cuda
$ sudo reboot

2.3. Installation of cuDNN

2.3.1. Download cuDNN

Download cuDNN from the NVIDIA page (https://developer.nvidia.com/cudnn). This download requires account registration, so please create an appropriate account.

Also, since the archive file of cuDNN is not directly fetched by wget, Must be sent to the instance via tera term etc.

bash


$ tar -xvf 『cnDNN』.tgz
$ cd cuda
$ sudo cp include/cudnn.h /usr/local/『cuda-X.X』/include/
$ sudo cp lib64/libcudnn* /usr/local/『cuda-X.X』/lib64
$ sudo ldconfig /usr/local/cuda/lib64

If ldconfig fails, delete the conflicting file.

2.3.2. Setting environment variables

Go back home and rewrite .bashrc.

bash


$ cd ~
$ vim .bashrc

~/.bashrc


export PATH=/usr/local/『cuda-X.X』/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/『cuda-X.X』/lib64:$LD_LIBRARY_PATH

Finally, enable the contents of .bashrc.

bash


$ source .bashrc

2.3.3. Confirmation of successful installation

If CUDA information is displayed by typing the following command, The settings for the GPU are complete.

bash


$ nvcc -V

3. Install Python related

Python is used to implement Deep Learning. To put Python's mathematical calculation modules and development environment (jupyter notebook) in a batch Install a distro called Anaconda.

In addition, Theano of the Deep Learning library and Enables the use of openCV, an image processing library.

3.1. Install Anaconda

bash


$ wget https://3230d63b5fc54e62148e-c95ac804525aac4b6dba79b00b39d1d3.ssl.cf1.rackcdn.com/Anaconda3-2.4.1-Linux-x86_64.sh
$ bash Anaconda3-2.4.1-Linux-x86_64.sh
$ source .bashrc

I used the bash command because sh didn't work.

3.2. Install Theano

bash


$ sudo aptitude install git
$ pip install git+git://github.com/Theano/Theano.git --upgrade --no-deps

If you pull Theano directly with pip without going through git, some older modules may throw an error.

3.3. Install openCV

With Anaconda, you can also install openCV in one line.

bash


$ conda install -c https://conda.binstar.org/menpo opencv3

4. GPU operation check

To check the success or failure of the previous setup

gpu_check.py


from theano import function, config, shared, sandbox
import theano.tensor as T
import numpy
import time

vlen = 10 * 30 * 768  # 10 x #cores x # threads per core
iters = 1000

rng = numpy.random.RandomState(22)
x = shared(numpy.asarray(rng.rand(vlen), config.floatX))
f = function([], T.exp(x))
print(f.maker.fgraph.toposort())
t0 = time.time()
for i in range(iters):
    r = f()
t1 = time.time()
print("Looping %d times took %f seconds" % (iters, t1 - t0))
print("Result is %s" % (r,))
if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):
    print('Used the cpu')
else:
    print('Used the gpu')

Let's execute the above Python script (gpu_check.py) with the following command.

bash


$ THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python gpu_check.py

The result of this execution is

bash


Using gpu device 0: GeForce GTX 580
[GpuElemwise{exp,no_inplace}(<CudaNdarrayType(float32, vector)>), HostFromGpu(GpuElemwise{exp,no_inplace}.0)]
Looping 1000 times took 0.638810873032 seconds
Result is [ 1.23178029  1.61879349  1.52278066 ...,  2.20771813  2.29967761
  1.62323296]
Used the gpu

If it says, you can confirm that Theano is running on the GPU.

5. Setup complete

You have now set up AWS for Deep Learning.

Finally, let's launch the development environment and implement artificial intelligence.

bash


$ jupyter notebook --no-browser --ip="*"

References

Installation of graphic driver driver / CUDA on AWS: http://qiita.com/shinya_ohtani/items/f374ed0dd51737087369

Installation of cuDNN: http://www.computervisionbytecnalia.com/es/2016/06/deep-learning-development-setup-for-ubuntu-16-04-xenial/

cuDNN environment variable settings: http://qiita.com/bohemian916/items/a48e6496b04bbbf09fb3

Anaconda installation: http://morimori2008.web.fc2.com/contents/PCprograming/python/pythonAnaconda.html

Install Theano: https://github.com/fchollet/keras/issues/1888

Install openCV: http://hikuichi.hatenablog.com/entry/2015/12/22/124044

Get Theano on GPU: http://deeplearning.net/software/theano/tutorial/using_gpu.html

Recommended Posts

Set up AWS (Ubuntu 14.04) for Deep Learning (install CUDA, cuDNN)
Install CUDA10.1 + cuDNN7.6.5 + tensorflow-2.3.0 on Ubuntu 18.04
Install the latest Cuda + CuDNN on Ubuntu 18.04 @ Spring 2020
Let's Deep Learning on Windows! (VS2013 + caffe + CUDA7.5 + cudnn5.1)
Set up python and machine learning libraries on Ubuntu
Set up Python 3.4 on Ubuntu
Data set for machine learning
How to set up Ubuntu for Windows Subsystem for Linux 2 (WSL2)
Set Up for Mac (Python)
Deep learning for compound formation?
Install CUDA 8.0 and Chainer on Ubuntu 16.04
Install confluent-kafka for Python on Ubuntu
[AI] Deep Learning for Image Denoising
Until the Deep Learning environment (TensorFlow) using GPU is prepared for Ubuntu 14.04
Make your own PC for deep learning
[Deep learning] Nogizaka face detection ~ For beginners ~
About data expansion processing for deep learning
From nothing on Ubuntu 18.04 to setting up a Deep Learning environment in Tensor
Install NVIDIA-driver (GeForce RTX 2070 SUPER), cuda 10.1, cudnn 7.6 on Ubuntu 18.04.3 LTS + python environment construction