Professional Documents
Culture Documents
• Tính trị riêng λi và vector riêng νi D chiều của ma trận S thỏa mãn
công thức
Sνi i νi , i 1..N
• Ma trận S vuông đối xứng có kích thước D2 khá lớn nên ta sẽ tìm
cách khác
TS NGUYỄN ĐỨC THÀNH 4
• Đặt X=[x1 -μ,x2 -μ...,xN- μ] ma trận D*N.
• Nhận xét rằng
S XX T , XX T νi i νi ,
X T X ( X T νi ) i ( X T νi ),
• Ví dụ: C:/faces/person0/image0.jpg,…,
C:/faces/person0/image9.jpg,
C:/faces/person1/image0.jpg,…, C:/faces/person9/image9.jpg,
Tạo file CSV (Comma Separated Values) face.txt ghi tên ảnh và
nhãn, theo từng dòng, có dạng như sau
/đường dẫn/person1/image0.jpg;0
/đường dẫn/person9/image9.jpg;9
TS NGUYỄN ĐỨC THÀNH 16
create_csv.py
• Ví dụ file ảnh trong thư mục d:/ATTDATABASE, dùng file
create_csv.py chạy trong python 3.6 như sau
• Phương pháp này trích xuất đặc trưng của ảnh bất biến với độ chiếu
sáng, tỷ lệ, tịnh tiến, quay.
• Dựa trên Local Binary Patterns xét 1 pixel với 8 điểm bao quanh,
so sánh cường độ điểm lân cận và pixel giữa, nếu nhỏ hơn ta thay
điểm lân cân cận bằng số 0, ngược lại thay bằng số 0. Như vậy ta
có số nhị phân 8 bit cho mỗi pixel không phụ thuộc vào độ chiếu
sáng. Tính toán cho toàn bột điểm ảnh ta có ảnh LBP, mỗi điểm
ảnh giá trị từ 0 đến 255
Thuật toán cải tiến dùng các cận trên vòng tròn bán kính R, vậy
có hai tham số là bán kính R thường là 1 và số lân cận thường là
8
• Áp dụng histogram cho ảnh LBP, ta chia ảnh thành các cell theo
chiều X và Y, gridX và gridY là số cell chiều X và chiều Y, tạo
vector histogram cho mỗi cell, sau đó ghép với nhau ta được vector
histogram cho toàn ảnh, chính là đặc trưng của ảnh.
• So sánh khoảng cách vector đặc trưng ảnh cần nhận dạng với ảnh
huấn luyện, ta gán nhãn dựa trên khoảng cách nhỏ hơn.
• Confidence là thông số khoảng cách, nếu confidence=0.0 kết quả
nhận dạng là chính xác.
• Thông thường ta đặt ngưỡng threshold, nếu confidence<threshold
thì công nhận kết quả phân lớp. Phân lớp cũng có thể dùng SVM
hoặc KNN.
TS NGUYỄN ĐỨC THÀNH 41
LOCAL BINARY PATTERN HISTOGRAM LBPH(1996)
Ví dụ ảnh LBP có 9 cell, dùng số lân cận là P=8, mỗi cell biểu thị
bằng histogram chiều dại 256 có giá trị là số pixel cùng cường độ.
Histogram toàn ảnh là vector dài 9*236. Con số này rất lớn.
Giảm kích thước của vector histogram mỗi cell bằng cách chỉ xét các
pixel kế nhau tạo thành uniform pattern nghia là có nhiều nhất hai
chuyển tiếp từ 0 sang 1 hay ngược lại ví dụ như 00000000, 00011110
10000011, 11111110...
TS NGUYỄN ĐỨC THÀNH 42
LOCAL BINARY PATTERN HISTOGRAM LBPH(1996)
• 58 uniform binary patterns tương ứng các số 0, 1, 2, 3, 4, 6, 7, 8,
12, 14, 15, 16, 24, 28, 30, 31, 32, 48, 56, 60, 62, 63, 64, 96, 112,
120, 124, 126, 127, 128, 129, 131, 135, 143, 159, 191, 192, 193,
195, 199, 207, 223, 224, 225, 227, 231, 239, 240, 241, 243, 247,
248, 249, 251, 252, 253, 254 và 255.
• Vị trí còn lại cho các mẫu khác, vậy chiều dài vector đặc trưng mỗi
cell là P*(P-1)+3 thay vì 2P . Tại sao?
• Nếu khảo sát thêm phép bất biến quay, hai vector quay trái hay
phải là giống nhau như vậy các số 1, 2, 4, 8, 16, 32, 64,128 gom
thành bin 1. Có thể chứng minh là số bin với uniform pattern và
rotation invariant giảm còn P+2?. Với 8 lân cận số bin là 10: (0),
(255), (1,2,4,8,16,32,64,128), (3,6,12,24,48,96,129,192),
(7,14,28,56,112,131,193,240), (15,30,60,120,135,195,215,240),
(31,62,124,227,248,249), (63,126,243,249,250, 252), (các số còn
lại)
TS NGUYỄN ĐỨC THÀNH 43
FACRECOGNIZER OPENCV C++ LBPH
• Opencv_contrib/modules/face/samples/facerec_lbph.cpp
• https://docs.opencv.org/3.0-
beta/modules/face/doc/facerec/facerec_tutorial.html#local-binary-
patterns-histograms
// FaceRecognition.cpp : Defines the entry point for the console
application.
//Adapted from opencv_contrib/modules/face/samples/facerec_lbph.cpp
#include "stdafx.h"
#include "opencv2/opencv.hpp"
#include "opencv2/face.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
using namespace cv;
using namespace cv::face;
using namespace std;
TS NGUYỄN ĐỨC THÀNH 44
FACRECOGNIZER OPENCV C++ LBPH
static Mat norm_0_255(InputArray _src) {…}
static void read_csv(const string& filename, vector<Mat>& images,
vector<int>& labels, char separator = ';') {…]
int main(int argc, const char *argv[]) {
vector<Mat> images;
vector<int> labels;
read_csv("d:/face.txt", images, labels);
// Get the height from the first image.
int height = images[0].rows;
//The last image is used for test
Mat testSample = images[images.size() - 1];
Mat dst;
int testLabel = labels[labels.size() - 1];
images.pop_back();
labels.pop_back();
TS NGUYỄN ĐỨC THÀNH 45
FACRECOGNIZER OPENCV C++ LBPH
Ptr<LBPHFaceRecognizer> model =
LBPHFaceRecognizer::create();
model->train(images, labels);
model->setThreshold( 0.0);
int PredictedLabel = model->predict(testSample);
string result_message = format("Predicted class = %d / Actual
class = %d.", PredictedLabel, testLabel);
cout << result_message << endl;
cout << "Model Information:" << endl; string
model_info = format("LBPH(radius= %i, neighbors= %i, grid_x=
%i, grid_y= %i, threshold= % 0.2f)",
model->getRadius(),
model->getNeighbors(),
model->getGridX(),
TS NGUYỄN ĐỨC THÀNH 46
FACRECOGNIZER OPENCV C++ LBPH
model->getGridY(),
model->getThreshold());
cout << model_info << endl;
// We could get the histograms for example:
vector<Mat> histograms = model->getHistograms(); //
Probably the length is interesting:
cout << "Size of the histograms: " << histograms[0].total() <<
endl;return 0;
}
Bấm Control F5
Cực tiểu hàm tổn thất triplet loss L bằng cách huấn luyện hàm f
• FaceNet huấn luyện với một số lượng lớn hình ảnh (hơn 200 triệu
ảnh của 8 triệu đối tượng). Deepface do nhóm nghiên cứu
Facebook phát triển, dùng mạng DNN 9 lớp và huấn luyện 4 triệu
ảnh với độ chính xác 97%.
• OpenFace is a deep learning facial recognition model developed
by Brandon Amos, Bartosz Ludwiczuk, and Mahadev
Satyanarayanan. It’s based off of the paper: FaceNet: A Unified
Embedding for Face Recognition and Clustering and is
implemented using Python and Torch so it can be run on CPU or
GPU’s.
TS NGUYỄN ĐỨC THÀNH 80
OpenFace Dlib
https://blog.algorithmia.com/understanding-facial-recognition-
openface/
"Openface: A general-purpose face recognition library with mobile
applications,“Brandon Amos, Bartosz Ludwiczuk, Mahadev
Satyanarayanan
From a high level perspective, OpenFace uses Torch, a scientific
computing framework to do training offline, meaning it’s only done
once by OpenFace and the user doesn’t have to get their hands dirty
training hundreds of thousands of images themselves. Those images
are then thrown into a neural net for feature extraction using Google’s
FaceNet model. FaceNet relies on a triplet loss function to compute
the accuracy of the neural net classifying a face and is able to cluster
faces because of the resulting measurements on a hypersphere.
This trained neural net is later used in the Python implementation after
new images are run through dlib’s face detection model. Once the
faces are normalized by OpenCV’s Affine transformation so all faces
are pointing in the same direction, they are sent through the trained
neural net in a single forward pass. This results in 128 facial
embeddings used for classification for matching or can even used in a
clustering algorithm for similarity detection.
TS NGUYỄN ĐỨC THÀNH 81
OpenFace Dlib