@@ -4,7 +4,11 @@
|
||||
|
||||
#include "facenet.h"
|
||||
|
||||
|
||||
/**
|
||||
* stem网络
|
||||
* @param image 输入图片
|
||||
* @param output 输出featuremap 指针形式
|
||||
*/
|
||||
void facenet::Stem(Mat &image, pBox *output) {
|
||||
pBox *rgb = new pBox;
|
||||
pBox *conv1_out = new pBox;
|
||||
@@ -161,6 +165,13 @@ void facenet::Stem(Mat &image, pBox *output) {
|
||||
freeBN(conv6_beta);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inception_resnet_A网络
|
||||
* @param input 输入featuremap
|
||||
* @param output 输出featuremap
|
||||
* @param filepath 模型文件路径
|
||||
* @param scale 比例系数
|
||||
*/
|
||||
void facenet::Inception_resnet_A(pBox *input, pBox *output, string filepath, float scale) {
|
||||
pBox *conv1_out = new pBox;
|
||||
pBox *conv2_out = new pBox;
|
||||
@@ -327,6 +338,11 @@ void facenet::Inception_resnet_A(pBox *input, pBox *output, string filepath, flo
|
||||
freeBN(conv6_beta);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reduction_A
|
||||
* @param input 输入featuremap
|
||||
* @param output 输出featuremap
|
||||
*/
|
||||
void facenet::Reduction_A(pBox *input, pBox *output) {
|
||||
pBox *conv1_out = new pBox;
|
||||
pBox *conv2_out = new pBox;
|
||||
@@ -429,6 +445,13 @@ void facenet::Reduction_A(pBox *input, pBox *output) {
|
||||
freeBN(conv4_beta);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inception_resnet_B网络
|
||||
* @param input 输入featuremap
|
||||
* @param output 输出featuremap
|
||||
* @param filepath 模型文件路径
|
||||
* @param scale 比例系数
|
||||
*/
|
||||
void facenet::Inception_resnet_B(pBox *input, pBox *output, string filepath, float scale) {
|
||||
pBox *conv1_out = new pBox;
|
||||
pBox *conv2_out = new pBox;
|
||||
@@ -550,6 +573,11 @@ void facenet::Inception_resnet_B(pBox *input, pBox *output, string filepath, flo
|
||||
freeBN(conv4_beta);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reduction_B
|
||||
* @param input 输入featuremap
|
||||
* @param output 输出featuremap
|
||||
*/
|
||||
void facenet::Reduction_B(pBox *input, pBox *output) {
|
||||
pBox *conv1_out = new pBox;
|
||||
pBox *conv2_out = new pBox;
|
||||
@@ -714,6 +742,13 @@ void facenet::Reduction_B(pBox *input, pBox *output) {
|
||||
freeBN(conv7_beta);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inception_resnet_C网络
|
||||
* @param input 输入featuremap
|
||||
* @param output 输出featuremap
|
||||
* @param filepath 模型文件路径
|
||||
* @param scale 比例系数
|
||||
*/
|
||||
void facenet::Inception_resnet_C(pBox *input, pBox *output, string filepath, float scale) {
|
||||
pBox *conv1_out = new pBox;
|
||||
pBox *conv2_out = new pBox;
|
||||
@@ -836,6 +871,13 @@ void facenet::Inception_resnet_C(pBox *input, pBox *output, string filepath, flo
|
||||
freeBN(conv4_beta);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inception_resnet_C网络 最后无激活函数
|
||||
* @param input 输入featuremap
|
||||
* @param output 输出featuremap
|
||||
* @param filepath 模型文件路径
|
||||
* @param scale 比例系数
|
||||
*/
|
||||
void facenet::Inception_resnet_C_None(pBox *input, pBox *output, string filepath) {
|
||||
pBox *conv1_out = new pBox;
|
||||
pBox *conv2_out = new pBox;
|
||||
@@ -950,6 +992,11 @@ void facenet::Inception_resnet_C_None(pBox *input, pBox *output, string filepath
|
||||
freeBN(conv4_beta);
|
||||
}
|
||||
|
||||
/**
|
||||
* 平均池化
|
||||
* @param input 输入featuremap
|
||||
* @param output 输出featuremap
|
||||
*/
|
||||
void facenet::AveragePooling(pBox *input, pBox *output) {
|
||||
// cout << "size:" << input->height << endl;
|
||||
avePoolingInit(input, output, input->height, 2);
|
||||
@@ -965,6 +1012,12 @@ void facenet::Flatten(pBox *input, pBox *output) {
|
||||
memcpy(output->pdata, input->pdata, output->channel * output->width * output->height * sizeof(mydataFmt));
|
||||
}
|
||||
|
||||
/**
|
||||
* 全连接网络
|
||||
* @param input 输入featuremap
|
||||
* @param output 输出featuremap
|
||||
* @param filepath 网络模型参数文件路径
|
||||
*/
|
||||
//参数还未设置
|
||||
void facenet::fully_connect(pBox *input, pBox *output, string filepath) {
|
||||
struct Weight *conv1_wb = new Weight;
|
||||
@@ -1014,8 +1067,6 @@ void facenet::run(Mat &image, vector<mydataFmt> &o, int count) {
|
||||
pBox *output = new pBox;
|
||||
pBox *input;
|
||||
Stem(image, output);
|
||||
// printData(output);
|
||||
// return;
|
||||
cout << "Stem Finally" << endl;
|
||||
input = output;
|
||||
output = new pBox;
|
||||
@@ -1030,7 +1081,6 @@ void facenet::run(Mat &image, vector<mydataFmt> &o, int count) {
|
||||
Reduction_A(input, output);
|
||||
cout << "Reduction_A Finally" << endl;
|
||||
input = output;
|
||||
// freepBox(output);
|
||||
output = new pBox;
|
||||
for (int j = 0; j < 10; ++j) {
|
||||
// model_128/block17_1_list.txt
|
||||
@@ -1048,10 +1098,8 @@ void facenet::run(Mat &image, vector<mydataFmt> &o, int count) {
|
||||
for (int k = 0; k < 5; ++k) {
|
||||
// model_128/block8_1_list.txt
|
||||
string filepath = "../model_" + to_string(Num) + "/block8_" + to_string((k + 1)) + "_list.txt";
|
||||
// cout << filepath << endl;
|
||||
Inception_resnet_C(input, output, filepath, 0.2);
|
||||
input = output;
|
||||
// freepBox(output);
|
||||
output = new pBox;
|
||||
}
|
||||
cout << "Inception_resnet_C Finally" << endl;
|
||||
@@ -1070,6 +1118,10 @@ void facenet::run(Mat &image, vector<mydataFmt> &o, int count) {
|
||||
output = new pBox;
|
||||
fully_connect(input, output, "../model_" + to_string(Num) + "/Bottleneck_list.txt");
|
||||
cout << "Fully_Connect Finally" << endl;
|
||||
|
||||
/**
|
||||
* L2归一化
|
||||
*/
|
||||
mydataFmt sq = 0, sum = 0;
|
||||
for (int i = 0; i < Num; ++i) {
|
||||
sq = pow(output->pdata[i], 2);
|
||||
|
||||
109
src/pikaqiu.cpp
109
src/pikaqiu.cpp
@@ -3,6 +3,34 @@
|
||||
#include "facenet.h"
|
||||
#include <time.h>
|
||||
|
||||
/**
|
||||
* 图片缩小
|
||||
* @param src 输入图片
|
||||
* @return 返回图片
|
||||
*/
|
||||
Mat RS(Mat &src) {
|
||||
int w = src.cols;
|
||||
int h = src.rows;
|
||||
int wtemp, htemp;
|
||||
Mat dst;
|
||||
cout << w << "\t" << h << endl;
|
||||
float threshold = 300.0;
|
||||
if (h > threshold) {
|
||||
wtemp = (int) (threshold / h * w);
|
||||
htemp = threshold;
|
||||
dst = Mat::zeros(htemp, wtemp, CV_8UC3); //我要转化为htemp*wtemp大小的
|
||||
resize(src, dst, dst.size());
|
||||
}
|
||||
cout << wtemp << "\t" << htemp << endl;
|
||||
cout << "-------------------" << endl;
|
||||
return dst;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载csv中的emb
|
||||
* @param num csv的个数
|
||||
* @param vecVec 将emb参数存在二维vector中,外层定位某个人,内层定位某个emb值
|
||||
*/
|
||||
void load_emb_csv(int num, vector<vector<mydataFmt>> &vecVec) {
|
||||
for (int i = 0; i < num; ++i) {
|
||||
ifstream inFile("../emb_csv/" + to_string(i) + ".csv", ios::in);
|
||||
@@ -28,6 +56,11 @@ void load_emb_csv(int num, vector<vector<mydataFmt>> &vecVec) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 预处理,保存emb值到csv中
|
||||
* @param o 保存一个人的emb值
|
||||
* @param num 第几个人,确定文件名
|
||||
*/
|
||||
void write_emb_csv(vector<mydataFmt> &o, int num) {
|
||||
ofstream outFile;
|
||||
outFile.open("../emb_csv/" + to_string(num) + ".csv", ios::out); // 打开模式可省略
|
||||
@@ -44,6 +77,12 @@ void write_emb_csv(vector<mydataFmt> &o, int num) {
|
||||
cout << "write over!" << endl;
|
||||
}
|
||||
|
||||
/**
|
||||
* 对比两个人的emb值,计算空间欧氏距离
|
||||
* @param lineArray0 第一个人的emb值
|
||||
* @param lineArray1 第二个人的emb值
|
||||
* @return
|
||||
*/
|
||||
float compare(vector<mydataFmt> &lineArray0, vector<mydataFmt> &lineArray1) {
|
||||
mydataFmt sum = 0;
|
||||
for (int i = 0; i < Num; ++i) {
|
||||
@@ -56,6 +95,12 @@ float compare(vector<mydataFmt> &lineArray0, vector<mydataFmt> &lineArray1) {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行mtcnn网络
|
||||
* @param image 图片
|
||||
* @param vecRect 获取人脸框
|
||||
* @param vecPoint 获取人脸五个点
|
||||
*/
|
||||
void run_mtcnn(Mat &image, vector<Rect> &vecRect, vector<Point> &vecPoint) {
|
||||
// vector<Point> vecPoint;
|
||||
mtcnn find(image.rows, image.cols);
|
||||
@@ -68,6 +113,12 @@ void run_mtcnn(Mat &image, vector<Rect> &vecRect, vector<Point> &vecPoint) {
|
||||
// }
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行facenet网络(一张图与数据库里的多个人脸做比对)
|
||||
* @param image 图片
|
||||
* @param vecRect 人脸框
|
||||
* @param csv_num csv数量 缺省 0 写csv模式
|
||||
*/
|
||||
void run_facenet(Mat &image, vector<Rect> &vecRect, int csv_num = 0) {
|
||||
for (int i = 0; i < vecRect.size(); ++i) {
|
||||
Mat fourthImage;
|
||||
@@ -99,28 +150,11 @@ void run_facenet(Mat &image, vector<Rect> &vecRect, int csv_num = 0) {
|
||||
}
|
||||
}
|
||||
|
||||
float test_compare(vector<mydataFmt> &lineArray0, vector<mydataFmt> &lineArray1) {
|
||||
mydataFmt sum = 0;
|
||||
for (int i = 0; i < Num; ++i) {
|
||||
// cout << lineArray0[i] << "===" << lineArray1[i] << endl;
|
||||
mydataFmt sub = lineArray0[i] - lineArray1[i];
|
||||
mydataFmt square = pow(sub, 2);
|
||||
sum += square;
|
||||
}
|
||||
mydataFmt result = sqrt(sum);
|
||||
return result;
|
||||
}
|
||||
|
||||
void test_facenet(Mat &image, vector<Rect> &vecRect, vector<mydataFmt> &n) {
|
||||
Mat fourthImage;
|
||||
resize(image(vecRect[0]), fourthImage, Size(160, 160), 0, 0, cv::INTER_LINEAR);
|
||||
facenet ggg;
|
||||
// mydataFmt *o = new mydataFmt[Num];
|
||||
// vector<mydataFmt> n;
|
||||
vector<vector<mydataFmt>> o;
|
||||
ggg.run(fourthImage, n, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行run_mtcnn和run_facenet
|
||||
* 一张图,通过run_mtcnn找到n个人脸
|
||||
* n个人脸通过run_facenet与数据库里的m个人脸数据做匹配,看看这n个人分别是谁
|
||||
*/
|
||||
void run() {
|
||||
int b = 0;
|
||||
if (b == 0) {
|
||||
@@ -182,9 +216,30 @@ void run() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行单次单人的facenet网络
|
||||
* @param image 输入图片
|
||||
* @param vecRect 人脸框
|
||||
* @param n emb值
|
||||
*/
|
||||
void test_facenet(Mat &image, vector<Rect> &vecRect, vector<mydataFmt> &n) {
|
||||
Mat fourthImage;
|
||||
resize(image(vecRect[0]), fourthImage, Size(160, 160), 0, 0, cv::INTER_LINEAR);
|
||||
facenet ggg;
|
||||
// mydataFmt *o = new mydataFmt[Num];
|
||||
// vector<mydataFmt> n;
|
||||
// vector<vector<mydataFmt>> o;
|
||||
ggg.run(fourthImage, n, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对比两张图两个人的emb
|
||||
*/
|
||||
void test() {
|
||||
Mat image0 = imread("../hejiong1.jpeg");
|
||||
Mat image1 = imread("../hejiong0.jpeg");
|
||||
image0 = RS(image0);
|
||||
image1 = RS(image1);
|
||||
|
||||
clock_t start;
|
||||
start = clock();
|
||||
@@ -209,7 +264,7 @@ void test() {
|
||||
-1);
|
||||
}
|
||||
|
||||
float result = test_compare(n0, n1);
|
||||
float result = compare(n0, n1);
|
||||
cout << "-------------------" << endl;
|
||||
cout << result << endl;
|
||||
if (result < 0.45)
|
||||
@@ -218,20 +273,26 @@ void test() {
|
||||
cout << "Probably not the same person" << endl;
|
||||
|
||||
imshow("result0", image0);
|
||||
// resizeWindow("result0", w0, h0); //创建一个固定值大小的窗口
|
||||
imwrite("../result0.jpg", image0);
|
||||
imshow("result1", image1);
|
||||
imwrite("../result1.jpg", image1);
|
||||
start = clock() - start;
|
||||
// cout<<"time is "<<start/10e3<<endl;
|
||||
cout << "time is " << (double) start / CLOCKS_PER_SEC * 1000 << "ms" << endl;
|
||||
waitKey(0);
|
||||
waitKey(5000);
|
||||
image0.release();
|
||||
image1.release();
|
||||
}
|
||||
|
||||
/**
|
||||
* 程序主体
|
||||
* @return
|
||||
*/
|
||||
int main() {
|
||||
for (int i = 0; i < 1; ++i) {
|
||||
test();
|
||||
cout << "==============================" << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user