instruction of install caffe on ubuntu

43
Deep Learning Workshop Pouya Ahmadvand Winter, 2016 [email protected] 1/43

Upload: pouya-ahv

Post on 12-Apr-2017

128 views

Category:

Software


4 download

TRANSCRIPT

Page 1: instruction of install Caffe on ubuntu

Deep LearningWorkshop

Pouya AhmadvandWinter, 2016

[email protected] 1/43

Page 2: instruction of install Caffe on ubuntu

Deep Learning in the Real World!

# Deep Learning has become the most popular approach for developing Artificial Intelligence (AI) – machines that perceive and understand the world.

# The focus is currently on specific perceptual tasks, and there are many successes.

# Today, some of the world`s largest internet companies, as well as foremost research institutions (e.g. Google, Facebook, Microsoft , etc.) are using deep learning in research and production.

2/43Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

http//:www.nvidia.com

Page 3: instruction of install Caffe on ubuntu

Practical Deep Learning Examples

3/43Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

http//:www.nvidia.com

Page 4: instruction of install Caffe on ubuntu

Deep Learning Advantages

# Robust :

• No need to design the features ahead of time – features are automatically learned to be optimal for the task at hand

• Robustness to natural variations in the data is automatically learned

# Generalizable :

• The same neural net approach can be used for many different applications and data types

# Scalable :

• Performance improves with more data, method is massively parallelizable

4/43Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

http//:www.nvidia.com

Page 5: instruction of install Caffe on ubuntu

Deep Learning Approach

5/43

Cat

Dog

Tiger

Dog

Train :

Deploy :

Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

http//:www.nvidia.com

Page 6: instruction of install Caffe on ubuntu

Training CNNs (CPUs VS GPUs)

GPU SpeedUp

Training Time GPU

TrainingTime CPU

Batch Size

8.5X7.5 s64 s64 Images

8.5X14.5 s128 s128 Images

9.0X28.5 s257 s256 Images

AlexNet (5 Coevolution Layers, 2 Fully-connected)Implemented with CaffeTraining time is for 20 iterations CPU: Dual 10-core Ivy Bridge CPUsGPU: 1 Tesla K40 GPU

6/43Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

http//:www.nvidia.com

Page 7: instruction of install Caffe on ubuntu

How GPU Acceleration works

7/43Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

http//:www.nvidia.com

Page 8: instruction of install Caffe on ubuntu

Deep Learning Frameworks

MatconvnetKaldiTorchCaffe

Deep Learning Framework

SpeechRecognition

Toolkit

Scientific Computing Framework

Deep Learning Framework

Domain

-CuDNN

-Multi-GPU

--Multi-CPU

MatlabPythonPython, Matlab, Lua

Command line, Python, Matlab

Interface(s)

8/43Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

http//:www.nvidia.com

Page 9: instruction of install Caffe on ubuntu

Caffe Framework

# Caffe :

• Caffe is a deep learning framework made with expression, speed,and modularity in mind. It is developed by the Berkeley Vision andLearning Center (BVLC) and by community contributors. YangqingJia created the project during his PhD at UC Berkeley. Caffe isreleased under the BSD 2-Clause license.

http://caffe.berkeleyvision.org9/43

Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

Page 10: instruction of install Caffe on ubuntu

Caffe Framework# Why Caffe ?

# Expressive architecture: encourages application and innovation. Models and optimization aredefined by configuration without hard-coding. Switch between CPU and GPU by setting a singleflag to train on a GPU machine then deploy to commodity clusters or mobile devices.

# Extensible code: fosters active development. In Caffe’s first year, it has been forked by over 1,000developers and had many significant changes contributed back. Thanks to these contributors theframework tracks the state-of-the-art in both code and models.

# Speed: makes Caffe perfect for research experiments and industry deployment. Caffe can processover 60M images per day with a single NVIDIA K40 GPU. That’s 1 ms/image for inference and 4ms/image for learning. We believe that Caffe is the fastest convnet implementation available.

# Community: Caffe already powers academic research projects, startup prototypes, and evenlarge-scale industrial applications in vision, speech, and multimedia.

10/43Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

http://caffe.berkeleyvision.org

Page 11: instruction of install Caffe on ubuntu

Ubuntu Installation

# What you need :

• A flash with at least 8GB space

• At least 40GB unallocated space at the end of your hard drive

# What you must to do :

• Make the flash bootable by using Rufus software

• Reboot your system and boot from the bootabled flash

11/43Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

Page 12: instruction of install Caffe on ubuntu

Ubuntu Installation

12/43Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

Page 13: instruction of install Caffe on ubuntu

Ubuntu Installation

13/43Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

Page 14: instruction of install Caffe on ubuntu

Update Ubuntu

sudo apt-get updatesudo apt-get dist-upgradesudo apt-get install build-essentialsudo apt-get install linux-sourcesudo apt-get install linux-headers-genericsudo apt-get dist-upgrade sudo apt-get upgradesudo reboot

# Open a Terminal and type these commands :

14/43Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

Page 15: instruction of install Caffe on ubuntu

CUDA

# CUDA is a parallel computing platform and application programminginterface (API) model created by NVIDIA. It allows software developers to use aCUDA-enabled graphics processing unit (GPU) for general purpose processing –an approach known as GPGPU. The CUDA platform is a software layer that givesdirect access to the GPU's virtual instruction set and parallel computationalelements.

# The CUDA platform is designed to work with programming languages suchas C, C++ and Fortran. When it was first introduced by NVIDIA, the name CUDAwas an acronym for Compute Unified Device Architecture, but NVIDIAsubsequently dropped the use of the acronym.

15/43Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

http//:www.wikipedia.com

Page 16: instruction of install Caffe on ubuntu

CUDA Installation

# You Need A NVIDIA GPU with CUDA Capability

# You can check your GPU CUDA Capability from this link :

• https://en.wikipedia.org/wiki/CUDA

# Open a Terminal and type these commands :

sudo add-apt-repository -r ppa:bumblebee/stablesudo add-apt-repository ppa:graphics-drivers/ppasudo apt-get updatechmod +x cuda_7.5.18_linux.run

16/43Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

Page 17: instruction of install Caffe on ubuntu

CUDA Installation

# Press Ctrl + Alt + F2 for enter to tty2

# Stop lightdm service by this command

# Run cuda_7.5.18_linux.run by this command :

# Follow install instructions

# Update graphic driver

sudo service lightdm stop

./cuda_7.5.18_linux.run

sudo apt-get install nvidia-361 nvidia-prime -ysudo apt-get install freeglut3 freeglut3-devsudo nvidia-modprobe -ysudo reboot

17/43Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

Page 18: instruction of install Caffe on ubuntu

CUDA Installation

# Add this lines to .bashrc file (~/.bashrc)

# Create “cuda.conf” file in /etc/ld.so.conf.d/

# Copy this lines to the file that you just created

# And finally run this command

export CUDA_HOME=/usr/local/cuda-7.5export LD_LIBRARY_PATH=${CUDA_HOME}/lib64:${CUDA_HOME}/libPATH=${CUDA_HOME}/bin:${PATH}export PATHexport CUDA_ROOT=${CUDA_HOME}/bin

/usr/local/cuda-7.5/lib/usr/local/cuda-7.5/lib64

sudo ldconfig18/43

Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

Page 19: instruction of install Caffe on ubuntu

Break

19/43

Page 20: instruction of install Caffe on ubuntu

OpenCV

# Open-source Computer Vison Library

# It’s an open source library written in C++ for computer vision.

# It was originally designed by Intel (1991).

# 2,500+ algorithms and functions

# Corss-platform, portable API

# Real-time performance

# BSD Licensed (free and open source)

# Some of users

20/43Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

http//:www.opencv.org

Page 21: instruction of install Caffe on ubuntu

OpenCv Environment

21/43Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

http//:www.opencv.org

Page 22: instruction of install Caffe on ubuntu

OpenCV Installation

sudo apt-get install libxine-devsudo apt-get install libxine2-devsudo apt-get install qt-sdk cmake git libopencv-dev build-essential checkinstall cmake pkg-config yasm libjpeg-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev python-dev python-numpy libtbb-dev libqt4-dev libgtk2.0-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils libpng-dev libtiff-dev qt5-default libvtk6-dev zlib1g-dev libwebp-dev libtiff5-dev libopenexr-dev libgdal-dev libx264-dev libxine2-dev libeigen3-dev python-dev python-tk python-numpy python3-dev python3-tk python3-numpy ant default-jdk doxygensudo apt-get -qq remove ffmpeg x264 libx264-devsudo add-apt-repository ppa:mc3man/trusty-mediasudo apt-get updatesudo apt-get install ffmpeg gstreamer0.10-ffmpeg

# To install OpenCV dependencies open a Terminal and type these commands :

22/43Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

Page 23: instruction of install Caffe on ubuntu

OpenCV Installation

OpenCVver="opencv-3.1.0“unzip $OpenCVver -d ~/Developcd ~/Develop/$OpenCVvermkdir releasecd releasecmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..

# Enter to OpenCV Folder and type these commands :

23/43Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

Page 24: instruction of install Caffe on ubuntu

OpenCV Installation

OpenCVver="opencv-3.1.0"cp ippicv_linux_20151201.tgz ~/Develop/$OpenCVver/3rdparty/ippicv/downloads/linux-808b791a6eac9ed78d32a7666804320ecmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..

# Copy ippicv_linux_20151201.tgz to “opencv-3.1.0/3rdparty/ippicv/downloads/linux-808b791a6eac9ed78d32a7666804320e” and type these commands :

24/43Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

Page 25: instruction of install Caffe on ubuntu

OpenCV Installation

OpenCVver="opencv-3.1.0“cd ~/Develop/$OpenCVver/releasemake -j $(nproc)sudo make install -j $(nproc)sudo /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf‘sudo sh -c 'echo "/usr/local/lib">/etc/ld.so.conf.d/opencv.conf‘sudo ldconfigsudo ln -s ~/Develop//$OpenCVver/release/lib/cv2.so /usr/lib/python2.7/dist-packages/cv2.so

# Finally, run these commands :

25/43Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

Page 26: instruction of install Caffe on ubuntu

Matlab Installation

mkdir matlabunzip matlab_2014a.zip -d ./matlab/sudo chmod 777 -R matlabcd matlabsudo ./install -javadir /usr/lib/jvm/java-7-openjdk-amd64/jrecd ..sudo cp ./libmwservices.so /usr/local/MATLAB/R2014a/bin/glnxa64/sudo rm -dr matlabsudo apt-get install matlab-support

# Go to the Matlab folder and type these commands :

26/43Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

Page 27: instruction of install Caffe on ubuntu

CuDNN

# The NVIDIA CUDA Deep Neural Network library (cuDNN) is a GPU-accelerated library of primitives for deep neural networks.

# Deep learning developers and researchers worldwide rely on the highly optimized routines in cuDNN which allow them to focus on designing and training neural network models rather than spending time on low-level performance tuning.

CPU is 16 core Haswell E5-2698 at 2.3 GHz, with 3.6 GHz TurboGPU is NVIDIA Titan X

27/43Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

http//:www.nvidia.com

Page 28: instruction of install Caffe on ubuntu

CuDNN Instalation

tar -zxf cudnn-7.0-linux-x64-v4.0-rc.tgz -C ~/Developcd ~/Develop/cudasudo cp ~/Develop/cuda/lib64/* /usr/local/cuda/lib64/sudo cp ~/Develop/cuda/include/cudnn.h /usr/local/cuda/include/

# Go to CuDNN folder and type these commands :

28/43Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

Page 29: instruction of install Caffe on ubuntu

OpenBLAS

# OpenBLAS is an open source implementation of the BLAS (Basic Linear AlgebraSubprograms) API with many hand-crafted optimizations for specific processor types. It isdeveloped at the Lab of Parallel Software and Computational Science, ISCAS.

# OpenBLAS adds optimized implementations of linear algebra kernels for severalprocessor architectures. It claims to achieve performance comparable to the Intel MKL.

29/43Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

https://github.com/xianyi/OpenBLAS

Page 30: instruction of install Caffe on ubuntu

OpenBLAS Installation

cd ~/Developgit clone https://github.com/xianyi/OpenBLAS.gitcd OpenBLASmake -j $(nproc)make PREFIX=~/Develop/OpenBLAS/ installsudo ln -s ~/Develop/OpenBLAS/libopenblas.so /usr/lib/libopenblas.sosudo ln -s ~/Develop/OpenBLAS/libopenblas.so.0 /usr/lib/libopenblas.so.0

# Go to OpenBLAS folder and type these commands :

30/43Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

Page 31: instruction of install Caffe on ubuntu

Caffe Dependencies

sudo apt-get install build-essential autoconf libtool pkg-config python-opengl python-imaging python-pyrex python-pyside.qtopengl idle-python2.7 qt4-dev-tools qt4-designer libqtgui4 libqtcore4 libqt4-xml libqt4-test libqt4-script libqt4-network libqt4-dbus python-qt4 python-qt4-gl libgle3 python-dev libatlas-base-dev libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compilersudo apt-get install --no-install-recommends libboost-all-devsudo apt-get install python-pipsudo apt-get install python-numpy python-scipy python-matplotlib ipython ipython-notebook python-pandas python-sympy python-nose python-flasksudo pip install setuptools –upgradesudo easy_install green letsudo easy_install gevent

# To install Caffe dependencies open an Terminal and type these commands :

31/43Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

Page 32: instruction of install Caffe on ubuntu

Caffe Installation

cd ~/Developsudo apt-get install gitgit clone https://github.com/BVLC/caffe.gitcd caffecd pythonfor req in $(cat requirements.txt); do sudo pip install $req; doneexport PYTHONPATH=~/Develop/caffe/python:$PYTHONPATHexport caffe_root=~/Develop/caffe/$caffe_rootecho "export PYTHONPATH=~/Develop/caffe/python:$PYTHONPATHexport caffe_root=~/Develop/caffe/$caffe_root" >> ~/.bashrc

# To download Caffe framework open a Terminal and type these commands :

32/43Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

Page 33: instruction of install Caffe on ubuntu

Caffe Installation

5) # USE_CUDNN := 1 -> USE_CUDNN := 121) # OPENCV_VERSION := 3 -> OPENCV_VERSION := 346) BLAS := atlas -> BLAS := open50) # BLAS_INCLUDE := /path/to/your/blas -> BLAS_INCLUDE := ~/Develop/OpenBLAS/include51) # BLAS_LIB := /path/to/your/blas -> BLAS_LIB := ~/Develop/OpenBLAS/lib59) # MATLAB_DIR := /usr/local -> MATLAB_DIR := /usr/local/MATLAB/R2014a

# Rename Makefile.config.example to Makefile.config and change this lines :

# Complie Caffe and download AlexNet :

cd ~/Develop/caffemake everything -j $(nproc)./scripts/download_model_binary.py models/bvlc_reference_caffenet./data/ilsvrc12/get_ilsvrc_aux.sh

33/43Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

Page 34: instruction of install Caffe on ubuntu

Break

34/43

Page 35: instruction of install Caffe on ubuntu

Design and Train your Network

35/43

Page 36: instruction of install Caffe on ubuntu

Step :Collect your Data

36/43

Iranshahr Dataset# Collect your Dataset and create training and validation files :

• train.txt and val.txt

# Labels file :

Filepath ClasslableClass_1_sample_1.jpg 0Class_1_sample_2.jpg 0Class_2_sample_1.jpg 1Class_2_sample_2.jpg 1

LableNameRashtZahedan

Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

1

Page 37: instruction of install Caffe on ubuntu

Step :Create LMDB Database

2# Lightning Memory-Mapped Database (LMDB) is a software library that provides a high-

performance embedded transactional database in the form of a key-value store.

# A script for creating LMDB database is located at : CAFFE_ROOT/examples/imagenet

# Set these variables and run the script (create_imagenet.sh) :

# After running this script, two folder will be created in the EXAMPLE path

# You can make your data mean by running make_imagenet_mean.sh

37/43

EXAMPLE= export pathDATA= where the train.txt and val.txt located TOOLS= Caffe tools folder pathTRAIN_DATA_ROOT= where the train data locatedVAL_DATA_ROOT= where the val data located

Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

Page 38: instruction of install Caffe on ubuntu

Step :

Define/Modify Network

# Define/Modify your model by using Protobuf model format

• Strongly typed format

• Human readable

• Auto-generates and checks Caffe code

• Developed by Google

• Used to define network architecture and training parameters

• No coding required!

38/43

name: "CaffeNet“..layer {name: "pool1“type: "Pooling" bottom: "conv1" top: "pool1" pooling_param {pool: MAXkernel_size: 3stride: 2

}}..

Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

3

Page 39: instruction of install Caffe on ubuntu

Step :

Define/Modify Network

39/43

layer {name: "data"type: "Data"...data_param {source: "Iranshahr_train_lmdb/"...

}layer {name: "data"type: "Data"...data_param {source: "Iranshahr_val_lmdb/"...

}...layer {name: "Iranshahr_fc8"type: "InnerProduct"...inner_product_param {num_output: 26...

}}

layer {name: "data"type: "Data"...data_param {source: “ImageNet_train_lmdb/"...

}layer {name: "data"type: "Data"...data_param {source: " ImageNet_train_lmdb/"...

}...layer {name: "Iranshahr_fc8"type: "InnerProduct"...inner_product_param {num_output: 1000...

}}

Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

4

Page 40: instruction of install Caffe on ubuntu

Step :

Set Training Parameters

# Set your training parameters in the Solver file

40/43

net: “Iranshahr_train_val.prototxt”test_iter: 1000

test_interval: 1000

base_lr: 0.001

lr_policy: “step”gamma: 0.1

stepsize: 1000

display: 20

max_iter: 3000

momentum: 0.9

weight_decay: 0.0005

snapshot: 1000

snapshot_prefix: “Iranshahr_train”solver_mode: GPU

Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

5

Page 41: instruction of install Caffe on ubuntu

Step :

Train the network

# Run Train.py for training the network :

41/43

solverPath = './solver.prototxt‘wightsPath = './bvlc_reference_caffenet.caffemodel‘niter = 1000caffe.set_device(0)caffe.set_mode_gpu()solver = caffe.SGDSolver(solverPath)solver.net.copy_from(wightsPath)for it in range(niter):

solver.step(1) print 'iter %d, finetune_loss=%f' % (it, solver.net.blobs['loss'].data)

print 'done'

Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

6

Page 42: instruction of install Caffe on ubuntu

Step :

Test the network

# Run Test.py for testing the network :

42/43

….net = caffe.Classifier(MODEL_FILE, PRETRAINED,

mean=np.load(PROJECT_PATH + 'ilsvrc_2012_mean.npy').mean(1).mean(1),channel_swap=(2,1,0),raw_scale=255,image_dims=(256, 256))

caffe.set_mode_gpu()…input_image = caffe.io.load_image(TEST_FOLDER+files[i])prediction = net.predict([input_image])print 'class :', classes[i],' predicted class:', prediction[0].argmax()

Pouya Ahmadvand, Deep Learning Workshop ,Shahid Rajaei Teacher Training University, 5 March 2016

7

Page 43: instruction of install Caffe on ubuntu

Thank You

43/43