Referred to link [http://hanzratech.in/2015/07/27/installing-caffe-on-ubuntu.html]
Firstly, let’s make a directory named in
deep-learning the folder
$HOME where we will download all the packages.
cd ~ mkdir deep-learning cd deep-learning
1. Installing the dependencies
To successfully compile Caffe we need to install a few packages first. Execute the below command in the terminal. (These commands will require root access)
NOTE -> Since we will not be using GPUs, we do not require CUDA installation.
# General Dependencies sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler sudo apt-get install --no-install-recommends libboost-all-dev # BLAS -- for better CPU performance sudo apt-get install libatlas-base-dev # Python -- It comes preinstalled on Ubuntu 14.04 # Required if you want to use Python wrappers for Caffe sudo apt-get install the python-dev # Remaining dependencies sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
2. Compile and Install Caffe
The Caffe repository is hosted on GitHub. To clone the repository enter the below command in the terminal.
cd ~/deep-learning git clone http://github.com/BVLC/caffe.git cd caffe
The contains the template for
Makefile.config.example a configuration file. We’ll use it to write the configuration
Makefile.config the file the file
cp Makefile.config.example Makefile.config
Makefile.config, we need to set options if we want to use a CPU or a GPU. Open your favorite editor and uncomment the line
CPU_ONLY := 1(most probably, line number 8) as shown below.
8 CPU_ONLY := 1
After setting the configuration, the next step is to make Caffe or brew Caffe .
make all make test make runtest
If you get an OpenCV
undefined reference error in
make all as shown below follow the instructions below else move to the section 3.
.build_release/lib/libcaffe.so: undefined reference to `cv::imread(cv::String const&, int)' .build_release/lib/libcaffe.so: undefined reference to `cv::imencode(cv::String const&, cv::_InputArray const&, std::vector<unsigned char, std::allocator<unsigned char> >&, std::vector<int, std::allocator<int> > const&)' .build_release/lib/libcaffe.so: undefined reference to `cv::imdecode(cv::_InputArray const&, int)' collect2: error: ld returned 1 exit status make: *** [.build_release/tools/caffe.bin] Error 1
(Check this issue) for more details. In order to fix the errror, append
opencv_imgcodecs on line 176(most probably) as shown below.
174 LIBRARIES += glog gflags protobuf leveldb snappy \ 175 mdb boost_system hdf5_hl hdf5 m \ 176 opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs
Now clear of the contents of
build. This is important else the error will persist.
rm -rf ./build/*
Now rerun the previous commands.
make all make test make runtest
3. Adding PYTHON support
To add PYTHON support, run the below commands in the terminal.
cd ~/deep-learning/caffe make pycaffe echo export PYTHONPATH=~/deep-learning/caffe/python:$PYTHONPATH >> ~/.bashrc
4. Adding Matlab support
To add matlab support, run the below commands in the terminal. Use this only if you need MatCaffe interface for Caffe. Following our idea of simplistic installation, we will skip this part.
Makefile.config file modify this ..
# This is required only if you will compile the matlab interface. # MATLAB directory should contain the mex binary in /bin. MATLAB_DIR := /usr/local/MATLAB/R2016a/ -- add this comment for matlab 2016 # MATLAB_DIR := /Applications/MATLAB_R2012b.app -- for mac
cd ~/deep-learning/caffe make matcaffe
5. Run Caffe
Keep Track of Training and Testing Loss When Using Caffe
When using Caffe to train a deep neural network, how to record the training/testing loss or accuracy, throughout iterations?
I know the following works, though without understanding the details.
Run in terminal:
$ caffe_root/build/tools/caffe train --solver=solver.prototxt 2>&1 | tee mylog.log
Notice the appended part in red, which will log the information shown in the terminal to “mylog.log”
Then run in terminal,
$ python caffe_root/tools/extra/parse_log.py mylog.log ./
Now you will see under ./, there are two files, mylog.log.train, and mylog.log.test. They are two csv files that record training and testing loss.
Then you could run gnuplot to quickly visualize the loss/accuracy during iterations. But first you need to comment the 1st line of the two files by #
The .train (or .test) file is of the following form:
net: "models/train_val.prototxt" # path to the network definition test_iter: 200 # how many mini-batches to test in each validation phase test_interval: 500 # how often do we call the test phase base_lr: 1e-5 # base learning rate lr_policy: "step" # step means to decrease lr after a number of iterations gamma: 0.1 # ratio of decrement in each step stepsize: 5000 # how often do we step (should be called step_interval) display: 20 # how often do we print training loss max_iter: 450000 momentum: 0.9 weight_decay: 0.0005 # regularization! snapshot: 2000 # taking snapshot is like saving your progress in a game snapshot_prefix: "models/model3_train_0422" solver_mode: GPU Ref http://stackoverflow.com/questions/30033096/what-is-lr-policy-in-caffe https://groups.google.com/forum/#!topic/caffe-users/OX8j7Kd8k0M
# The learning rate policy lr_policy: "inv"
Possible values seem to be:
The learning rate decay policy. The currently implemented learning rate // policies are as follows: // - fixed: always return base_lr. // - step: return base_lr * gamma ^ (floor(iter / step)) // - exp: return base_lr * gamma ^ iter // - inv: return base_lr * (1 + gamma * iter) ^ (- power) // - multistep: similar to step but it allows non uniform steps defined by // stepvalue // - poly: the effective learning rate follows a polynomial decay, to be // zero by the max_iter. return base_lr (1 - iter/max_iter) ^ (power) // - sigmoid: the effective learning rate follows a sigmod decay // return base_lr ( 1/(1 + exp(-gamma * (iter - stepsize)))) // // where base_lr, max_iter, gamma, step, stepvalue and power are defined // in the solver parameter protocol buffer, and iter is the current iteration.
|// Return the current learning rate. The currently implemented learning rate|
|// policies are as follows:|
|// – fixed: always return base_lr.|
|// – step: return base_lr * gamma ^ (floor(iter / step))|
|// – exp: return base_lr * gamma ^ iter|
|// – inv: return base_lr * (1 + gamma * iter) ^ (- power)|
|// – multistep: similar to step but it allows non uniform steps defined by|
|// – poly: the effective learning rate follows a polynomial decay, to be|
|// zero by the max_iter. return base_lr (1 – iter/max_iter) ^ (power)|
|// – sigmoid: the effective learning rate follows a sigmod decay|
|// return base_lr ( 1/(1 + exp(-gamma * (iter – stepsize))))|
|// where base_lr, max_iter, gamma, step, stepvalue and power are defined|
|// in the solver parameter protocol buffer, and iter is the current iteration|