Difference between revisions of "Openface: Instalasi Deep Learning di Ubuntu 16.04 Server"

From OnnoWiki
Jump to navigation Jump to search
(Created page with "Sumber: http://allskyee.blogspot.co.id/2017/03/face-detection-and-recognition-using.html Having come across an interesting article on Medium titled "Machine Learning is Fun!...")
 
 
(36 intermediate revisions by the same user not shown)
Line 2: Line 2:
  
  
Having come across an interesting article on Medium titled "Machine Learning is Fun! Part 4: Modern Face Recognition with Deep Learning" by Adam Geitgey, I decided to try out this OpenFace which uses deep learning extracted features for face recognition.
+
Disini menggunakan dlib, yang katanya lebih baik daripada Haar-cascade based classifier OpenCV.
  
An interesting discovery along the way was Dlib and how it was used to create a face detector based on HOG features that claims to beat OpenCV's Haar-cascade based classifier. It's written up nicely on their blog here.
 
  
Back to Openface. This is a project authored by Brandon Amos and the theoretical background is very well documented on one of his blog posts. Instructions to install and use can be found towards the end of Adam's blog post I mentioned above.
+
==Instalasi Paket Pendukung==
  
You can easily spin up an environment using Docker, but it does take the fun out of it. Plus, things tend to get a little odd if you're using a GPU for acceleration anyways on Docker, so I'm going to install this on a native environment but keep it compartmentalized as much as possible on both my PC and Raspberry Pi 3.
+
sudo su
0. Install necessary packages and setup virtualenv
+
locale-gen id_ID.UTF-8
  
Some packages to install.
+
apt update
 +
apt -y install git \
 +
        libopenblas-dev libopencv-dev libboost-dev \
 +
        libboost-python-dev python-dev \
 +
        build-essential gcc g++ cmake
 +
apt -y install software-properties-common \
 +
        libgraphicsmagick1-dev libfftw3-dev sox libsox-dev \
 +
        libsox-fmt-all python-software-properties \
 +
        build-essential gcc g++ curl \
 +
        cmake libreadline-dev git-core libqt4-dev libjpeg-dev \
 +
        libpng-dev ncurses-dev imagemagick libzmq3-dev gfortran \
 +
        unzip gnuplot gnuplot-x11 ipython \
 +
        gcc-4.9 libgfortran-4.9-dev g++-4.9
  
sudo apt-get install virtualenv git \
+
Lakukan 2-3 kali supaya memastikan apps di install dengan benar.
libopenblas-dev libopencv-dev libboost-dev libboost-python-dev python-dev \ build-essential gcc g++ cmake
 
  
Create a compartmentalized Python environment with Python 2 (has to be this version required by Openface) and copy necessary OpenCV's Python bindings onto it. Let's call the fd_fr directory VENV_ROOT.
+
==Instalasi dlib face landmark detection==
  
  mkdir fd_fr; cd fd_fr
+
  sudo su
  export VENV_ROOT=$(pwd)
+
  apt -y install build-essential cmake libgtk-3-dev \
  virtualenv -p /usr/bin/python2.7 .
+
        python-pip libboost-all-dev libboost-dev
  cp /usr/lib/python2.7/dist-packages/cv* lib/python2.7/site-packages/
+
  apt -y install libboost-python-dev
  source bin/activate
+
  pip install numpy
 +
pip install scipy
 +
pip install scikit-image
 +
pip install dlib
 +
  pip install opencv-python # opencv tampaknya masih dibutuhkan
  
Tip when running on RPI : As RPI devices have very little memory and the storage device being an SDcard, performance can drop dramatically when run with tons of file I/O. One way to remedy is by flushing the page cache after large file I/O operations with the following command periodically during the installation process going forward.  
+
Lakukan 2-3x supaya memastikan apps di install dengan benar.
  
sync
+
==Instalasi Torch==
sudo bash -c "echo 3 > /proc/sys/vm/drop_caches"
 
 
 
1. Install and run Dlib face landmark detection
 
Get Dlib either by cloning it from their github repository or downloading a released version. At the time of this writing, that is version v19.3 so I'm going to clone the repository and checkout that tag.
 
 
 
git clone https://github.com/davisking/dlib.git dlib
 
cd dlib
 
git checkout -b v19.3
 
 
 
If on RPI, do not compile Dlib on the device as some files require 800+MB of memory which cause massive swap to device and CPU utilization falls below 5%. I recommend doing this on the host PC and run ARM emulation using qemu-arm-static and then copying. But if you're feeling lazy, I have done all this (and subsequent steps) that you can access via this link. More details on this in Section 5.
 
 
 
Tip with qemu-arm-static internet connection : Especially if behind a proxy, be sure to share contents of /etc/resolv.conf with chroot environment either by copying or bind mount
 
 
 
mount --bind /etc/resolv.conf /rpi/mount/etc/resolv.conf for behind proxy
 
 
 
The build process is done with cmake. If you have a relatively new CPU, it should have the AVX (Advanced Vector Extensions) which greatly enhances the performance. You can find either or not your CPU supports this functionality by executing "cat /proc/cpuinfo | grep avx". If you get a non-empty text, then your CPU supports it. If it does, include the "-DUSE_AVX..." statement below.
 
 
 
mkdir build; cd build
 
cmake ../tools/python -DUSE_AVX_INSTRUCTIONS=1
 
cmake --build . --config Release
 
 
 
Copy generated dlib.so file to Python's lib/ path and test it by importing it from Python.
 
 
 
cp dlib.so ${VENV_ROOT}/lib/python2.7/dist-packages/
 
python -c "import dlib"
 
 
 
If this second statement returns an error, you have done something wrong.
 
 
 
(optional) To run Dlib with a face and landmark detector on a webcam feed, first download the model for the latter from here and unzip it. I have written a short script on my gist page which requires the webcam class. You need to download the latter to run the former as well as the model.
 
2. Install and build Torch
 
Torch is an opensource machine learning library based on Lua. Detailed instructions to get started available on their site. They recommend using a script to install some packages but I've been burned one too many times by erroneous scripts with sudo rights and so I will write out what it's doing in plain English.
 
 
 
On another folder (not Dlib), clone the repository.
 
  
 +
sudo su
 +
cd /usr/local/src
 
  git clone https://github.com/torch/distro.git torch --recursive
 
  git clone https://github.com/torch/distro.git torch --recursive
  cd torch
+
  cd torch; bash install-deps;
 
 
To install necessary packages (if on an Ubuntu 16.04 system like moi), run
 
 
 
# for Ubuntu 16.04
 
sudo apt-get install software-properties-common \
 
                libgraphicsmagick1-dev libfftw3-dev sox libsox-dev \
 
                libsox-fmt-all
 
sudo apt-get install python-software-properties
 
sudo apt-get install build-essential gcc g++ curl \
 
            cmake libreadline-dev git-core libqt4-dev libjpeg-dev \
 
            libpng-dev ncurses-dev imagemagick libzmq3-dev gfortran \
 
            unzip gnuplot gnuplot-x11 ipython
 
sudo apt-get install -y gcc-4.9 libgfortran-4.9-dev g++-4.9
 
 
 
For Raspbian
 
 
 
# for Raspbian Jessie
 
sudo apt-get install -y build-essential gcc g++ curl \
 
            cmake libreadline-dev git-core libqt4-dev libjpeg-dev \
 
            libpng-dev ncurses-dev imagemagick libzmq3-dev gfortran \
 
            unzip gnuplot gnuplot-x11 ipython
 
 
 
Now run the install.sh script (which is more like a build script) and source the environment activation file.
 
 
 
 
  ./install.sh
 
  ./install.sh
  source install/bin/torch-activate
+
  source ~/.bashrc
 
 
Caveat 1.
 
The necessary paths in "install/bin/torch-activate" are hard-coded absolute paths so if you move the installed directory, be sure to change these as well.
 
  
Caveat 2.
+
Lakukan 2-3x untuk memastikan terinstalasi dengan baik.
Older versions of CUDA will not work. If you don't feel like updating, just install without it by adding "path_to_nvcc=" to line 82 in install.sh file.
 
  
Caveat 2-1.
+
# apt -y install luarocks
For old or Atom based systems that do not have AVX, the library is automatically going to use SSE which has a bug on randperm function. Rest assured, it was fixed recently so be sure to pull that patch if running on Atom.
+
# update common package ke versi terakhir
 +
luarocks install torch
 +
luarocks install nn
 +
luarocks install graph
 +
luarocks install cunn
 +
luarocks install cutorch
 +
luarocks install torchnet
 +
luarocks install optnet
 +
luarocks install iterm
  
3. Install Openface
+
==Instalasi Openface==
Now for the final piece of the puzzle, clone the repository.
 
  
 +
cd /usr/local/src
 
  git clone https://github.com/cmusatyalab/openface.git openface
 
  git clone https://github.com/cmusatyalab/openface.git openface
cd openface
 
 
There has been a release on Feb 26, 2016 and it's been roughly a year since. So I think it's better to just use the tip rather than checking out  the last release. Next, install and download necessary artifacts.
 
  
 +
cd /usr/local/src/openface
 
  python setup.py install
 
  python setup.py install
 
  ./models/get-models.sh
 
  ./models/get-models.sh
Line 113: Line 74:
 
  luarocks install dpnn
 
  luarocks install dpnn
  
4. Get labeled faces and train on them
+
==Ambil Muka / Face yang sudah di label==
It's now finally time to run Openface and see what it can do. To get a good batch of labeled faces on which to run the recognition task on, there is the LFW (labeled faces in the wild) dataset. Download then unzip which creates a directory lfw containing 5k directory of IDs.
+
 
 +
Contoh2 foto muka untuk training.
  
 +
cd /usr/local/src/openface/
 
  wget http://vis-www.cs.umass.edu/lfw/lfw.tgz
 
  wget http://vis-www.cs.umass.edu/lfw/lfw.tgz
 
  tar -zxvf lfw.tgz
 
  tar -zxvf lfw.tgz
  
I'm going to select from this a list that only has an excess of 10 faces and save it into a file called big_db.
+
Pilih data yang tidak lebih dari 10 muka, simpan di big_db.
  
 +
cd /usr/local/src/openface/
 
  find lfw/ -mindepth 1 -maxdepth 2 -type d -exec bash -c "echo -ne '{} '; ls '{}' | wc -l" \; | awk '$NF>10{print $1}' > big_db
 
  find lfw/ -mindepth 1 -maxdepth 2 -type d -exec bash -c "echo -ne '{} '; ls '{}' | wc -l" \; | awk '$NF>10{print $1}' > big_db
  
Next, I'm going to select 10 random people out of this list and copy them to another folder called training-images.
+
Ambil 10 orang (random) dari daftar big_db, copy ke training_images
  
 +
cd /usr/local/src/openface/
 
  mkdir -p training-images
 
  mkdir -p training-images
 
  cat big_db | shuf -n 10 | xargs cp -avt training-images/
 
  cat big_db | shuf -n 10 | xargs cp -avt training-images/
  
If you'd like to recognize yourself, add a folder in training-images. Be sure to include many photos in which you're the single identifiable human face.
+
Jika anda ingin wajah anda di recognize, tambahkan folder di training-images. Pastikan memasukan beberapa foto anda yang berisi muka anda sendirian.
 
 
We're now doing to run face landmark detection on each photo which will
 
  
* Detect the biggest face
+
Selanjutnya kita akan menjalankan face landmark detection untuk setiap foto yang akan
* Detect the facial landmarks (outer eyes, nose and lower lip)
 
* Warp affine to a canonical face
 
* Save output (96x96) to a file in an easy to access format
 
  
 +
* Mendeteksi muka yang terbesar
 +
* Mendeteksi tanda2 di muka (outer eye, hidung dan bibir bawah)
 +
* Warp affine ke  canonical face
 +
* Simpan output (96x96) ke file dalam format yang mudah di akses.
  
To do this, run
+
Perintah yang di jalankan,
  
 +
cd /usr/local/src/openface/
 
  ./util/align-dlib.py ./training-images/ align outerEyesAndNose ./aligned-images/ --size 96
 
  ./util/align-dlib.py ./training-images/ align outerEyesAndNose ./aligned-images/ --size 96
  
Next, run feature extraction on each of the images.
+
Selanjutnya extrak fitur dari masing-masing gambar,
  
 
  ./batch-represent/main.lua -outDir ./generated-embeddings/ -data ./aligned-images/
 
  ./batch-represent/main.lua -outDir ./generated-embeddings/ -data ./aligned-images/
  
As a final step, train a classifier from generated representations.
+
Langkah terakhir, train classifier untuk membuat representasi
  
 
  ./demos/classifier.py train ./generated-embeddings/
 
  ./demos/classifier.py train ./generated-embeddings/
  
This will create a file called classifier.pkl in generated-embeddings folder.
+
File yang dihasilkan adalah classifier.pkl di folder generated-embeddings
 +
 
 +
Masukan foto2 referensi ke bawah folder training-images/Nama_Orang_Tersebut.  
 +
Supaya mudah proses training ada baiknya di batch sekaligus, caranya,
  
 +
cd /usr/local/src/openface/
 +
rm -Rf aligned-images
 +
rm -Rf generated-embeddings/classifier.pkl
 +
./util/align-dlib.py ./training-images/ align outerEyesAndNose ./aligned-images/ --size 96
 +
./batch-represent/main.lua -outDir ./generated-embeddings/ -data ./aligned-images/
 +
./demos/classifier.py train ./generated-embeddings/
  
 +
==Run==
  
5. Run
+
Contoh run menggunakan pre-trained model yang berlokasi di celebrities folder.
To test if everything is working properly before working with the trained model from Section 4, let's try this on a pre-trained model. There is a pre-trained model on celebrities in folder. To test using this image, run
+
Jalankan,
  
 
  ./demos/classifier.py infer models/openface/celeb-classifier.nn4.small2.v1.pkl images/examples/adams.jpg
 
  ./demos/classifier.py infer models/openface/celeb-classifier.nn4.small2.v1.pkl images/examples/adams.jpg
  
Artifacts of sections 1~4 done on RPI are on this link. Be sure to extract it from /home/pi/ to produce /home/pi/fd_fr as the internal scripts are hardcoded to that location. I've made a couple of changes to the base as this is run on a 32bit environment. Running "git diff" from /home/pi/fd_fr/openface will reveal the changes.
+
Hasilnya kira-kira,
  
This should correctly predict the sample image as Amy Adams which predicts with 81% certainty but on RPI is 34% which I find odd...
+
Predict AmyAdams with 0.64 confidence.
  
To complete the final leg of our journey, I recommend testing on yourself. I saved a photo of myself that I didn't include in the training set as sky_chon.jpg. To test the trained model on me, I run
 
  
./demos/classifier.py infer generated-embeddings/classifier.pkl sky_chon.jpg
+
Beberapa perintah yang menarik
  
To run using the webcam (dev/video0) on VGA.
+
* Mencari target operasi
 
 
./demos/classifier_webcam.py --width 640 --height 480 --captureDevice 0 generated-embeddings/classifier.pkl
 
 
 
Works like a charm!!
 
 
 
References
 
 
 
    https://medium.com/@ageitgey/machine-learning-is-fun-part-4-modern-face-recognition-with-deep-learning-c3cffc121d78#.lugw83dgc
 
    https://cmusatyalab.github.io/openface/
 
    http://dlib.net/
 
    http://blog.dlib.net/2014/02/dlib-186-released-make-your-own-object.html
 
    http://bamos.github.io/2016/01/19/openface-0.2.0/
 
    https://github.com/davisking/dlib
 
    http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
 
    https://github.com/torch/torch7/issues/966
 
    https://wiki.debian.org/RaspberryPi/qemu-user-static
 
    https://hblok.net/blog/posts/2014/02/06/chroot-to-arm/
 
    https://lukeplant.me.uk/blog/posts/sharing-internet-connection-to-chroot/
 
    https://hblok.net/blog/posts/2014/02/06/chroot-to-arm/
 
    https://github.com/cmusatyalab/openface/issues/42
 
  
 +
./demos/classifier.py infer generated-embeddings/classifier.pkl target-operasi.jpg
  
 +
* Mengenali dari webcam (dev/video0) on VGA.
  
 +
./demos/classifier_webcam.py --width 640 --height 480 --captureDevice 0 generated-embeddings/classifier.pkl
  
 +
==Referensi==
  
 +
* https://medium.com/@ageitgey/machine-learning-is-fun-part-4-modern-face-recognition-with-deep-learning-c3cffc121d78#.lugw83dgc
 +
* https://cmusatyalab.github.io/openface/
 +
* http://dlib.net/
 +
* http://blog.dlib.net/2014/02/dlib-186-released-make-your-own-object.html
 +
* http://bamos.github.io/2016/01/19/openface-0.2.0/
 +
* https://github.com/davisking/dlib
 +
* http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
 +
* https://github.com/torch/torch7/issues/966
 +
* https://wiki.debian.org/RaspberryPi/qemu-user-static
 +
* https://hblok.net/blog/posts/2014/02/06/chroot-to-arm/
 +
* https://lukeplant.me.uk/blog/posts/sharing-internet-connection-to-chroot/
 +
* https://hblok.net/blog/posts/2014/02/06/chroot-to-arm/
 +
* https://github.com/cmusatyalab/openface/issues/42
  
 
==Referensi==
 
==Referensi==
  
 
* http://allskyee.blogspot.co.id/2017/03/face-detection-and-recognition-using.html
 
* http://allskyee.blogspot.co.id/2017/03/face-detection-and-recognition-using.html

Latest revision as of 13:15, 23 May 2018

Sumber: http://allskyee.blogspot.co.id/2017/03/face-detection-and-recognition-using.html


Disini menggunakan dlib, yang katanya lebih baik daripada Haar-cascade based classifier OpenCV.


Instalasi Paket Pendukung

sudo su
locale-gen id_ID.UTF-8
apt update
apt -y install git \
       libopenblas-dev libopencv-dev libboost-dev \
       libboost-python-dev python-dev \
       build-essential gcc g++ cmake
apt -y install software-properties-common \
       libgraphicsmagick1-dev libfftw3-dev sox libsox-dev \
       libsox-fmt-all python-software-properties \
       build-essential gcc g++ curl \
       cmake libreadline-dev git-core libqt4-dev libjpeg-dev \
       libpng-dev ncurses-dev imagemagick libzmq3-dev gfortran \
       unzip gnuplot gnuplot-x11 ipython \
       gcc-4.9 libgfortran-4.9-dev g++-4.9

Lakukan 2-3 kali supaya memastikan apps di install dengan benar.

Instalasi dlib face landmark detection

sudo su
apt -y install build-essential cmake libgtk-3-dev \
       python-pip libboost-all-dev libboost-dev
apt -y install libboost-python-dev
pip install numpy
pip install scipy
pip install scikit-image
pip install dlib
pip install opencv-python # opencv tampaknya masih dibutuhkan

Lakukan 2-3x supaya memastikan apps di install dengan benar.

Instalasi Torch

sudo su
cd /usr/local/src
git clone https://github.com/torch/distro.git torch --recursive
cd torch; bash install-deps;
./install.sh
source ~/.bashrc

Lakukan 2-3x untuk memastikan terinstalasi dengan baik.

# apt -y install luarocks
# update common package ke versi terakhir
luarocks install torch
luarocks install nn
luarocks install graph
luarocks install cunn
luarocks install cutorch
luarocks install torchnet
luarocks install optnet
luarocks install iterm

Instalasi Openface

cd /usr/local/src
git clone https://github.com/cmusatyalab/openface.git openface
cd /usr/local/src/openface
python setup.py install
./models/get-models.sh
pip install -r requirements.txt
luarocks install csvigo 
luarocks install dpnn

Ambil Muka / Face yang sudah di label

Contoh2 foto muka untuk training.

cd /usr/local/src/openface/
wget http://vis-www.cs.umass.edu/lfw/lfw.tgz
tar -zxvf lfw.tgz

Pilih data yang tidak lebih dari 10 muka, simpan di big_db.

cd /usr/local/src/openface/
find lfw/ -mindepth 1 -maxdepth 2 -type d -exec bash -c "echo -ne '{} '; ls '{}' | wc -l" \; | awk '$NF>10{print $1}' > big_db

Ambil 10 orang (random) dari daftar big_db, copy ke training_images

cd /usr/local/src/openface/
mkdir -p training-images
cat big_db | shuf -n 10 | xargs cp -avt training-images/

Jika anda ingin wajah anda di recognize, tambahkan folder di training-images. Pastikan memasukan beberapa foto anda yang berisi muka anda sendirian.

Selanjutnya kita akan menjalankan face landmark detection untuk setiap foto yang akan

  • Mendeteksi muka yang terbesar
  • Mendeteksi tanda2 di muka (outer eye, hidung dan bibir bawah)
  • Warp affine ke canonical face
  • Simpan output (96x96) ke file dalam format yang mudah di akses.

Perintah yang di jalankan,

cd /usr/local/src/openface/
./util/align-dlib.py ./training-images/ align outerEyesAndNose ./aligned-images/ --size 96

Selanjutnya extrak fitur dari masing-masing gambar,

./batch-represent/main.lua -outDir ./generated-embeddings/ -data ./aligned-images/

Langkah terakhir, train classifier untuk membuat representasi

./demos/classifier.py train ./generated-embeddings/

File yang dihasilkan adalah classifier.pkl di folder generated-embeddings

Masukan foto2 referensi ke bawah folder training-images/Nama_Orang_Tersebut. Supaya mudah proses training ada baiknya di batch sekaligus, caranya,

cd /usr/local/src/openface/
rm -Rf aligned-images
rm -Rf generated-embeddings/classifier.pkl
./util/align-dlib.py ./training-images/ align outerEyesAndNose ./aligned-images/ --size 96
./batch-represent/main.lua -outDir ./generated-embeddings/ -data ./aligned-images/
./demos/classifier.py train ./generated-embeddings/

Run

Contoh run menggunakan pre-trained model yang berlokasi di celebrities folder. Jalankan,

./demos/classifier.py infer models/openface/celeb-classifier.nn4.small2.v1.pkl images/examples/adams.jpg

Hasilnya kira-kira,

Predict AmyAdams with 0.64 confidence.


Beberapa perintah yang menarik

  • Mencari target operasi
./demos/classifier.py infer generated-embeddings/classifier.pkl target-operasi.jpg
  • Mengenali dari webcam (dev/video0) on VGA.
./demos/classifier_webcam.py --width 640 --height 480 --captureDevice 0 generated-embeddings/classifier.pkl

Referensi

Referensi