Wednesday, July 31, 2013

Trying sklearn for machine learning (with the face recognition sample)

I was trying to evaluate the feasibility of a project, and Python of course was my first choice. During the build-up of the developing environment, however, I was frustrated due to the installation of scikit-learn package.

Quick tip: download the latest stable version (0.14a1) of scikit-learn and play with the sample code given in the source package.

Installation by pip (failed)

The first frustration might be caused by my stupidity.

I followed the instruction on the scikit-learn page and used pip to complete the installation. Then I googled an example code and found it couldn't be run successfully. The Python interpreter always complained with
ImportError: cannot import name scikits.learn
I googled for the solution again and again, and found all the answers pointed to ``multiple versions of Python installed in the system.'' But I have only Python 2.7 in my Ubuntu!

What I had done was uninstall the scikit-learn and reinstall. Also I tried to install it from the source, but nothing changed.

Then I thought of something and tried to find some sample code on the scikit-learn page. It turned out that the module should be sklearn instead of scikits.learn... Orz

So what I had found was a sample code using old module names.


Using version 0.13.1 (failed)

I am not sure whether this is a bug. I could not run the example code (fa_recognition.py) located in the source package of version 0.13.1. When my scikit-learn modules were also the version 0.13.1. The error message was:
ImportError: cannot import name column_or_1d
and I found ``import sklearn.datasets'' would trigger this error.

I also tried to follow the traceback message given by the interpreter but only knew it was due to an importing of label.py. My skill on debugging couldn't bring me further.


Verion 0.14a1 (Succeeded)

Okay, I'd run out my approaches... I almost gave up, but then I thought of the possibility of using the latest version to solve the problem. So I downloaded the source of version 0.14a1 and installed it. Finally, I got the sample code run with expected outputs.

Face recognition example test

If you have downloaded the source package, you can find the example in the path of: YOUR_FOLDER/scikit-learn-0.14a1/examples/applications/face_recognition.py.

Frankly, I have no idea about the output yet, but I would like to post the text output of running face_recognition.py with the figures of result.

Text output


===================================================
Faces recognition example using eigenfaces and SVMs
===================================================

The dataset used in this example is a preprocessed excerpt of the
"Labeled Faces in the Wild", aka LFW_:

  http://vis-www.cs.umass.edu/lfw/lfw-funneled.tgz (233MB)

.. _LFW: http://vis-www.cs.umass.edu/lfw/

Expected results for the top 5 most represented people in the dataset::

                     precision    recall  f1-score   support

  Gerhard_Schroeder       0.91      0.75      0.82        28
    Donald_Rumsfeld       0.84      0.82      0.83        33
         Tony_Blair       0.65      0.82      0.73        34
       Colin_Powell       0.78      0.88      0.83        58
      George_W_Bush       0.93      0.86      0.90       129

        avg / total       0.86      0.84      0.85       282




2013-07-31 08:04:43,243 Downloading LFW metadata: http://vis-www.cs.umass.edu/lfw/pairsDevTrain.txt
2013-07-31 08:04:46,028 Downloading LFW metadata: http://vis-www.cs.umass.edu/lfw/pairsDevTest.txt
2013-07-31 08:04:46,740 Downloading LFW metadata: http://vis-www.cs.umass.edu/lfw/pairs.txt
2013-07-31 08:04:48,140 Downloading LFW data (~200MB): http://vis-www.cs.umass.edu/lfw/lfw-funneled.tgz
2013-07-31 08:11:24,620 Decompressing the data archive to /home/thk/scikit_learn_data/lfw_home/lfw_funneled
2013-07-31 08:11:33,822 Loading LFW people faces from /home/thk/scikit_learn_data/lfw_home
2013-07-31 08:11:33,981 Loading face #00001 / 01288
2013-07-31 08:11:36,218 Loading face #01001 / 01288
Total dataset size:
n_samples: 1288
n_features: 1850
n_classes: 7
Extracting the top 150 eigenfaces from 966 faces
done in 0.806s
Projecting the input data on the eigenfaces orthonormal basis
done in 0.065s
Fitting the classifier to the training set
done in 16.244s
Best estimator found by grid search:
SVC(C=1000.0, cache_size=200, class_weight=auto, coef0=0.0, degree=3,
  gamma=0.001, kernel=rbf, max_iter=-1, probability=False,
  random_state=None, shrinking=True, tol=0.001, verbose=False)
Predicting people's names on the test set
done in 0.049s
                   precision    recall  f1-score   support

     Ariel Sharon       0.67      0.78      0.72        18
     Colin Powell       0.77      0.80      0.78        61
  Donald Rumsfeld       0.71      0.76      0.73        29
    George W Bush       0.90      0.89      0.89       134
Gerhard Schroeder       0.71      0.63      0.67        27
      Hugo Chavez       0.93      0.58      0.72        24
       Tony Blair       0.69      0.83      0.75        29

      avg / total       0.81      0.80      0.80       322

[[ 14   2   1   1   0   0   0]
 [  3  49   1   3   0   1   4]
 [  1   3  22   2   0   0   1]
 [  2   6   4 119   1   0   2]
 [  1   1   1   4  17   0   3]
 [  0   3   1   0   5  14   1]
 [  0   0   1   3   1   0  24]]


Figures



Compiling NiSimpleViewer -- from Kinect OpenNI sample code

(This was an old article kept in draft state for about four months)

In previous post I installed the OpenNI SDK and tested some of its samples [1]. I am trying to study something from the sample code and what I've chosen now is the NiSimpleViewer example (which can be found in openni/Samples/NiSimpleViewer).

To avoid messing up the original sample code, I copied the whole directory of NiSimpleViewer and renamed it to mySimpleViewer.

The first work was to compile the source code. Of course the samples included makefiles but they were used for more general and more complicated cases. What I needed was just a simple and self-contained makefile. My first attempt was to simplify the makefile, but unfortunately it's too complicate for me to understand or even to modify it. So I wrote a simple one as the follows (I knew it's unnecessary to do so for such a simple case, but I just wanted to do a bit of exercise on writing makefiles.) :

CC = g++ 
CFLAGS = -g -Wall
LDFLAGS = -lglut -lOpenNI -I /usr/include/ni 
EXECUTABLE = mySimpleViewer
SOURCES = $(EXECUTABLE).cpp
OBJECTS = $(EXECUTABLE).o

all:
        $(CC) $(CFLAGS) -o $(EXECUTABLE) $(SOURCES) $(LDFLAGS)


After running mySimpleViewer, the program complained that the file SamplesConfig.xml cannot be found. I checked the source code as well as the files in related directories and found the path should be changed from
../../Config/SamplesConfig.xml
to
../Config/SamplesConfig.xml
In fact the relative path for the source code to find the xml file is ``../Config'' but not ``../../Config''. This was because the original Makefile putted the binaries in openni/Samples/Bin/Release/ which was one more directory deeper than my test example (thanks to my colleague's reminder).

Also, I modified some #define macros to const type. Based on my knowledge, this would be more ``c++ style''.
const XnChar* SAMPLE_XML_PATH = "../Config/SamplesConfig.xml";

const int GL_WIN_SIZE_X = 1280;
const int GL_WIN_SIZE_Y = 1024;

const unsigned int DISPLAY_MODE_OVERLAY = 1;
const unsigned int DISPLAY_MODE_DEPTH = 2;
const unsigned int DISPLAY_MODE_IMAGE = 3;
const unsigned int DEFAULT_DISPLAY_MODE = DISPLAY_MODE_DEPTH;
---
[1] Test Kinect in Ubuntu 12.04

Monday, July 15, 2013

Network problems of Linux Mint

I have installed and used Linux Mint (Maya) on my Toshiba Satellite for several months. The network setting has always troubled and annoyed me, especially the wireless one.

I had encountered three problems:
  1. If the cable doesn't connect to the laptop or the network is not working, the system always wait for a long time with the messages read:
    ``Waiting for network configuration...''
    ``Waiting up to 60 more seconds for network configuration...''
  2. When the network is interrupted, it won't recover automatically.So every time I close the laptop lid to sleep the system and open it to resume it, I have to open the terminal and type ``sudo pon dsl-provider'' to get the network connection back.
  3. I couldn't connect to my wireless network at home, and found nowhere to get the settings done. My wireless network has been set as hidden, and I have added it in the Network Connections. But when I tried to connect it, the icon always showed the processing state and the connection was never done.
Today I got all these problems solved and I am happy now.

Here are the solutions I found and tested successfully:
  1. Edit the file: /etc/init/failsafe.conf
    Find the lines with ``sleep'' and comment out the two which related to the system messages just mentioned above.
  2. Reinstall network-manager by
    sudo apt-get --reinstall install network-manager
    and use the following command to start the manager:
    sudo /etc/init.d/network-manager restart
  3. Edit the file: /etc/NetworkManager/NetworkManager.conf
    change ``managed=false'' to ``managed=true''
    restart the network manager by
    sudo /etc/init.d/network-manager restart
    After doing these, the available wireless network will show up. My hidden wireless network still couldn't be connected so I tried to click its icon and a window popped out for me to enter the password. I inputed the password and everything went as expected as shown in the figure:
---
ref:
http://forums.linuxmint.com/viewtopic.php?f=150&t=110218
http://ubuntuforums.org/showthread.php?t=1528695&p=9575190#post9575190