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).
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.
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.
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.
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.
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.
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
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.
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.
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
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.
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
If CUDA information is displayed by typing the following command, The settings for the GPU are complete.
bash
$ nvcc -V
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.
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.
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.
With Anaconda, you can also install openCV in one line.
bash
$ conda install -c https://conda.binstar.org/menpo opencv3
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.
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="*"
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