2016年4月25日 星期一

OpenCV Install

先說結論:

Step 1: Use VMware to install Ubuntu 14.04.4 (TLS) version.

Step 2: Install anaconda2  (OpenCV2.x and 3.1.x 似乎和 python 2.7 相容,如果 python 3.x 會遇到相容問題)

Step 3: Use conda install -c https://conda.binstar.org/menpo opencv=3.1.0 

如果要重新 compile and cmake opencv.  我尚未成功。

--> another problem is ffmpeg doesn't seem to work using conda install

-> so all video using VideoCapture not working!!!

-> it seems that need to include opencv_contribute

 

-------------------------------------------------------

 

1. Need to install anaconda2; reinstall from contiumm

2. Need to run face detection

 

sudo apt-get install libgtk2.0-dev

sudo apt-get install pkg-config

 

More complete installation guide of opencv, please follow:

 

http://ccw1986.blogspot.tw/2013/09/learningopencv.html

OpenCV Tutorial

 

http://ccw1986.blogspot.tw/2016/03/install-python-and-opencv-31-on-ubuntu.html

Installation of OpenCV on Ubuntu TLS14.04

 

It works on c++ version, but failed on the anaconda python.

 

Update latest packages and installed

$ sudo apt-get update
$ sudo apt-get upgrade

apt-get update - 更新最新的套件資訊 apt-get upgrade - 更新套件

安裝基本開發工具

$ sudo apt-get install build-essential cmake pkg-config git
  • build-essential - 基本編譯器
  • git - 分散式的版本控制系統
  • cmake - 跨平台自動化建構系統
  • pkg-config - 查詢套件include與library的位置

安裝影像I/O開發工具,例:JPEG, PNG, TIFF, etc.

$ sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev

安裝影音I/O開發工具,讀影影片、接收串流、影音解碼...

$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev

安裝GTK開發工具,OpenCV highgui module顯示視窗時需要

$ sudo apt-get install libgtk2.0-dev
  • libgtk2.0-dev - (Gimp ToolKit)跨平台的圖形介面開發套件
$ sudo apt-get install libtbb-dev
  • libtbb-dev - (Intel® Threading Building Blocks )平行計算引擎

安裝OpenCV優化的開發工具

$ sudo apt-get install libatlas-base-dev gfortran

以下 install python 部份 ignore.  因為要改用 anaconda2

http://dr-kayai.hatenablog.com/entry/2015/03/30/134856

 

 

So I remove the anaconda2 directory and re-build the opencv!!

 

NOT WORKING!!!!! for python ==> import cv2 has problem.

 

最後找到直接可用 anaconda install opencv3 方法

http://danwin.com/2014/12/compile-opencv-2-4-10-ubuntu-14-04-14-10/

不過以上是 opencv2, 必須修改

 

  1. conda install binstar 
  2. 似乎 binstar 改為 anaconda-client  -> conda install anaconda-client 做一次
  3. conda install -c https://conda.binstar.org/menpo opencv=3.1.0  (可以直接到 url 找最新的 opencv)
It works!!!!
不過 opencv3 和 opencv2 有些地方不同, example face detection
opencv2 --> cv2.cv.CV_HAAR_SCALE_IMAGE
opencv3 --> cv2.CASCADE_SCALE_IMAGE

 

 

 

2016年3月19日 星期六

New Idea From Andrew Ng

 

Overfit is a big problem --> assuming too many parameter space and high variance learning (not high bias)

Solution A:  constraints: e.g. regularization, sparsity (L1-regularization), ???

 

Solution B: Use a lot of data (10x or 100x),  how?  

artificial synthesis of data,  adaboost, random forest ?

 

Are they (more data and regularization) equivalent or behavior similarly?

Raspberry Pi 3 記錄

 

繼上次買 Raspberry Pi 1, 這次又買了 RP 3.  

RP3 最大的好處: (1) 4 核且 1.2GHz, 是 CA53 ARM 64-bit processors; (2) 板上有 WiFi + BT (from Broadcom, too?).  其它和之前的 RP 應差異不大。

 

Step 1:  (MicroSD card)

I bought 32G Kingston microSD card (UHS ..   U3) 號稱 80MB/s write and 90M/s read speed.  不過我用 RP3 測試只有 20MB/s, 應該是 PR3 performance limitation.

Download NOOBS zip file, decompress to microSD card.   用此 boot 自動會 install Raspbian OS (derivative of Linux Debian).

 

Step 2: (installation Raspbian 4.1.18-v7+, based on Debian Jessie version); armv7l 32-bit instruction set!!!  

1. 設定 keyboard layout from UK to US.

2. 設定 WiFi AP

3. Update Raspbain to the latest update

$ sudo apt-get update  (update apt-get inself)

$ sudo apt-get upgrade  (upgrade all packages)

 

Step 3: (Install 中文顯示和輸入)

請參考葉難的 blog.

先更新一下套件清單。
$ sudo apt-get update

安裝中文字型,文泉驛微米黑、文泉驛正黑、文泉驛點陣宋體。
$ sudo apt-get install ttf-wqy-microhei ttf-wqy-zenhei xfonts-wqy

然後將locale設定成繁體中文。
$ sudo dpkg-reconfigure locales

繁體中文的部份,我選了三個zh_TW BIG5、zh_TW.EUC_TW EUC-TW、zh_TW.UTF-8 UTF-8。

 

然後將zh_TW.UTF-8設定為預設的locale。

然後要安裝中文輸入法。
$ sudo apt-get install scim scim-tables-zh scim-chewing

其中scim-chewing是注音的輸入法,若是大陸用的拼音輸入法,請安裝scim-pinyin。

重開機後,以Ctrl-Space即可切換輸入法,輸入中文。

 

Step 3: (Remote Desktop)   Raspberry Pi 的 default username: pi   passwd: raspberry --> change to aluxxx4

可以用 VNC or microsoft remote desktop.  因為 mac 也可以在 app store download microsoft remote desktop.  因此先 install xrdp.

$ sudo apt-get install xrdp --> mac 端必須 download Microsoft remote desktop.  見 xxx blog.

--> mac 可以直接用 microsoft remote desktop, 不用 download any software!!

不過連 ping 都不 work, 上網卻 ok.   $ ping www.google.com --> icml open socket: operation not permitted

-->  $ sudo chmod u+s /bin/ping  --> ping ok and ssh ok and remote desktop ok!!!

ip addr:  192.168.1.245

 

Install VNC server 就先 delay. 

 

Install miniconda python (2016/?)

不知何時 install miniconda python.

 

Install Camera Module (2016/04/16)

主要想做一些 computer vision 有關的應用,從淘寶買了 Element 14 官方版的 camera module (RMB$95 + RMB$20 shipping).

Install camera module 可以參考葉難的 article.

硬體裝好後,軟體的設定及測試如下。 

$ sudo apt-get update 
$ sudo apt-get upgrade
然後執行raspi-config:
$ sudo raspi-config

進入裡頭的「camera」項目,啟用相機模組,再重開機。
 

接下來,便能使用一些準備好的程式與指令拍攝靜態照片與錄製動態影片。
以指令raspistill可拍照,如下
$ raspistill -o image.jpg -t 1000

-o指定輸出檔名,-t指定預覽時間(單位為千分之一秒),你會在螢幕上看到預覽畫面。如果你發現相片左右或上下顛倒,可加入參數-hf(horizontal flip)與-vf(vertical flip)調整。

這是我的手與我房間的天花板牆壁。
 


另外可利用指令raspivid進行錄影,如下:

$ raspivid  -o video.h264 -t 20000

-o指定輸出檔名,為H.264格式,-t指定錄製時間(單位為千分之一秒)。

可以用 omxplayer video.h264 來顯示 video. 

omxplayer 會利用 BRCM SoC 內的 BRCM GPU to play H.264 影片。

如果用 VLC player, 軟解無法正確顯示 video!!!

可利用ffmpeg轉成其他格式,例如:

$ ffmpeg -r 30 -i video.h264 -vcodec copy outputfile.mkv

或是

$ avconv -r 30 -i video.h264  outputfile.avi
 

 

Install Bluetooth (2016/04/16)

Follow the article in element 14.

sudo apt-get update

sudo apt-get upgrade

sudo apt-get dist-upgrade

sudo dpi-update --->  update the firmware

 

>> then reboot 

sudo apt-get install pi-bluetooth >> show it's already installed in the new firmware

sudo apt-get install blueman

>> then reboot and the bluetooth icon shows

 

>> then reboot and the bluetooth icon disappear!!

 

 

Install Camera Software Codec and OpenCV

$ sudo apt-get install -y picamera

$ sudo apt-get install -y vlc

$ sudo apt-get install -y gpac mplayer

$ sudo apt-get install -y mencoder  (# obsolete!!)

$ sudo apt-get install -y python-opencv

 

 

2016年3月16日 星期三

Jupyter Notebook with Octave Kernel

在學 machine learning 時常會用到兩種 languages.  一類是 matlab or octave (e.g. Andrew Ng).

另一類是 python (e.g. sckit-learn, etc., 林軒田; UC Washington ML classes).   少數用 R and maybe other languages.

 

Matlab/Octave 的好處是簡單清楚。處理 vector or matrix or plot 乾淨利落。缺點是 large scale ML 的速度太慢。另外 matlab/octave 沒有很好的 notebook UI.

Python 則相反。即使在用 ipython 的 vector, matrix, plot, 仍然處處有 python 的影子。但好處是速度快, scalable.  最重要的是 ipython notebook 是非常簡單好用的 math or engineering notebook UI.

是否能結合兩者?  Yes, Jupyter Noteook.   Jupyter notebook 是 ipython notebook 的下一代。把 UI 和 kernel 分開。 UI 就是 ipython notebook interface. 

Kernel 則可以是 python, octave, matlab, R, etc.

 

為了要 install Jupyster, 首先 install python. 

 

Step 1: Use Vmware to install Ubuntu 14.04.4 (TLS) version.  

Detailed refer to previous article.  

為了方便再 install Dropbox (ubuntu version) for .bashrc, and install kompare, kdiff3, emacs

Step 2: Install Anaconda python (3).   

-> Recommend to use Anaconda python 3.5 from the Jupyter website.

Download PYTHON 3.5 64-bit version (Anaconda3-2.5.0-Linux-x86_64.sh)

=>  bash ...

 

(1) Do "conda install jupyter  -> update jupyter related files

(2) Do "pip install bash_kernel"  -> No need for Octave kernel

 

Step 3: install octave:   sudo apt-get install octave

(3) Do "pip install octave_kernel" 

(4) Do "python -m octave_kernel.install"

 

--> Done.

 

You can either do on the web:

jupyter notebook -> choose new octave notebook

or 

jupyter qtconsole --kernel octave

jupyter console --kernel octave

 

It works now!

2016年3月15日 星期二

Anaconda Python

一般在做用 python 時,最常用到的三個 packages.

1. pip:  取代 easy_install.  是 python 最常用的 package manager

2. virtualenv: environment manager, 主要是自由切換 python2.x and python3.x.  不是很好用,也不常用。不過為了相容性,還是會 install.

3. Numerical packages (numpy, scipy, matplotlib, etc.), 或是用 Enthough, 或是 python(x,y).  

 

一個好消息是只要 install anaconda python, 可以集三者為一。不用再 install above!

 

TaskConda package and environment manager commandPip package manager commandVirtualenv environment manager command
Install a package conda install $PACKAGE_NAME pip install $PACKAGE_NAME X
Update a package conda update --name$ENVIRONMENT_NAME $PACKAGE_NAME pip install --upgrade $PACKAGE_NAME X
Update package manager conda update conda Linux/OSX: pip install -U pip Win: python -mpip install -U pip X
Uninstall a package conda remove --name$ENVIRONMENT_NAME $PACKAGE_NAME pip uninstall $PACKAGE_NAME X
Create an environment conda create --name$ENVIRONMENT_NAME python X cd $ENV_BASE_DIR; virtualenv$ENVIRONMENT_NAME
Activate an environment source activate $ENVIRONMENT_NAME X source$ENV_BASE_DIR/$ENVIRONMENT_NAME/bin/activate
Deactivate an environment source deactivate X deactivate
Search available packages conda search $SEARCH_TERM pip search $SEARCH_TERM X
Install package from specific source conda install --channel $URL$PACKAGE_NAME pip install --index-url $URL $PACKAGE_NAME X
List installed packages conda list --name $ENVIRONMENT_NAME pip list X
Create requirements file conda list --export pip freeze X
List all environments conda info --envs X Install virtualenv wrapper, then lsvirtualenv
Install other package manager conda install pip pip install conda X
Install Python conda install python=x.x X X
Update Python conda update python * X X
  • conda update python updates to the most recent in the series, so Python 2 to latest 2.x, Python 3 to latest 3.x, and so on.

2015年11月20日 星期五

Caffe - Deep Learning Tool Installation

 

Deep Learning 有幾種常用的 Tools

* Caffe based on C, but with Python interface: UC Berkeley 開發。Google, CEVA 使用。Mainly for image.

* Torch based on Lua (ANSI C): NYU 開發。Facebook AI and Google use it.

* Theano and Pylearn based on Python : University of Toronto

* TensorFlow: Google

* SystemML: IBM

 

本文主要 focus 在如何設定 Caffe.  

Caffe 幾個特點:

底層是 C (and C++?) 所以速度很快。不過可以用 python (even ipython) 做成 interface.  所以用起來容易。

Protobuf:  from Google, for easy and fast data structure.

LMDB: from SAMS, for lightning database

HDF5: from xxx, for database.

 

OS (Ubuntu 14.04 64bit)

我用 Microsoft surface pro 3 with windows 10.  不過為了方便 try-and-error, 我用 VMware workstation player 12 with guest OS:  Ubuntu 14.04 (64-bit)


Install Ubuntu on VMware workstation player 12

* First install VMware workstation player 12 (free)

* Ubuntu 部份:

update Ubuntu’s update --> Yes.

sudo apt-get install tcsh --> No need, just use bash

sudo apt-get install unit-tweak-tool (to increase terminal font size) --> No need, use system setting: displays: scale 1.5 instead!

sudo apt-get install samba  (1. edit /etc/smb-conf; 2. smbpasswd -a alu; 3. smb start)  -->  No need.

-> update Ubuntu 之後,重新 reboot 會有 could not apply the stored configuration for monitors 

Solution: 

Once logged in change your directory to .config folder in your home directory.

cd .config

Rename monitors.xml file to monitors.xml.bkp. In here, we are keeping a backup file just to play safe.

mv monitors.xml monitorx.xml.bkp

 

Ubuntu 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

CUDA:  download from Nvidia (only 64-bit).  如果沒有 GPU 可省略。

BLAS:  使用 ATLAS :  

sudo apt-get install libatlas-base-dev

Remaining dependencies 14.04:

sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev

 

 

Python 部份最好後處理

 

Caffe Installation

Download from Github

* cp Makefile.config.sample Makefile.config

* Edit Makefile.config

Set 1:  CPU only, no python layer link.  不支持 pycaffe

make all;  make test; make runtest  ---> OK  (只要先不 install python)

 

 

Python (for pycaffe interface)

sudo apt-get install python-dev  ----> NONONONO!!!!

不過問題是沒有 numpy.  所以需要 install pip (python package management) and maybe not virtuenv (for coexist of python 2.7 and python 3.x) --> NO!

—> 最直接是只用 python 2.7 且 install Anaconda Python (similar to Enthought python), 不要做 sudo apt-get install python-dev! 

 

 

Linux Anaconda Installation

  1. Download the installer.
  2. After downloading the installer, in your terminal window execute for Python 2.7.10
    bash Anaconda2-2.4.0-Linux-x86_64.sh
    Anaconda2-2.4.0 是 2.7.10
    Anaconda-2.0.0 是 2.7.6  (OK, but need conda upgrade xxx) 

      bash Ana… .sh  to install anaconda python  

 

      Option A: 2.4.0 --> (python 2.7.10)   ====>  NOT GOOD !!!

      2.4.0 (python 2.7.10) in /home/alu/anaconda2  

      Back to caffe and uncomment python part --> make clean; make all; make test OK; but make runtest with error:

       libhdf5_hl.so.10 not found.  

 

 ==> Option B: 2.0.0 --> (python 2.7.6) in /home/alu/anaconda  ====>  GOOD 

      No error 

 

      >  condo update conda

      > pip install protobuf

 

     > make pycaffe

 

 

How to Use or Verify Python?

Use ipython notebook (in caffe-master directory)

 

 

 

 

Install boost for python!??  no

 

Error when running ipython notebook

No module name google.protobuf.internal

> pip install protobuf

 

 

> make all; make test ok. but make runtest failed.  no libhdf5_hl.so.10 not found.  只有在 /usr/lib/x86_64-linux-gnu/libhdf5_hl.so.7  (這是在 general dependencies 時 install libhdf5-serial-dev 時 Ubuntu 的 default python 是 2.7.6 (?).  但 Anaconda python 是 python 2.7.10.

  Ubuntu built-in python lib seems to be out-of-date.   Solution:

  - install Anaconda python package, and check the location of libhdf5_hl.so.10  一般是在 ~/anaconda/lib

  - export LD_LIBRARY_PATH=“$LD_LIBRARY_PATH:~/anaconda/lib”  —> work now.

 - 需要把 python search path 也設到 .bashrc (anaconda install 自動加入) or .tcshrc; 以及 LD_LIBRARY_PATH

 - 檢討是否還是用 equival 來 install 不同版本的 python.

-  重新整理 .bashrc, .bash_aliases, and .tcshrc —> add python search path and LD_LIBRARY_PATH

 —> 不過 LD_LIBRARY_PATH 的設定後反而後讓 make all failed!  所以只能在已 compile 之後才加上。

 

Set 2:  CPU only, with python link.  has problem!!

 

 

 

 

 

 

2014年12月7日 星期日

Steady State Kalman Filter

Time Varying or Stationary?

一般而言 Kalman filter 是 time varying 的 filter.  多數 Kalman filter 的應用也是 time varying system, 例如 navigation, gesture recognition, etc.  

另一類的應用: 如果 A, B, Bw, C, (參考 Berkeley article) matrices and (A, C) is observable or detectable; (A, Bw) is controllable or stabilizable. 

同時 covariance matrices Q, R 是 stationary and semi-positive definite.  Kalman filter 有 steady state 的 solution.  

Steady state 的參數包含:

  • state mean, x(k) -> x,  or m(k) -> m (only one)
  • state covariance 有兩個:  P+ 是 prediction step 之後的 covariance;  P- 是 update step 之後的 covariance.

        P+ > P-;   P+ = A' P- A + Q

       or P- = A'.inv (P+ - Q) A.inv

  • Kalman gain (only one)
 

Method A:  Brute force iterative simulation

Method B:  P = P

Method C:  discrete algebraic Ricartti Equation (DARE) steady state equation 如下:

(可以參考 const_v.py code for simulation example)

 

ARE

RE: Riccarti Equation    ARE: (Algebraic Riccarti Equation)

Stanford EE363 note

NewImage

NewImage

NewImage

 

 

Another Berkeley article

NewImage

NewImage

Ms 是 prediction step 之後的 covariance.  

NewImage

Fs 是 steady state 時的 Kalman Gain!!!

 

Example1:  DC offset estimation: A=C=1 ; Bw=1  

P+ = P- +Q

P+ = P+ + Q - P+ ( P+ + R)^-1 P+

P+ + R = P+^2 / Q

P+ = 0.5 * (Q + sqrt(Q^2 + 4QR))  as expected

  

Example 2:  Constant Velocity Model: A = [1, 1; 0, 1] , C =[1, 0],  Bw=1, V=R, W=[qo, q1; q1, q2]

case 1:  W = [q0, 0; 0, 0]  process noise only on position, reduce to example 1??  Q = q0

case 2:  W = ?? process noise only on velocity ->?

 

 

追蹤者