This is a guide on how to install Caffe for Ubuntu 16.04 and above, without GPU support (No CUDA required).
sudo apt-get install libopencv-dev python-opencv opencv-contrib-python
sudo apt-get install libopenblas-dev
sudo apt-get install libatlas-base-dev
sudo apt-get install libboost-all-dev
sudo apt-get install python-pip python-dev build-essential
sudo pip install --upgrade pip
sudo pip install protobuf
sudo pip install scikit-image
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install the python-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
git clone https://github.com/BVLC/caffe
You will now find the caffe
folder in your Home
directory.
We have to make a copy of Makefile.config.example
, which we generally name as, Makefile.config
to which we can make changes based on our system settings.
cd caffe
cp Makefile.config.example Makefile.config
Make sure you are still in the caffe
directory, then use this command to open Makefile.config
gedit Makefile.config
-
On line 8, uncomment
CPU_ONLY := 1
-
On line 25, uncomment
CUSTOM_CXX := g++
-
uncomment
WITH_PYTHON_LAYER := 1
-
On line 50, set
BLAS := open
if you've installedOpenBLAS
, or let it be the defaultBLAS := atlas
if you've installedAtlas
. -
On line 94, change
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
to
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
- On line 95, change
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib
to
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib/x86_64-linux-gnu/hdf5/serial
This should resolve hdf5 errors when running make
Now within the caffe
directory, run the following one after the other
make all
make test
make runtest
These should run smoothly without any errors. If you do however, encounter errors, refer the ones at the end of this post or elsewhere to resolve them.
Make sure to REMOVE BUILD every time you resolve an error, and run those three commands again to rebuild.
This is VERY IMPORTANT, otherwise the errors will persist.
To remove build,
rm -rf ./build*/
Once all three run without errors, while in the caffe
directory, type
make pycaffe
This will build a python wrapper. You will also find a python folder within the caffe folder now.
To use caffe within python, export its path as
export PYTHONPATH=/home/_username_/caffe/python:$PYTHONPATH
Replace _username_
with the your username in the system.
Once you've done this, run the python terminal and import caffe
>>> import caffe
>>>
This, should work. If it throws a 'module not found' error, check if it has been appended in pythonpath properly by typing
>>> import sys
>>> sys.path
['', '/home/nikita/caffe/python', '/home/nikita', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib
/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/nikita/.local/lib/python2.7/site-
packages']
If you see that the /home/_username_/caffe/python
path, isn't there, then do
>>> sys.path.append('/Home/_username_/caffe/python')
Alternatively, just use
caffe/python
depending on whether your export is already under home/username
.
.
/usr/bin/ld: cannot find -lopencv_imgcodecs
/usr/bin/ld: cannot find -lopencv_videoio
collect2: error: ld returned 1 exit status
This usually happens when you've either forgotten to uncomment line 21 when you have OpenCV 3,
or if you haven't checked the version properly.
If you're using OpenCV < 3, this shouldn't be an error at all.
If it still shows on OpenCV >= 3.0, then in the Makefile
( NOT Makefile.config
)
on line 181 (in my case), add libraries such that it looks like
LIBRARIES += glog gflags protobuf leveldb snappy \
lmdb boost_system hdf5_hl hdf5 \
opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs
Also, make sure to rebuild!
/usr/bin/ld: cannot find -lcblas
collect2: ld returned 1 exit status
make: *** [.build_release/lib/libcaffe.so] Error 1
This happens if you've not installed OpenBLAS properly, or if you've set
BLAS := open
when you have only Atlas installed. It may sound silly, but 'to err is human'.
If it persists after rebuild,
install Atlas instead and change to BLAS := atlas