优化代码结构

优化代码
This commit is contained in:
2020-07-09 00:26:46 +08:00
parent 10d9ff36ae
commit 705c8bcb7a
9 changed files with 24 additions and 124 deletions

3
.gitignore vendored
View File

@@ -37,3 +37,6 @@ cmake-build-debug/
cmake-build-release/
model_128/
model_512/
model_128.zip
model_512.zip
*.cfg

View File

@@ -1,2 +1,12 @@
# FaceNet-light
FaceNet-light with c++
### FaceNet-light with c++
##### 使用C++实现FaceNet脱离TensorFlow只是用OpenCV
##### 模型参数是提取FaceNet的官方模型2017版本和2018版本
##### 切换模型文件 在pBox.h开头注释或者打开宏
```c++
#define Num 512
//#define Num 128
```

22
src/facenet.cpp Executable file → Normal file
View File

@@ -1063,20 +1063,6 @@ void facenet::AveragePooling(pBox *input, pBox *output) {
avePooling(input, output, input->height, 2);
}
/**
* flatten 多维转换到一维
* @param input
* @param output
*/
void facenet::Flatten(pBox *input, pBox *output) {
output->width = input->channel;
output->height = 1;
output->channel = 1;
output->pdata = (mydataFmt *) malloc(output->channel * output->width * output->height * sizeof(mydataFmt));
if (output->pdata == NULL)cout << "the maxPoolingInit is failed!!" << endl;
memcpy(output->pdata, input->pdata, output->channel * output->width * output->height * sizeof(mydataFmt));
}
/**
* 全连接网络
* @param input 输入featuremap
@@ -1120,14 +1106,6 @@ facenet::~facenet() {
}
void facenet::printData(pBox *in) {
for (long i = 0; i < in->height * in->width * in->channel; ++i) {
// if (in->pdata[i] != 0)
printf("%f\n", in->pdata[i]);
}
cout << "printData" << endl;
}
/**
* facenet网络运行入口
* @param image

View File

@@ -34,10 +34,6 @@ private:
void AveragePooling(pBox *input, pBox *output);
void fully_connect(pBox *input, pBox *output, string filepath = "");
void Flatten(pBox *input, pBox *output);
void printData(pBox *output);
};
#endif //MAIN_FACENET_H

43
src/network.cpp Executable file → Normal file
View File

@@ -400,49 +400,6 @@ void avePooling(const pBox *pbox, pBox *Matrix, int kernelSize, int stride) {
}
}
/**
* 激活函数 有系数 初始化
* @param prelu 激活函数权重
* @param width 长度
*/
void pReluInit(struct pRelu *prelu, int width) {
prelu->width = width;
prelu->pdata = (mydataFmt *) malloc(width * sizeof(mydataFmt));
if (prelu->pdata == NULL)cout << "prelu apply for memory failed!!!!";
memset(prelu->pdata, 0, width * sizeof(mydataFmt));
}
/**
* 激活函数 有系数
* @param pbox 输入feature
* @param pbias 偏移
* @param prelu_gmma 激活函数权重
*/
void prelu(struct pBox *pbox, mydataFmt *pbias, mydataFmt *prelu_gmma) {
if (pbox->pdata == NULL) {
cout << "the pRelu feature is NULL!!" << endl;
return;
}
if (pbias == NULL) {
cout << "the pRelu bias is NULL!!" << endl;
return;
}
mydataFmt *op = pbox->pdata;
mydataFmt *pb = pbias;
mydataFmt *pg = prelu_gmma;
long dis = pbox->width * pbox->height;
for (int channel = 0; channel < pbox->channel; channel++) {
for (int col = 0; col < dis; col++) {
*op = *op + *pb;
*op = (*op > 0) ? (*op) : ((*op) * (*pg));
op++;
}
pb++;
pg++;
}
}
/**
* 激活函数 没有系数
* @param pbox 输入feature

13
src/network.h Executable file → Normal file
View File

@@ -24,8 +24,6 @@ void avePooling(const pBox *pbox, pBox *Matrix, int kernelSize, int stride);
void featurePad(const pBox *pbox, pBox *outpBox, const int pad, const int padw = 0, const int padh = 0);
void prelu(struct pBox *pbox, mydataFmt *pbias, mydataFmt *prelu_gmma);
void relu(struct pBox *pbox, mydataFmt *pbias);
void fullconnect(const Weight *weight, const pBox *pbox, pBox *outpBox);
@@ -35,10 +33,6 @@ void readData(string filename, long dataNumber[], mydataFmt *pTeam[], int length
long ConvAndFcInit(struct Weight *weight, int schannel, int lchannel, int kersize, int stride, int pad,
int w = 0, int h = 0, int padw = 0, int padh = 0);
void pReluInit(struct pRelu *prelu, int width);
void softmax(const struct pBox *pbox);
void image2MatrixInit(Mat &image, struct pBox *pbox);
void featurePadInit(const pBox *pbox, pBox *outpBox, const int pad, const int padw = 0, const int padh = 0);
@@ -51,13 +45,6 @@ void convolutionInit(const Weight *weight, pBox *pbox, pBox *outpBox);
void fullconnectInit(const Weight *weight, pBox *outpBox);
bool cmpScore(struct orderScore lsh, struct orderScore rsh);
void nms(vector<struct Bbox> &boundingBox_, vector<struct orderScore> &bboxScore_, const mydataFmt overlap_threshold,
string modelname = "Union");
void refineAndSquareBbox(vector<struct Bbox> &vecBbox, const int &height, const int &width);
void vectorXmatrix(mydataFmt *matrix, mydataFmt *v, int v_w, int v_h, mydataFmt *p);
void convolution(const Weight *weight, const pBox *pbox, pBox *outpBox);

View File

@@ -8,14 +8,6 @@ void freepBox(struct pBox *pbox) {
delete pbox;
}
void freepRelu(struct pRelu *prelu) {
if (prelu->pdata == NULL)cout << "prelu is NULL!" << endl;
else
free(prelu->pdata);
prelu->pdata = NULL;
delete prelu;
}
void freeWeight(struct Weight *weight) {
if (weight->pdata == NULL)cout << "weight is NULL!" << endl;
else

View File

@@ -12,7 +12,8 @@
using namespace std;
//#define mydataFmt double
#define Num 128
#define Num 512
//#define Num 128
typedef float mydataFmt;
struct pBox : public cv::String {
@@ -22,11 +23,6 @@ struct pBox : public cv::String {
int channel;
};
struct pRelu {
mydataFmt *pdata;
int width;
};
struct BN {
mydataFmt *pdata;
int width;
@@ -46,29 +42,10 @@ struct Weight {
int padh;
};
struct Bbox {
float score;
int x1;
int y1;
int x2;
int y2;
float area;
bool exist;
mydataFmt ppoint[10];
mydataFmt regreCoord[4];
};
struct orderScore {
mydataFmt score;
int oriOrder;
};
void freepBox(struct pBox *pbox);
void freeWeight(struct Weight *weight);
void freepRelu(struct pRelu *prelu);
void freeBN(struct BN *bn);
#endif

View File

@@ -113,7 +113,7 @@ void compareperson() {
start = clock() - start;
// cout<<"time is "<<start/10e3<<endl;
cout << "time is " << (double) start / CLOCKS_PER_SEC * 1000 << "ms" << endl;
waitKey(5000);
waitKey(0);
image0.release();
image1.release();
}