Install shogun with python modular (OS X Yosemite)

As a memorandum, I had a hard time dealing with shogun, a library that supports machine learning in various languages, with python, so I will write a solution.

reference

official site of shogun shogun's Github page

The official website is quite hard to see & information on past versions is mixed and I can not understand various things, so I think it is better to look at the wiki on the Github page.

0. Preparation

Before installing shogun, install what you need. What you need to pay attention to here is the SWIG version. SWIG

As of 06/05/2015, the latest version is 3.0.5, but apparently if you install shogun using this, a Segmentation Fault will occur when using it ([here](https://github.com/shogun] -toolbox / shogun / issues / 2313) or here There is a notation that seems to be solved, but in my environment Segfault appeared) Therefore, install the SWIG version 2.x in advance and downgrade it.

・ If you are using Homebrew

First of all, so that you can install the past version of SWIG

$brew tap homebrew/versions

To execute. This will allow you to see past versions of the formula when you run a brew search.

$brew search
homebrew/versions/swig2	    homebrew/versions/swig304	swig
$brew install homebrew/versions/swig2

... #Various are displayed

$brew link swig2 
#If you are already using swig, brew link--overwrite overwrite with swig2

This completes the preparation around SWIG.

1. Download shogun

It seems that it can be done with Homebrew and MacPorts, but with Homebrew, the python sample is not installed & Segmentation Fault occurs and it is not usable, so I bring it directly from Git. Officially there is an example on MacPorts, but I have not tried it, so please let me know who has tried it.

$git clone https://github.com/shogun-toolbox/shogun.git 
$cd shogun
$mkdir build

Create a directory called build in shogn according to the formula.

2. Installation of shogun

I'm doing cmake from here, but there are a lot of options, and I couldn't find the page that specifically describes it, so it's better to use CMake's GUI tool. Please download CMake.

Start CMake and make the Where is the source code and Where to build the binaries part as shown in the image. (The following is an example of cloning shogun on the desktop) スクリーンショット 2015-06-05 15.18.52.png

This will bring up various options, check what you need and run CMake.

Then start the terminal and

$cd /Users/YOUR_NAME/Desktop/shogun/build
$make && make install

To execute.

$sudo find /usr -name "shogun" -type d

/usr/local/include/shogun
/usr/local/lib/python2.7/site-packages/shogun
/usr/local/share/shogun

If, the installation is successful. After that, follow the official website

$ export LD_LIBRARY_PATH=SHOGUN_INSTALL_DIR/lib
$ export PYTHONPATH=SHOGUN_INSTALL_DIR/lib/pythonX.Y/dist-packages/

Please enter. I think it depends on the installation location and OS, but in my case (and most of the people who installed it with this procedure)

$ export LD_LIBRARY_PATH=/usr/local/lib
$ export PYTHONPATH=/usr/local/lib/python2.7/site-packages/

It will be.

3. Change the path of the library that _modshogun.so depends on

In this state, if you still try to load shogun from python, a Segmentation Fault will occur, so change the path of the library referenced by _modshogun.so.

$sudo find /usr -name "*modshogun*"
/usr/local/lib/python2.7/site-packages/_modshogun.so
/usr/local/lib/python2.7/site-packages/modshogun.py
/usr/local/lib/python2.7/site-packages/modshogun.pyc

It's the top one. modshogun is generated by SWIG during the installation of shogun, and I'm not sure about the details, but it feels like an abbreviation for modular shogun. It seems that various classes can be dynamically loaded and called. I don't know the details.

Let's take a look at the libraries that this _modshogun depends on.

$cd /usr/local/lib/python2.7/site-packages/
$otool -L _modshogun.so
_modshogun.so:
	libshogun.17.dylib (compatibility version 17.0.0, current version 17.1.0)
	/System/Library/Frameworks/Python.framework/Versions/2.7/Python (compatibility version 2.7.0, current version 2.7.6)
	/System/Library/Frameworks/Accelerate.framework/Versions/A/Accelerate (compatibility version 1.0.0, current version 4.0.0)
	/usr/local/lib/libglpk.35.dylib (compatibility version 37.0.0, current version 37.0.0)
	/usr/local/opt/arpack/libexec/lib/libarpack.2.dylib (compatibility version 3.0.0, current version 3.0.0)
	/usr/local/lib/libopencv_videostab.2.4.dylib (compatibility version 2.4.0, current version 2.4.11)
	/usr/local/lib/libtbb.dylib (compatibility version 0.0.0, current version 0.0.0)
	/usr/local/lib/libopencv_superres.2.4.dylib (compatibility version 2.4.0, current version 2.4.11)
	/usr/local/lib/libopencv_stitching.2.4.dylib (compatibility version 2.4.0, current version 2.4.11)
	/usr/local/lib/libopencv_contrib.2.4.dylib (compatibility version 2.4.0, current version 2.4.11)
	/usr/local/lib/libopencv_nonfree.2.4.dylib (compatibility version 2.4.0, current version 2.4.11)
	/usr/local/lib/libopencv_ocl.2.4.dylib (compatibility version 2.4.0, current version 2.4.11)
	/usr/local/lib/libopencv_gpu.2.4.dylib (compatibility version 2.4.0, current version 2.4.11)
	/usr/local/lib/libopencv_photo.2.4.dylib (compatibility version 2.4.0, current version 2.4.11)
	/usr/local/lib/libopencv_objdetect.2.4.dylib (compatibility version 2.4.0, current version 2.4.11)
	/usr/local/lib/libopencv_legacy.2.4.dylib (compatibility version 2.4.0, current version 2.4.11)
	/usr/local/lib/libopencv_video.2.4.dylib (compatibility version 2.4.0, current version 2.4.11)
	/usr/local/lib/libopencv_ml.2.4.dylib (compatibility version 2.4.0, current version 2.4.11)
	/usr/local/lib/libopencv_calib3d.2.4.dylib (compatibility version 2.4.0, current version 2.4.11)
	/usr/local/lib/libopencv_features2d.2.4.dylib (compatibility version 2.4.0, current version 2.4.11)
	/usr/local/lib/libopencv_highgui.2.4.dylib (compatibility version 2.4.0, current version 2.4.11)
	/usr/local/lib/libopencv_imgproc.2.4.dylib (compatibility version 2.4.0, current version 2.4.11)
	/usr/local/lib/libopencv_flann.2.4.dylib (compatibility version 2.4.0, current version 2.4.11)
	/usr/local/lib/libopencv_core.2.4.dylib (compatibility version 2.4.0, current version 2.4.11)
	/usr/local/lib/libnlopt_cxx.0.dylib (compatibility version 9.0.0, current version 9.2.0)
	/usr/local/lib/libColPack.0.dylib (compatibility version 1.0.0, current version 1.0.0)
	/usr/local/lib/libjson-c.2.dylib (compatibility version 3.0.0, current version 3.1.0)
	/usr/local/opt/libxml2/lib/libxml2.2.dylib (compatibility version 12.0.0, current version 12.2.0)
	/usr/local/lib/libhdf5.9.dylib (compatibility version 10.0.0, current version 10.0.0)
	/usr/local/lib/libsz.2.dylib (compatibility version 3.0.0, current version 3.0.0)
	/usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.5)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1213.0.0)
	/usr/lib/libcurl.4.dylib (compatibility version 7.0.0, current version 8.0.0)
	/usr/lib/libbz2.1.0.dylib (compatibility version 1.0.0, current version 1.0.5)
	/usr/local/lib/liblzma.5.dylib (compatibility version 8.0.0, current version 8.1.0)
	/usr/local/lib/libsnappy.1.dylib (compatibility version 4.0.0, current version 4.1.0)
	/usr/local/lib/liblzo2.2.dylib (compatibility version 3.0.0, current version 3.0.0)
	/usr/local/lib/libprotobuf.9.dylib (compatibility version 10.0.0, current version 10.1.0)
	/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.0.0)

It comes out in a row like this. In my case, I installed it with python-modular and OpenCV options, so it looks like this. I think this part will change depending on the person.

At the top of this

 /System/Library/Frameworks/Python.framework/Versions/2.7/Python

Seems to be python installed on OSX from the beginning, and there seem to be various reasons, but since it is said that using this is the cause of Segmentation Fault, I will change this.

You can change the path of the dependent libraries of _modshogun.so using something called install_name_tool.

$install_name_tool -change Path you want to change Path to change Library name

With that feeling

$install_name_tool -change /System/Library/Frameworks/Python.framework/Versions/2.7/Python /usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib _modshogun.so

I wrote like this. It's the path to change, but I made it by passing it through the shared library of the version of python that came out when shogun was installed. I don't know if this is correct, so please let me know who you are.

4. Try moving the sample

Now try starting python once.

$python
Python 2.7.10 (default, Jun  3 2015, 16:47:53) 
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import modshogun
>>> import shogun
>>> 

The Segmentation Fault has disappeared. It was long...

I will also move the sample. Go from Git to the cloned shogun directory

$python examples/undocumented/python_modular/graphical/svm.py

スクリーンショット 2015-06-05 15.54.37.png

That shouldn't be a problem ...

By the way, there are various samples in the example, so you may want to take a look. There is a reference on the official website, but it's pretty rough, so it may be quicker to read and understand the sample code.

By the way

If a Segmentation Fault occurs so far, and if a Segmentation Fault like "wrap_delete {some class name}" is displayed in the error log and a Segmentation Fault occurs at the timing of PyArg_UnpackTuple, it is due to the SWIG version. There is a high possibility of. Check the version, and if it is 3.x series, it may work if you change it to 2.x series and install it again.

Recommended Posts

Install shogun with python modular (OS X Yosemite)
Install Python 2.7.9 and Python 3.4.x with pip.
Install lp_solve on Mac OS X and call it with python.
Install Voluptuous with Python 2.5
Install python with pyenv
How to install caffe on OS X with macports
mac OS X 10.15.x pyenv Python If you can't install
Install PyQt5 with homebrew on Mac OS X Marvericks (10.9.2)
x86 compiler self-made with python
Install Python environment with Anaconda
Install external libraries with Python
Word Count with Apache Spark and python (Mac OS X)
Test Python with Miniconda on OS X and Linux with travis-ci
Build a python environment with pyenv (OS X El Capitan 10.11.3)
How to install Theano on Mac OS X with homebrew
Install Python 3.8 on Ubuntu 18.04 (OS standard)
I can't install python3 with pyenv-vertualenv
Install Python from source with Ansible
Install Python 3.8 on Ubuntu 20.04 (OS standard)
Install Sphinx on Mac OS X
Install the Python plugin with Netbeans 8.0.2
Unable to install Python with pyenv
Install mitmproxy on Mac OS X
Install Python 3.9 on Ubuntu 20.04 (OS standard?)
Install Python 2.7 on Ubuntu 20.04 (OS standard?)
Install python with mac vs code
[Python Windows] pip install with Python version
Install pgmagick on Mac OS X 10.9
Get started with the Python framework Django on Mac OS X
Quickly install OpenCV 2.4 (+ python) on OS X and try the sample
Install matplotlib on OS X El Capitan
Put OpenCV in OS X with Homebrew and input / output video with python
scipy stumbles with pip install on python 2.7.8
Mac OS X Yosemite 10.10 Development environment construction
Install NumPy, SciPy, Matplotlib with Homebrew on OS X (as of November 2015)
Join an online judge with Python 3.x
Installing PIL with Python 3.x on macOS
Light image processing with Python x OpenCV
How to install python3 with docker centos
[Blender x Python] Let's get started with Blender Python !!
I installed Pygame with Python 3.5.1 in the environment of pyenv on OS X
[Ansible] Install dnf on Centos7 with Python3 interpreter
GUI automation with Python x Windows App Driver
I wanted to install Python 3.4.3 with Homebrew + pyenv
Run Zookeeper x python (kazoo) on Mac OS X
Put Python 2.7.x on Mac OSX 10.15.5 with pyenv
Install vim7.3 (+ python2.4) from source (compatible with Gundo.vim)
Install Python 3 on MacOS Catalina (with Homebrew only)
Shpinx (Python documentation builder) on Mac OS X
[Blender x Python] Think of code with symbols
Install LightGBM in an OS X virtualenv environment
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Install Python locally
Play with 2016-Python
AES256 with python