Install Caffe (CPU only) with Python3 on Ubuntu __python

Source: Internet
Author: User
Tags stdin git clone continuum analytics install matplotlib intel mkl
install Caffe with Python3 on Ubuntu (CPU only)


Now the online installation Caffe version is basically based on Python 2.7, this tutorial is designed to use Anaconda 3 to install the Caffe CPU, the process is very difficult, continuous three days to finally have a complete tutorial.



My system environment: Ubuntu 16.04, Anaconda 3.5.2 (that is, Python 3.5.2), low-end notebooks, installing the Caffe CPU, installed OPENCV before this 3.2.0



Part I: Install dependencies 

sudo apt-get install Libprotobuf-dev libleveldb-dev Libsnappy-dev Libhdf5-serial-dev protobuf-compiler sudo apt-get install--no-install-recommends libboost-all-dev sudo apt-get install Libatlas-base-dev sudo apt-get install Libhdf5-serial-dev






sudo apt-get install Libgflags-dev libgoogle-glog-dev Liblmdb-dev



Then download the source code of Caffe: Git clone https://github.com/BVLC/caffe.git (without installing Git, of course) Download complete, enter the Caffe folder, into the inside of the Python folder, Then enter for the req in $ (cat requirements.txt); do pip install $req; Done (PIP should be installed first if not installed: sudo apt install python-pip)






Part II: Install Caffe 


to Caffe folder, write a makefile.config using templates. The specific thing is to copy the template first, and then change some content (I like using Emacs). CP Makefile.config.example Makefile.config-because CPU MODE, so in cpu_only: = 1 before the # to be removed. -Two paths to change to this: (Add the following two hdf5 path, otherwise compile times HDF5 error) # Whatever else you'll find your need goes here. Include_dirs: = $ (python_include)/usr/local/include /usr/include/hdf5/serial library_dirs: = $ (PYTHON_LIB)/ Usr/local/lib/usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial






You ready. Make Pycaffe-j 4
In addition, this make is by default CPU single core operation, if want to hurry up, for example I want to use four cores, add-j4 tag after make. If you want to try again after an error in line 4 on a row above, it is recommended to make clean before starting again.






Part III: Set up Python caffe 


to the Python folder inside the Caffe folder and record the current path (PWD). Then enter the following command (place the noted path where appropriate) export Pythonpath=/path/to/caffe/python: $PYTHONPATH









Part IV: Also the most important part--sharing the process and solution of my error resolution



1, error: According to the above tutorial has not changed, always said that can not find Python.h



Solution: Online Check the version of the argument is not resolved, after a careful thought, must be the system can not find their own Python path, I found Python.h in my Anaconda file, but the path to the Anaconda environment in the configuration file Makefile.config is not "anaconda_home: = $ (Home)/anaconda", So the system could not find Python.h at all. So I immediately changed the Anaconda_home, changed to the location of the file when I loaded anaconda, after the revised screenshot as follows:





# Anaconda Python distribution is quite popular. Include Path:
# Verify Anaconda location, sometimes it ' s in root.
# Anaconda_home: = $ (Home)/anaconda
Anaconda_home: = $ (Home)/downloads/yes

# Python_include: = $ (anaconda_home)/include \
# $ (anaconda_home)/include/python2.7 \
# $ (anaconda_home)/lib/python2.7/site-packages/numpy/core/include

# Uncomment to use Python 3 (default is Python 2)
Python_libraries: = Boost_python3 python3.5m
# python_include: =/usr/include/python3.5m \
#/usr/lib/python3.5/dist-packages/numpy/core/include

Python_include: = $ (anaconda_home)/include \
$ (anaconda_home)/include/python3.5m \
$ (anaconda_home)/lib/python3.5/site-packages/numpy/core/include

# We need to is able to find libpythonx.x.so or. dylib.
# Python_lib: =/usr/lib
Python_lib: = $ (anaconda_home)/lib





2. Error: Make Pycaffe can be passed, but error made all, I did not record the specific error, but seems to be because the system installed a number of OPENCV reasons, anyway, at least in the Python environment import Caffe, But import made another mistake:



Python 3.5.2 | Continuum Analytics, inc.| (Default, June 2 2016, 17:53:06)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on Linux
Type ' help ', ' copyright ', ' credits ' or ' license ' for the more information.
>>> Import Caffe
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/.../downloads/caffe-master/python/caffe/__init__.py", line 1, in <module>
From. Pycaffe import Net, Sgdsolver, Nesterovsolver, Adagradsolver, Rmspropsolver, Adadeltasolver, Adamsolver, NCCL, Timer
File "/home/.../downloads/caffe-master/python/caffe/pycaffe.py", line, in <module>
From. _caffe import Net, Sgdsolver, Nesterovsolver, Adagradsolver, \
Importerror:/home/.../downloads/caffe-master/python/caffe/. /.. /build/lib/libcaffe.so.1.0.0-rc5:undefined symbol: _zn2cv6imreaderkns_6stringei



Error occurred while make:



. 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& <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
Makefile:625:recipe for Target '. BUILD_RELEASE/TOOLS/UPGRADE_NET_PR Oto_text.bin ' failed
Make: * * * [. build_release/tools/upgrade_net_proto_text.bin] Error 1
Make: * * * waiting for UN Finished jobs ....



The solution: Yes, I did, and I've installed OpenCV under Python 3.5 and Python 2.7, and I've got a libopencv-dev in front of the tutorial, so I suspect this But I tried to uninstall OpenCV or import the same error, and finally to find solutions, but also tried their various solutions, do not know how to make a clean and then made all the number of times, or does not work. Finally in the website https://github.com/BVLC/caffe/issues/2348#issuecomment-95156848 see someone say "uncomment if you ' re using OpenCV 3" I immediately thought of the configuration file, so I removed "opencv_ VERSION: = 3" In the configuration file, I made clean, then make all, the miracle finally appeared, the above error disappeared, incredibly made all successful, haha, too happy ...






The above two problems took the longest time, spent more than two days, the following problems online solutions are correct, spend less time.






3, Error: Can now import Caffe, but the following error occurred:


Python 3.5.2 | Continuum Analytics, inc.| (Default, June 2 2016, 17:53:06)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on Linux
Type ' help ', ' copyright ', ' credits ' or ' license ' for the more information.
>>> Import Caffe
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/.../downloads/caffe-master/python/caffe/__init__.py", line 1, in <module>
From. Pycaffe import Net, Sgdsolver, Nesterovsolver, Adagradsolver, Rmspropsolver, Adadeltasolver, Adamsolver, NCCL, Timer
File "/home/.../downloads/caffe-master/python/caffe/pycaffe.py", line, in <module>
Import Caffe.io
File "/home/..../downloads/caffe-master/python/caffe/io.py", line 2, in <module>
Import Skimage.io
File "/home/.../downloads/yes/lib/python3.5/site-packages/skimage/io/__init__.py", line, in <module>
Reset_plugins ()
File "/home/.../downloads/yes/lib/python3.5/site-packages/skimage/io/manage_plugins.py", line, in Reset_plugins
_load_preferred_plugins ()
File "/home/.../downloads/yes/lib/python3.5/site-packages/skimage/io/manage_plugins.py", line, in _load_ Preferred_plugins
_set_plugin (P_type, preferred_plugins[' all '])
File "/home/.../downloads/yes/lib/python3.5/site-packages/skimage/io/manage_plugins.py", line, in _set_plugin
Use_plugin (plugin, kind=plugin_type)
File "/home/.../downloads/yes/lib/python3.5/site-packages/skimage/io/manage_plugins.py", line 255, in Use_plugin
_load (name)
File "/home/.../downloads/yes/lib/python3.5/site-packages/skimage/io/manage_plugins.py", line 299, in _load
Fromlist=[modname])
File "/home/.../downloads/yes/lib/python3.5/site-packages/skimage/io/_plugins/matplotlib_plugin.py", line 3, in <module>
Import Matplotlib.pyplot as Plt
File "/home/.../downloads/yes/lib/python3.5/site-packages/matplotlib/pyplot.py", line <module>
From matplotlib.figure import figure, Figaspect
File "/home/.../downloads/yes/lib/python3.5/site-packages/matplotlib/figure.py", line, in <module>
From matplotlib.axes import axes, subplotbase, subplot_class_factory
File "/home/..../downloads/yes/lib/python3.5/site-packages/matplotlib/axes/__init__.py", line 4, in <module>
From. _subplots Import *
File "/home/.../downloads/yes/lib/python3.5/site-packages/matplotlib/axes/_subplots.py", line, in <module>
From matplotlib.axes._axes import axes
File "/home/.../downloads/yes/lib/python3.5/site-packages/matplotlib/axes/_axes.py", line, in <module>
Import Matplotlib.dates As _ # <-registers a date unit converter
File "/home/.../downloads/yes/lib/python3.5/site-packages/matplotlib/dates.py", line 126, in <module>
From Dateutil.rrule import (Rrule, MO, TU, WE, TH, FR, SA, SU, yearly, monthly, WEEKLY, daily, hourly, minutely, secondly)
File "/home/.../downloads/yes/lib/python3.5/site-packages/dateutil/rrule.py", line 55
Raise ValueError, "Can ' t create weekday with n = 0"
^
Syntaxerror:invalid syntax

Error Resolution: From the above can be seen in the import Matplotlib.pyplot as PLT error, Baidu the following, tried several solutions, originally is Matplotlib version is too low, need to upgrade, so I pip install Matplotlib--upgrade The following, sure enough, but there are the following problems.








3. Error: Intel MKL FATAL error:cannot load libmkl_avx.so or libmkl_def.so.



Python 3.5.2 | Continuum Analytics, inc.| (Default, June 2 2016, 17:53:06)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on Linux
Type ' help ', ' copyright ', ' credits ' or ' license ' for the more information.
>>> Import Caffe
Intel MKL FATAL error:cannot load libmkl_avx.so or libmkl_def.so.

Solution: Continue to Baidu under, Haha, originally because NumPy version is too low, so I conda update numpy, haha and successful. Therefore, my anaconda inside the bag must be upgraded Ah, inside the bag is still installed anaconda when the system itself, too old.






















haha, I can now loudly say that I can finally successfully use Caffe under Ubuntu, screenshots for proof ...









Reference website:



Http://www.voidcn.com/blog/u012675539/article/p-5962112.html



Http://www.linuxidc.com/Linux/2016-09/135034.htm



Http://caffe.berkeleyvision.org/installation.html#compilation



https://github.com/BVLC/caffe/issues/2348#issuecomment-95156848



Http://stackoverflow.com/questions/36659453/intel-mkl-fatal-error-cannot-load-libmkl-avx2-so-or-libmkl-def-so



Http://blog.csdn.net/10km/article/details/65936155?locationNum=14&fps=1


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.